| 1 | #!/usr/bin/python |
|---|
| 2 | # coding=utf-8 |
|---|
| 3 | |
|---|
| 4 | from __future__ import with_statement |
|---|
| 5 | |
|---|
| 6 | import dbus |
|---|
| 7 | import sys, os |
|---|
| 8 | import tempfile |
|---|
| 9 | import re, string, time |
|---|
| 10 | |
|---|
| 11 | ps = os.popen ('ps auxwwwwe | grep -m 1 DBUS_SESSION_BUS_ADDRESS') |
|---|
| 12 | l = ps.read () |
|---|
| 13 | r = re.compile ('DBUS_SESSION_BUS_ADDRESS=(\S+)') |
|---|
| 14 | m = r.search (l) |
|---|
| 15 | a = m.expand ('\\1') |
|---|
| 16 | os.environ ['DBUS_SESSION_BUS_ADDRESS'] = a |
|---|
| 17 | |
|---|
| 18 | bus_name = 'org.gnome.evolution.dataserver.AddressBook' |
|---|
| 19 | obj_name = '/org/gnome/evolution/dataserver/addressbook/file_3a__2f__2f__2f_root_2f__2e_evolution_2f_addressbook_2f_local_2f_system' |
|---|
| 20 | |
|---|
| 21 | addressBook = None |
|---|
| 22 | def getAddressBook (): |
|---|
| 23 | global addressBook |
|---|
| 24 | if addressBook is None: |
|---|
| 25 | sb = dbus.SessionBus () |
|---|
| 26 | obj = sb.get_object (bus_name, obj_name) |
|---|
| 27 | addressBook = dbus.Interface (obj, 'org.gnome.evolution.dataserver.addressbook.Book') |
|---|
| 28 | return addressBook |
|---|
| 29 | |
|---|
| 30 | if len (sys.argv) != 2: |
|---|
| 31 | print ("Expects a single argument, 'dump' or 'load'") |
|---|
| 32 | print ("With 'dump', dumps all contacts as vcards to STDOUT") |
|---|
| 33 | print ("With 'load', loads vcards from STDIN") |
|---|
| 34 | exit (1) |
|---|
| 35 | |
|---|
| 36 | def dump_contacts (): |
|---|
| 37 | # Note: this is a gross hack, but I didn't manage to get getContactList to work |
|---|
| 38 | strings = os.popen ('strings /root/.evolution/addressbook/local/system/addressbook.db | grep ^pas-id-................ | sort -u').readlines () |
|---|
| 39 | for id in strings: |
|---|
| 40 | id = id.rstrip () |
|---|
| 41 | try: |
|---|
| 42 | print getAddressBook ().getContact (id) + "\r" |
|---|
| 43 | except: |
|---|
| 44 | pass |
|---|
| 45 | |
|---|
| 46 | def load_contacts (): |
|---|
| 47 | contacts = parse_stdin () |
|---|
| 48 | ab = getAddressBook () |
|---|
| 49 | l = contacts.keys () |
|---|
| 50 | l.sort () |
|---|
| 51 | for k in contacts.keys (): |
|---|
| 52 | try: |
|---|
| 53 | c = ab.getContact (k) |
|---|
| 54 | print "Contact already exists, modifying" |
|---|
| 55 | try: |
|---|
| 56 | ab.modifyContact (contacts [k]) |
|---|
| 57 | except: |
|---|
| 58 | print "Got error when modifying " + c |
|---|
| 59 | except: |
|---|
| 60 | print "New contact " + contacts[k] |
|---|
| 61 | ab.addContact (contacts [k]) |
|---|
| 62 | |
|---|
| 63 | def parse_stdin (): |
|---|
| 64 | lines = sys.stdin.readlines () |
|---|
| 65 | contacts = {} |
|---|
| 66 | cur = [] |
|---|
| 67 | index = 0 |
|---|
| 68 | for l in lines: |
|---|
| 69 | line = l.rstrip () |
|---|
| 70 | if line == '': |
|---|
| 71 | continue |
|---|
| 72 | if line == 'END:VCARD': |
|---|
| 73 | cur.append (line) |
|---|
| 74 | seen = '' |
|---|
| 75 | for record in cur: |
|---|
| 76 | if record.startswith ('UID:'): |
|---|
| 77 | seen = record [4:] |
|---|
| 78 | seen = seen.rstrip () |
|---|
| 79 | if seen == '': |
|---|
| 80 | seen = 'new-contact-' + str(index) |
|---|
| 81 | index += 1 |
|---|
| 82 | contacts [seen] = string.join (cur, '\r\n') |
|---|
| 83 | cur = [] |
|---|
| 84 | else: |
|---|
| 85 | if line.startswith ('REV:'): |
|---|
| 86 | cur.append ('REV: ' + time.strftime ('%Y-%m-%dT%H:%M:%SZ', time.gmtime())) |
|---|
| 87 | else: |
|---|
| 88 | cur.append (line) |
|---|
| 89 | return contacts |
|---|
| 90 | |
|---|
| 91 | if sys.argv [1] == 'load': |
|---|
| 92 | load_contacts () |
|---|
| 93 | else: |
|---|
| 94 | dump_contacts () |
|---|