Merge branch 'master' of git+ssh://scm.schwerkraft.elitedvb.net/scmrepos/git/enigma2...
[enigma2-plugins.git] / fritzcall / src / FritzLDIF.py
1 # -*- coding: utf-8 -*-
2 '''
3 $Author$
4 $Revision$
5 $Date$
6 $Id$
7 '''
8 #
9 # needs python-ldap for ldif
10 #
11
12 import ldif, re
13 try:
14         from . import _, debug, normalizePhoneNumber #@UnresolvedImport # pylint: disable-msg=F0401
15 except ValueError:
16         def _(string): # pylint: disable-msg=C0103
17                 return string
18         
19         def debug(text):
20                 print text
21         
22         def normalizePhoneNumber(intNo):
23                 found = re.match('^\+49(.*)', intNo)
24                 if found:
25                         intNo = '0' + found.group(1)
26                 found = re.match('^\+(.*)', intNo)
27                 if found:
28                         intNo = '00' + found.group(1)
29                 intNo = intNo.replace('(', '').replace(')', '').replace(' ', '').replace('/', '').replace('-', '')
30                 found = re.match('^49(.*)', intNo) # this is most probably an error
31                 if found:
32                         intNo = '0' + found.group(1)
33                 found = re.match('.*?([0-9]+)', intNo)
34                 if found:
35                         return found.group(1)
36                 else:
37                         return '0'
38
39 def out(number, name):
40         print number + '#' + name
41
42 class FindNumber(ldif.LDIFParser):
43         def __init__(self, number, inp, outFun):
44                 ldif.LDIFParser.__init__(self, inp)
45                 self.outFun = outFun
46                 self.number = number
47                 try:
48                         self.parse()
49                 except ValueError:
50                         # this is to exit the parse loop
51                         pass
52
53         def handle(self, dn, entry):
54                 # debug("[FritzCallPhonebook] LDIF handle: " + dn)
55                 found = re.match('.*cn=(.*),', str(dn))
56                 if found:
57                         name = found.group(1)
58                 else:
59                         return
60         
61                 address = ""
62                 addressB = ""
63                 if entry.has_key('telephoneNumber') or (entry.has_key('homePhone') and self.number == normalizePhoneNumber(entry['homePhone'][0])) or (entry.has_key('mobile') and self.number == normalizePhoneNumber(entry['mobile'][0])):
64                         # debug("[FritzCallPhonebook] LDIF get address")
65                         if entry.has_key('telephoneNumber'):
66                                 no = normalizePhoneNumber(entry['telephoneNumber'][0])
67                         else:
68                                 no = 0
69                         if self.number == no or (entry.has_key('homePhone') and self.number == normalizePhoneNumber(entry['homePhone'][0])) or (entry.has_key('mobile') and self.number == normalizePhoneNumber(entry['mobile'][0])):
70                                 nameB = (name + ' (' + _('business') + ')') if name else ""
71                                 if entry.has_key('company'):
72                                         nameB = (nameB + ', ' + entry['company'][0]) if nameB else entry['company'][0]
73                                 if entry.has_key('l'):
74                                         addressB = entry['l'][0]
75                                         if entry.has_key('postalCode'):
76                                                 addressB = entry['postalCode'][0] + ' ' + addressB
77                                         if entry.has_key('c'):
78                                                 addressB = addressB + ', ' + entry['c'][0]
79                                         if entry.has_key('street'):
80                                                 addressB = entry['street'][0] + ', ' + addressB
81                                         # debug("[FritzCallPhonebook] LDIF address: " + addressB)
82                                         if self.number == no:
83                                                 result = nameB + ', ' + addressB.replace('\n', ', ').replace('\r', '').replace('#', '')
84                                                 debug("[FritzCallPhonebook] LDIF result: " + result)
85                                                 self.outFun(no, result)
86                                                 self._input_file.close()
87                                                 return
88                                 else:
89                                         if self.number == no:
90                                                 result = nameB.replace('\n', ', ').replace('\r', '').replace('#', '')
91                                                 debug("[FritzCallPhonebook] LDIF result: " + result)
92                                                 self.outFun(no, result)
93                                                 self._input_file.close()
94                                                 return
95                 for i in ['homePhone', 'mobile']:
96                         if entry.has_key(i):
97                                 no = normalizePhoneNumber(entry[i][0])
98                                 if self.number == no:
99                                         if i == 'mobile':
100                                                 name = name + ' (' + _('mobile') + ')'
101                                         else:
102                                                 name = name + ' (' + _('home') + ')'
103                                         if entry.has_key('mozillaHomeLocalityName'):
104                                                 address = entry['mozillaHomeLocalityName'][0]
105                                                 if entry.has_key('mozillaHomePostalCode'):
106                                                         address = entry['mozillaHomePostalCode'][0] + ' ' + address
107                                                 if entry.has_key('mozillaHomeCountryName'):
108                                                         address = address + ', ' + entry['mozillaHomeCountryName'][0]
109                                                         debug("[FritzCallPhonebook] LDIF home address: " + addressB)
110                                                 result = name + ', ' + address.replace('\n', ', ').replace('\r', '').replace('#', '')
111                                                 debug("[FritzCallPhonebook] LDIF result: " + result)
112                                                 self.outFun(no, result)
113                                                 self._input_file.close()
114                                                 return
115                                         else:
116                                                 if addressB:
117                                                         name = name + ', ' + addressB.replace('\n', ', ').replace('\r', '').replace('#', '')
118                                                 debug("[FritzCallPhonebook] LDIF result: " + name)
119                                                 self.outFun(no, name)
120                                                 self._input_file.close()
121                                                 return
122
123 class ReadNumbers(ldif.LDIFParser):
124         def __init__(self, inPut, outFun):
125                 ldif.LDIFParser.__init__(self, inPut)
126                 self.outFun = outFun
127                 try:
128                         self.parse()
129                 except ValueError:
130                         #
131                         # this is to exit the parse loop:
132                         # we close the input file as soon as we have a result...
133                         #
134                         pass
135
136         def handle(self, dn, entry):
137                 # debug("[FritzCallPhonebook] LDIF handle: " + dn)
138                 found = re.match('.*cn=(.*),', str(dn))
139                 if found:
140                         name = found.group(1)
141                 else:
142                         return
143         
144                 address = ""
145                 addressB = ""
146                 if entry.has_key('telephoneNumber') or entry.has_key('homePhone') or entry.has_key('mobile'):
147                         # debug("[FritzCallPhonebook] LDIF get address")
148                         nameB = (name + ' (' + _('business') + ')') if name else ""
149                         if entry.has_key('company'):
150                                 nameB = (nameB + ', ' + entry['company'][0]) if nameB else entry['company'][0]
151                         if entry.has_key('l'):
152                                 addressB = entry['l'][0]
153                                 if entry.has_key('postalCode'):
154                                         addressB = entry['postalCode'][0] + ' ' + addressB
155                                 if entry.has_key('c'):
156                                         addressB = addressB + ', ' + entry['c'][0]
157                                 if entry.has_key('street'):
158                                         addressB = entry['street'][0] + ', ' + addressB
159                                 # debug("[FritzCallPhonebook] LDIF address: " + addressB)
160                                 if entry.has_key('telephoneNumber'):
161                                         no = normalizePhoneNumber(entry['telephoneNumber'][0])
162                                         result = nameB + ', ' + addressB.replace('\n', ', ').replace('\r', '').replace('#', '')
163                                         self.outFun(no, result)
164                         else:
165                                 if entry.has_key('telephoneNumber'):
166                                         no = normalizePhoneNumber(entry['telephoneNumber'][0])
167                                         result = nameB.replace('\n', ', ').replace('\r', '').replace('#', '')
168                                         self.outFun(no, result)
169                 for i in ['homePhone', 'mobile']:
170                         if entry.has_key(i):
171                                 no = normalizePhoneNumber(entry[i][0])
172                                 if i == 'mobile':
173                                         nameHM = name + ' (' + _('mobile') + ')'
174                                 else:
175                                         nameHM = name + ' (' + _('home') + ')'
176                                 if entry.has_key('mozillaHomeLocalityName'):
177                                         address = entry['mozillaHomeLocalityName'][0]
178                                         if entry.has_key('mozillaHomePostalCode'):
179                                                 address = entry['mozillaHomePostalCode'][0] + ' ' + address
180                                         if entry.has_key('mozillaHomeCountryName'):
181                                                 address = address + ', ' + entry['mozillaHomeCountryName'][0]
182                                         result = nameHM + ', ' + address.replace('\n', ', ').replace('\r', '').replace('#', '')
183                                         self.outFun(no, result)
184                                 else:
185                                         if addressB:
186                                                 nameHM = nameHM + ', ' + addressB.replace('\n', ', ').replace('\r', '').replace('#', '')
187                                         self.outFun(no, nameHM)
188
189 def lookedUp(number, name):
190         print number + ' ' + name
191
192 if __name__ == '__main__':
193         import os, sys
194         cwd = os.path.dirname(sys.argv[0])
195         if (len(sys.argv) == 1):
196                 ReadNumbers(open("Kontakte.ldif"), out)
197         elif (len(sys.argv) == 2):
198                 # nrzuname.py Nummer
199                 FindNumber(sys.argv[1], open("Kontakte.ldif"), lookedUp)