Commit d2a6a7e9 authored by Citronalco's avatar Citronalco
Browse files

inital commit

parent ac7df02f
# CardDAV-to-Asterisk import
This script reads all contacts from a CardDAV addressbook and puts them into Asterisk's internal caller id database.
That way Asterisk can show a caller's name instead of just the number.
If there's already an entry in Asterisk's caller database for a specific number, this script updates the number with the new name.
So far I have only tested with OwnCloud 9.
### Usage
#### Configuration
1. This script uses Asterisk Manager to access the caller id database, so you need to set up an user with "system" permissions if you have not already done so. To add an user create the file "/etc/asterisk/manager.d/carddav2asterisk.conf":
```[carddavimport]
secret = cidpwd
permit = 127.0.0.1/255.255.255.0
read = system
write = system```
1. Open the script in an editor and edit the user's credentials and host and port of Asterisk's Manager interface:```
# ASTERISK MANAGER CONNECTION
HOST = 'localhost'
PORT = 5038
USER = 'carddavimport'
PASS = 'cidpwd'```
1. Change NATIONALPREFIX and DOMESTICPREFIX to match your location:```
# PERSONAL SETTINGS
NATIONALPREFIX = "0049"
DOMESTICPREFIX = "0841"```
1. If you haven't configured CID lookup in Asterisk yet, you may want to add something like this somewhere at the beginning of your dialplan in extensions.conf: ``exten => <yourExtension>,n,Set(CALLERID(name)=${IF(${DB_EXISTS(cidname/${CALLERID(num)})}?${DB(cidname/${CALLERID(num)})}:${CALLERID(name)})})``
#### How to run the script
``./carddav2asterisk.py <URl to CardDAV addressbook> <username> <password>``
Example:
``./carddav2asterisk.py https://owncloud.example.com/remote.php/dav/addressbooks/users/russmeyer/contacts/ meyerr p8a55w0rd``
#!/usr/bin/env python
import sys
import requests
import vobject
import re
import time
from asterisk import manager
from requests.auth import HTTPBasicAuth
from lxml import etree
from urlparse import urlparse
# ASTERISK MANAGER CONNECTION
HOST = 'localhost'
PORT = 5038
USER = 'carddavimport'
PASS = 'cidpwd'
# PERSONAL SETTINGS
NATIONALPREFIX = "0049"
DOMESTICPREFIX = "0841"
# get list with links to all available vcards
def getAllVcardLinks(url,auth):
baseurl = urlparse(url).scheme+'://'+urlparse(url).netloc
r = requests.request('PROPFIND',url,auth=auth)
root = etree.XML(r.content)
vcardUrlList=[]
for record in root.xpath(".//d:response",namespaces={"d" : "DAV:"}):
type = record.xpath(".//d:getcontenttype",namespaces={"d" : "DAV:"})
if (type) and type[0].text.startswith("text/vcard"):
vcardlinks = record.xpath(".//d:href",namespaces={"d" : "DAV:"})
for link in vcardlinks:
vcardUrlList.append(baseurl + '/' + link.text);
return vcardUrlList
def tidyPhoneNumber(num):
num = re.sub("^\+","00",num) # +39 -> 0039
num = re.sub("\D","",num) # remove all non-digits
num = re.sub("^"+NATIONALPREFIX+"0*","0",num) # strip own national prefix
num = re.sub("^[^0]","0"+DOMESTICPREFIX,num) # add domestic prefix, if missing
return num
def main(argv):
auth = HTTPBasicAuth(argv[2],argv[3])
url = argv[1]
# connect to asterisk
ami = manager.Manager()
ami.connect(HOST)
ami.login(USER,PASS)
# get phone numbers from vcard
starttime=int(time.time());
for vurl in getAllVcardLinks(url,auth):
r = requests.request("GET",vurl,auth=auth)
vcard = vobject.readOne(r.content)
if "tel" in vcard.contents:
for telno in vcard.contents['tel']:
num = tidyPhoneNumber(telno.value)
if "fn" in vcard.contents:
name = vcard.fn.value
print "Adding/updating Number: "+num+" Name: "+name
ami.send_action({"Action": "DBPut", "Family": "cidname", "Key": num, "Val": name})
ami.logoff()
ami.close()
if __name__ == "__main__":
if len(sys.argv)!=4:
print "Must be called with eight arguments: <carddav-url> <carddav-user> <carddav-password>"
print "Example: %s https://owncloud.example.com/remote.php/dav/addressbooks/users/russmeyer/contacts/ meyerr p8a55w0rd" % sys.argv[0]
sys.exit(1)
sys.exit(main(sys.argv))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment