Add support for preview of conferences;

Fix some errors;
Improve display of node items of type Atom Over XMPP (XEP-0277 and XEP-0472).
This commit is contained in:
Schimon Jehudah, Adv. 2024-10-13 18:42:44 +03:00
parent 44718051d0
commit aa90d922b0
10 changed files with 758 additions and 80 deletions

474
fasi.py
View file

@ -16,7 +16,7 @@ import qrcode
import random
import re
from slixmpp import ClientXMPP, stanza
from slixmpp.exceptions import IqError, IqTimeout
from slixmpp.exceptions import IqError, IqTimeout, PresenceError
from starlette.responses import RedirectResponse
#import time
import tomli_w
@ -57,7 +57,7 @@ class XmppInstance(ClientXMPP):
#self.disconnect()
class HttpInstance:
def __init__(self, jabber_id, password):
def __init__(self, jabber_id, password, alias):
self.app = FastAPI()
templates = Jinja2Templates(directory='xhtml')
@ -78,9 +78,231 @@ class HttpInstance:
# def logo_get():
# return FileResponse('graphic/hermes.svg')
@self.app.get('/v/{jid}')
async def view_jid(request: Request, jid):
"""View messages of jabber id"""
jid_path = urlsplit(jid).path
if parseaddr(jid_path)[1] == jid_path:
jid_bare = jid_path.lower()
else:
jid_bare = jid
note = 'Jabber ID appears to be malformed'
try:
exception = node_title = note = number_of_pages = page_number = previous = selection = services_sorted = None
node_name = 'urn:xmpp:microblog:0'
link_href = 'xmpp:{}?join'.format(jid_bare)
link_text = 'Join'
xmpp_uri = '{}'.format(jid_bare)
# Start an XMPP instance and retrieve information
xmpp_instance = XmppInstance(jabber_id, password, jid_bare)
xmpp_instance.connect()
# JID kind
instance = message = node_id = None
jid_info = await XmppXep0030.get_jid_info(xmpp_instance, jid_bare)
jid_info_iq = jid_info['iq']
jid_kind = jid_info['kind']
links = []
if jid_info['error']:
message = '{}: {} (XEP-0030)'.format(jid_info['text'], jid_info['condition'])
action = 'Connect with'
link_href = 'xmpp:{}'.format(jid_bare)
links.append(['Connect', link_href])
xmpp_uri = jid_bare
elif jid_kind in ('conference', 'server'):
action = 'Discover'
if jid_kind == 'conference':
instance = 'conferences'
elif jid_kind == 'server':
instance = 'services'
link_href = 'xmpp:{}?disco;type=get;request=items'.format(jid_bare)
links.append(['Discover', link_href])
view_href = '/d/' + jid_bare
xmpp_uri = jid_bare
elif jid_kind in ('mix', 'muc'):
#title = 'Group Chat ' + title
# TODO Set group chat subject as description.
action = 'Join to'
instance = 'participants'
link_href = 'xmpp:{}?join'.format(jid_bare)
links.append(['Join', link_href])
view_href = '/v/' + jid_bare
xmpp_uri = jid_bare
# room_info = await XmppXep0045.get_room_data(xmpp_instance, jid_bare)
# breakpoint()
elif jid_kind == 'pubsub':
#node_name = request.query_params.get('node', '')
if node_name:
action = 'Subscribe to'
instance = 'articles'
link_href = 'xmpp:{}?pubsub;node={};action=subscribe'.format(jid_bare, node_name)
view_href = '/d/{}/{}'.format(jid_bare, node_name)
xmpp_uri = '{}?;node={}'.format(jid_bare, node_name)
else:
action = 'Browse'
instance = 'nodes'
link_href = 'xmpp:{}?disco;type=get;request=items'.format(jid_bare)
links.append(['Browse', link_href])
view_href = '/d/' + jid_bare
xmpp_uri = jid_bare
else:
action = 'Message'
instance = 'articles'
link_href = 'xmpp:{}?message'.format(jid_bare)
links.append(['Add', 'xmpp:{}?roster'.format(jid_bare)])
links.append(['Message', link_href])
node_name = 'urn:xmpp:microblog:0'
view_href = '/d/{}/{}'.format(jid_bare, node_name)
xmpp_uri = jid_bare
links.append(['Subscribe', 'xmpp:{}?pubsub;node={};action=subscribe'.format(jid_bare, node_name)])
links.append(['View', 'xmpp:{}?pubsub;node={}'.format(jid_bare, node_name)])
links.append(['vCard', 'xmpp:{}?vcard'.format(jid_bare)])
# JID info
# NOTE Group chat of Psi+ Project at jabber.ru has a note in its vCard.
# TODO Retrieve group chat title (try also with xep_0045
vcard_data = await XmppXep0054.get_vcard_data(xmpp_instance, jid_bare)
if vcard_data['error']:
jid_detail = {}
#jid_detail['note'] = '{}: {}'.format(vcard_data['text'], vcard_data['condition'])
jid_detail['name'] = jid_detail['note'] = jid_detail['note'] = jid_detail['type'] = jid_detail['bin'] = None
else:
conference_title = None
if jid_kind in ('mix', 'muc'):
for identity in jid_info_iq['disco_info']['identities']:
if identity[3]:
conference_title = identity[3]
break
vcard_temp = vcard_data['iq']['vcard_temp']
jid_detail = {
'name' : vcard_temp['FN'] or conference_title,
'note' : vcard_temp['notes'] or node_id,
'type' : vcard_temp['PHOTO']['TYPE'],
'bin' : vcard_temp['PHOTO']['BINVAL']
}
# Group chat messages
action = 'Join'
messages = []
room_info_muc = await XmppXep0045.get_room_information(xmpp_instance, jid_bare, alias, maxstanzas=50)
messages = room_info_muc['iq'][3]
messages.reverse()
subject = room_info_muc['iq'][1]['subject']
page_number = request.query_params.get('page', '')
if page_number:
try:
page_number = int(page_number)
ix = (page_number -1) * 10
except:
ix = page_number = 0
else:
ix = page_number = 0
messages_10 = messages[ix:][:10]
number_of_pages = int(len(messages) / 10)
if number_of_pages < len(messages) / 10: number_of_pages += 1
if not room_info_muc:
action = 'Warning'
node_title = jid_info['condition']
node_note = jid_info['text']
services = services_sorted = None
elif isinstance(room_info_muc, IqTimeout):
action = 'Warning'
node_title = 'Timeout'
node_note = 'Timeout error'
services = services_sorted = None
elif isinstance(room_info_muc, IqError):
action = 'Warning'
breakpoint()
node_title = room_info_muc['condition']
node_note = room_info_muc['text']
services = services_sorted = None
else:
#title = title or node_name
if not node_title: node_title = node_name
node_note = jid_bare
xmpp_instance.disconnect()
# Notes
jid_detail_note = jid_detail['note']
if isinstance(jid_detail_note, list) and len(jid_detail_note):
note = jid_detail_note[0]['NOTE']
else:
note = jid_detail_note
#if not note and jid_detail['name'] and not 'undefined' in jid_detail['name'] and title != jid_detail['name']:
# note = jid_detail['name']
# File type
mimetype = filename = filepath = None
if jid_detail['type']:
mimetype = jid_detail['type']
if mimetype:
filetype = mimetype.split('/')[1]
if filetype == 'svg+xml': filetype = 'svg'
filename = '{}.{}'.format(jid_bare, filetype)
filepath = 'photo/{}.{}'.format(jid_bare, filetype)
#img.save(filename)
# Write the decoded bytes to a file
with open(filepath, 'wb') as file:
file.write(jid_detail['bin'])
#from PIL import Image
#img = Image.open(filepath)
#rgb_im = im.convert("RGB")
#rgb_im.save('{}_mod.jpg'.format(jid_bare))
# Default photo. Utilized, if there is no image file.
if not filepath or not os.path.exists(filepath) or os.path.getsize(filepath) == 0:
filename = 'default.svg'
elif filetype == 'svg':
selection = Graphics.extract_colours_from_vector(filepath)
else:
selection = Graphics.extract_colours_from_raster(filepath)
# QR code
Graphics.generate_qr_code_graphics_from_string(link_href, jid_bare)
except Exception as e:
exception = str(e)
action = 'Error'
title = 'Slixmpp error'
xmpp_uri = note = jid
filename = jid_bare = link_href = link_tex = node_note = node_title = number_of_pages = page_number = previous = selection = services = services_sorted = url = None
#if title == 'remote-server-timeout':
# raise HTTPException(status_code=408, detail='remote-server-timeout')
#else:
template_file = 'conference.xhtml'
template_dict = {
'action' : action,
'exception' : exception,
'filename' : filename,
'jid_bare' : jid,
'jid_note' : jid_detail['note'],
'jid_title' : jid_detail['name'],
'links' : links,
'messages' : messages_10,
'node_title' : node_title,
'node_note' : node_note,
'node_name' : node_name,
'number_of_pages' : number_of_pages,
'page_number' : page_number,
'previous' : previous,
'request' : request,
'subject' : subject,
'url' : request.url._url,
'xmpp_uri' : xmpp_uri}
response = templates.TemplateResponse(template_file, template_dict)
response.headers['Content-Type'] = 'application/xhtml+xml'
return response
# NOTE Was /b/
@self.app.get('/d/{jid}/{node_name}')
async def browse_jid_node_get(request: Request, jid, node_name):
@self.app.get('/d/{jid}/{node_name}/{item_id}')
async def browse_jid_node_get(request: Request, jid, node_name, item_id=None):
"""Browse items of a pubsub node"""
jid_path = urlsplit(jid).path
if parseaddr(jid_path)[1] == jid_path:
@ -90,8 +312,8 @@ class HttpInstance:
note = 'Jabber ID appears to be malformed'
try:
exception = note = selection = services_sorted = None
title = node_name
exception = note = number_of_pages = page_number = previous = selection = services_sorted = None
node_title = node_name
link_href = 'xmpp:{}?pubsub;node={};action=subscribe'.format(jid_bare, node_name)
link_text = 'Subscribe'
xmpp_uri = '{}?;node={}'.format(jid_bare, node_name)
@ -100,9 +322,92 @@ class HttpInstance:
xmpp_instance = XmppInstance(jabber_id, password, jid_bare)
xmpp_instance.connect()
# JID kind
instance = message = node_id = None
jid_info = await XmppXep0030.get_jid_info(xmpp_instance, jid_bare)
jid_info_iq = jid_info['iq']
jid_kind = jid_info['kind']
links = []
if jid_info['error']:
message = '{}: {} (XEP-0030)'.format(jid_info['text'], jid_info['condition'])
action = 'Connect with'
link_href = 'xmpp:{}'.format(jid_bare)
links.append(['Connect', link_href])
xmpp_uri = jid_bare
elif jid_kind in ('conference', 'server'):
action = 'Discover'
if jid_kind == 'conference':
instance = 'conferences'
elif jid_kind == 'server':
instance = 'services'
link_href = 'xmpp:{}?disco;type=get;request=items'.format(jid_bare)
links.append(['Discover', link_href])
view_href = '/d/' + jid_bare
xmpp_uri = jid_bare
elif jid_kind in ('mix', 'muc'):
#title = 'Group Chat ' + title
# TODO Set group chat subject as description.
action = 'Join to'
instance = 'participants'
link_href = 'xmpp:{}?join'.format(jid_bare)
links.append(['Join', link_href])
view_href = '/v/' + jid_bare
xmpp_uri = jid_bare
# room_info = await XmppXep0045.get_room_data(xmpp_instance, jid_bare)
# breakpoint()
elif jid_kind == 'pubsub':
#node_name = request.query_params.get('node', '')
if node_name:
action = 'Subscribe to'
instance = 'articles'
link_href = 'xmpp:{}?pubsub;node={};action=subscribe'.format(jid_bare, node_name)
view_href = '/d/{}/{}'.format(jid_bare, node_name)
xmpp_uri = '{}?;node={}'.format(jid_bare, node_name)
else:
action = 'Browse'
instance = 'nodes'
link_href = 'xmpp:{}?disco;type=get;request=items'.format(jid_bare)
links.append(['Browse', link_href])
view_href = '/d/' + jid_bare
xmpp_uri = jid_bare
else:
action = 'Message'
instance = 'articles'
link_href = 'xmpp:{}?message'.format(jid_bare)
links.append(['Add', 'xmpp:{}?roster'.format(jid_bare)])
links.append(['Message', link_href])
node_name = 'urn:xmpp:microblog:0'
view_href = '/d/{}/{}'.format(jid_bare, node_name)
xmpp_uri = jid_bare
links.append(['Subscribe', 'xmpp:{}?pubsub;node={};action=subscribe'.format(jid_bare, node_name)])
links.append(['View', 'xmpp:{}?pubsub;node={}'.format(jid_bare, node_name)])
links.append(['vCard', 'xmpp:{}?vcard'.format(jid_bare)])
# JID info
# NOTE Group chat of Psi+ Project at jabber.ru has a note in its vCard.
vcard_data = await XmppXep0054.get_vcard_data(xmpp_instance, jid_bare)
if vcard_data['error']:
jid_detail = {}
#jid_detail['note'] = '{}: {}'.format(vcard_data['text'], vcard_data['condition'])
jid_detail['name'] = jid_detail['note'] = jid_detail['note'] = jid_detail['type'] = jid_detail['bin'] = None
else:
conference_title = None
if jid_kind in ('mix', 'muc'):
for identity in jid_info_iq['disco_info']['identities']:
if identity[3]:
conference_title = identity[3]
break
vcard_temp = vcard_data['iq']['vcard_temp']
jid_detail = {
'name' : vcard_temp['FN'] or conference_title,
'note' : vcard_temp['notes'] or node_id,
'type' : vcard_temp['PHOTO']['TYPE'],
'bin' : vcard_temp['PHOTO']['BINVAL']
}
# Title
if '@' in jid_bare and node_name == 'urn:xmpp:microblog:0':
title = 'Journal'
node_title = 'Journal'
else:
jid_items = await XmppXep0030.get_jid_items(xmpp_instance, jid_bare)
iq = jid_items['iq']
@ -111,47 +416,111 @@ class HttpInstance:
category = 'unsorted'
for item in iq_disco_items_items:
if item[2] and item[1] == node_name:
title = item[2]
node_title = item[2]
break
# Node items
action = 'Browse'
entries = []
node_items = await XmppXep0060.get_node_items(xmpp_instance, jid_bare, node_name)
if item_id:
previous = True
node_items = await XmppXep0060.get_node_items(xmpp_instance, jid_bare, node_name, item_ids=[item_id])
else:
item_ids = []
node_item_ids = await XmppXep0060.get_node_item_ids(xmpp_instance, jid_bare, node_name)
for item_id in node_item_ids['disco_items']['items']:
item_ids.append(item_id[2])
# NOTE Consider to skip the reversal of order, because, then, items can be found at the same page.
item_ids.reverse()
page_number = request.query_params.get('page', '')
if page_number:
try:
page_number = int(page_number)
ix = (page_number -1) * 10
except:
ix = page_number = 0
else:
ix = page_number = 0
item_ids_10 = item_ids[ix:][:10]
node_items = await XmppXep0060.get_node_items(xmpp_instance, jid_bare, node_name, item_ids=item_ids_10)
number_of_pages = int(len(item_ids) / 10)
if number_of_pages < len(item_ids) / 10: number_of_pages += 1
if not node_items:
action = 'Warning'
title = jid_info['condition']
note = jid_info['text']
node_title = jid_info['condition']
node_note = jid_info['text']
services = services_sorted = None
elif isinstance(node_items, IqTimeout):
action = 'Warning'
title = 'Timeout'
note = 'Timeout error'
node_title = 'Timeout'
node_note = 'Timeout error'
services = services_sorted = None
elif isinstance(node_items, IqError):
action = 'Warning'
breakpoint()
title = node_items['condition']
note = node_items['text']
node_title = node_items['condition']
node_note = node_items['text']
services = services_sorted = None
else:
#title = title or node_name
if not title: title = node_name
note = jid_bare
if not node_title: node_title = node_name
node_note = jid_bare
for item in node_items['pubsub']['items']:
item_payload = item['payload']
entry = Syndication.extract_items(item_payload)
entry['id'] = item['id']
entries.append(entry)
#if len(entries) > 10: break
if entries: entries.reverse()
xmpp_instance.disconnect()
# Notes
jid_detail_note = jid_detail['note']
if isinstance(jid_detail_note, list) and len(jid_detail_note):
note = jid_detail_note[0]['NOTE']
else:
note = jid_detail_note
#if not note and jid_detail['name'] and not 'undefined' in jid_detail['name'] and title != jid_detail['name']:
# note = jid_detail['name']
# File type
mimetype = filename = filepath = None
if jid_detail['type']:
mimetype = jid_detail['type']
if mimetype:
filetype = mimetype.split('/')[1]
if filetype == 'svg+xml': filetype = 'svg'
filename = '{}.{}'.format(jid_bare, filetype)
filepath = 'photo/{}.{}'.format(jid_bare, filetype)
#img.save(filename)
# Write the decoded bytes to a file
with open(filepath, 'wb') as file:
file.write(jid_detail['bin'])
#from PIL import Image
#img = Image.open(filepath)
#rgb_im = im.convert("RGB")
#rgb_im.save('{}_mod.jpg'.format(jid_bare))
# Default photo. Utilized, if there is no image file.
if not filepath or not os.path.exists(filepath) or os.path.getsize(filepath) == 0:
filename = 'default.svg'
elif filetype == 'svg':
selection = Graphics.extract_colours_from_vector(filepath)
else:
selection = Graphics.extract_colours_from_raster(filepath)
# QR code
Graphics.generate_qr_code_graphics_from_string(link_href, jid_bare)
except Exception as e:
exception = str(e)
action = 'Error'
title = 'Slixmpp error'
xmpp_uri = note = jid
filename = jid_bare = services = url = link_href = link_text = None
filename = jid_bare = link_href = link_tex = node_note = node_title = number_of_pages = page_number = previous = selection = services = services_sorted = url = None
#if title == 'remote-server-timeout':
# raise HTTPException(status_code=408, detail='remote-server-timeout')
@ -159,16 +528,21 @@ class HttpInstance:
template_file = 'node.xhtml'
template_dict = {
'action' : action,
'exception' : exception,
'filename' : 'default.svg',
'jid_bare' : jid,
'note' : note,
'request' : request,
'entries' : entries,
'title' : title,
'exception' : exception,
'filename' : filename,
'jid_bare' : jid,
'jid_note' : jid_detail['note'],
'jid_title' : jid_detail['name'],
'links' : links,
'node_title' : node_title,
'node_note' : node_note,
'node_name' : node_name,
'number_of_pages' : number_of_pages,
'page_number' : page_number,
'previous' : previous,
'request' : request,
'url' : request.url._url,
'link_href' : link_href,
'link_text' : link_text,
'xmpp_uri' : xmpp_uri}
response = templates.TemplateResponse(template_file, template_dict)
response.headers['Content-Type'] = 'application/xhtml+xml'
@ -260,7 +634,7 @@ class HttpInstance:
action = 'Error'
title = 'Slixmpp error'
xmpp_uri = note = jid
filename = jid_bare = services = url = link_href = link_text = None
filename = jid_bare = link_href = link_text = selection = services = services_sorted = url = None
#if title == 'remote-server-timeout':
# raise HTTPException(status_code=408, detail='remote-server-timeout')
@ -294,7 +668,7 @@ class HttpInstance:
return response
@self.app.get('/{jid}')
async def jid_node_get(request: Request, jid):
async def jid_get(request: Request, jid):
node_name = request.query_params.get('node', '')
if node_name:
response = RedirectResponse(url='/{}/{}'.format(jid, node_name))
@ -436,7 +810,8 @@ class HttpInstance:
title = jid_detail['name']
else:
title = jid_bare.split('@')[0]
xmpp_instance.disconnect()
# Notes
jid_detail_note = jid_detail['note']
@ -475,8 +850,6 @@ class HttpInstance:
else:
selection = Graphics.extract_colours_from_raster(filepath)
xmpp_instance.disconnect()
# QR code
Graphics.generate_qr_code_graphics_from_string(link_href, jid_bare)
@ -622,6 +995,7 @@ class Graphics:
print(selection)
except Exception as e:
selection = None
exception = str(e)
print(exception)
@ -841,11 +1215,44 @@ class XmppXep0030:
class XmppXep0045:
async def get_room_information(self, jid, alias, maxchars=None, maxstanzas=None, seconds=None):
#logger.info('Joining groupchat\nJID : {}\n'.format(jid))
#jid_from = str(self.boundjid) if self.is_component else None
if not maxchars: maxchars = 1000
if not maxstanzas: maxstanzas = 50
if not seconds: seconds = 864000
try:
error = False
condition = text = None
#since = datetime.fromtimestamp(time.time()-seconds)
iq = await self['xep_0045'].join_muc_wait(
jid,
alias,
#maxchars=maxchars,
maxstanzas=maxstanzas,
#password=None,
#presence_options = {"pfrom" : jid_from},
#seconds=seconds,
#since=since,
#timeout=30
)
except (IqError, IqTimeout, PresenceError) as e:
error = True
iq = None
condition = e.iq['error']['condition']
text = e.iq['error']['text']
result = {
'error' : error,
'condition' : condition,
'text' : text,
'iq' : iq}
return result
async def get_room_data(self, jid_bare):
return await self['xep_0045'].get_room_config(jid_bare)
async def get_number_of_participants(self, jid_bare):
return len(await self['xep_0045'].get_roster(jid_bare))
async def get_room_participants(self, jid_bare):
return await self['xep_0045'].get_roster(jid_bare)
# NOTE: "Item not found", yet is a group chat
@ -938,8 +1345,9 @@ def main():
account = data['account']
jabber_id = account['xmpp']
password = account['pass']
alias = account['alias']
http_instance = HttpInstance(jabber_id, password)
http_instance = HttpInstance(jabber_id, password, alias)
return http_instance.app
app = main()