-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdirectory.py
More file actions
184 lines (139 loc) · 6.43 KB
/
directory.py
File metadata and controls
184 lines (139 loc) · 6.43 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
#
# Copyright (c) 2020 Gareth Palmer <gareth.palmer3@gmail.com>
# This program is free software, distributed under the terms of
# the GNU General Public License Version 2.
import re
from math import ceil
from urllib.parse import quote_plus
from html import escape
import requests
from lxml import etree
from flask import Blueprint, Response, request, g
import config
blueprint = Blueprint('directory', __name__)
@blueprint.route('/directory')
def directory_index():
xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<CiscoIPPhoneMenu>\n'
' <Title>Local Directory</Title>\n')
for index in ('1', '2ABC', '3DEF', '4GHI', '5JKL', '6MNO', '7PRQS', '8TUV', '9WXYZ', '0'):
xml += (' <MenuItem>\n'
' <Name>' + escape(index) + '</Name>\n'
' <URL>' + request.url_root + 'directory/entries/' + quote_plus(index) + '</URL>\n'
' </MenuItem>\n')
if g.is_79xx:
xml += ' <Prompt>Your current options</Prompt>\n'
xml += (' <SoftKeyItem>\n'
' <Name>Exit</Name>\n'
' <Position>' + ('3' if g.is_79xx else '1') + '</Position>\n'
' <URL>Init:Directories</URL>\n'
' </SoftKeyItem>\n'
' <SoftKeyItem>\n'
' <Name>' + ('Select' if g.is_79xx else 'View') + '</Name>\n'
' <Position>' + ('1' if g.is_79xx else '2') + '</Position>\n'
' <URL>SoftKey:Select</URL>\n'
' </SoftKeyItem>\n'
' <SoftKeyItem>\n'
' <Name>Help</Name>\n'
' <Position>' + ('2' if g.is_79xx else '3') + '</Position>\n'
' <URL>' + request.url_root + 'directory/help</URL>\n'
' </SoftKeyItem>\n'
'</CiscoIPPhoneMenu>\n')
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.route('/directory/entries/<index>')
def directory_entries(index):
if not re.search(r'(?x) ^ [A-Z0-9]+ $', index):
return directory_index()
session = requests.Session()
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'Login',
'Username': config.manager_username,
'Secret': config.manager_secret})
response.raise_for_status()
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'VoicemailUsersList'})
response.raise_for_status()
document = etree.fromstring(response.content)
entries = []
for element in document.findall('response/generic[@event="VoicemailUserEntry"]'):
mailbox = element.get('voicemailbox')
name = element.get('fullname', '')
if not len(name) or name[0].upper() not in index:
continue
entries.append((mailbox, name))
entries.sort(key = lambda entry: entry[1])
response = session.get(config.manager_url, timeout = 5, params = {'Action': 'Logoff'})
response.raise_for_status()
# 10 entries per page
pages = ceil(len(entries) / 10)
try:
page = int(request.args.get('page', '1'))
if page > pages:
raise ValueError
except ValueError:
page = 1
xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<CiscoIPPhoneDirectory>\n'
' <Title>' + escape(index) + (' ' + str(page) + '/' + str(pages) if pages > 1 else '') + '</Title>\n')
for mailbox, name in entries[(page - 1) * 10:page * 10]:
xml += (' <DirectoryEntry>\n'
' <Name>' + escape(name) + '</Name>\n'
' <Telephone>' + quote_plus(mailbox) + '</Telephone>\n'
' </DirectoryEntry>\n')
if g.is_79xx:
xml += ' <Prompt>Select entry</Prompt>\n'
xml += (' <SoftKeyItem>\n'
' <Name>Exit</Name>\n'
' <Position>' + ('3' if g.is_79xx else '1') + '</Position>\n'
' <URL>' + request.url_root + 'directory</URL>\n'
' </SoftKeyItem>\n'
' <SoftKeyItem>\n'
' <Name>' + ('Dial' if g.is_79xx else 'Call') + '</Name>\n'
' <Position>' + ('1' if g.is_79xx else '2') + '</Position>\n'
' <URL>SoftKey:Select</URL>\n'
' </SoftKeyItem>\n')
if page < pages:
xml += (' <SoftKeyItem>\n'
' <Name>Next</Name>\n'
' <URL>' + request.url_root + 'directory/entries/' + quote_plus(index) + '?page=' + str(page + 1) + '</URL>\n'
' <Position>' + ('2' if g.is_79xx else '3') + '</Position>\n'
' </SoftKeyItem>\n')
if page > 1:
xml += (' <SoftKeyItem>\n'
' <Name>Previous</Name>\n'
' <URL>' + request.url_root + 'directory/entries/' + quote_plus(index) + '?page=' + str(page - 1) + '</URL>\n'
' <Position>' + ('4' if g.is_79xx else '4') + '</Position>\n'
' </SoftKeyItem>\n')
xml += '</CiscoIPPhoneDirectory>\n'
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.route('/directory/help')
def directory_help():
xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<CiscoIPPhoneText>\n'
' <Title>How To Use</Title>\n'
' <Text>Use the keypad or navigation key to select the first letter of the person\'s name.</Text>\n')
if g.is_79xx:
xml += ' <Prompt>Your current options</Prompt>\n'
xml += (' <SoftKeyItem>\n'
' <Name>Back</Name>\n'
' <URL>SoftKey:Exit</URL>\n'
' <Position>' + ('3' if g.is_79xx else '1') + '</Position>\n'
' </SoftKeyItem>\n'
'</CiscoIPPhoneText>\n')
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.route('/directory/79xx')
def directory_menuitem():
# 79xx series need a menu item before the index
xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
'<CiscoIPPhoneMenu>\n'
' <MenuItem>\n'
' <Name>Local Directory</Name>\n'
' <URL>' + request.url_root + 'directory</URL>\n'
' </MenuItem>\n'
'</CiscoIPPhoneMenu>\n')
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.before_request
def before_request():
g.is_79xx = re.search(r'(?x) ^ CP-79', request.headers.get('X-CiscoIPPhoneModelName', ''))
@blueprint.errorhandler(Exception)
def error_handler(error):
return Response(str(error), mimetype = 'text/plain'), 500