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