Phonebook - mknx/smarthome GitHub Wiki
This phonebook logic allows the user (a.k.a. wife) to add an entry to the snom xml address book and asterisk database without a special interface. She just has to send an email with a vcf attachment to [email protected].
# etc/logic.conf
[phonebook]
filename = phonebook.py
mail_to = [email protected]
#!/usr/bin/env python
# vim: set encoding=utf-8 tabstop=4 softtabstop=4 shiftwidth=4 expandtab
#########################################################################
#
# logics/phonebook.py
#
if not trigger['source'].endswith('@mydomain.com'): # ignore mails from third party domains
exit() # for security reasons you should (if you are able to)
# restrict clients sending your domain (mydomain.com)
def parse_vcf(vcf):
# dict to translate name of the phone types
types = {'CELL': 'Handy', 'HOME': 'Privat', 'WORK': 'Arbeit', 'IPHONE': 'iPhone'}
name = False
for line in vcf.splitlines():
if line.startswith('FN:'):
name = line.split(':')[-1]
if name:
for line in vcf.splitlines():
if line.startswith('TEL'):
typ, number = line.rsplit(':', 1)
number = number.translate(None, '-() ')
if not number.startswith('+'):
if number.startswith('00'):
number = number.replace('00', '+', 1)
elif number.startswith('0'):
number = number.replace('0', '+49', 1)
typ = typ.split(';')[1].split('=')[1]
if typ in types:
typ = types[typ]
else:
typ = typ.capitalize()
yield ("{0} ({1})".format(name, typ), number)
mail = trigger['value']
for part in mail.walk():
c_type = part.get_content_type()
c_disp = part.get('Content-Disposition')
if c_disp != None:
if c_disp.startswith('attachment'): # there is an attachement
filename = part.get_filename()
if filename.endswith('.vcf'): # and it ends with vcf
vcf = part.get_payload(decode=True)
for name, number in parse_vcf(vcf):
sh.snom.phonebook_add(name, number) # add it to the snome xml addressbook
sh.ast.db_write('known/' + number, name) # and to the asterisk database