-
Notifications
You must be signed in to change notification settings - Fork 2
/
directories.py
109 lines (90 loc) · 3.37 KB
/
directories.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
print "Content-Type: text/xml"
print
from lxml.builder import E
from lxml import etree
import hashlib
import cgi
import pyodbc
connection = pyodbc.connect('DRIVER={MySQL};DSN=sipdb')
cursor = connection.cursor()
def create_base_directory_xml_doc():
doc = (
E.document(
E.section(name="directory"),
type="freeswitch/xml"
)
)
return doc
def not_found():
doc = (
E.document(
E.section(name="result"),
type="freeswitch/xml"
)
)
result = E.result(status="not found")
section = doc.find("section")
section.append(result)
return doc
def hash_password(domain, username, password):
hash = hashlib.md5()
hash.update(username + ":" + domain + ":" + password)
password_hash = hash.hexdigest()
password_param = "a1-hash"
return password_param, password_hash
def add_directory_domain_user(doc, domain, username, password, user_context, callgroup):
section = doc.find("section")
dom = (
E.domain(E.params(E.param(
name="dial-string",
value='{^^:sip_invite_domain=${dialed_domain}:presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(*/${dialed_user}@${dialed_domain})}')),
E.groups(), name=domain))
section.append(dom)
grp = E.group(E.users(), name=domain)
groups = dom.find("groups")
groups.append(grp)
usr = E.user(
E.params(
E.param(name="password", value=password),
E.param(name="vm-password", value=password),
E.param(name="register", value="false"),
E.param(name="register-transport", value="ws"),
# E.param(name="session-timeout", value="true"),
# E.param(name="minimum-session-expires", value="120"),
E.param(name="rtp-timer-name", value="soft"),
E.param(name="apply-inbound-acl", value="goip"),
),
E.variables(
E.variable(name="toll_allow", value="domestic,international,local"),
E.variable(name="user_context", value=user_context),
E.variable(name="accountcode", value=username),
E.variable(name="effective_caller_id_name", value=username),
E.variable(name="effective_caller_id_number", value=username),
E.variable(name="outbound_caller_id_name", value="$${outbound_caller_name}"),
E.variable(name="outbound_caller_id_number", value="$${outbound_caller_id}"),
E.variable(name="callgroup", value=callgroup),
E.variable(name="rtp_secure_media", value="true"),
E.variable(name="max_calls", value="1"),
),
id=username
)
users = grp.find("users")
users.append(usr)
def select_by_username(username):
row = cursor.execute("select * from fs_users WHERE accountcode=?", username).fetchone()
return row
form = cgi.FieldStorage()
domain = form.getvalue('domain', None)
username = form.getvalue('user', None)
if domain and username:
user = select_by_username(username)
if user:
document = create_base_directory_xml_doc()
add_directory_domain_user(document, domain, username, user.password, user.user_context, user.callgroup)
else:
document = not_found()
else:
document = not_found()
print(etree.tostring(document, encoding='UTF-8', standalone=False, pretty_print=True))