-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync
executable file
·66 lines (51 loc) · 1.92 KB
/
sync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#/usr/bin/env python3
import ldap3
import requests
import vobject
import secrets
LDAP_URL = ''
LDAP_BIND_DN = ''
LDAP_BIND_PASSWORD = secrets.LDAP_BIND_PASSWORD
LDAP_BASE_DN = ''
LDAP_FILTER = ''
CARDDAV_USERNAME = ''
CARDDAV_PASSWORD = secrets.CARDDAV_PASSWORD
CARDDAV_URL = 'your url here/{}/your calendar name/{{}}.vcf'.format(CARDDAV_USERNAME)
def download_ldif():
server = ldap3.Server(LDAP_URL)
conn = ldap3.Connection(server, user=LDAP_BIND_DN,
password=LDAP_BIND_PASSWORD, auto_bind=ldap3.AUTO_BIND_TLS_BEFORE_BIND)
g = conn.extend.standard.paged_search(LDAP_BASE_DN, LDAP_FILTER,
attributes=ldap3.ALL_ATTRIBUTES, paged_size=10, generator=True)
return g
def convert_ldif_to_vcards(record):
dn, attributes = record['dn'], record['attributes']
vc = vobject.vCard()
uid = attributes['ipaUniqueID'][0]
vc.add('n').value = vobject.vcard.Name(family=attributes['sn'][0],
given=attributes['givenName'][0])
vc.add('uid').value = uid
vc.add('fn').value = attributes['cn'][0]
for tel in attributes.get('telephoneNumber') or ():
vc.add('tel;TYPE=').value = tel
for tel in attributes.get('mobile') or ():
vc.add('tel;TYPE=cell').value = tel
for email in attributes.get('mail') or ():
vc.add('email').value = email
for title in attributes.get('title') or ():
vc.add('title').value = title
return vc
def upload_vcard(vcard):
url = CARDDAV_URL.format(vcard.uid.value)
data = vcard.serialize()
result = requests.put(url, data=data, auth=(CARDDAV_USERNAME, CARDDAV_PASSWORD))
result.raise_for_status()
if __name__ == '__main__':
count = 0
for record in download_ldif():
print('Processing {}'.format(record['dn']))
vcard = convert_ldif_to_vcards(record)
upload_vcard(vcard)
count += 1
print('Successfully uploaded {} vcards.'.format(count))
# vim: set ts=4 sts=4 sw=4 et ft=python: