-
Notifications
You must be signed in to change notification settings - Fork 1
/
information.py
55 lines (40 loc) · 1.51 KB
/
information.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
#!/usr/bin/env python3
#
# Copyright (c) 2024 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 html import escape
from lxml import etree
from flask import Blueprint, Response, request
blueprint = Blueprint('information', __name__)
@blueprint.route('/information')
def help_information():
id = request.args.get('id', '')
if not re.search(r'(?x) ^ [0-9]+ $', id):
return Response('Invalid id', mimetype = 'text/plain'), 500
document = etree.parse('./phone_help.xml')
if not document:
return Response('XML error', mimetype = 'text/plain'), 500
element = document.find(f'HelpItem[ID="{id}"]')
if element:
title = element.find('Title').text
text = element.find('Text').text
else:
title = 'Information'
text = 'Sorry, no help on that topic.'
xml = '<?xml version="1.0" encoding="UTF-8"?>' \
'<CiscoIPPhoneText>' \
'<Title>' + escape(title) + '</Title>' \
'<Text>' + escape(text) + '</Text>' \
'<Prompt>Your current options</Prompt>' \
'<SoftKeyItem>' \
'<Name>Exit</Name>' \
'<Position>3</Position>' \
'<URL>Key:Info</URL>' \
'</SoftKeyItem>' \
'</CiscoIPPhoneText>'
return Response(xml, mimetype = 'text/xml'), 200
@blueprint.errorhandler(Exception)
def error_handler(error):
return Response(str(error), mimetype = 'text/plain'), 500