2024-10-08 09:13:40 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2024-10-21 16:01:31 +02:00
|
|
|
from asyncio import TimeoutError
|
2024-10-08 09:13:40 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from email.utils import parseaddr
|
|
|
|
from fastapi import FastAPI, Form, HTTPException, Request, Response
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from fastapi.responses import FileResponse, HTMLResponse
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from fastapi.templating import Jinja2Templates
|
2024-10-15 14:38:27 +02:00
|
|
|
import glob
|
2024-10-08 09:13:40 +02:00
|
|
|
#import logging
|
|
|
|
#from os import mkdir
|
|
|
|
#from os.path import getsize, exists
|
|
|
|
import os
|
|
|
|
import qrcode
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
from slixmpp import ClientXMPP, stanza
|
2024-10-13 17:42:44 +02:00
|
|
|
from slixmpp.exceptions import IqError, IqTimeout, PresenceError
|
2024-10-08 09:13:40 +02:00
|
|
|
from starlette.responses import RedirectResponse
|
|
|
|
#import time
|
|
|
|
import tomli_w
|
|
|
|
from urllib.parse import urlsplit
|
|
|
|
import uvicorn
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
try:
|
|
|
|
import cv2
|
|
|
|
except:
|
|
|
|
print('OpenCV (cv2) is required for dynamic background.')
|
|
|
|
|
|
|
|
try:
|
|
|
|
import numpy
|
|
|
|
except:
|
|
|
|
print('NumPy (numpy) is required for dynamic background.')
|
|
|
|
|
|
|
|
try:
|
|
|
|
import tomllib
|
|
|
|
except:
|
|
|
|
import tomli as tomllib
|
|
|
|
|
|
|
|
class XmppInstance(ClientXMPP):
|
|
|
|
def __init__(self, jid, password, jid_bare):
|
|
|
|
super().__init__(jid, password)
|
|
|
|
|
|
|
|
self.jid_bare = jid_bare
|
|
|
|
|
|
|
|
self.register_plugin('xep_0030') # XEP-0030: Service Discovery
|
|
|
|
self.register_plugin('xep_0045') # XEP-0045: Multi-User Chat
|
|
|
|
self.register_plugin('xep_0054') # XEP-0054: vcard-temp
|
|
|
|
self.register_plugin('xep_0060') # XEP-0060: Publish-Subscribe
|
|
|
|
|
|
|
|
self.add_event_handler("session_start", self.on_session_start)
|
|
|
|
|
|
|
|
async def on_session_start(self, event):
|
|
|
|
self.send_presence()
|
|
|
|
#self.disconnect()
|
|
|
|
|
|
|
|
class HttpInstance:
|
2024-10-16 12:24:11 +02:00
|
|
|
def __init__(self, configuration):
|
|
|
|
|
|
|
|
account = configuration['account']
|
|
|
|
jabber_id = account['xmpp']
|
|
|
|
password = account['pass']
|
|
|
|
alias = account['alias']
|
|
|
|
|
|
|
|
brand = configuration['brand']
|
|
|
|
brand_name = brand['name']
|
|
|
|
brand_site = brand['site']
|
|
|
|
chat_client = brand['chat']
|
|
|
|
news_client = brand['news']
|
2024-10-08 09:13:40 +02:00
|
|
|
|
|
|
|
self.app = FastAPI()
|
|
|
|
templates = Jinja2Templates(directory='xhtml')
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
# 1) Mount at the same mountpoint /img.
|
|
|
|
# 2) Image filename to be constant, i.e. /img/photo.png and /img/qr.png.
|
|
|
|
self.app.mount('/photo', StaticFiles(directory='photo'), name='photo')
|
|
|
|
self.app.mount('/qr', StaticFiles(directory='qr'), name='qr')
|
|
|
|
self.app.mount('/css', StaticFiles(directory='css'), name='css')
|
|
|
|
self.app.mount('/img', StaticFiles(directory='img'), name='img')
|
|
|
|
|
|
|
|
# @self.app.get('/favicon.ico', include_in_schema=False)
|
|
|
|
# def favicon_get():
|
|
|
|
# return FileResponse('graphic/hermes.ico')
|
|
|
|
|
|
|
|
# @self.app.get('/hermes.svg')
|
|
|
|
# def logo_get():
|
|
|
|
# return FileResponse('graphic/hermes.svg')
|
|
|
|
|
2024-10-13 17:42:44 +02:00
|
|
|
@self.app.get('/v/{jid}')
|
|
|
|
async def view_jid(request: Request, jid):
|
2024-10-15 14:38:27 +02:00
|
|
|
"""View recent messages of a conference"""
|
2024-10-13 17:42:44 +02:00
|
|
|
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'
|
|
|
|
|
2024-10-22 16:34:07 +02:00
|
|
|
if jid_bare == jabber_id:
|
|
|
|
raise HTTPException(status_code=403, detail='access-denied')
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
#try:
|
|
|
|
if True:
|
2024-10-15 19:25:06 +02:00
|
|
|
exception = jid_vcard = messages_10 = note = node_title = \
|
2024-10-15 14:38:27 +02:00
|
|
|
node_note = number_of_pages = page_number = previous = \
|
|
|
|
selection = services_sorted = subject = None
|
2024-10-13 17:42:44 +02:00
|
|
|
node_name = 'urn:xmpp:microblog:0'
|
|
|
|
link_href = 'xmpp:{}?join'.format(jid_bare)
|
|
|
|
link_text = 'Join'
|
|
|
|
xmpp_uri = '{}'.format(jid_bare)
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
filename = 'details/{}.toml'.format(jid_bare)
|
|
|
|
if os.path.exists(filename) and os.path.getsize(filename) > 0:
|
|
|
|
jid_details = Data.open_file_toml(filename)
|
2024-10-13 17:42:44 +02:00
|
|
|
else:
|
2024-10-15 19:25:06 +02:00
|
|
|
jid_details = await FileUtilities.cache_jid_data(
|
|
|
|
jabber_id, password, jid_bare, node_name, alias=alias)
|
|
|
|
|
|
|
|
count = jid_details['count']
|
|
|
|
items = jid_details['items']
|
|
|
|
jid_info = {
|
|
|
|
'error' : jid_details['error'],
|
|
|
|
'text' : jid_details['error_text'],
|
|
|
|
'condition' : jid_details['error_condition']}
|
|
|
|
jid_kind = jid_details['kind']
|
|
|
|
jid_vcard = {
|
|
|
|
'name' : jid_details['name'],
|
|
|
|
'note' : jid_details['note'],
|
|
|
|
'type' : jid_details['image_type']}
|
|
|
|
messages = jid_details['messages']
|
|
|
|
nodes = jid_details['nodes']
|
|
|
|
note = jid_details['note']
|
2024-10-16 00:03:51 +02:00
|
|
|
subject = jid_details['subject']
|
2024-10-15 19:25:06 +02:00
|
|
|
title = jid_details['name']
|
|
|
|
|
|
|
|
# Group chat messages
|
|
|
|
# NOTE TODO
|
|
|
|
page_number = request.query_params.get('page', '')
|
|
|
|
if page_number:
|
|
|
|
try:
|
|
|
|
page_number = int(page_number)
|
|
|
|
ix = (page_number -1) * 10
|
|
|
|
except:
|
2024-10-13 20:24:14 +02:00
|
|
|
ix = 0
|
|
|
|
page_number = 1
|
2024-10-15 19:25:06 +02:00
|
|
|
else:
|
|
|
|
ix = 0
|
|
|
|
page_number = 1
|
|
|
|
messages_10 = messages[ix:][:10]
|
|
|
|
number_of_pages = int(len(messages) / 10)
|
|
|
|
if number_of_pages < len(messages) / 10: number_of_pages += 1
|
2024-10-13 17:42:44 +02:00
|
|
|
|
2024-10-22 15:34:39 +02:00
|
|
|
if jid_kind:
|
|
|
|
# Action and instance type
|
|
|
|
action, instance = XmppUtilities.set_action_instance_type(jid_kind, node_name)
|
|
|
|
else: # jid_info['error']
|
|
|
|
action = 'Contact'
|
|
|
|
instance = view_href = ''
|
|
|
|
message = '{}: {} (XEP-0030)'.format(jid_info['text'], jid_info['condition'])
|
|
|
|
xmpp_uri = jid_bare
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
# Query URI links
|
2024-10-22 15:34:39 +02:00
|
|
|
print('Query URI links')
|
|
|
|
links = XmppUtilities.get_query_uri_links(jid_bare, jid_kind, node_name)
|
|
|
|
link_href = XmppUtilities.get_link_href(jid_bare, jid_kind, node_name)
|
|
|
|
view_href = XmppUtilities.get_view_href(jid_bare, jid_kind, node_name)
|
|
|
|
xmpp_uri = XmppUtilities.get_xmpp_uri(jid_bare, jid_kind, node_name)
|
2024-10-13 17:42:44 +02:00
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
# Graphic files
|
2024-10-15 22:13:10 +02:00
|
|
|
filename, filepath, filetype, selection = FileUtilities.handle_photo(jid_bare, jid_vcard, link_href)
|
2024-10-13 17:42:44 +02:00
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
#except Exception as e:
|
|
|
|
else:
|
2024-10-13 17:42:44 +02:00
|
|
|
exception = str(e)
|
|
|
|
action = 'Error'
|
|
|
|
title = 'Slixmpp error'
|
|
|
|
xmpp_uri = note = jid
|
2024-10-15 19:25:06 +02:00
|
|
|
filename = jid_bare = link_href = link_tex = node_note = \
|
|
|
|
node_title = number_of_pages = page_number = previous = \
|
|
|
|
selection = services = services_sorted = url = None
|
2024-10-13 17:42:44 +02:00
|
|
|
|
|
|
|
#if title == 'remote-server-timeout':
|
|
|
|
# raise HTTPException(status_code=408, detail='remote-server-timeout')
|
|
|
|
#else:
|
|
|
|
template_file = 'conference.xhtml'
|
|
|
|
template_dict = {
|
|
|
|
'action' : action,
|
2024-10-16 12:24:11 +02:00
|
|
|
'brand_name' : brand_name,
|
|
|
|
'brand_site' : brand_site,
|
|
|
|
'chat_client' : chat_client,
|
2024-10-13 17:42:44 +02:00
|
|
|
'exception' : exception,
|
|
|
|
'filename' : filename,
|
|
|
|
'jid_bare' : jid,
|
2024-10-15 14:38:27 +02:00
|
|
|
'jid_note' : note,
|
|
|
|
'jid_title' : title,
|
2024-10-13 17:42:44 +02:00
|
|
|
'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,
|
2024-10-15 14:38:27 +02:00
|
|
|
'title' : title,
|
2024-10-13 17:42:44 +02:00
|
|
|
'url' : request.url._url,
|
|
|
|
'xmpp_uri' : xmpp_uri}
|
|
|
|
response = templates.TemplateResponse(template_file, template_dict)
|
|
|
|
response.headers['Content-Type'] = 'application/xhtml+xml'
|
|
|
|
return response
|
|
|
|
|
2024-10-08 09:13:40 +02:00
|
|
|
# NOTE Was /b/
|
|
|
|
@self.app.get('/d/{jid}/{node_name}')
|
2024-10-13 17:42:44 +02:00
|
|
|
@self.app.get('/d/{jid}/{node_name}/{item_id}')
|
|
|
|
async def browse_jid_node_get(request: Request, jid, node_name, item_id=None):
|
2024-10-08 09:13:40 +02:00
|
|
|
"""Browse items of a pubsub node"""
|
|
|
|
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'
|
|
|
|
|
2024-10-22 16:34:07 +02:00
|
|
|
if jid_bare == jabber_id:
|
|
|
|
raise HTTPException(status_code=403, detail='access-denied')
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
#try:
|
|
|
|
if True:
|
2024-10-15 19:25:06 +02:00
|
|
|
entries = []
|
|
|
|
exception = jid_vcard = note = node_note = number_of_pages = \
|
2024-10-22 15:34:39 +02:00
|
|
|
page_number = previous = selection = None
|
2024-10-08 09:13:40 +02:00
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
filename = 'details/{}.toml'.format(jid_bare)
|
|
|
|
if os.path.exists(filename) and os.path.getsize(filename) > 0:
|
|
|
|
jid_details = Data.open_file_toml(filename)
|
2024-10-13 17:42:44 +02:00
|
|
|
else:
|
2024-10-15 19:25:06 +02:00
|
|
|
jid_details = await FileUtilities.cache_jid_data(
|
|
|
|
jabber_id, password, jid_bare, node_name, item_id)
|
|
|
|
|
2024-10-22 15:34:39 +02:00
|
|
|
xmpp_instance = XmppInstance(jabber_id, password, jid_bare)
|
|
|
|
xmpp_instance.connect()
|
|
|
|
|
|
|
|
# Node item IDs
|
|
|
|
nodes = jid_details['nodes']
|
2024-10-15 19:25:06 +02:00
|
|
|
items = jid_details['items']
|
2024-10-22 15:34:39 +02:00
|
|
|
if node_name not in nodes:
|
|
|
|
nodes[node_name] = {}
|
|
|
|
node_item_ids = await XmppXep0060.get_node_item_ids(
|
|
|
|
xmpp_instance, jid_bare, node_name)
|
|
|
|
#node_item_ids = await XmppUtilities.get_item_ids_of_node(
|
|
|
|
# jabber_id, password, jid_bare, node_name, nodes)
|
|
|
|
if isinstance(node_item_ids['iq'], stanza.iq.Iq):
|
|
|
|
iq = node_item_ids['iq']
|
|
|
|
iq_disco_items_items = iq['disco_items']['items']
|
|
|
|
for item in items:
|
|
|
|
if item[1] == node_name:
|
|
|
|
nodes[node_name]['title'] = item[2]
|
|
|
|
break
|
|
|
|
nodes[node_name]['count'] = len(iq_disco_items_items)
|
|
|
|
nodes[node_name]['item_ids'] = []
|
|
|
|
for item in iq_disco_items_items:
|
|
|
|
nodes[node_name]['item_ids'].append(
|
|
|
|
[item[0] or '', item[1] or '', item[2] or ''])
|
|
|
|
|
|
|
|
count = jid_details['count']
|
2024-10-15 19:25:06 +02:00
|
|
|
jid_info = {
|
|
|
|
'error' : jid_details['error'],
|
|
|
|
'text' : jid_details['error_text'],
|
|
|
|
'condition' : jid_details['error_condition']}
|
|
|
|
jid_kind = jid_details['kind']
|
|
|
|
jid_vcard = {
|
|
|
|
'name' : jid_details['name'],
|
|
|
|
'note' : jid_details['note'],
|
|
|
|
'type' : jid_details['image_type']}
|
|
|
|
messages = jid_details['messages']
|
2024-10-22 15:34:39 +02:00
|
|
|
node_title = nodes[node_name]['title']
|
2024-10-15 19:25:06 +02:00
|
|
|
note = jid_details['note']
|
2024-10-16 12:24:11 +02:00
|
|
|
#title = nodes[node_name]['title'] if node_name else jid_details['name']
|
2024-10-22 15:34:39 +02:00
|
|
|
title = jid_details['name']
|
2024-10-15 19:25:06 +02:00
|
|
|
|
2024-10-16 12:24:11 +02:00
|
|
|
#link_href = 'xmpp:{}?pubsub;node={};action=subscribe'.format(
|
|
|
|
# jid_bare, node_name)
|
|
|
|
#link_text = 'Subscribe'
|
|
|
|
#xmpp_uri = '{}?;node={}'.format(jid_bare, node_name)
|
|
|
|
|
2024-10-15 19:25:06 +02:00
|
|
|
# Node items
|
|
|
|
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 = []
|
|
|
|
for item in nodes[node_name]['item_ids']:
|
|
|
|
item_ids.append(item[2])
|
|
|
|
# NOTE Consider to neglect 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 = 0
|
|
|
|
page_number = 1
|
|
|
|
else:
|
|
|
|
ix = 0
|
|
|
|
page_number = 1
|
|
|
|
item_ids_10 = item_ids[ix:][:10]
|
|
|
|
number_of_pages = int(len(item_ids) / 10)
|
|
|
|
if number_of_pages < len(item_ids) / 10: number_of_pages += 1
|
|
|
|
node_items = await XmppXep0060.get_node_items(
|
|
|
|
xmpp_instance, jid_bare, node_name, item_ids=item_ids_10)
|
2024-10-22 15:34:39 +02:00
|
|
|
|
|
|
|
if isinstance(node_items['iq'], stanza.iq.Iq):
|
2024-10-15 19:25:06 +02:00
|
|
|
#title = title or node_name
|
|
|
|
if not node_title: node_title = node_name
|
2024-10-16 12:24:11 +02:00
|
|
|
#node_note = nodes[node_name]['title'] if node_name else jid_details['name']
|
2024-10-22 15:34:39 +02:00
|
|
|
#node_note = xmpp_uri # jid_bare
|
|
|
|
iq = node_items['iq']
|
|
|
|
for item in iq['pubsub']['items']:
|
2024-10-15 19:25:06 +02:00
|
|
|
item_payload = item['payload']
|
|
|
|
entry = Syndication.extract_items(item_payload)
|
|
|
|
if entry: entry['id'] = item['id']
|
|
|
|
entries.append(entry)
|
|
|
|
#if len(entries) > 10: break
|
2024-10-22 15:34:39 +02:00
|
|
|
else:
|
|
|
|
message = '{}: {} (XEP-0060)'.format(node_items['condition'], node_items['text'])
|
2024-10-15 19:25:06 +02:00
|
|
|
if entries: entries.reverse()
|
|
|
|
|
|
|
|
xmpp_instance.disconnect()
|
2024-10-15 14:38:27 +02:00
|
|
|
|
2024-10-22 15:34:39 +02:00
|
|
|
if jid_kind:
|
|
|
|
# Action and instance type
|
|
|
|
action, instance = XmppUtilities.set_action_instance_type(jid_kind, node_name)
|
|
|
|
else: # jid_info['error']
|
|
|
|
action = 'Contact'
|
|
|
|
instance = view_href = ''
|
|
|
|
message = '{}: {} (XEP-0030)'.format(jid_info['text'], jid_info['condition'])
|
|
|
|
xmpp_uri = jid_bare
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
# Query URI links
|
2024-10-22 15:34:39 +02:00
|
|
|
print('Query URI links')
|
|
|
|
links = XmppUtilities.get_query_uri_links(jid_bare, jid_kind, node_name, item_id)
|
|
|
|
link_href = XmppUtilities.get_link_href(jid_bare, jid_kind, node_name)
|
|
|
|
view_href = XmppUtilities.get_view_href(jid_bare, jid_kind, node_name)
|
|
|
|
xmpp_uri = XmppUtilities.get_xmpp_uri(jid_bare, jid_kind, node_name)
|
|
|
|
|
|
|
|
node_note = xmpp_uri
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
# Graphic files
|
2024-10-15 22:20:51 +02:00
|
|
|
filename, filepath, filetype, selection = FileUtilities.handle_photo(jid_bare, jid_vcard, link_href)
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
#except Exception as e:
|
|
|
|
else:
|
2024-10-08 09:13:40 +02:00
|
|
|
exception = str(e)
|
|
|
|
action = 'Error'
|
|
|
|
title = 'Slixmpp error'
|
|
|
|
xmpp_uri = note = jid
|
2024-10-15 14:38:27 +02:00
|
|
|
filename = jid_bare = link_href = link_tex = node_note = \
|
|
|
|
node_title = number_of_pages = page_number = previous = \
|
2024-10-22 15:34:39 +02:00
|
|
|
selection = url = None
|
2024-10-08 09:13:40 +02:00
|
|
|
|
|
|
|
#if title == 'remote-server-timeout':
|
|
|
|
# raise HTTPException(status_code=408, detail='remote-server-timeout')
|
|
|
|
#else:
|
|
|
|
template_file = 'node.xhtml'
|
|
|
|
template_dict = {
|
|
|
|
'action' : action,
|
2024-10-16 12:24:11 +02:00
|
|
|
'brand_name' : brand_name,
|
|
|
|
'brand_site' : brand_site,
|
|
|
|
'chat_client' : chat_client,
|
2024-10-13 17:42:44 +02:00
|
|
|
'entries' : entries,
|
2024-10-08 09:13:40 +02:00
|
|
|
'exception' : exception,
|
2024-10-13 17:42:44 +02:00
|
|
|
'filename' : filename,
|
2024-10-08 09:13:40 +02:00
|
|
|
'jid_bare' : jid,
|
2024-10-15 14:38:27 +02:00
|
|
|
'jid_note' : note,
|
|
|
|
'jid_title' : title,
|
2024-10-13 17:42:44 +02:00
|
|
|
'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,
|
2024-10-08 09:13:40 +02:00
|
|
|
'request' : request,
|
2024-10-13 20:24:14 +02:00
|
|
|
'title' : node_title,
|
2024-10-08 09:13:40 +02:00
|
|
|
'url' : request.url._url,
|
|
|
|
'xmpp_uri' : xmpp_uri}
|
|
|
|
response = templates.TemplateResponse(template_file, template_dict)
|
|
|
|
response.headers['Content-Type'] = 'application/xhtml+xml'
|
|
|
|
return response
|
|
|
|
|
|
|
|
@self.app.get('/d/{jid}')
|
|
|
|
async def discover_jid_get(request: Request, jid):
|
|
|
|
"""View items of a selected service"""
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
2024-10-22 16:34:07 +02:00
|
|
|
if jid_bare == jabber_id:
|
|
|
|
raise HTTPException(status_code=403, detail='access-denied')
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
#try:
|
|
|
|
if True:
|
2024-10-08 09:13:40 +02:00
|
|
|
exception = note = selection = services_sorted = None
|
|
|
|
title = 'Services'
|
|
|
|
link_href = xmpp_uri = jid_bare
|
|
|
|
link_text = 'Reload'
|
|
|
|
|
|
|
|
# Start an XMPP instance and retrieve information
|
|
|
|
xmpp_instance = XmppInstance(jabber_id, password, jid_bare)
|
|
|
|
xmpp_instance.connect()
|
|
|
|
|
|
|
|
# JID services
|
|
|
|
action = 'Discover'
|
|
|
|
jid_info = await XmppXep0030.get_jid_info(xmpp_instance, jid_bare)
|
|
|
|
iq = jid_info['iq']
|
|
|
|
if iq:
|
|
|
|
jid_kind = jid_info['kind']
|
|
|
|
iq_disco_info = iq['disco_info']
|
|
|
|
for identity in iq_disco_info['identities']:
|
|
|
|
if jid_kind == identity[0] and identity[3]:
|
|
|
|
note = identity[3]
|
|
|
|
if not note: note = jid_bare
|
|
|
|
jid_items = await XmppXep0030.get_jid_items(xmpp_instance, jid_bare)
|
|
|
|
iq = jid_items['iq']
|
|
|
|
iq_disco_items = iq['disco_items']
|
|
|
|
iq_disco_items_items = iq_disco_items['items']
|
|
|
|
services = {}
|
|
|
|
#services_sorted = {}
|
|
|
|
category = 'unsorted'
|
|
|
|
for item in iq_disco_items_items:
|
|
|
|
jid_bare = item[0]
|
|
|
|
if len(iq_disco_items_items) > 20 or jid_kind and jid_kind in ('pubsub'):
|
|
|
|
identity = sub_jid_info = sub_jid_info_iq = ''
|
|
|
|
if jid_kind and jid_kind in ('conference', 'mix', 'muc'):
|
|
|
|
category = 'conference'
|
|
|
|
if jid_kind and jid_kind in ('pubsub'):
|
|
|
|
category = 'pubsub'
|
|
|
|
else:
|
|
|
|
sub_jid_info = await XmppXep0030.get_jid_info(xmpp_instance, jid_bare)
|
|
|
|
sub_jid_info_iq = sub_jid_info['iq']
|
|
|
|
try:
|
|
|
|
for identity_item in sub_jid_info_iq['disco_info']['identities']:
|
|
|
|
identity = identity_item
|
|
|
|
break
|
|
|
|
if sub_jid_info_iq:
|
|
|
|
category = identity[0] if (identity, list) and identity[0] else 'other'
|
|
|
|
except:
|
|
|
|
identity = None
|
|
|
|
category = 'unavailable'
|
|
|
|
|
|
|
|
sub_jid_kind = sub_jid_info['kind'] if 'kind' in sub_jid_info else None
|
|
|
|
if category not in services: services[category] = []
|
|
|
|
|
|
|
|
services[category].append(
|
|
|
|
{'identity' : identity,
|
|
|
|
'info' : sub_jid_info,
|
|
|
|
'jid' : jid_bare,
|
|
|
|
'kind' : sub_jid_kind,
|
|
|
|
'name' : item[2] or item[1] or item[0],
|
|
|
|
'node' : item[1]})
|
|
|
|
|
|
|
|
services_sorted = {k: v for k, v in services.items() if k != 'unavailable'}
|
|
|
|
if 'unavailable' in services: services_sorted['unavailable'] = services['unavailable']
|
|
|
|
else:
|
2024-10-22 15:34:39 +02:00
|
|
|
message = '{}: {} (XEP-0030)'.format(jid_info['condition'], jid_info['text'])
|
2024-10-08 09:13:40 +02:00
|
|
|
services = services_sorted = None
|
|
|
|
|
|
|
|
xmpp_instance.disconnect()
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
#except Exception as e:
|
|
|
|
else:
|
2024-10-08 09:13:40 +02:00
|
|
|
exception = str(e)
|
|
|
|
action = 'Error'
|
|
|
|
title = 'Slixmpp error'
|
|
|
|
xmpp_uri = note = jid
|
2024-10-13 17:42:44 +02:00
|
|
|
filename = jid_bare = link_href = link_text = selection = services = services_sorted = url = None
|
2024-10-08 09:13:40 +02:00
|
|
|
|
|
|
|
#if title == 'remote-server-timeout':
|
|
|
|
# raise HTTPException(status_code=408, detail='remote-server-timeout')
|
|
|
|
#else:
|
|
|
|
template_file = 'disco.xhtml'
|
|
|
|
template_dict = {
|
|
|
|
'action' : action,
|
|
|
|
'filename' : 'default.svg',
|
2024-10-16 12:24:11 +02:00
|
|
|
'brand_name' : brand_name,
|
|
|
|
'brand_site' : brand_site,
|
|
|
|
'chat_client' : chat_client,
|
|
|
|
'exception' : exception,
|
2024-10-08 09:13:40 +02:00
|
|
|
'jid_bare' : jid,
|
|
|
|
'note' : note,
|
|
|
|
'request' : request,
|
|
|
|
'services' : services_sorted,
|
|
|
|
'title' : title,
|
|
|
|
'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'
|
|
|
|
return response
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
@self.app.get('/j/{jid}/{node_name}')
|
2024-10-08 09:13:40 +02:00
|
|
|
async def jid_node_get(request: Request, jid, node_name):
|
|
|
|
response = await main_jid_node_get(request, jid, node_name)
|
|
|
|
return response
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
@self.app.get('/j/{jid}')
|
2024-10-13 17:42:44 +02:00
|
|
|
async def jid_get(request: Request, jid):
|
2024-10-08 09:13:40 +02:00
|
|
|
node_name = request.query_params.get('node', '')
|
|
|
|
if node_name:
|
2024-10-15 14:38:27 +02:00
|
|
|
response = RedirectResponse(url='/j/{}/{}'.format(jid, node_name))
|
2024-10-08 09:13:40 +02:00
|
|
|
else:
|
|
|
|
response = await main_jid_node_get(request, jid)
|
|
|
|
return response
|
|
|
|
|
|
|
|
async def main_jid_node_get(request: Request, jid, node_name=None):
|
|
|
|
|
|
|
|
jid_bare = jid
|
|
|
|
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'
|
|
|
|
|
2024-10-22 16:34:07 +02:00
|
|
|
if jid_bare == jabber_id:
|
|
|
|
raise HTTPException(status_code=403, detail='access-denied')
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
#try:
|
|
|
|
if True:
|
2024-10-15 19:25:06 +02:00
|
|
|
action = count = exception = instance = jid_vcard = \
|
2024-10-15 14:38:27 +02:00
|
|
|
jid_info = link_href = message = note = selection = title = \
|
|
|
|
view_href = xmpp_uri = None
|
|
|
|
#node_name = 'urn:xmpp:microblog:0'
|
|
|
|
|
|
|
|
filename = 'details/{}.toml'.format(jid_bare)
|
|
|
|
if os.path.exists(filename) and os.path.getsize(filename) > 0:
|
|
|
|
jid_details = Data.open_file_toml(filename)
|
2024-10-08 09:13:40 +02:00
|
|
|
else:
|
2024-10-15 19:25:06 +02:00
|
|
|
jid_details = await FileUtilities.cache_jid_data(
|
|
|
|
jabber_id, password, jid_bare, node_name, alias=alias)
|
|
|
|
|
|
|
|
# Set node name to 'urn:xmpp:microblog:0'
|
|
|
|
jid_kind = jid_details['kind']
|
2024-10-15 21:14:19 +02:00
|
|
|
nodes = jid_details['nodes']
|
|
|
|
if (jid_kind not in ('conference', 'mix', 'muc') and
|
|
|
|
'@' in jid_bare and
|
|
|
|
not node_name and
|
|
|
|
'urn:xmpp:microblog:0' in nodes):
|
2024-10-15 19:25:06 +02:00
|
|
|
node_name = 'urn:xmpp:microblog:0'
|
|
|
|
|
2024-10-15 21:14:19 +02:00
|
|
|
if ('@' in jid_bare and
|
|
|
|
'urn:xmpp:microblog:0' not in nodes and
|
|
|
|
jid_kind not in ('conference', 'mix', 'muc')):
|
|
|
|
count = 0
|
|
|
|
else:
|
|
|
|
count = nodes[node_name]['count'] if node_name in nodes else jid_details['count']
|
|
|
|
|
2024-10-15 19:25:06 +02:00
|
|
|
items = jid_details['items']
|
|
|
|
jid_info = {
|
|
|
|
'error' : jid_details['error'],
|
|
|
|
'text' : jid_details['error_text'],
|
|
|
|
'condition' : jid_details['error_condition']}
|
|
|
|
jid_vcard = {
|
|
|
|
'name' : jid_details['name'],
|
|
|
|
'note' : jid_details['note'],
|
|
|
|
'type' : jid_details['image_type']}
|
|
|
|
messages = jid_details['messages']
|
2024-10-22 15:34:39 +02:00
|
|
|
#note = nodes[node_name]['title'] if node_name in nodes else jid_details['note']
|
|
|
|
note = jid_details['note']
|
2024-10-15 19:25:06 +02:00
|
|
|
|
2024-10-22 15:34:39 +02:00
|
|
|
# TODO Append results to file
|
|
|
|
# Node item IDs
|
2024-10-15 19:25:06 +02:00
|
|
|
if node_name not in nodes:
|
|
|
|
nodes[node_name] = await XmppUtilities.get_item_ids_of_node(
|
|
|
|
jabber_id, password, jid_bare, node_name, nodes)
|
2024-10-22 15:34:39 +02:00
|
|
|
if isinstance(nodes[node_name]['iq'], stanza.iq.Iq):
|
|
|
|
iq = nodes[node_name]['iq']
|
|
|
|
iq_disco_items = iq['disco_items']
|
|
|
|
if iq_disco_items['items']:
|
|
|
|
count = len(nodes[node_name]['iq']['disco_items']['items'])
|
|
|
|
else:
|
|
|
|
count = 0
|
|
|
|
else:
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
if jid_kind == 'pubsub' and node_name:
|
|
|
|
items = jid_details['items']
|
|
|
|
for item in items:
|
|
|
|
if item[1] == node_name:
|
|
|
|
#nodes[node_name]['title'] = item[2]
|
|
|
|
title = item[2]
|
|
|
|
break
|
|
|
|
if not title: title = node_name
|
|
|
|
else:
|
|
|
|
title = jid_details['name']
|
|
|
|
|
|
|
|
# TODO Consider also the existence of a node /j/pubsub.movim.eu/i2p
|
|
|
|
if jid_kind:
|
|
|
|
# Action and instance type
|
|
|
|
action, instance = XmppUtilities.set_action_instance_type(jid_kind, node_name)
|
|
|
|
view_href = XmppUtilities.get_view_href(jid_bare, jid_kind, node_name)
|
|
|
|
xmpp_uri = XmppUtilities.get_xmpp_uri(jid_bare, jid_kind, node_name)
|
|
|
|
else: # jid_info['error']
|
|
|
|
action = 'Contact'
|
|
|
|
instance = view_href = ''
|
|
|
|
message = '{}: {} (XEP-0030)'.format(jid_info['text'], jid_info['condition'])
|
|
|
|
xmpp_uri = jid_bare
|
2024-10-22 19:27:29 +02:00
|
|
|
link_href = XmppUtilities.get_link_href(jid_bare, jid_kind, node_name)
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
# Query URI links
|
2024-10-22 15:34:39 +02:00
|
|
|
print('Query URI links')
|
|
|
|
links = XmppUtilities.get_query_uri_links(jid_bare, jid_kind, node_name)
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
# Graphic files
|
2024-10-22 19:27:29 +02:00
|
|
|
filename, filepath, filetype, selection = FileUtilities.handle_photo(
|
|
|
|
jid_bare, jid_vcard, link_href)
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
#except Exception as e:
|
|
|
|
else:
|
2024-10-08 09:13:40 +02:00
|
|
|
exception = str(e)
|
|
|
|
print(exception)
|
|
|
|
action = 'Error'
|
|
|
|
title = 'Slixmpp error'
|
|
|
|
xmpp_uri = jid
|
2024-10-15 19:25:06 +02:00
|
|
|
count = filename = jid_bare = jid_vcard = jid_kind = links = \
|
2024-10-15 14:38:27 +02:00
|
|
|
message = selection = url = None
|
2024-10-08 09:13:40 +02:00
|
|
|
|
2024-10-22 15:34:39 +02:00
|
|
|
# NOTE Handling of variables "title" and "note" in case of '/j/{jid}/{node_name}' is confusing.
|
|
|
|
# TODO Add new keys that are of 'node' and be utilized for nodes, instead of reusing a variable for several roles.
|
|
|
|
# FIXME If no title be provided to 'node name', use 'node name' itself as title (to be done at FileUtilities.cache_jid_data).
|
|
|
|
|
2024-10-08 09:13:40 +02:00
|
|
|
template_file = 'jid.xhtml'
|
|
|
|
template_dict = {
|
|
|
|
'action' : action,
|
2024-10-16 12:24:11 +02:00
|
|
|
'brand_name' : brand_name,
|
|
|
|
'brand_site' : brand_site,
|
|
|
|
'chat_client' : chat_client,
|
2024-10-08 09:13:40 +02:00
|
|
|
'count' : count,
|
|
|
|
'instance' : instance,
|
|
|
|
'exception' : exception,
|
|
|
|
'filename' : filename,
|
|
|
|
'jid_bare' : jid_bare,
|
|
|
|
'jid_kind' : jid_kind,
|
2024-10-09 22:07:16 +02:00
|
|
|
'links' : links,
|
2024-10-08 09:13:40 +02:00
|
|
|
'message' : message,
|
2024-10-22 15:34:39 +02:00
|
|
|
'note' : note, # TODO node_note or title of PubSub JID
|
2024-10-08 09:13:40 +02:00
|
|
|
'request' : request,
|
|
|
|
'selection' : selection,
|
2024-10-22 15:34:39 +02:00
|
|
|
'title' : title, # TODO node_title
|
2024-10-08 09:13:40 +02:00
|
|
|
'url' : request.url._url,
|
|
|
|
'view_href' : view_href,
|
|
|
|
'xmpp_uri' : xmpp_uri}
|
|
|
|
response = templates.TemplateResponse(template_file, template_dict)
|
|
|
|
response.headers['Content-Type'] = 'application/xhtml+xml'
|
|
|
|
return response
|
|
|
|
|
2024-10-20 16:30:06 +02:00
|
|
|
@self.app.get('/download')
|
|
|
|
@self.app.get('/download/{software}')
|
|
|
|
@self.app.get('/download/{software}/{kind}')
|
|
|
|
async def download_get(request: Request, software=None, kind=None):
|
|
|
|
|
|
|
|
# TODO admin, advanced, console, desktop, graphical, mobile
|
|
|
|
if not kind: kind = 'graphical'
|
|
|
|
|
|
|
|
if not software:
|
|
|
|
user_agent = request.headers.get("user-agent")
|
|
|
|
user_agent_lower = user_agent.lower()
|
|
|
|
match user_agent_lower:
|
|
|
|
case _ if 'linux' in user_agent_lower:
|
|
|
|
software = 'linux'
|
|
|
|
case _ if 'haiku' in user_agent_lower:
|
|
|
|
software = 'haiku'
|
|
|
|
case _ if 'android' in user_agent_lower:
|
2024-10-22 15:34:39 +02:00
|
|
|
software = 'android'
|
2024-10-20 16:30:06 +02:00
|
|
|
case _ if 'reactos' in user_agent_lower or 'windows' in user_agent_lower:
|
2024-10-22 15:34:39 +02:00
|
|
|
software = 'windows'
|
2024-10-20 16:30:06 +02:00
|
|
|
case _ if 'ios' in user_agent_lower or 'macos' in user_agent_lower:
|
|
|
|
software = 'apple'
|
|
|
|
|
|
|
|
name = software.title()
|
2024-10-22 15:34:39 +02:00
|
|
|
if software == 'posix': name = 'POSIX'
|
2024-10-20 16:30:06 +02:00
|
|
|
|
|
|
|
filename_clients = 'clients.toml'
|
|
|
|
clients = Data.open_file_toml(filename_clients)
|
|
|
|
client_selection = []
|
|
|
|
for client in clients:
|
|
|
|
if software in clients[client]:
|
|
|
|
client_selected = {
|
|
|
|
'name' : clients[client]['title'],
|
|
|
|
'about' : clients[client]['about'],
|
|
|
|
'features' : clients[client]['features'],
|
|
|
|
'iden' : client,
|
|
|
|
'href' : clients[client][software]}
|
|
|
|
client_selection.append(client_selected)
|
|
|
|
|
|
|
|
template_file = 'download.xhtml'
|
|
|
|
template_dict = {
|
|
|
|
'brand_name' : brand_name,
|
|
|
|
'brand_site' : brand_site,
|
|
|
|
'chat_client' : chat_client,
|
|
|
|
'client_selection' : client_selection,
|
|
|
|
'request' : request,
|
|
|
|
'software' : software,
|
|
|
|
'title' : name,
|
|
|
|
'url' : request.url._url}
|
|
|
|
response = templates.TemplateResponse(template_file, template_dict)
|
|
|
|
response.headers['Content-Type'] = 'application/xhtml+xml'
|
|
|
|
return response
|
|
|
|
|
2024-10-22 16:34:07 +02:00
|
|
|
@self.app.exception_handler(403)
|
2024-10-22 19:27:29 +02:00
|
|
|
def access_denied_exception_handler(request: Request, exc: HTTPException):
|
2024-10-22 16:34:07 +02:00
|
|
|
action = 'Warning'
|
|
|
|
title = 'Access Denied'
|
|
|
|
return result_get(request, action, title)
|
|
|
|
|
2024-10-08 09:13:40 +02:00
|
|
|
@self.app.exception_handler(404)
|
|
|
|
def not_found_exception_handler(request: Request, exc: HTTPException):
|
|
|
|
action = 'Warning'
|
|
|
|
title = 'Not Found'
|
|
|
|
return result_get(request, action, title)
|
|
|
|
|
|
|
|
@self.app.exception_handler(500)
|
|
|
|
def internal_error_exception_handler(request: Request, exc: HTTPException):
|
|
|
|
action = 'Error'
|
|
|
|
title = 'Internal Server Error'
|
|
|
|
return result_get(request, action, title)
|
|
|
|
|
2024-10-21 16:01:31 +02:00
|
|
|
@self.app.exception_handler(504)
|
|
|
|
def time_out_exception_handler(request: Request, exc: HTTPException):
|
|
|
|
action = 'Warning'
|
|
|
|
title = 'Time Out'
|
|
|
|
return result_get(request, action, title)
|
|
|
|
|
2024-10-08 09:13:40 +02:00
|
|
|
def result_get(request: Request, action: str, title: str):
|
|
|
|
template_file = 'result.xhtml'
|
|
|
|
template_dict = {
|
|
|
|
'action' : action,
|
2024-10-16 12:24:11 +02:00
|
|
|
'brand_name' : brand_name,
|
|
|
|
'brand_site' : brand_site,
|
|
|
|
'chat_client' : chat_client,
|
2024-10-08 09:13:40 +02:00
|
|
|
'request' : request,
|
|
|
|
'title' : title,
|
|
|
|
'url' : request.url._url}
|
|
|
|
response = templates.TemplateResponse(template_file, template_dict)
|
|
|
|
response.headers['Content-Type'] = 'application/xhtml+xml'
|
|
|
|
return response
|
|
|
|
|
|
|
|
@self.app.get('/')
|
|
|
|
async def main_get(request: Request):
|
|
|
|
jabber_id = request.query_params.get('jid', '')
|
|
|
|
if jabber_id:
|
2024-10-15 19:25:06 +02:00
|
|
|
response = RedirectResponse(url='/j/' + jabber_id)
|
2024-10-08 09:13:40 +02:00
|
|
|
else:
|
|
|
|
template_file = 'main.xhtml'
|
|
|
|
template_dict = {
|
2024-10-16 12:24:11 +02:00
|
|
|
'brand_name' : brand_name,
|
|
|
|
'brand_site' : brand_site,
|
|
|
|
'chat_client' : chat_client,
|
2024-10-08 09:13:40 +02:00
|
|
|
'request' : request,
|
|
|
|
'url' : request.url._url}
|
|
|
|
response = templates.TemplateResponse(template_file, template_dict)
|
|
|
|
response.headers['Content-Type'] = 'application/xhtml+xml'
|
|
|
|
return response
|
|
|
|
|
|
|
|
class Data:
|
|
|
|
|
|
|
|
def open_file_toml(filename: str) -> dict:
|
|
|
|
with open(filename, mode="rb") as fn:
|
|
|
|
data = tomllib.load(fn)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def save_to_toml(filename: str, data: dict) -> None:
|
|
|
|
with open(filename, 'w') as fn:
|
|
|
|
data_as_string = tomli_w.dumps(data)
|
|
|
|
fn.write(data_as_string)
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
class FileUtilities:
|
|
|
|
|
|
|
|
async def cache_jid_data(jabber_id, password, jid_bare, node_name=None, item_id=None, alias=None):
|
|
|
|
|
2024-10-22 15:34:39 +02:00
|
|
|
iq_disco_items_list = iq_disco_items_items_list = node_note = node_title = title = ''
|
2024-10-15 19:25:06 +02:00
|
|
|
jid_vcard = {
|
2024-10-15 14:38:27 +02:00
|
|
|
'name' : '',
|
|
|
|
'note' : '',
|
|
|
|
'type' : '',
|
|
|
|
'bin' : ''}
|
|
|
|
|
|
|
|
#filename = 'details/{}.toml'.format(jid_bare)
|
|
|
|
#if os.path.exists(filename): jid_details = Data.open_file_toml(filename)
|
|
|
|
|
|
|
|
# Start an XMPP instance and retrieve information
|
|
|
|
xmpp_instance = XmppInstance(jabber_id, password, jid_bare)
|
|
|
|
xmpp_instance.connect()
|
|
|
|
|
|
|
|
# JID kind
|
|
|
|
print('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']
|
|
|
|
|
|
|
|
# JID info
|
|
|
|
print('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 not vcard_data['error']:
|
|
|
|
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']
|
2024-10-15 19:25:06 +02:00
|
|
|
jid_vcard = {
|
2024-10-15 14:38:27 +02:00
|
|
|
'name' : vcard_temp['FN'] or conference_title or '',
|
|
|
|
'note' : vcard_temp['notes'] or node_id or '',
|
|
|
|
'type' : vcard_temp['PHOTO']['TYPE'] or '',
|
2024-10-16 12:24:11 +02:00
|
|
|
'bin' : vcard_temp['PHOTO']['BINVAL'] or ''}
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
# TODO /d/pubsub.nicoco.fr/blog/urn-uuid-53e43061-1962-3112-bb8a-1473dca61719
|
2024-10-16 00:03:51 +02:00
|
|
|
count = ''
|
2024-10-15 14:38:27 +02:00
|
|
|
jid_items = await XmppXep0030.get_jid_items(xmpp_instance, jid_bare)
|
2024-10-15 19:37:46 +02:00
|
|
|
if isinstance(jid_items['iq'], stanza.iq.Iq):
|
|
|
|
iq = jid_items['iq']
|
|
|
|
iq_disco_items = iq['disco_items']
|
|
|
|
iq_disco_items_items = iq_disco_items['items']
|
|
|
|
#iq_disco_items_set = {''}
|
|
|
|
iq_disco_items_list = []
|
|
|
|
iq_disco_items_items_list = []
|
|
|
|
for item in iq_disco_items_items:
|
|
|
|
if jid_kind == 'muc':
|
|
|
|
#iq_disco_items_set.update([item[2]])
|
|
|
|
iq_disco_items_list.append(item[2])
|
|
|
|
else:
|
|
|
|
#iq_disco_items_set.update([item[1]])
|
|
|
|
iq_disco_items_list.append(item[1])
|
|
|
|
iq_disco_items_items_list.append(
|
|
|
|
[item[0] or '', item[1] or '', item[2] or ''])
|
|
|
|
#count = len(iq_disco_items_set)
|
|
|
|
count = len(iq_disco_items_list)
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
# Title
|
|
|
|
print('Title')
|
2024-10-16 12:24:11 +02:00
|
|
|
if (jid_kind not in ('conference', 'mix', 'muc') and
|
|
|
|
'@' in jid_bare and
|
|
|
|
not node_name):
|
2024-10-15 19:25:06 +02:00
|
|
|
# NOTE Variables node_name and node_title do not appear to be utilized.
|
2024-10-15 14:38:27 +02:00
|
|
|
node_name = 'urn:xmpp:microblog:0'
|
2024-10-15 19:25:06 +02:00
|
|
|
node_title = 'Journal'
|
2024-10-15 14:38:27 +02:00
|
|
|
elif jid_kind == 'pubsub':
|
|
|
|
category = 'unsorted'
|
|
|
|
for item in iq_disco_items_items:
|
|
|
|
if item[2] and item[1] == node_name:
|
2024-10-16 12:24:11 +02:00
|
|
|
node_title = item[2]
|
2024-10-15 14:38:27 +02:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
jid_items = None
|
|
|
|
|
|
|
|
if jid_kind == 'server':
|
|
|
|
if jid_info_iq:
|
|
|
|
for identity in jid_info_iq['disco_info']['identities']:
|
|
|
|
if jid_kind == identity[0] and identity[1] == 'im' and identity[3]:
|
|
|
|
title = identity[3]
|
|
|
|
print(jid_bare)
|
|
|
|
print(identity)
|
|
|
|
print(jid_info)
|
|
|
|
# String 'undefined' is sourced from JID discuss@conference.conversejs.org
|
|
|
|
if not title:
|
2024-10-15 19:25:06 +02:00
|
|
|
if jid_vcard['name'] and not 'undefined' in jid_vcard['name']:
|
|
|
|
title = jid_vcard['name']
|
2024-10-15 14:38:27 +02:00
|
|
|
else:
|
|
|
|
title = jid_bare.split('@')[0]
|
|
|
|
|
|
|
|
# JID item count
|
|
|
|
#count = await XmppUtilities.count_jid_items(xmpp_instance, jid_bare, node_name, jid_kind)
|
|
|
|
#if jid_kind in ('mix', 'muc', 'conference', 'server'):
|
|
|
|
# jid_items = await XmppXep0030.get_jid_items(xmpp_instance, jid_bare)
|
|
|
|
# if isinstance(jid_items['iq'], stanza.iq.Iq):
|
2024-10-22 15:34:39 +02:00
|
|
|
# iq = jid_items['iq']
|
|
|
|
# count = len(iq['disco_items']['items'])
|
2024-10-15 14:38:27 +02:00
|
|
|
#elif jid_kind in ('account', 'pubsub'):
|
|
|
|
# node_item_ids = await XmppXep0060.get_node_item_ids(xmpp_instance, jid_bare, node_name)
|
|
|
|
# if isinstance(node_item_ids, stanza.iq.Iq):
|
|
|
|
# count = len(node_item_ids['disco_items']['items'])
|
|
|
|
|
|
|
|
# Group chat messages
|
|
|
|
print('Group chat messages')
|
|
|
|
messages = []
|
2024-10-16 00:03:51 +02:00
|
|
|
subject = ''
|
2024-10-15 23:14:34 +02:00
|
|
|
if jid_kind == 'muc':
|
2024-10-22 15:34:39 +02:00
|
|
|
#action = 'Join'
|
2024-10-15 14:38:27 +02:00
|
|
|
# TODO Create configurations for group chat preview
|
2024-10-22 15:34:39 +02:00
|
|
|
room_info_muc = await XmppXep0045.get_room_information(
|
|
|
|
xmpp_instance, jid_bare, alias, maxstanzas=50)
|
2024-10-22 16:34:07 +02:00
|
|
|
# NOTE Do not mix error messages with node titles and descriptions etc.
|
|
|
|
if isinstance(room_info_muc['iq'], tuple):
|
2024-10-22 15:34:39 +02:00
|
|
|
iq = room_info_muc['iq']
|
|
|
|
for message in iq[3]:
|
2024-10-15 23:14:34 +02:00
|
|
|
messages.append({
|
|
|
|
'id' : message['id'],
|
|
|
|
'alias' : message['mucnick'],
|
|
|
|
'body' : message['body'],
|
|
|
|
'timestamp' : message['delay']['stamp'].__str__()})
|
2024-10-15 14:38:27 +02:00
|
|
|
messages.reverse()
|
2024-10-22 15:34:39 +02:00
|
|
|
subject = iq[1]['subject']
|
2024-10-15 14:38:27 +02:00
|
|
|
#title = title or node_name
|
|
|
|
if not node_title: node_title = node_name
|
|
|
|
node_note = jid_bare
|
2024-10-22 15:34:39 +02:00
|
|
|
else:
|
|
|
|
message = '{}: {} (XEP-0045)'.format(room_info_muc['condition'], room_info_muc['text'])
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
# Node items
|
|
|
|
print('Node items')
|
|
|
|
nodes = {}
|
2024-10-15 19:25:06 +02:00
|
|
|
#if node_name and node_name in iq_disco_items_set:
|
2024-10-15 19:37:46 +02:00
|
|
|
if iq_disco_items_list and node_name and node_name in iq_disco_items_list:
|
2024-10-22 15:34:39 +02:00
|
|
|
#action = 'Browse'
|
2024-10-15 14:38:27 +02:00
|
|
|
node_item_ids = await XmppXep0060.get_node_item_ids(xmpp_instance, jid_bare, node_name)
|
2024-10-15 19:25:06 +02:00
|
|
|
if isinstance(node_item_ids['iq'], stanza.iq.Iq):
|
2024-10-22 15:34:39 +02:00
|
|
|
iq = node_item_ids['iq']
|
2024-10-15 19:25:06 +02:00
|
|
|
nodes[node_name] = {}
|
2024-10-16 12:24:11 +02:00
|
|
|
nodes[node_name]['title'] = node_title
|
2024-10-22 15:34:39 +02:00
|
|
|
nodes[node_name]['count'] = len(iq['disco_items']['items'])
|
2024-10-15 14:38:27 +02:00
|
|
|
nodes[node_name]['item_ids'] = []
|
2024-10-22 15:34:39 +02:00
|
|
|
for item_id in iq['disco_items']['items']:
|
2024-10-15 14:38:27 +02:00
|
|
|
nodes[node_name]['item_ids'].append(
|
|
|
|
[item_id[0] or '', item_id[1] or '', item_id[2] or ''])
|
|
|
|
|
|
|
|
xmpp_instance.disconnect()
|
|
|
|
|
|
|
|
# Notes
|
|
|
|
print('Notes')
|
2024-10-15 19:25:06 +02:00
|
|
|
jid_vcard_note = jid_vcard['note']
|
|
|
|
if isinstance(jid_vcard_note, list) and len(jid_vcard_note):
|
|
|
|
note = jid_vcard_note[0]['NOTE']
|
2024-10-15 14:38:27 +02:00
|
|
|
else:
|
2024-10-15 19:25:06 +02:00
|
|
|
note = jid_vcard_note
|
|
|
|
#if not note and jid_vcard['name'] and not 'undefined' in jid_vcard['name'] and title != jid_vcard['name']:
|
|
|
|
# note = jid_vcard['name']
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
jid_details = {
|
|
|
|
'count' : count or '',
|
|
|
|
'error' : jid_info['error'],
|
|
|
|
'error_text' : jid_info['text'] or '',
|
|
|
|
'error_condition' : jid_info['condition'] or '',
|
2024-10-15 19:25:06 +02:00
|
|
|
'image_type' : jid_vcard['type'],
|
|
|
|
'items' : iq_disco_items_items_list,
|
2024-10-15 14:38:27 +02:00
|
|
|
'kind' : jid_kind or '',
|
|
|
|
'messages' : messages or '',
|
|
|
|
'name' : title,
|
|
|
|
'nodes' : nodes,
|
|
|
|
'note' : note or '',
|
2024-10-22 15:34:39 +02:00
|
|
|
'subject' : subject or ''}
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
print(jid_details)
|
|
|
|
|
2024-10-22 15:34:39 +02:00
|
|
|
# Query URI href
|
|
|
|
link_href = XmppUtilities.get_link_href(jid_bare, jid_kind, node_name)
|
|
|
|
|
2024-10-15 22:13:10 +02:00
|
|
|
FileUtilities.handle_photo(jid_bare, jid_vcard, link_href)
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
filename = 'details/{}.toml'.format(jid_bare)
|
|
|
|
Data.save_to_toml(filename, jid_details)
|
|
|
|
|
2024-10-15 19:25:06 +02:00
|
|
|
return jid_details
|
2024-10-15 14:38:27 +02:00
|
|
|
|
2024-10-15 22:13:10 +02:00
|
|
|
def handle_photo(jid_bare, jid_vcard, link_href):
|
2024-10-15 22:20:51 +02:00
|
|
|
filename = filepath = filetype = mimetype = selection = None
|
2024-10-15 14:38:27 +02:00
|
|
|
filecirca = 'photo/{}.*'.format(jid_bare)
|
2024-10-15 22:13:10 +02:00
|
|
|
filepath_guess = glob.glob(filecirca)
|
|
|
|
if filepath_guess:
|
2024-10-15 22:20:51 +02:00
|
|
|
filepath = filepath_guess[0]
|
|
|
|
filetype = filepath.split('.').pop()
|
2024-10-15 14:38:27 +02:00
|
|
|
filename = '{}.{}'.format(jid_bare, filetype)
|
2024-10-15 19:25:06 +02:00
|
|
|
elif jid_vcard:
|
|
|
|
if jid_vcard['type']:
|
|
|
|
mimetype = jid_vcard['type']
|
2024-10-15 14:38:27 +02:00
|
|
|
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:
|
2024-10-15 19:25:06 +02:00
|
|
|
file.write(jid_vcard['bin'])
|
2024-10-15 14:38:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
filepath_qrcode = 'qr/{}.png'.format(jid_bare)
|
|
|
|
if not os.path.exists(filepath_qrcode) or os.path.getsize(filepath_qrcode) == 0:
|
|
|
|
Graphics.generate_qr_code_graphics_from_string(link_href, jid_bare)
|
|
|
|
|
|
|
|
return filename, filepath, filetype, selection
|
|
|
|
|
2024-10-08 09:13:40 +02:00
|
|
|
class Graphics:
|
|
|
|
|
|
|
|
|
|
|
|
def extract_colours_from_raster(filepath):
|
|
|
|
try:
|
|
|
|
img = cv2.imread(filepath)
|
|
|
|
#thresholded = cv2.inRange(img, (50, 100, 200), (50, 100, 200))
|
|
|
|
thresholded = cv2.inRange(img, (90, 90, 90), (190, 190, 190))
|
|
|
|
#thresholded = cv2.bitwise_not(thresholded)
|
|
|
|
#thresholded = cv2.inRange(img, (0, 0, 0), (0, 0, 0))
|
|
|
|
#res = img + cv2.cvtColor(thresholded, cv2.COLOR_GRAY2BGR)
|
|
|
|
|
|
|
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
|
|
|
#result = numpy.clip(img, 90, 190)
|
|
|
|
#result = numpy.clip(img, 50, 200)
|
|
|
|
#result = numpy.clip(img, 100, 150)
|
|
|
|
result = numpy.clip(img, 100, 200)
|
|
|
|
res = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
|
|
"""
|
|
|
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
|
|
|
mask = numpy.all(numpy.logical_and(img >= 90, img <= 190), axis=2)
|
|
|
|
result = numpy.where(mask[...,None], img, 255)
|
|
|
|
res = cv2.cvtColor(result, cv2.COLOR_RGB2BGR)
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
# Thresholding for black:
|
|
|
|
lower_black = numpy.array([0, 0, 0])
|
|
|
|
upper_black = numpy.array([50, 50, 50]) # Adjust this value for the black range
|
|
|
|
black_mask = cv2.inRange(img, lower_black, upper_black)
|
|
|
|
|
|
|
|
# Thresholding for white:
|
|
|
|
lower_white = numpy.array([250, 250, 250])
|
|
|
|
upper_white = numpy.array([255, 255, 255])
|
|
|
|
white_mask = cv2.inRange(img, lower_white, upper_white)
|
|
|
|
|
|
|
|
# Combine the masks
|
|
|
|
combined_mask = cv2.bitwise_or(black_mask, white_mask)
|
|
|
|
|
|
|
|
# Invert the combined mask
|
|
|
|
inverted_mask = cv2.bitwise_not(combined_mask)
|
|
|
|
|
|
|
|
# Apply the mask to the original image
|
|
|
|
res = cv2.bitwise_and(img, img, mask=inverted_mask)
|
|
|
|
"""
|
|
|
|
|
|
|
|
selection = []
|
|
|
|
|
|
|
|
ix_1st = random.randint(1, len(res)-1)
|
|
|
|
res_ix_1st = res[ix_1st]
|
|
|
|
ix_ix_1st = random.randint(1, len(res_ix_1st)-1)
|
|
|
|
res_ix_ix_1st = res_ix_1st[ix_ix_1st]
|
|
|
|
selection.append(numpy.array(res_ix_ix_1st).tolist())
|
|
|
|
|
|
|
|
ix_2nd = random.randint(1, len(res)-1)
|
|
|
|
res_ix_2nd = res[ix_2nd]
|
|
|
|
ix_ix_2nd = random.randint(1, len(res_ix_2nd)-1)
|
|
|
|
res_ix_ix_2nd = res_ix_2nd[ix_ix_2nd]
|
|
|
|
selection.append(numpy.array(res_ix_ix_2nd).tolist())
|
|
|
|
print(selection)
|
|
|
|
|
|
|
|
except Exception as e:
|
2024-10-13 17:42:44 +02:00
|
|
|
selection = None
|
2024-10-08 09:13:40 +02:00
|
|
|
exception = str(e)
|
|
|
|
print(exception)
|
|
|
|
|
|
|
|
return selection
|
|
|
|
|
|
|
|
def extract_colours_from_vector(filepath):
|
|
|
|
# Parse the SVG file
|
|
|
|
tree = ET.parse(filepath)
|
|
|
|
root = tree.getroot()
|
|
|
|
|
|
|
|
# Set to store unique colours
|
|
|
|
colours_hex = set()
|
|
|
|
colours_rgb = []
|
|
|
|
|
|
|
|
# SVG namespace
|
|
|
|
namespace = {'svg': 'http://www.w3.org/2000/svg'}
|
|
|
|
|
|
|
|
# Find all possible elements
|
|
|
|
for elem in root.findall('.//svg:circle', namespace) + \
|
|
|
|
root.findall('.//svg:ellipse', namespace) + \
|
|
|
|
root.findall('.//svg:line', namespace) + \
|
|
|
|
root.findall('.//svg:path', namespace) + \
|
|
|
|
root.findall('.//svg:polygon', namespace) + \
|
|
|
|
root.findall('.//svg:rect', namespace) + \
|
|
|
|
root.findall('.//svg:text', namespace):
|
|
|
|
|
|
|
|
fill = elem.get('fill')
|
|
|
|
stroke = elem.get('stroke')
|
|
|
|
|
|
|
|
# Add colours to the set if they are not None or 'none'
|
|
|
|
if fill and fill.startswith('#') and len(fill) > 4 and fill.lower() != 'none':
|
|
|
|
colours_hex.add(fill)
|
|
|
|
if stroke and stroke.startswith('#') and len(stroke) > 4 and stroke.lower() != 'none':
|
|
|
|
colours_hex.add(stroke)
|
|
|
|
|
|
|
|
for colour in colours_hex:
|
|
|
|
hex = colour.lstrip('#')
|
|
|
|
rgb = list(int(hex[i:i+2], 16) for i in (0, 2, 4))
|
|
|
|
rgb.reverse()
|
|
|
|
colours_rgb.append(rgb)
|
|
|
|
|
|
|
|
selection = []
|
|
|
|
if len(colours_rgb) > 1:
|
|
|
|
for i in range(2):
|
|
|
|
ix = random.randint(0, len(colours_rgb)-1)
|
|
|
|
selection.append(colours_rgb[ix])
|
|
|
|
del colours_rgb[ix]
|
|
|
|
elif len(colours_rgb) == 1:
|
|
|
|
selection = [colours_rgb[0], colours_rgb[0]]
|
|
|
|
|
|
|
|
return selection
|
|
|
|
|
|
|
|
def generate_qr_code_graphics_from_string(text, jid_bare):
|
2024-10-09 15:40:51 +02:00
|
|
|
#qrcode_graphics = qrcode.make(text)
|
|
|
|
qr = qrcode.QRCode(border=2, box_size=10)
|
|
|
|
qr.add_data(text)
|
2024-10-09 22:07:16 +02:00
|
|
|
qrcode_graphics = qr.make_image(fill_color='#333', back_color='#f2f2f2')
|
2024-10-08 09:13:40 +02:00
|
|
|
qrcode_graphics.save('qr/{}.png'.format(jid_bare))
|
|
|
|
|
|
|
|
class Syndication:
|
|
|
|
|
|
|
|
def extract_items(item_payload, limit=False):
|
|
|
|
namespace = '{http://www.w3.org/2005/Atom}'
|
|
|
|
title = item_payload.find(namespace + 'title')
|
|
|
|
links = item_payload.find(namespace + 'link')
|
|
|
|
if (not isinstance(title, ET.Element) and
|
|
|
|
not isinstance(links, ET.Element)): return None
|
|
|
|
title_text = '' if title == None else title.text
|
2024-10-15 21:14:19 +02:00
|
|
|
link_href = ''
|
2024-10-08 09:13:40 +02:00
|
|
|
if isinstance(links, ET.Element):
|
|
|
|
for link in item_payload.findall(namespace + 'link'):
|
|
|
|
link_href = link.attrib['href'] if 'href' in link.attrib else ''
|
|
|
|
if link_href: break
|
|
|
|
contents = item_payload.find(namespace + 'content')
|
|
|
|
content_text = ''
|
|
|
|
if isinstance(contents, ET.Element):
|
|
|
|
for content in item_payload.findall(namespace + 'content'):
|
|
|
|
content_text = content.text or ''
|
|
|
|
if content_text: break
|
|
|
|
summaries = item_payload.find(namespace + 'summary')
|
|
|
|
summary_text = ''
|
|
|
|
if isinstance(summaries, ET.Element):
|
|
|
|
for summary in item_payload.findall(namespace + 'summary'):
|
|
|
|
summary_text = summary.text or ''
|
|
|
|
if summary_text: break
|
|
|
|
published = item_payload.find(namespace + 'published')
|
|
|
|
published_text = '' if published == None else published.text
|
|
|
|
categories = item_payload.find(namespace + 'category')
|
|
|
|
tags = []
|
|
|
|
if isinstance(categories, ET.Element):
|
|
|
|
for category in item_payload.findall(namespace + 'category'):
|
|
|
|
if 'term' in category.attrib and category.attrib['term']:
|
|
|
|
category_term = category.attrib['term']
|
|
|
|
if len(category_term) < 20:
|
|
|
|
tags.append(category_term)
|
|
|
|
elif len(category_term) < 50:
|
|
|
|
tags.append(category_term)
|
|
|
|
if limit and len(tags) > 4: break
|
|
|
|
|
|
|
|
|
|
|
|
identifier = item_payload.find(namespace + 'id')
|
|
|
|
if identifier and identifier.attrib: print(identifier.attrib)
|
|
|
|
identifier_text = '' if identifier == None else identifier.text
|
|
|
|
|
|
|
|
instances = '' # TODO Check the Blasta database for instances.
|
|
|
|
|
|
|
|
entry = {'content' : content_text,
|
2024-10-14 11:31:31 +02:00
|
|
|
'href' : link_href,
|
2024-10-08 09:13:40 +02:00
|
|
|
'published' : published_text,
|
|
|
|
'summary' : summary_text,
|
|
|
|
'tags' : tags,
|
|
|
|
'title' : title_text,
|
|
|
|
'updated' : published_text} # TODO "Updated" is missing
|
|
|
|
return entry
|
|
|
|
|
2024-10-15 14:38:27 +02:00
|
|
|
class XmppUtilities:
|
|
|
|
|
2024-10-15 19:25:06 +02:00
|
|
|
async def get_item_ids_of_node(jabber_id, password, jid_bare, node_name, nodes):
|
|
|
|
xmpp_instance = XmppInstance(jabber_id, password, jid_bare)
|
|
|
|
xmpp_instance.connect()
|
|
|
|
node_item_ids = await XmppXep0060.get_node_item_ids(xmpp_instance, jid_bare, node_name)
|
|
|
|
xmpp_instance.disconnect()
|
|
|
|
return node_item_ids
|
2024-10-15 14:38:27 +02:00
|
|
|
|
2024-10-22 15:34:39 +02:00
|
|
|
def set_action_instance_type(jid_kind, node_name=None):
|
|
|
|
if jid_kind in ('conference', 'server'):
|
2024-10-15 14:38:27 +02:00
|
|
|
action = 'Discover'
|
|
|
|
if jid_kind == 'conference':
|
|
|
|
instance = 'conferences'
|
|
|
|
elif jid_kind == 'server':
|
|
|
|
instance = 'services'
|
|
|
|
elif jid_kind in ('mix', 'muc'):
|
|
|
|
action = 'Join'
|
2024-10-22 15:34:39 +02:00
|
|
|
instance = 'occupants'
|
2024-10-15 14:38:27 +02:00
|
|
|
elif jid_kind == 'pubsub':
|
|
|
|
if node_name:
|
|
|
|
action = 'Subscribe'
|
|
|
|
instance = 'articles'
|
|
|
|
else:
|
|
|
|
action = 'Browse'
|
|
|
|
instance = 'nodes'
|
2024-10-22 15:34:39 +02:00
|
|
|
elif jid_kind == 'account':
|
2024-10-15 14:38:27 +02:00
|
|
|
action = 'Message'
|
|
|
|
instance = 'articles'
|
2024-10-22 15:34:39 +02:00
|
|
|
else: # jid_info['error']
|
|
|
|
action = 'Contact'
|
|
|
|
return action, instance
|
|
|
|
|
|
|
|
def get_link_href(jid_bare, jid_kind, node_name=None):
|
|
|
|
if jid_kind in ('conference', 'server'):
|
|
|
|
link_href = 'xmpp:{}?disco;type=get;request=items'.format(jid_bare)
|
|
|
|
elif jid_kind in ('mix', 'muc'):
|
|
|
|
link_href = 'xmpp:{}?join'.format(jid_bare)
|
|
|
|
elif jid_kind == 'pubsub':
|
|
|
|
if node_name:
|
|
|
|
link_href = 'xmpp:{}?pubsub;node={};action=subscribe'.format(jid_bare, node_name)
|
|
|
|
else:
|
|
|
|
link_href = 'xmpp:{}?disco;type=get;request=items'.format(jid_bare)
|
|
|
|
elif jid_kind == 'account':
|
2024-10-15 14:38:27 +02:00
|
|
|
link_href = 'xmpp:{}?message'.format(jid_bare)
|
2024-10-22 15:34:39 +02:00
|
|
|
else: # jid_info['error']
|
|
|
|
link_href = 'xmpp:{}'.format(jid_bare)
|
|
|
|
return link_href
|
|
|
|
|
|
|
|
def get_view_href(jid_bare, jid_kind, node_name=None):
|
|
|
|
links = []
|
|
|
|
view_href = None
|
|
|
|
if jid_kind in ('conference', 'server'):
|
|
|
|
view_href = '/d/' + jid_bare
|
|
|
|
elif jid_kind in ('mix', 'muc'):
|
|
|
|
view_href = '/v/' + jid_bare
|
|
|
|
elif jid_kind == 'pubsub':
|
|
|
|
if node_name:
|
|
|
|
view_href = '/d/{}/{}'.format(jid_bare, node_name)
|
|
|
|
else:
|
|
|
|
view_href = '/d/' + jid_bare
|
|
|
|
elif jid_kind == 'account':
|
|
|
|
view_href = '/d/{}/{}'.format(jid_bare, node_name)
|
|
|
|
return view_href
|
|
|
|
|
|
|
|
def get_xmpp_uri(jid_bare, jid_kind, node_name=None):
|
|
|
|
links = []
|
|
|
|
view_href = None
|
|
|
|
xmpp_uri = jid_bare
|
|
|
|
if jid_kind in ('conference', 'server'):
|
|
|
|
xmpp_uri = jid_bare
|
|
|
|
elif jid_kind in ('mix', 'muc'):
|
|
|
|
xmpp_uri = jid_bare
|
|
|
|
elif jid_kind == 'pubsub':
|
|
|
|
if node_name:
|
|
|
|
xmpp_uri = '{}?;node={}'.format(jid_bare, node_name)
|
|
|
|
else:
|
|
|
|
xmpp_uri = jid_bare
|
|
|
|
elif jid_kind == 'account':
|
|
|
|
xmpp_uri = jid_bare
|
|
|
|
return xmpp_uri
|
|
|
|
|
|
|
|
def get_query_uri_links(jid_bare, jid_kind, node_name=None, item_id=None):
|
|
|
|
links = []
|
|
|
|
if jid_kind in ('conference', 'server'):
|
|
|
|
links.append({'name' : 'Discover',
|
|
|
|
'href' : 'xmpp:{}?disco;type=get;request=items'.format(jid_bare),
|
|
|
|
'iden' : 'discover'})
|
|
|
|
xmpp_uri = jid_bare
|
|
|
|
elif jid_kind in ('mix', 'muc'):
|
|
|
|
links.append({'name' : 'Join',
|
|
|
|
'href' : 'xmpp:{}?join'.format(jid_bare),
|
|
|
|
'iden' : 'join'})
|
|
|
|
elif jid_kind == 'pubsub':
|
|
|
|
links.append({'name' : 'Browse',
|
|
|
|
'href' : 'xmpp:{}?disco;type=get;request=items'.format(jid_bare),
|
|
|
|
'iden' : 'browse'})
|
|
|
|
elif jid_kind == 'account':
|
2024-10-15 14:38:27 +02:00
|
|
|
links.append({'name' : 'Message',
|
2024-10-22 15:34:39 +02:00
|
|
|
'href' : 'xmpp:{}?message'.format(jid_bare),
|
2024-10-15 14:38:27 +02:00
|
|
|
'iden' : 'message'})
|
2024-10-16 12:24:11 +02:00
|
|
|
links.append({'name' : 'Add',
|
|
|
|
'href' : 'xmpp:{}?roster'.format(jid_bare),
|
|
|
|
'iden' : 'add'})
|
2024-10-22 15:34:39 +02:00
|
|
|
else: # jid_info['error']
|
|
|
|
links.append({'name' : 'Connect',
|
|
|
|
'href' : 'xmpp:{}'.format(jid_bare),
|
|
|
|
'iden' : 'connect'})
|
|
|
|
links.append({'name' : 'Add',
|
|
|
|
'href' : 'xmpp:{}?roster'.format(jid_bare),
|
|
|
|
'iden' : 'add'})
|
2024-10-15 14:38:27 +02:00
|
|
|
if item_id:
|
|
|
|
links.append({'name' : 'Subscribe',
|
|
|
|
'href' : 'xmpp:{}?pubsub;node={};item={};action=subscribe'.format(jid_bare, node_name, item_id),
|
|
|
|
'iden' : 'subscribe'})
|
|
|
|
links.append({'name' : 'View',
|
|
|
|
'href' : 'xmpp:{}?pubsub;node={};item={}'.format(jid_bare, node_name, item_id),
|
|
|
|
'iden' : 'view'})
|
|
|
|
elif node_name:
|
|
|
|
links.append({'name' : 'Subscribe',
|
|
|
|
'href' : 'xmpp:{}?pubsub;node={};action=subscribe'.format(jid_bare, node_name),
|
|
|
|
'iden' : 'subscribe'})
|
|
|
|
links.append({'name' : 'View',
|
|
|
|
'href' : 'xmpp:{}?pubsub;node={}'.format(jid_bare, node_name),
|
|
|
|
'iden' : 'view'})
|
|
|
|
links.append({'name' : 'vCard',
|
|
|
|
'href' : 'xmpp:{}?vcard'.format(jid_bare),
|
|
|
|
'iden' : 'vcard'})
|
2024-10-22 15:34:39 +02:00
|
|
|
return links
|
2024-10-15 14:38:27 +02:00
|
|
|
|
2024-10-08 09:13:40 +02:00
|
|
|
class XmppXep0030:
|
|
|
|
|
|
|
|
async def get_jid_items(self, jid_bare):
|
|
|
|
try:
|
|
|
|
condition = text = None
|
|
|
|
error = False
|
|
|
|
iq = await self['xep_0030'].get_items(jid=jid_bare)
|
|
|
|
except (IqError, IqTimeout) as e:
|
|
|
|
#logger.warning('Chat type could not be determined for {}'.format(jid_bare))
|
|
|
|
#logger.error(e)
|
|
|
|
iq = None
|
|
|
|
error = True
|
|
|
|
condition = e.iq['error']['condition']
|
|
|
|
text = e.iq['error']['text'] or 'Error'
|
|
|
|
#if not text:
|
|
|
|
# # NOTE We might want to set a specific photo for condition remote-server-not-found
|
|
|
|
# if condition:
|
|
|
|
# text = 'Could not determine JID type'
|
|
|
|
# else:
|
|
|
|
# text = 'Unknown Error'
|
|
|
|
result = {
|
|
|
|
'condition' : condition,
|
|
|
|
'error' : error,
|
|
|
|
'iq' : iq,
|
|
|
|
'text' : text}
|
|
|
|
return result
|
|
|
|
|
|
|
|
# NOTE
|
|
|
|
# Feature "urn:xmpp:mucsub:0" is present in both, MUC local and MUC hostname
|
|
|
|
# Feature "urn:xmpp:serverinfo:0" is present in both, MUC hostname and main hostname
|
|
|
|
async def get_jid_info(self, jid_bare):
|
|
|
|
jid_kind = None
|
|
|
|
try:
|
|
|
|
error = False
|
|
|
|
condition = text = None
|
|
|
|
iq = await self['xep_0030'].get_info(jid=jid_bare)
|
|
|
|
iq_disco_info = iq['disco_info']
|
|
|
|
if iq_disco_info:
|
|
|
|
features = iq_disco_info['features']
|
|
|
|
if 'http://jabber.org/protocol/muc#unique' in features:
|
|
|
|
jid_kind = 'conference'
|
|
|
|
elif 'urn:xmpp:mix:core:1' in features:
|
|
|
|
jid_kind = 'mix'
|
|
|
|
elif ('muc_moderated' in features or
|
2024-10-22 15:34:39 +02:00
|
|
|
'muc_open' in features or
|
|
|
|
'muc_persistent' in features or
|
|
|
|
'muc_public' in features or
|
|
|
|
'muc_semianonymous' in features or
|
|
|
|
'muc_unmoderated' in features or
|
|
|
|
'muc_unsecured' in features):
|
2024-10-08 09:13:40 +02:00
|
|
|
jid_kind = 'muc'
|
|
|
|
else:
|
|
|
|
for identity in iq_disco_info['identities']:
|
|
|
|
if identity[0] == 'pubsub' and identity[1] == 'service':
|
|
|
|
#if 'http://jabber.org/protocol/pubsub' in features:
|
|
|
|
#if 'http://jabber.org/protocol/pubsub#access-authorize' in features:
|
|
|
|
#if 'http://jabber.org/protocol/rsm' in features:
|
|
|
|
jid_kind = 'pubsub'
|
|
|
|
break
|
|
|
|
if identity[0] == 'server' and identity[1] == 'im':
|
|
|
|
jid_kind = 'server'
|
|
|
|
break
|
|
|
|
#if identity[0] == 'pubsub' and identity[1] == 'pep':
|
|
|
|
if identity[0] == 'account':
|
|
|
|
#if 'urn:xmpp:bookmarks:1#compat-pep' in features:
|
|
|
|
#if 'urn:xmpp:bookmarks:1#compat' in features:
|
|
|
|
#if 'urn:xmpp:push:0' in features:
|
|
|
|
#if 'urn:xmpp:pep-vcard-conversion:0' in features:
|
|
|
|
#if 'urn:xmpp:sid:0' in features:
|
|
|
|
|
|
|
|
# Also in MIX
|
|
|
|
#if 'urn:xmpp:mam:2' in features:
|
|
|
|
#if 'urn:xmpp:mam:2#extended' in features:
|
|
|
|
jid_kind = 'account'
|
|
|
|
break
|
|
|
|
if identity[0] == 'client' and identity[1] == 'bot':
|
|
|
|
jid_kind = 'bot'
|
|
|
|
#logger.info('Jabber ID: {}\n'
|
|
|
|
# 'Chat Type: {}'.format(jid_bare, result))
|
|
|
|
else:
|
|
|
|
iq = condition = text = None
|
|
|
|
except (IqError, IqTimeout) as e:
|
|
|
|
#logger.warning('Chat type could not be determined for {}'.format(jid_bare))
|
|
|
|
#logger.error(e)
|
|
|
|
iq = None
|
|
|
|
error = True
|
|
|
|
condition = e.iq['error']['condition']
|
|
|
|
text = e.iq['error']['text'] or 'Error'
|
|
|
|
#if not text:
|
|
|
|
# # NOTE We might want to set a specific photo for condition remote-server-not-found
|
|
|
|
# if condition:
|
|
|
|
# text = 'Could not determine JID type'
|
|
|
|
# else:
|
|
|
|
# text = 'Unknown Error'
|
|
|
|
result = {
|
|
|
|
'condition' : condition,
|
|
|
|
'error' : error,
|
|
|
|
'iq' : iq,
|
|
|
|
'text' : text,
|
|
|
|
'kind' : jid_kind}
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
class XmppXep0045:
|
|
|
|
|
2024-10-13 17:42:44 +02:00
|
|
|
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,
|
2024-10-22 16:34:07 +02:00
|
|
|
timeout=10
|
2024-10-13 17:42:44 +02:00
|
|
|
)
|
2024-10-21 16:01:31 +02:00
|
|
|
except TimeoutError as e:
|
|
|
|
#raise HTTPException(status_code=504, detail='request-timeout-reached')
|
|
|
|
error = True
|
|
|
|
iq = e
|
|
|
|
condition = 'Request timeout reached'
|
|
|
|
text = str(e)
|
2024-10-13 17:42:44 +02:00
|
|
|
except (IqError, IqTimeout, PresenceError) as e:
|
|
|
|
error = True
|
2024-10-21 16:01:31 +02:00
|
|
|
iq = e
|
2024-10-13 17:42:44 +02:00
|
|
|
condition = e.iq['error']['condition']
|
|
|
|
text = e.iq['error']['text']
|
|
|
|
result = {
|
|
|
|
'error' : error,
|
|
|
|
'condition' : condition,
|
|
|
|
'text' : text,
|
|
|
|
'iq' : iq}
|
|
|
|
return result
|
|
|
|
|
2024-10-08 09:13:40 +02:00
|
|
|
async def get_room_data(self, jid_bare):
|
|
|
|
return await self['xep_0045'].get_room_config(jid_bare)
|
|
|
|
|
2024-10-13 17:42:44 +02:00
|
|
|
async def get_room_participants(self, jid_bare):
|
|
|
|
return await self['xep_0045'].get_roster(jid_bare)
|
2024-10-08 09:13:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
# NOTE: "Item not found", yet is a group chat
|
|
|
|
# That is, JID has no vcard
|
|
|
|
# messaging-off@conference.movim.eu
|
|
|
|
|
|
|
|
class XmppXep0054:
|
|
|
|
|
|
|
|
async def get_vcard_data(self, jid_bare):
|
|
|
|
try:
|
|
|
|
error = False
|
|
|
|
condition = text = None
|
|
|
|
iq = await self['xep_0054'].get_vcard(jid_bare)
|
|
|
|
except (IqError, IqTimeout) as e:
|
|
|
|
error = True
|
|
|
|
condition = e.iq['error']['condition']
|
|
|
|
text = e.iq['error']['text']
|
|
|
|
if not text:
|
|
|
|
if condition:
|
|
|
|
text = 'Could not retrieve vCard'
|
|
|
|
else:
|
|
|
|
text = 'Unknown Error'
|
|
|
|
iq = None
|
|
|
|
result = {
|
|
|
|
'error' : error,
|
|
|
|
'condition' : condition,
|
|
|
|
'text' : text,
|
|
|
|
'iq' : iq}
|
|
|
|
return result
|
|
|
|
|
|
|
|
class XmppXep0060:
|
|
|
|
|
|
|
|
async def get_node_items(self, jid_bare, node_name, item_ids=None, max_items=None):
|
|
|
|
try:
|
2024-10-22 15:34:39 +02:00
|
|
|
error = False
|
|
|
|
condition = text = None
|
2024-10-08 09:13:40 +02:00
|
|
|
if max_items:
|
|
|
|
iq = await self['xep_0060'].get_items(
|
|
|
|
jid_bare, node_name, timeout=5)
|
|
|
|
it = self['xep_0060'].get_items(
|
|
|
|
jid_bare, node_name, timeout=5, max_items=max_items, iterator=True)
|
|
|
|
q = rsm.Iq()
|
|
|
|
q['to'] = jid_bare
|
|
|
|
q['disco_items']['node'] = node_name
|
|
|
|
async for item in rsm.ResultIterator(q, 'disco_items', '10'):
|
|
|
|
print(item['disco_items']['items'])
|
|
|
|
|
|
|
|
else:
|
|
|
|
iq = await self['xep_0060'].get_items(
|
|
|
|
jid_bare, node_name, timeout=5, item_ids=item_ids)
|
|
|
|
result = iq
|
2024-10-22 15:34:39 +02:00
|
|
|
except (IqError, IqTimeout) as e:
|
|
|
|
error = True
|
|
|
|
iq = None
|
|
|
|
condition = e.iq['error']['condition']
|
|
|
|
text = e.iq['error']['text']
|
|
|
|
if not text:
|
|
|
|
if condition:
|
|
|
|
text = 'Could not retrieve node items'
|
|
|
|
else:
|
|
|
|
text = 'Unknown Error'
|
|
|
|
result = {
|
|
|
|
'error' : error,
|
|
|
|
'condition' : condition,
|
|
|
|
'text' : text,
|
|
|
|
'iq' : iq}
|
2024-10-08 09:13:40 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
async def get_node_item_ids(self, jid_bare, node_name):
|
|
|
|
try:
|
2024-10-15 19:25:06 +02:00
|
|
|
error = False
|
|
|
|
condition = text = None
|
2024-10-08 09:13:40 +02:00
|
|
|
iq = await self['xep_0030'].get_items(
|
|
|
|
jid_bare, node_name)
|
|
|
|
# Broken. See https://codeberg.org/poezio/slixmpp/issues/3548
|
|
|
|
#iq = await self['xep_0060'].get_item_ids(
|
|
|
|
# jid_bare, node_name, timeout=5)
|
2024-10-15 19:25:06 +02:00
|
|
|
except (IqError, IqTimeout) as e:
|
|
|
|
error = True
|
2024-10-22 15:34:39 +02:00
|
|
|
iq = None
|
2024-10-15 19:25:06 +02:00
|
|
|
condition = e.iq['error']['condition']
|
|
|
|
text = e.iq['error']['text']
|
|
|
|
if not text:
|
|
|
|
if condition:
|
2024-10-22 15:34:39 +02:00
|
|
|
text = 'Could not retrieve node item IDs'
|
2024-10-15 19:25:06 +02:00
|
|
|
else:
|
|
|
|
text = 'Unknown Error'
|
|
|
|
result = {
|
|
|
|
'error' : error,
|
|
|
|
'condition' : condition,
|
|
|
|
'text' : text,
|
|
|
|
'iq' : iq}
|
2024-10-08 09:13:40 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
class XmppXep0369:
|
|
|
|
|
|
|
|
async def get_room_data(self, jid_bare):
|
|
|
|
return await self['xep_0369'].get_channel_info(jid_bare)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
filename_configuration = 'configuration.toml'
|
2024-10-16 12:24:11 +02:00
|
|
|
configuration = Data.open_file_toml(filename_configuration)
|
2024-10-08 09:13:40 +02:00
|
|
|
|
2024-10-16 12:24:11 +02:00
|
|
|
http_instance = HttpInstance(configuration)
|
2024-10-08 09:13:40 +02:00
|
|
|
return http_instance.app
|
|
|
|
|
|
|
|
app = main()
|
|
|
|
|
|
|
|
# FIXME
|
|
|
|
if __name__ == '__main__':
|
|
|
|
uvicorn.run(app, host='127.0.0.1', port=8000, reload=True)
|