Add Phone contacts

This commit is contained in:
2026-08-28 23:20:29 +02:00
parent 258e6a0d12
commit ef296919c0
+30 -3
View File
@@ -20,7 +20,7 @@ import hashlib
import hmac
VERSION: str = '0.0.7'
VERSION: str = '0.0.8'
CNX: sqlite3.Cursor
USER_FILE: str
@@ -150,7 +150,7 @@ def get_delivery_schedule_filter_clauses(filters: list[Filter]) -> list[str]:
class ElwigApi(BaseHTTPRequestHandler):
def send(self, data: str, status_code: int = 200, url: str = None) -> None:
def send(self, data: str, status_code: int = 200, url: str = None, content_type: str = 'application/json') -> None:
raw = data.encode('utf-8')
self.send_response(status_code)
self.send_header('Access-Control-Allow-Origin', '*')
@@ -168,7 +168,7 @@ class ElwigApi(BaseHTTPRequestHandler):
if 'gzip' in accept_encoding:
raw = gzip.compress(raw)
self.send_header('Content-Encoding', 'gzip')
self.send_header('Content-Type', 'application/json; charset=UTF-8')
self.send_header('Content-Type', content_type + '; charset=UTF-8')
self.send_header('Content-Length', str(len(raw)))
self.end_headers()
if self.request.type != 'HEAD' and self.request.type != 'OPTIONS':
@@ -368,6 +368,29 @@ class ElwigApi(BaseHTTPRequestHandler):
f'"announcement_to":{jdmp(r[9])}}}',
filters, offset, limit, distinct=(['s.year', 's.dsnr'], [1, 2]))
def do_GET_contacts(self):
data = '<?xml version="1.0" encoding="UTF-8"?>\n'
data += '<AddressBook>\n'
rows = CNX.execute("""
SELECT m.mgnr, m.name AS name_1,
COALESCE(m.given_name, '') AS name_2,
p.comment, REPLACE(p.number, '+43 ', '0') AS number
FROM member m
JOIN member_telephone_number p ON p.mgnr = m.mgnr
ORDER BY m.name, m.given_name, m.mgnr, p.nr
""")
for r in rows:
# max field len = 18
extra = f' - {r[3]}' if r[3] else ''
mgnr = f' ({r[0]})'
last_name = r[1].upper() # MUSTERMANN
first_name = (r[2] if len(r[2]) + len(mgnr) <= 18 else r[2][:18 - len(mgnr) - 1] + '.') + mgnr # Max (123) - Erika
if 18 - len(first_name) > 4:
first_name += extra
data += f' <Contact><LastName>{last_name}</LastName><FirstName>{first_name}</FirstName><Phone type="Work"><phonenumber>{r[4]}</phonenumber><accountindex>0</accountindex></Phone></Contact>\n'
data += '</AddressBook>\n'
self.send(data, content_type='application/xml')
def do_HEAD(self) -> None:
self.do_GET()
@@ -386,6 +409,10 @@ class ElwigApi(BaseHTTPRequestHandler):
username, role, auth_method = self.authorize()
if self.path == '/contacts/grandstream/phonebook.xml':
self.do_GET_contacts()
return
parts = self.path.split('?', 1)
if len(parts) == 1:
path, query = parts[0], {}