-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLCI_contacts.py
146 lines (115 loc) · 4.73 KB
/
LCI_contacts.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
* mailcontacts.txt
* Generate uid:
- Pontus Hellman -> cphel
- (remove 'ext-' -prefix)
* Add mail - parsed from: "ExternalEmailAddress : SMTP:aarne.asiakas@customer.fi
* password -> "!"
* discard ExternalEmailAddress: endswith futurice.com.test-google-a.com
"""
import codecs
from LCI_util import *
def read_contacts(contacts_file, uidlist, used_uid_names):
"""
Return a dictionary with "uid" -> "email" mapping.
"""
# Read in out values to a list
lines = read_contacts_to_list(contacts_file)
# Convert our list to dict
contactsdict = contacts_from_list_to_dict(lines, uidlist, used_uid_names)
#logging.debug("Aliasdict: %s" % aliasdict)
return contactsdict
def contacts_from_list_to_dict(lines, uidlist, used_uid_names):
"""
Quite ugly alias list to mapping conversion.
"""
contactdict = {}
# Our dict keys are the PrimarySmtpAddresses
for i in range(len(lines)):
if i % 2 != 0:
contact_key_value = extract_contact_key(lines[i-1])
contact_list_string = lines[i]
if contact_list_string.endswith("futurice.com.test-google-a.com"):
#contactdict.pop(contact_key_value)
continue
else:
mail = str(contact_list_string.split("SMTP:")[1]).lower()
displayname = extract_contact_name(lines[i-1]).encode("ascii", "ignore")
if len(displayname.split()) >= 2:
sn = displayname.split()[-1]
else:
sn = displayname
if contact_key_value in used_uid_names:
logging.warn("UID '%s' was already taken, check manually if it is a collision or the same person." % contact_key_value)
continue
uidNumber = get_free_uidNumber(uidlist)
uidlist.append(uidNumber)
contactdict[contact_key_value] = {
"uid": contact_key_value,
"mail": mail,
"cn": displayname,
#rdn_value', 'cn', 'title', 'sn', 'display
"displayName" : displayname,
"title" : "customer",
"sn": sn,
"ntUserDomainId" : contact_key_value,
"gidNumber" : "2000",
"homeDirectory" : "/home/" + contact_key_value[0] + "/" + contact_key_value,
"uidNumber" : str(uidNumber),
"sambaSID" : 'S-1-5-21-1049098856-3271850987-3507249052-%s' % (uidNumber * 2 + 1000),
"shadowLastChange" : "0",
#"userPassword" : "!",
"googlePassword" : "!"
#"shadowMaxChange" : "0"
}
return contactdict
def get_free_uidNumber(uidlist):
"""
Returns:
str. A uidNumber that is not yet in use in the LDAP.
"""
nbrs = [int(number) for number in uidlist]
uidNumber = max(nbrs) + 1
if uidNumber >= 2000 and uidNumber < 3000:
return uidNumber
else :
raise RuntimeError('No uidNumber left in range %s' % [2000, 3000])
def extract_contact_name(str):
"""
"""
str_split = [val.strip() for val in str.split(":")]
if len(str_split) != 2 or str_split[0] != u"DisplayName":
logging.error("String '%s' doesn't look like a DisplayName value. Split value: %s" % (str, str_split))
return None
contactname = str_split[1]
# Strip leading "ext-"
if contactname.startswith("ext-"):
contactname = contactname[4:]
contactname = contactname.replace(".", "")
return contactname
def extract_contact_key(str):
"""
"""
contactkey = extract_contact_name(str).lower()
# Try and convert the displayname to an uid
str_split = contactkey.split()
if len(str_split) == 3 and len(str_split[1]) <= 2:
str_split = [str_split[0], str_split[2]]
if len(str_split) == 2:
contactkey = ("c" + str_split[0][0] + str_split[1][0:3]).encode('ascii','ignore')
return contactkey
contactkey = contactkey.replace("-", "")
contactkey = contactkey.replace(".", "")
return contactkey.encode('ascii', 'ignore')
def read_contacts_to_list(aliases_file):
f = codecs.open(aliases_file, encoding='utf_16')
lines = []
for line in f:
# Ugly continue
# Our lines of interest have a ':'
line = line.strip()
if not ":" in line: continue
if "PrimarySmtpAddress" in line or "EmailAddresses" in line: continue
lines.append(line)
#logging.debug(u"Lines: %s" % lines)
return lines