2024-04-14 14:56:45 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
TODO
|
|
|
|
|
|
|
|
1) Deprecate "add" (see above) and make it interactive.
|
|
|
|
Slixfeed: Do you still want to add this URL to subscription list?
|
2024-06-10 17:54:27 +02:00
|
|
|
See: case _ if command_lowercase.startswith("add"):
|
2024-04-14 14:56:45 +02:00
|
|
|
|
|
|
|
2) If subscription is inadequate (see XmppPresence.request), send a message that says so.
|
|
|
|
|
|
|
|
elif not self.client_roster[jid]["to"]:
|
|
|
|
breakpoint()
|
|
|
|
message.reply("Share online status to activate bot.").send()
|
|
|
|
return
|
|
|
|
|
|
|
|
3) Set timeout for moderator interaction.
|
|
|
|
If moderator interaction has been made, and moderator approves the bot, then
|
|
|
|
the bot will add the given groupchat to bookmarks; otherwise, the bot will
|
|
|
|
send a message that it was not approved and therefore leaves the groupchat.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import asyncio
|
2024-07-05 18:04:24 +02:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
2024-06-13 17:53:53 +02:00
|
|
|
from random import randrange # pending_tasks: Use a list and read the first index (i.e. index 0).
|
2024-04-14 14:56:45 +02:00
|
|
|
import slixfeed.config as config
|
|
|
|
from slixfeed.config import Config
|
2024-07-05 18:04:24 +02:00
|
|
|
import slixfeed.fetch as fetch
|
|
|
|
from slixfeed.fetch import Http
|
2024-06-13 17:53:53 +02:00
|
|
|
from slixfeed.log import Logger
|
2024-04-14 14:56:45 +02:00
|
|
|
import slixfeed.sqlite as sqlite
|
2024-06-13 17:53:53 +02:00
|
|
|
from slixfeed.syndication import FeedTask
|
2024-06-16 10:55:22 +02:00
|
|
|
from slixfeed.utilities import Documentation, Html, MD, Task, Url
|
2024-06-10 17:54:27 +02:00
|
|
|
from slixfeed.xmpp.commands import XmppCommands
|
2024-04-14 14:56:45 +02:00
|
|
|
from slixfeed.xmpp.message import XmppMessage
|
|
|
|
from slixfeed.xmpp.presence import XmppPresence
|
2024-06-13 17:53:53 +02:00
|
|
|
from slixfeed.xmpp.status import XmppStatusTask
|
2024-04-14 14:56:45 +02:00
|
|
|
from slixfeed.xmpp.upload import XmppUpload
|
2024-06-13 17:53:53 +02:00
|
|
|
from slixfeed.xmpp.utilities import XmppUtilities
|
2024-07-05 18:04:24 +02:00
|
|
|
from slixmpp import JID
|
|
|
|
from slixmpp.stanza import Message
|
2024-06-13 17:53:53 +02:00
|
|
|
import sys
|
2024-04-14 14:56:45 +02:00
|
|
|
import time
|
2024-07-07 10:16:00 +02:00
|
|
|
from typing import Optional
|
2024-04-14 14:56:45 +02:00
|
|
|
|
2024-09-11 08:48:27 +02:00
|
|
|
try:
|
|
|
|
from slixfeed.xmpp.encryption import XmppOmemo
|
|
|
|
except Exception as e:
|
|
|
|
print('Encryption of type OMEMO is not enabled. Reason: ' + str(e))
|
2024-04-14 14:56:45 +02:00
|
|
|
|
2024-06-13 17:53:53 +02:00
|
|
|
logger = Logger(__name__)
|
2024-04-14 14:56:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
# for task in main_task:
|
|
|
|
# task.cancel()
|
|
|
|
|
|
|
|
# Deprecated in favour of event "presence_available"
|
|
|
|
# if not main_task:
|
|
|
|
# await select_file()
|
|
|
|
|
2024-06-13 17:53:53 +02:00
|
|
|
|
|
|
|
class XmppChat:
|
|
|
|
|
2024-04-14 14:56:45 +02:00
|
|
|
|
2024-07-05 18:04:24 +02:00
|
|
|
async def process_message(self, message: Message, allow_untrusted: bool = False) -> None:
|
2024-04-14 14:56:45 +02:00
|
|
|
"""
|
|
|
|
Process incoming message stanzas. Be aware that this also
|
|
|
|
includes MUC messages and error messages. It is usually
|
|
|
|
a good practice to check the messages's type before
|
|
|
|
processing or sending replies.
|
2024-06-10 17:54:27 +02:00
|
|
|
|
2024-04-14 14:56:45 +02:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
message : str
|
|
|
|
The received message stanza. See the documentation
|
|
|
|
for stanza objects and the Message stanza to see
|
|
|
|
how it may be used.
|
|
|
|
"""
|
2024-07-05 18:04:24 +02:00
|
|
|
message_from = message['from']
|
|
|
|
message_type = message['type']
|
|
|
|
if message_type in ('chat', 'groupchat', 'normal'):
|
|
|
|
jid_bare = message_from.bare
|
2024-06-10 17:54:27 +02:00
|
|
|
command = ' '.join(message['body'].split())
|
2024-04-14 14:56:45 +02:00
|
|
|
command_time_start = time.time()
|
2024-06-10 17:54:27 +02:00
|
|
|
|
2024-07-07 11:57:10 +02:00
|
|
|
# if (message_type == 'groupchat' and
|
2024-04-14 14:56:45 +02:00
|
|
|
# message['muc']['nick'] == self.alias):
|
|
|
|
# return
|
2024-06-10 17:54:27 +02:00
|
|
|
|
2024-04-14 14:56:45 +02:00
|
|
|
# FIXME Code repetition. See below.
|
|
|
|
# TODO Check alias by nickname associated with conference
|
2024-07-05 18:04:24 +02:00
|
|
|
if message_type == 'groupchat':
|
2024-09-12 16:22:03 +02:00
|
|
|
alias = message['muc']['nick']
|
|
|
|
if (alias == self.alias):
|
2024-04-14 14:56:45 +02:00
|
|
|
return
|
2024-09-12 16:22:03 +02:00
|
|
|
if not XmppUtilities.is_moderator(self, jid_bare, alias):
|
2024-04-14 14:56:45 +02:00
|
|
|
return
|
|
|
|
# nick = message['from'][message['from'].index('/')+1:]
|
|
|
|
# nick = str(message['from'])
|
|
|
|
# nick = nick[nick.index('/')+1:]
|
2024-09-12 16:22:03 +02:00
|
|
|
if (alias == self.alias or
|
2024-04-14 14:56:45 +02:00
|
|
|
not message['body'].startswith('!')):
|
|
|
|
return
|
|
|
|
# token = await initdb(
|
|
|
|
# jid_bare,
|
|
|
|
# sqlite.get_setting_value,
|
|
|
|
# 'token'
|
|
|
|
# )
|
|
|
|
# if token == 'accepted':
|
|
|
|
# operator = await initdb(
|
|
|
|
# jid_bare,
|
|
|
|
# sqlite.get_setting_value,
|
|
|
|
# 'masters'
|
|
|
|
# )
|
|
|
|
# if operator:
|
|
|
|
# if nick not in operator:
|
|
|
|
# return
|
|
|
|
# approved = False
|
2024-09-12 16:22:03 +02:00
|
|
|
if not XmppUtilities.is_moderator(self, jid_bare, alias):
|
2024-04-14 14:56:45 +02:00
|
|
|
return
|
|
|
|
# if role == 'moderator':
|
|
|
|
# approved = True
|
|
|
|
# TODO Implement a list of temporary operators
|
|
|
|
# Once an operator is appointed, the control would last
|
|
|
|
# untile the participant has been disconnected from MUC
|
|
|
|
# An operator is a function to appoint non moderators.
|
|
|
|
# Changing nickname is fine and consist of no problem.
|
|
|
|
# if not approved:
|
|
|
|
# operator = await initdb(
|
|
|
|
# jid_bare,
|
|
|
|
# sqlite.get_setting_value,
|
|
|
|
# 'masters'
|
|
|
|
# )
|
|
|
|
# if operator:
|
|
|
|
# if nick in operator:
|
|
|
|
# approved = True
|
|
|
|
# if not approved:
|
|
|
|
# return
|
|
|
|
|
|
|
|
# # Begin processing new JID
|
|
|
|
# # Deprecated in favour of event 'presence_available'
|
|
|
|
# db_dir = config.get_default_data_directory()
|
|
|
|
# os.chdir(db_dir)
|
|
|
|
# if jid + '.db' not in os.listdir():
|
|
|
|
# await task_jid(jid)
|
2024-06-10 17:54:27 +02:00
|
|
|
|
2024-04-14 14:56:45 +02:00
|
|
|
# await compose.message(self, jid_bare, message)
|
2024-06-10 17:54:27 +02:00
|
|
|
|
2024-09-11 08:48:27 +02:00
|
|
|
if self.omemo_present and self['xep_0384'].is_encrypted(message):
|
2024-09-12 14:20:14 +02:00
|
|
|
command, omemo_decrypted = await XmppOmemo.decrypt(
|
|
|
|
self, message)
|
2024-07-05 18:04:24 +02:00
|
|
|
else:
|
|
|
|
omemo_decrypted = None
|
|
|
|
|
2024-09-12 15:06:05 +02:00
|
|
|
if message_type == 'groupchat': command = command[1:]
|
|
|
|
if isinstance(command, Message): command = command['body']
|
2024-09-12 14:20:14 +02:00
|
|
|
|
2024-09-12 15:06:05 +02:00
|
|
|
command_lowercase = command.lower()
|
2024-09-12 14:20:14 +02:00
|
|
|
|
|
|
|
# This is a work-around to empty messages that are caused by function
|
|
|
|
# self.register_handler(CoroutineCallback( of module client.py.
|
|
|
|
# The code was taken from the cho bot xample of slixmpp-omemo.
|
|
|
|
#if not command_lowercase: return
|
2024-06-10 17:54:27 +02:00
|
|
|
|
2024-07-05 18:04:24 +02:00
|
|
|
logger.debug([message_from.full, ':', command])
|
2024-06-10 17:54:27 +02:00
|
|
|
|
2024-04-14 14:56:45 +02:00
|
|
|
# Support private message via groupchat
|
|
|
|
# See https://codeberg.org/poezio/slixmpp/issues/3506
|
2024-07-07 11:57:10 +02:00
|
|
|
if message_type == 'chat' and message.get_plugin('muc', check=True):
|
2024-07-05 18:04:24 +02:00
|
|
|
# jid_bare = message_from.bare
|
|
|
|
jid_full = message_from.full
|
2024-04-14 14:56:45 +02:00
|
|
|
if (jid_bare == jid_full[:jid_full.index('/')]):
|
2024-05-30 18:42:26 +02:00
|
|
|
# TODO Count and alert of MUC-PM attempts
|
|
|
|
return
|
2024-04-14 14:56:45 +02:00
|
|
|
|
|
|
|
response = None
|
2024-06-10 17:54:27 +02:00
|
|
|
db_file = config.get_pathname_to_database(jid_bare)
|
|
|
|
match command_lowercase:
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'help':
|
2024-06-10 17:54:27 +02:00
|
|
|
command_list = XmppCommands.print_help()
|
2024-04-14 14:56:45 +02:00
|
|
|
response = ('Available command keys:\n'
|
|
|
|
'```\n{}\n```\n'
|
|
|
|
'Usage: `help <key>`'
|
|
|
|
.format(command_list))
|
|
|
|
case 'help all':
|
2024-06-13 17:53:53 +02:00
|
|
|
command_list = Documentation.manual('commands.toml', section='all')
|
2024-04-14 14:56:45 +02:00
|
|
|
response = ('Complete list of commands:\n'
|
|
|
|
'```\n{}\n```'
|
|
|
|
.format(command_list))
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('help'):
|
|
|
|
command = command[5:].lower()
|
2024-04-14 14:56:45 +02:00
|
|
|
command = command.split(' ')
|
|
|
|
if len(command) == 2:
|
|
|
|
command_root = command[0]
|
|
|
|
command_name = command[1]
|
2024-06-13 17:53:53 +02:00
|
|
|
command_list = Documentation.manual(
|
|
|
|
'commands.toml', section=command_root, command=command_name)
|
2024-04-14 14:56:45 +02:00
|
|
|
if command_list:
|
|
|
|
command_list = ''.join(command_list)
|
|
|
|
response = (command_list)
|
|
|
|
else:
|
|
|
|
response = ('KeyError for {} {}'
|
|
|
|
.format(command_root, command_name))
|
|
|
|
elif len(command) == 1:
|
|
|
|
command = command[0]
|
2024-06-13 17:53:53 +02:00
|
|
|
command_list = Documentation.manual('commands.toml', command)
|
2024-04-14 14:56:45 +02:00
|
|
|
if command_list:
|
|
|
|
command_list = ' '.join(command_list)
|
|
|
|
response = ('Available command `{}` keys:\n'
|
|
|
|
'```\n{}\n```\n'
|
|
|
|
'Usage: `help {} <command>`'
|
|
|
|
.format(command, command_list, command))
|
|
|
|
else:
|
|
|
|
response = 'KeyError for {}'.format(command)
|
|
|
|
else:
|
|
|
|
response = ('Invalid. Enter command key '
|
|
|
|
'or command key & name')
|
|
|
|
case 'info':
|
2024-06-10 17:54:27 +02:00
|
|
|
entries = XmppCommands.print_info_list()
|
2024-04-14 14:56:45 +02:00
|
|
|
response = ('Available command options:\n'
|
|
|
|
'```\n{}\n```\n'
|
|
|
|
'Usage: `info <option>`'
|
2024-06-10 17:54:27 +02:00
|
|
|
.format(entries))
|
|
|
|
case _ if command_lowercase.startswith('info'):
|
|
|
|
entry = command[5:].lower()
|
|
|
|
response = XmppCommands.print_info_specific(entry)
|
|
|
|
case _ if command_lowercase in ['greetings', 'hallo', 'hello',
|
2024-04-14 14:56:45 +02:00
|
|
|
'hey', 'hi', 'hola', 'holla',
|
|
|
|
'hollo']:
|
2024-09-11 08:48:27 +02:00
|
|
|
response = ('Greeting. My name is {}.\n'
|
|
|
|
'I am an Atom/RSS News Bot.\n'
|
2024-04-14 14:56:45 +02:00
|
|
|
'Send "help" for further instructions.\n'
|
|
|
|
.format(self.alias))
|
2024-06-13 17:53:53 +02:00
|
|
|
case _ if command_lowercase.startswith('add'):
|
2024-06-10 17:54:27 +02:00
|
|
|
command = command[4:]
|
|
|
|
url = command.split(' ')[0]
|
|
|
|
title = ' '.join(command.split(' ')[1:])
|
2024-07-05 18:04:24 +02:00
|
|
|
response = await XmppCommands.feed_add(
|
2024-06-10 17:54:27 +02:00
|
|
|
url, db_file, jid_bare, title)
|
|
|
|
case _ if command_lowercase.startswith('allow +'):
|
|
|
|
val = command[7:]
|
2024-04-14 14:56:45 +02:00
|
|
|
if val:
|
2024-06-10 17:54:27 +02:00
|
|
|
await XmppCommands.set_filter_allow(
|
|
|
|
db_file, val, True)
|
2024-04-14 14:56:45 +02:00
|
|
|
response = ('Approved keywords\n'
|
|
|
|
'```\n{}\n```'
|
|
|
|
.format(val))
|
|
|
|
else:
|
|
|
|
response = ('No action has been taken.'
|
|
|
|
'\n'
|
|
|
|
'Missing keywords.')
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('allow -'):
|
|
|
|
val = command[7:]
|
2024-04-14 14:56:45 +02:00
|
|
|
if val:
|
2024-06-10 17:54:27 +02:00
|
|
|
await XmppCommands.set_filter_allow(
|
|
|
|
db_file, val, False)
|
2024-04-14 14:56:45 +02:00
|
|
|
response = ('Approved keywords\n'
|
|
|
|
'```\n{}\n```'
|
|
|
|
.format(val))
|
|
|
|
else:
|
|
|
|
response = ('No action has been taken.'
|
|
|
|
'\n'
|
|
|
|
'Missing keywords.')
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('archive'):
|
|
|
|
val = command[8:]
|
2024-04-14 14:56:45 +02:00
|
|
|
if val:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.set_archive(
|
|
|
|
self, db_file, jid_bare, val)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = 'Current value for archive: '
|
|
|
|
response += XmppCommands.get_archive(self, jid_bare)
|
|
|
|
case _ if command_lowercase.startswith('bookmark +'):
|
2024-06-16 10:55:22 +02:00
|
|
|
if XmppUtilities.is_operator(self, jid_bare):
|
2024-06-10 17:54:27 +02:00
|
|
|
muc_jid = command[11:]
|
|
|
|
response = await XmppCommands.bookmark_add(
|
|
|
|
self, muc_jid)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
|
|
|
response = ('This action is restricted. '
|
|
|
|
'Type: adding bookmarks.')
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('bookmark -'):
|
2024-06-16 10:55:22 +02:00
|
|
|
if XmppUtilities.is_operator(self, jid_bare):
|
2024-06-10 17:54:27 +02:00
|
|
|
muc_jid = command[11:]
|
|
|
|
response = await XmppCommands.bookmark_del(
|
|
|
|
self, muc_jid)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
|
|
|
response = ('This action is restricted. '
|
|
|
|
'Type: removing bookmarks.')
|
|
|
|
case 'bookmarks':
|
2024-06-16 10:55:22 +02:00
|
|
|
if XmppUtilities.is_operator(self, jid_bare):
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.print_bookmarks(self)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
|
|
|
response = ('This action is restricted. '
|
|
|
|
'Type: viewing bookmarks.')
|
2024-06-13 17:53:53 +02:00
|
|
|
case _ if command_lowercase.startswith('clear'):
|
2024-06-10 17:54:27 +02:00
|
|
|
key = command[6:]
|
|
|
|
response = await XmppCommands.clear_filter(db_file, key)
|
2024-06-13 17:53:53 +02:00
|
|
|
case _ if command_lowercase.startswith('default'):
|
2024-06-10 17:54:27 +02:00
|
|
|
key = command[8:]
|
|
|
|
response = await XmppCommands.restore_default(
|
|
|
|
self, jid_bare, key=None)
|
|
|
|
case 'defaults':
|
|
|
|
response = await XmppCommands.restore_default(self, jid_bare)
|
|
|
|
case _ if command_lowercase.startswith('deny +'):
|
|
|
|
val = command[6:]
|
2024-04-14 14:56:45 +02:00
|
|
|
if val:
|
2024-06-10 17:54:27 +02:00
|
|
|
await XmppCommands.set_filter_allow(
|
|
|
|
db_file, val, True)
|
2024-04-14 14:56:45 +02:00
|
|
|
response = ('Rejected keywords\n'
|
|
|
|
'```\n{}\n```'
|
|
|
|
.format(val))
|
|
|
|
else:
|
|
|
|
response = ('No action has been taken.'
|
|
|
|
'\n'
|
|
|
|
'Missing keywords.')
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('deny -'):
|
|
|
|
val = command[6:]
|
2024-04-14 14:56:45 +02:00
|
|
|
if val:
|
2024-06-10 17:54:27 +02:00
|
|
|
await XmppCommands.set_filter_allow(
|
|
|
|
db_file, val, False)
|
2024-04-14 14:56:45 +02:00
|
|
|
response = ('Rejected keywords\n'
|
|
|
|
'```\n{}\n```'
|
|
|
|
.format(val))
|
|
|
|
else:
|
|
|
|
response = ('No action has been taken.'
|
|
|
|
'\n'
|
|
|
|
'Missing keywords.')
|
2024-06-13 17:53:53 +02:00
|
|
|
case _ if command_lowercase.startswith('disable'):
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.feed_disable(
|
|
|
|
self, db_file, jid_bare, command)
|
2024-06-13 17:53:53 +02:00
|
|
|
XmppStatusTask.restart_task(self, jid_bare)
|
|
|
|
case _ if command_lowercase.startswith('enable'):
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.feed_enable(
|
|
|
|
self, db_file, command)
|
2024-06-13 17:53:53 +02:00
|
|
|
case _ if command_lowercase.startswith('export'):
|
2024-06-10 17:54:27 +02:00
|
|
|
ext = command[7:]
|
2024-04-14 14:56:45 +02:00
|
|
|
if ext in ('md', 'opml'): # html xbel
|
|
|
|
status_type = 'dnd'
|
|
|
|
status_message = ('📤️ Procesing request to '
|
|
|
|
'export feeds into {}...'
|
|
|
|
.format(ext.upper()))
|
|
|
|
# pending_tasks_num = len(self.pending_tasks[jid_bare])
|
|
|
|
pending_tasks_num = randrange(10000, 99999)
|
|
|
|
self.pending_tasks[jid_bare][pending_tasks_num] = status_message
|
|
|
|
# self.pending_tasks_counter += 1
|
|
|
|
# self.pending_tasks[jid_bare][self.pending_tasks_counter] = status_message
|
|
|
|
XmppPresence.send(self, jid_bare, status_message,
|
|
|
|
status_type=status_type)
|
2024-07-05 18:04:24 +02:00
|
|
|
pathname, response = XmppCommands.export_feeds(
|
2024-06-16 10:55:22 +02:00
|
|
|
jid_bare, ext)
|
2024-07-07 11:57:10 +02:00
|
|
|
encrypt_omemo = Config.get_setting_value(self, jid_bare, 'omemo')
|
2024-07-05 18:04:24 +02:00
|
|
|
encrypted = True if encrypt_omemo else False
|
|
|
|
url = await XmppUpload.start(self, jid_bare, Path(pathname), encrypted=encrypted)
|
2024-04-14 14:56:45 +02:00
|
|
|
# response = (
|
|
|
|
# 'Feeds exported successfully to {}.\n{}'
|
|
|
|
# ).format(ex, url)
|
|
|
|
# XmppMessage.send_oob_reply_message(message, url, response)
|
2024-07-05 18:04:24 +02:00
|
|
|
if url:
|
|
|
|
chat_type = await XmppUtilities.get_chat_type(self, jid_bare)
|
2024-09-11 08:48:27 +02:00
|
|
|
if self.omemo_present and encrypted:
|
2024-07-07 10:16:00 +02:00
|
|
|
url_encrypted, omemo_encrypted = await XmppOmemo.encrypt(
|
2024-09-12 14:20:14 +02:00
|
|
|
self, message_from, 'chat', url)
|
2024-07-07 11:57:10 +02:00
|
|
|
XmppMessage.send_omemo_oob(self, message_from, url_encrypted, chat_type)
|
2024-07-07 10:16:00 +02:00
|
|
|
else:
|
|
|
|
XmppMessage.send_oob(self, jid_bare, url, chat_type)
|
2024-07-05 18:04:24 +02:00
|
|
|
else:
|
|
|
|
response = 'OPML file export has been failed.'
|
2024-04-14 14:56:45 +02:00
|
|
|
del self.pending_tasks[jid_bare][pending_tasks_num]
|
|
|
|
# del self.pending_tasks[jid_bare][self.pending_tasks_counter]
|
2024-06-13 17:53:53 +02:00
|
|
|
XmppStatusTask.restart_task(self, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
|
|
|
response = ('Unsupported filetype.\n'
|
|
|
|
'Try: md or opml')
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('feeds'):
|
|
|
|
query = command[6:]
|
|
|
|
result, number = XmppCommands.list_feeds(db_file, query)
|
|
|
|
if number:
|
|
|
|
if query:
|
|
|
|
first_line = 'Subscriptions containing "{}":\n\n```\n'.format(query)
|
|
|
|
else:
|
|
|
|
first_line = 'Subscriptions:\n\n```\n'
|
|
|
|
response = (first_line + result +
|
|
|
|
'\n```\nTotal of {} feeds'.format(number))
|
|
|
|
case 'goodbye':
|
2024-07-07 11:57:10 +02:00
|
|
|
if message_type == 'groupchat':
|
2024-06-10 17:54:27 +02:00
|
|
|
await XmppCommands.muc_leave(self, jid_bare)
|
|
|
|
else:
|
|
|
|
response = 'This command is valid in groupchat only.'
|
|
|
|
case _ if (command_lowercase.startswith('gemini:') or
|
|
|
|
command_lowercase.startswith('gopher:')):
|
|
|
|
response = XmppCommands.fetch_gemini()
|
|
|
|
case _ if (command_lowercase.startswith('http') and
|
|
|
|
command_lowercase.endswith('.opml')):
|
2024-06-13 17:53:53 +02:00
|
|
|
Task.stop(self, jid_bare, 'status')
|
2024-04-14 14:56:45 +02:00
|
|
|
status_type = 'dnd'
|
|
|
|
status_message = '📥️ Procesing request to import feeds...'
|
|
|
|
# pending_tasks_num = len(self.pending_tasks[jid_bare])
|
|
|
|
pending_tasks_num = randrange(10000, 99999)
|
|
|
|
self.pending_tasks[jid_bare][pending_tasks_num] = status_message
|
|
|
|
# self.pending_tasks_counter += 1
|
|
|
|
# self.pending_tasks[jid_bare][self.pending_tasks_counter] = status_message
|
|
|
|
XmppPresence.send(self, jid_bare, status_message,
|
|
|
|
status_type=status_type)
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.import_opml(
|
|
|
|
self, db_file, jid_bare, command)
|
2024-04-14 14:56:45 +02:00
|
|
|
del self.pending_tasks[jid_bare][pending_tasks_num]
|
|
|
|
# del self.pending_tasks[jid_bare][self.pending_tasks_counter]
|
2024-06-13 17:53:53 +02:00
|
|
|
XmppStatusTask.restart_task(self, jid_bare)
|
|
|
|
case _ if command_lowercase.startswith('pubsub list'):
|
2024-07-05 18:04:24 +02:00
|
|
|
jid_full_pubsub = command[12:]
|
|
|
|
response = 'List of nodes for {}:\n```\n'.format(jid_full_pubsub)
|
|
|
|
response = await XmppCommands.pubsub_list(self, jid_full_pubsub)
|
2024-05-14 21:39:58 +02:00
|
|
|
response += '```'
|
2024-06-13 17:53:53 +02:00
|
|
|
case _ if command_lowercase.startswith('pubsub send'):
|
2024-06-16 10:55:22 +02:00
|
|
|
if XmppUtilities.is_operator(self, jid_bare):
|
2024-06-10 17:54:27 +02:00
|
|
|
info = command[12:]
|
2024-05-14 21:39:58 +02:00
|
|
|
info = info.split(' ')
|
2024-07-05 18:04:24 +02:00
|
|
|
jid_full_pubsub = info[0]
|
2024-05-14 21:39:58 +02:00
|
|
|
# num = int(info[1])
|
2024-07-05 18:04:24 +02:00
|
|
|
if jid_full_pubsub:
|
|
|
|
response = XmppCommands.pubsub_send(self, info, jid_full_pubsub)
|
2024-05-14 21:39:58 +02:00
|
|
|
else:
|
|
|
|
response = ('This action is restricted. '
|
|
|
|
'Type: sending news to PubSub.')
|
2024-04-14 14:56:45 +02:00
|
|
|
# TODO Handle node error
|
|
|
|
# sqlite3.IntegrityError: UNIQUE constraint failed: feeds_pubsub.node
|
|
|
|
# ERROR:slixmpp.basexmpp:UNIQUE constraint failed: feeds_pubsub.node
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if (command_lowercase.startswith('http') or
|
|
|
|
command_lowercase.startswith('feed:/') or
|
|
|
|
command_lowercase.startswith('itpc:/') or
|
|
|
|
command_lowercase.startswith('rss:/')):
|
|
|
|
url = command
|
2024-04-14 14:56:45 +02:00
|
|
|
status_type = 'dnd'
|
|
|
|
status_message = ('📫️ Processing request to fetch data from {}'
|
|
|
|
.format(url))
|
|
|
|
# pending_tasks_num = len(self.pending_tasks[jid_bare])
|
|
|
|
pending_tasks_num = randrange(10000, 99999)
|
|
|
|
self.pending_tasks[jid_bare][pending_tasks_num] = status_message
|
|
|
|
# self.pending_tasks_counter += 1
|
|
|
|
# self.pending_tasks[jid_bare][self.pending_tasks_counter] = status_message
|
|
|
|
XmppPresence.send(self, jid_bare, status_message,
|
|
|
|
status_type=status_type)
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.fetch_http(
|
|
|
|
self, command, db_file, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
del self.pending_tasks[jid_bare][pending_tasks_num]
|
|
|
|
# del self.pending_tasks[jid_bare][self.pending_tasks_counter]
|
2024-06-13 17:53:53 +02:00
|
|
|
XmppStatusTask.restart_task(self, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
# except:
|
|
|
|
# response = (
|
|
|
|
# '> {}\nNews source is in the process '
|
|
|
|
# 'of being added to the subscription '
|
|
|
|
# 'list.'.format(url)
|
|
|
|
# )
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('interval'):
|
|
|
|
val = command[9:]
|
2024-04-14 14:56:45 +02:00
|
|
|
if val:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.set_interval(
|
|
|
|
self, db_file, jid_bare, val)
|
2024-06-13 17:53:53 +02:00
|
|
|
XmppChatTask.restart_task(self, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = 'Current value for interval: '
|
|
|
|
response += XmppCommands.get_interval(self, jid_bare)
|
|
|
|
case _ if command_lowercase.startswith('join'):
|
|
|
|
muc_jid = command[5:]
|
|
|
|
response = await XmppCommands.muc_join(self, muc_jid)
|
|
|
|
case _ if command_lowercase.startswith('length'):
|
|
|
|
val = command[7:]
|
2024-04-14 14:56:45 +02:00
|
|
|
if val:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.set_length(
|
|
|
|
self, db_file, jid_bare, val)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = 'Current value for length: '
|
|
|
|
response += XmppCommands.get_length(self, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'media off':
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.set_media_off(
|
|
|
|
self, jid_bare, db_file)
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'media on':
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.set_media_on(
|
|
|
|
self, jid_bare, db_file)
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'new':
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.set_old_off(
|
|
|
|
self, jid_bare, db_file)
|
|
|
|
case _ if command_lowercase.startswith('next'):
|
2024-06-13 17:53:53 +02:00
|
|
|
num = command[5:]
|
2024-07-07 10:16:00 +02:00
|
|
|
if num:
|
|
|
|
try:
|
|
|
|
int(num)
|
|
|
|
except:
|
|
|
|
# NOTE Show this text as a status message
|
|
|
|
# response = 'Argument for command "next" must be an integer.'
|
|
|
|
num = None
|
2024-06-13 17:53:53 +02:00
|
|
|
await XmppChatAction.send_unread_items(self, jid_bare, num)
|
|
|
|
XmppStatusTask.restart_task(self, jid_bare)
|
|
|
|
case _ if command_lowercase.startswith('node delete'):
|
2024-06-16 10:55:22 +02:00
|
|
|
if XmppUtilities.is_operator(self, jid_bare):
|
2024-06-10 17:54:27 +02:00
|
|
|
info = command[12:]
|
2024-04-14 14:56:45 +02:00
|
|
|
info = info.split(' ')
|
2024-06-10 17:54:27 +02:00
|
|
|
response = XmppCommands.node_delete(self, info)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
|
|
|
response = ('This action is restricted. '
|
|
|
|
'Type: sending news to PubSub.')
|
2024-06-13 17:53:53 +02:00
|
|
|
case _ if command_lowercase.startswith('node purge'):
|
2024-06-16 10:55:22 +02:00
|
|
|
if XmppUtilities.is_operator(self, jid_bare):
|
2024-06-10 17:54:27 +02:00
|
|
|
info = command[11:]
|
2024-04-14 14:56:45 +02:00
|
|
|
info = info.split(' ')
|
2024-06-10 17:54:27 +02:00
|
|
|
response = XmppCommands.node_purge(self, info)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
|
|
|
response = ('This action is restricted. '
|
|
|
|
'Type: sending news to PubSub.')
|
|
|
|
case 'old':
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.set_old_on(
|
|
|
|
self, jid_bare, db_file)
|
2024-07-07 10:16:00 +02:00
|
|
|
case 'omemo off':
|
|
|
|
response = await XmppCommands.set_omemo_off(
|
|
|
|
self, jid_bare, db_file)
|
|
|
|
case 'omemo on':
|
|
|
|
response = await XmppCommands.set_omemo_on(
|
|
|
|
self, jid_bare, db_file)
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'options':
|
|
|
|
response = 'Options:\n```'
|
2024-06-10 17:54:27 +02:00
|
|
|
response += XmppCommands.print_options(self, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
response += '\n```'
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('quantum'):
|
|
|
|
val = command[8:]
|
2024-04-14 14:56:45 +02:00
|
|
|
if val:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.set_quantum(
|
|
|
|
self, db_file, jid_bare, val)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = 'Current value for quantum: '
|
|
|
|
response += XmppCommands.get_quantum(self, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'random':
|
2024-06-10 17:54:27 +02:00
|
|
|
response = XmppCommands.set_random(self, jid_bare, db_file)
|
2024-06-13 17:53:53 +02:00
|
|
|
case _ if command_lowercase.startswith('read'):
|
2024-06-10 17:54:27 +02:00
|
|
|
data = command[5:]
|
2024-04-14 14:56:45 +02:00
|
|
|
data = data.split()
|
|
|
|
url = data[0]
|
|
|
|
if url:
|
2024-06-13 17:53:53 +02:00
|
|
|
Task.stop(self, jid_bare, 'status')
|
2024-04-14 14:56:45 +02:00
|
|
|
status_type = 'dnd'
|
|
|
|
status_message = ('📫️ Processing request to fetch data '
|
|
|
|
'from {}'.format(url))
|
|
|
|
pending_tasks_num = randrange(10000, 99999)
|
|
|
|
self.pending_tasks[jid_bare][pending_tasks_num] = status_message
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.feed_read(
|
|
|
|
self, jid_bare, data, url)
|
|
|
|
del self.pending_tasks[jid_bare][pending_tasks_num]
|
2024-06-13 17:53:53 +02:00
|
|
|
XmppStatusTask.restart_task(self, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
|
|
|
response = ('No action has been taken.'
|
|
|
|
'\n'
|
|
|
|
'Missing URL.')
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('recent'):
|
|
|
|
num = command[7:]
|
|
|
|
if not num: num = 5
|
|
|
|
count, result = XmppCommands.print_recent(self, db_file, num)
|
|
|
|
if count:
|
|
|
|
response = 'Recent {} fetched titles:\n\n```'.format(num)
|
|
|
|
response += result + '```\n'
|
2024-04-14 14:56:45 +02:00
|
|
|
else:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = result
|
2024-06-13 17:53:53 +02:00
|
|
|
case _ if command_lowercase.startswith('remove'):
|
2024-06-10 17:54:27 +02:00
|
|
|
ix_url = command[7:]
|
|
|
|
ix_url = ix_url.split(' ')
|
|
|
|
response = await XmppCommands.feed_remove(
|
|
|
|
self, jid_bare, db_file, ix_url)
|
2024-06-13 17:53:53 +02:00
|
|
|
XmppStatusTask.restart_task(self, jid_bare)
|
|
|
|
case _ if command_lowercase.startswith('rename'):
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.feed_rename(
|
|
|
|
self, db_file, jid_bare, command)
|
|
|
|
case _ if command_lowercase.startswith('reset'):
|
|
|
|
ix_url = command[6:]
|
2024-06-13 17:53:53 +02:00
|
|
|
if ix_url: ix_url = ix_url.split(' ')
|
|
|
|
Task.stop(self, jid_bare, 'status')
|
2024-04-14 14:56:45 +02:00
|
|
|
status_type = 'dnd'
|
|
|
|
status_message = '📫️ Marking entries as read...'
|
|
|
|
# pending_tasks_num = len(self.pending_tasks[jid_bare])
|
|
|
|
pending_tasks_num = randrange(10000, 99999)
|
|
|
|
self.pending_tasks[jid_bare][pending_tasks_num] = status_message
|
|
|
|
# self.pending_tasks_counter += 1
|
|
|
|
# self.pending_tasks[jid_bare][self.pending_tasks_counter] = status_message
|
|
|
|
XmppPresence.send(self, jid_bare, status_message,
|
|
|
|
status_type=status_type)
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.mark_as_read(
|
2024-06-13 17:53:53 +02:00
|
|
|
jid_bare, db_file, ix_url)
|
2024-04-14 14:56:45 +02:00
|
|
|
del self.pending_tasks[jid_bare][pending_tasks_num]
|
|
|
|
# del self.pending_tasks[jid_bare][self.pending_tasks_counter]
|
2024-06-13 17:53:53 +02:00
|
|
|
XmppStatusTask.restart_task(self, jid_bare)
|
|
|
|
case _ if command_lowercase.startswith('search'):
|
2024-06-10 17:54:27 +02:00
|
|
|
query = command[7:]
|
2024-06-13 17:53:53 +02:00
|
|
|
response = XmppCommands.search_items(db_file, query)
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'start':
|
|
|
|
status_type = 'available'
|
2024-09-11 08:48:27 +02:00
|
|
|
status_message = '📫️ Welcome back.'
|
2024-04-14 14:56:45 +02:00
|
|
|
XmppPresence.send(self, jid_bare, status_message,
|
|
|
|
status_type=status_type)
|
|
|
|
await asyncio.sleep(5)
|
2024-07-07 10:16:00 +02:00
|
|
|
callbacks = (FeedTask, XmppChatTask, XmppStatusTask)
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.scheduler_start(
|
2024-07-07 10:16:00 +02:00
|
|
|
self, db_file, jid_bare, callbacks)
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'stats':
|
2024-06-10 17:54:27 +02:00
|
|
|
response = XmppCommands.print_statistics(db_file)
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'stop':
|
2024-06-10 17:54:27 +02:00
|
|
|
response = await XmppCommands.scheduler_stop(
|
|
|
|
self, db_file, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
status_type = 'xa'
|
|
|
|
status_message = '📪️ Send "Start" to receive Jabber updates'
|
|
|
|
XmppPresence.send(self, jid_bare, status_message,
|
|
|
|
status_type=status_type)
|
|
|
|
case 'support':
|
2024-06-10 17:54:27 +02:00
|
|
|
response = XmppCommands.print_support_jid()
|
|
|
|
await XmppCommands.invite_jid_to_muc(self, jid_bare)
|
2024-04-14 14:56:45 +02:00
|
|
|
case 'version':
|
2024-06-13 19:40:30 +02:00
|
|
|
response = XmppCommands.print_version()
|
2024-06-10 17:54:27 +02:00
|
|
|
case _ if command_lowercase.startswith('xmpp:'):
|
|
|
|
response = await XmppCommands.muc_join(self, command)
|
2024-04-14 14:56:45 +02:00
|
|
|
case _:
|
2024-06-10 17:54:27 +02:00
|
|
|
response = XmppCommands.print_unknown()
|
2024-04-14 14:56:45 +02:00
|
|
|
# TODO Use message correction here
|
|
|
|
# NOTE This might not be a good idea if
|
|
|
|
# commands are sent one close to the next
|
|
|
|
# if response: message.reply(response).send()
|
|
|
|
|
|
|
|
command_time_finish = time.time()
|
|
|
|
command_time_total = command_time_finish - command_time_start
|
|
|
|
command_time_total = round(command_time_total, 3)
|
2024-07-05 18:04:24 +02:00
|
|
|
if response:
|
2024-07-07 11:57:10 +02:00
|
|
|
encrypt_omemo = Config.get_setting_value(self, jid_bare, 'omemo')
|
|
|
|
encrypted = True if encrypt_omemo else False
|
2024-09-11 08:48:27 +02:00
|
|
|
if self.omemo_present and encrypted and self['xep_0384'].is_encrypted(message):
|
2024-07-07 11:57:10 +02:00
|
|
|
response_encrypted, omemo_encrypted = await XmppOmemo.encrypt(
|
2024-09-12 14:20:14 +02:00
|
|
|
self, message_from, 'chat', response)
|
2024-07-07 11:57:10 +02:00
|
|
|
if omemo_decrypted and omemo_encrypted:
|
|
|
|
# message_from = message['from']
|
|
|
|
# message_type = message['type']
|
|
|
|
XmppMessage.send_omemo(self, message_from, message_type, response_encrypted)
|
|
|
|
# XmppMessage.send_omemo_reply(self, message, response_encrypted)
|
2024-07-05 18:04:24 +02:00
|
|
|
else:
|
|
|
|
XmppMessage.send_reply(self, message, response)
|
2024-07-07 11:57:10 +02:00
|
|
|
if Config.get_setting_value(self, jid_bare, 'finished'):
|
2024-06-10 17:54:27 +02:00
|
|
|
response_finished = 'Finished. Total time: {}s'.format(command_time_total)
|
|
|
|
XmppMessage.send_reply(self, message, response_finished)
|
|
|
|
|
|
|
|
# if not response: response = 'EMPTY MESSAGE - ACTION ONLY'
|
|
|
|
# data_dir = config.get_default_data_directory()
|
|
|
|
# if not os.path.isdir(data_dir):
|
|
|
|
# os.mkdir(data_dir)
|
|
|
|
# if not os.path.isdir(data_dir + '/logs/'):
|
|
|
|
# os.mkdir(data_dir + '/logs/')
|
2024-06-13 17:53:53 +02:00
|
|
|
# MD.log_to_markdown(
|
2024-06-10 17:54:27 +02:00
|
|
|
# dt.current_time(), os.path.join(data_dir, 'logs', jid_bare),
|
|
|
|
# jid_bare, command)
|
2024-06-13 17:53:53 +02:00
|
|
|
# MD.log_to_markdown(
|
2024-06-10 17:54:27 +02:00
|
|
|
# dt.current_time(), os.path.join(data_dir, 'logs', jid_bare),
|
|
|
|
# jid_bare, response)
|
2024-04-14 14:56:45 +02:00
|
|
|
|
2024-06-10 17:54:27 +02:00
|
|
|
# print(
|
|
|
|
# 'Message : {}\n'
|
|
|
|
# 'JID : {}\n'
|
|
|
|
# '{}\n'
|
|
|
|
# .format(command, jid_bare, response)
|
|
|
|
# )
|
2024-06-13 17:53:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
class XmppChatAction:
|
|
|
|
|
|
|
|
|
2024-07-07 10:16:00 +02:00
|
|
|
async def send_unread_items(self, jid_bare, num: Optional[int] = None):
|
2024-06-13 17:53:53 +02:00
|
|
|
"""
|
|
|
|
Send news items as messages.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2024-07-05 18:04:24 +02:00
|
|
|
jid_bare : str
|
2024-06-13 17:53:53 +02:00
|
|
|
Jabber ID.
|
|
|
|
num : str, optional
|
|
|
|
Number. The default is None.
|
|
|
|
"""
|
|
|
|
function_name = sys._getframe().f_code.co_name
|
|
|
|
logger.debug('{}: jid: {} num: {}'.format(function_name, jid_bare, num))
|
|
|
|
db_file = config.get_pathname_to_database(jid_bare)
|
2024-07-07 11:57:10 +02:00
|
|
|
encrypt_omemo = Config.get_setting_value(self, jid_bare, 'omemo')
|
2024-07-05 18:04:24 +02:00
|
|
|
encrypted = True if encrypt_omemo else False
|
|
|
|
jid = JID(jid_bare)
|
2024-07-07 11:57:10 +02:00
|
|
|
show_media = Config.get_setting_value(self, jid_bare, 'media')
|
2024-06-13 17:53:53 +02:00
|
|
|
if not num:
|
2024-07-07 11:57:10 +02:00
|
|
|
num = Config.get_setting_value(self, jid_bare, 'quantum')
|
2024-06-13 17:53:53 +02:00
|
|
|
else:
|
|
|
|
num = int(num)
|
|
|
|
results = sqlite.get_unread_entries(db_file, num)
|
|
|
|
news_digest = ''
|
2024-07-05 18:04:24 +02:00
|
|
|
media_url = None
|
2024-06-13 17:53:53 +02:00
|
|
|
chat_type = await XmppUtilities.get_chat_type(self, jid_bare)
|
|
|
|
for result in results:
|
|
|
|
ix = result[0]
|
|
|
|
title_e = result[1]
|
|
|
|
url = result[2]
|
|
|
|
summary = result[3]
|
|
|
|
feed_id = result[4]
|
|
|
|
date = result[5]
|
|
|
|
enclosure = sqlite.get_enclosure_by_entry_id(db_file, ix)
|
|
|
|
if enclosure: enclosure = enclosure[0]
|
|
|
|
title_f = sqlite.get_feed_title(db_file, feed_id)
|
|
|
|
title_f = title_f[0]
|
|
|
|
news_digest += await XmppChatAction.list_unread_entries(self, result, title_f, jid_bare)
|
|
|
|
# print(db_file)
|
|
|
|
# print(result[0])
|
|
|
|
# breakpoint()
|
|
|
|
await sqlite.mark_as_read(db_file, ix)
|
|
|
|
|
|
|
|
# Find media
|
|
|
|
# if url.startswith("magnet:"):
|
|
|
|
# media = action.get_magnet(url)
|
|
|
|
# elif enclosure.startswith("magnet:"):
|
|
|
|
# media = action.get_magnet(enclosure)
|
|
|
|
# elif enclosure:
|
|
|
|
if show_media:
|
|
|
|
if enclosure:
|
2024-07-05 18:04:24 +02:00
|
|
|
media_url = enclosure
|
2024-06-13 17:53:53 +02:00
|
|
|
else:
|
2024-07-05 18:04:24 +02:00
|
|
|
media_url = await Html.extract_image_from_html(url)
|
2024-07-07 10:16:00 +02:00
|
|
|
try:
|
|
|
|
http_headers = await Http.fetch_headers(media_url)
|
|
|
|
if ('Content-Length' in http_headers):
|
|
|
|
if int(http_headers['Content-Length']) < 100000:
|
|
|
|
media_url = None
|
|
|
|
else:
|
|
|
|
media_url = None
|
|
|
|
except Exception as e:
|
|
|
|
print(media_url)
|
|
|
|
logger.error(e)
|
|
|
|
media_url = None
|
2024-07-05 18:04:24 +02:00
|
|
|
|
|
|
|
if media_url and news_digest:
|
2024-09-11 08:48:27 +02:00
|
|
|
if self.omemo_present and encrypt_omemo:
|
2024-07-05 18:04:24 +02:00
|
|
|
news_digest_encrypted, omemo_encrypted = await XmppOmemo.encrypt(
|
2024-09-12 14:20:14 +02:00
|
|
|
self, jid, 'chat', news_digest)
|
2024-09-11 08:48:27 +02:00
|
|
|
if self.omemo_present and encrypt_omemo and omemo_encrypted:
|
2024-07-05 18:04:24 +02:00
|
|
|
XmppMessage.send_omemo(self, jid, chat_type, news_digest_encrypted)
|
|
|
|
else:
|
|
|
|
# Send textual message
|
|
|
|
XmppMessage.send(self, jid_bare, news_digest, chat_type)
|
2024-06-13 17:53:53 +02:00
|
|
|
news_digest = ''
|
|
|
|
# Send media
|
2024-09-11 08:48:27 +02:00
|
|
|
if self.omemo_present and encrypt_omemo:
|
2024-07-05 18:04:24 +02:00
|
|
|
cache_dir = config.get_default_cache_directory()
|
2024-07-07 10:16:00 +02:00
|
|
|
# if not media_url.startswith('data:'):
|
2024-07-05 18:04:24 +02:00
|
|
|
filename = media_url.split('/').pop().split('?')[0]
|
2024-07-07 10:16:00 +02:00
|
|
|
if not filename: breakpoint()
|
2024-07-05 18:04:24 +02:00
|
|
|
pathname = os.path.join(cache_dir, filename)
|
|
|
|
# http_response = await Http.response(media_url)
|
2024-07-07 10:16:00 +02:00
|
|
|
http_headers = await Http.fetch_headers(media_url)
|
|
|
|
if ('Content-Length' in http_headers and
|
|
|
|
int(http_headers['Content-Length']) < 3000000):
|
|
|
|
status = await Http.fetch_media(media_url, pathname)
|
|
|
|
if status:
|
|
|
|
filesize = os.path.getsize(pathname)
|
|
|
|
media_url_new = await XmppUpload.start(
|
|
|
|
self, jid_bare, Path(pathname), filesize, encrypted=encrypted)
|
|
|
|
else:
|
|
|
|
media_url_new = media_url
|
|
|
|
else:
|
|
|
|
media_url_new = media_url
|
|
|
|
# else:
|
|
|
|
# import io, base64
|
|
|
|
# from PIL import Image
|
|
|
|
# file_content = media_url.split(',').pop()
|
|
|
|
# file_extension = media_url.split(';')[0].split(':').pop().split('/').pop()
|
|
|
|
# img = Image.open(io.BytesIO(base64.decodebytes(bytes(file_content, "utf-8"))))
|
|
|
|
# filename = 'image.' + file_extension
|
|
|
|
# pathname = os.path.join(cache_dir, filename)
|
|
|
|
# img.save(pathname)
|
2024-07-05 18:04:24 +02:00
|
|
|
# filesize = os.path.getsize(pathname)
|
|
|
|
# media_url_new = await XmppUpload.start(
|
|
|
|
# self, jid_bare, Path(pathname), filesize, encrypted=encrypted)
|
|
|
|
media_url_new_encrypted, omemo_encrypted = await XmppOmemo.encrypt(
|
2024-09-12 14:20:14 +02:00
|
|
|
self, jid, 'chat', media_url_new)
|
2024-07-07 10:16:00 +02:00
|
|
|
if media_url_new_encrypted and omemo_encrypted:
|
|
|
|
# NOTE Tested against Gajim.
|
|
|
|
# FIXME This only works with aesgcm URLs, and it does
|
|
|
|
# not work with http URLs.
|
|
|
|
# url = saxutils.escape(url)
|
|
|
|
# AttributeError: 'Encrypted' object has no attribute 'replace'
|
|
|
|
XmppMessage.send_omemo_oob(self, jid, media_url_new_encrypted, chat_type)
|
2024-07-05 18:04:24 +02:00
|
|
|
else:
|
2024-07-07 10:16:00 +02:00
|
|
|
# NOTE Tested against Gajim.
|
|
|
|
# FIXME Jandle data: URIs.
|
|
|
|
if not media_url.startswith('data:'):
|
|
|
|
http_headers = await Http.fetch_headers(media_url)
|
|
|
|
if ('Content-Length' in http_headers and
|
|
|
|
int(http_headers['Content-Length']) > 100000):
|
|
|
|
print(http_headers['Content-Length'])
|
|
|
|
XmppMessage.send_oob(self, jid_bare, media_url, chat_type)
|
|
|
|
else:
|
|
|
|
XmppMessage.send_oob(self, jid_bare, media_url, chat_type)
|
2024-07-05 18:04:24 +02:00
|
|
|
media_url = None
|
|
|
|
|
2024-06-13 17:53:53 +02:00
|
|
|
if news_digest:
|
2024-09-11 08:48:27 +02:00
|
|
|
if self.omemo_present and encrypt_omemo:
|
|
|
|
news_digest_encrypted, omemo_encrypted = await XmppOmemo.encrypt(
|
2024-09-12 14:20:14 +02:00
|
|
|
self, jid, 'chat', news_digest)
|
2024-09-11 08:48:27 +02:00
|
|
|
if self.omemo_present and encrypt_omemo and omemo_encrypted:
|
2024-07-05 18:04:24 +02:00
|
|
|
XmppMessage.send_omemo(self, jid, chat_type, news_digest_encrypted)
|
|
|
|
else:
|
|
|
|
XmppMessage.send(self, jid_bare, news_digest, chat_type)
|
2024-06-13 17:53:53 +02:00
|
|
|
# TODO Add while loop to assure delivery.
|
|
|
|
# print(await current_time(), ">>> ACT send_message",jid)
|
|
|
|
# NOTE Do we need "if statement"? See NOTE at is_muc.
|
|
|
|
# if chat_type in ('chat', 'groupchat'):
|
|
|
|
# # TODO Provide a choice (with or without images)
|
|
|
|
# XmppMessage.send(self, jid, news_digest, chat_type)
|
|
|
|
# See XEP-0367
|
|
|
|
# if media:
|
|
|
|
# # message = xmpp.Slixfeed.make_message(
|
|
|
|
# # self, mto=jid, mbody=new, mtype=chat_type)
|
|
|
|
# message = xmpp.Slixfeed.make_message(
|
|
|
|
# self, mto=jid, mbody=media, mtype=chat_type)
|
|
|
|
# message['oob']['url'] = media
|
|
|
|
# message.send()
|
|
|
|
|
|
|
|
# TODO Do not refresh task before
|
|
|
|
# verifying that it was completed.
|
|
|
|
|
|
|
|
# XmppStatusTask.restart_task(self, jid_bare)
|
|
|
|
# XmppCommands.task_start(self, jid_bare, 'interval')
|
|
|
|
|
|
|
|
# interval = await initdb(
|
|
|
|
# jid,
|
|
|
|
# sqlite.is_setting_key,
|
|
|
|
# "interval"
|
|
|
|
# )
|
|
|
|
# self.task_manager[jid]["interval"] = loop.call_at(
|
|
|
|
# loop.time() + 60 * interval,
|
|
|
|
# loop.create_task,
|
|
|
|
# send_update(jid)
|
|
|
|
# )
|
|
|
|
|
|
|
|
# print(await current_time(), "asyncio.get_event_loop().time()")
|
|
|
|
# print(await current_time(), asyncio.get_event_loop().time())
|
|
|
|
# await asyncio.sleep(60 * interval)
|
|
|
|
|
|
|
|
# loop.call_later(
|
|
|
|
# 60 * interval,
|
|
|
|
# loop.create_task,
|
|
|
|
# send_update(jid)
|
|
|
|
# )
|
|
|
|
|
|
|
|
# print
|
|
|
|
# await handle_event()
|
|
|
|
|
|
|
|
|
|
|
|
async def list_unread_entries(self, result, feed_title, jid):
|
|
|
|
function_name = sys._getframe().f_code.co_name
|
|
|
|
logger.debug('{}: feed_title: {} jid: {}'
|
|
|
|
.format(function_name, feed_title, jid))
|
|
|
|
# TODO Add filtering
|
|
|
|
# TODO Do this when entry is added to list and mark it as read
|
|
|
|
# DONE!
|
|
|
|
# results = []
|
|
|
|
# if sqlite.is_setting_key(db_file, "deny"):
|
|
|
|
# while len(results) < num:
|
|
|
|
# result = cur.execute(sql).fetchone()
|
|
|
|
# blacklist = sqlite.get_setting_value(db_file, "deny").split(",")
|
|
|
|
# for i in blacklist:
|
|
|
|
# if i in result[1]:
|
|
|
|
# continue
|
|
|
|
# print("rejected:", result[1])
|
|
|
|
# print("accepted:", result[1])
|
|
|
|
# results.extend([result])
|
|
|
|
|
|
|
|
# news_list = "You've got {} news items:\n".format(num)
|
|
|
|
# NOTE Why doesn't this work without list?
|
|
|
|
# i.e. for result in results
|
|
|
|
# for result in results.fetchall():
|
|
|
|
ix = str(result[0])
|
|
|
|
title = str(result[1]) or '*** No title ***' # [No Title]
|
|
|
|
# Remove HTML tags
|
|
|
|
title = Html.remove_html_tags(title) if title else '*** No title ***'
|
|
|
|
# # TODO Retrieve summary from feed
|
|
|
|
# # See fetch.view_entry
|
|
|
|
summary = result[3]
|
|
|
|
if summary:
|
|
|
|
summary = Html.remove_html_tags(summary)
|
|
|
|
# TODO Limit text length
|
|
|
|
# summary = summary.replace("\n\n\n", "\n\n")
|
|
|
|
summary = summary.replace('\n', ' ')
|
|
|
|
summary = summary.replace(' ', ' ')
|
|
|
|
# summary = summary.replace(' ', ' ')
|
|
|
|
summary = ' '.join(summary.split())
|
2024-07-07 11:57:10 +02:00
|
|
|
length = Config.get_setting_value(self, jid, 'length')
|
2024-06-13 17:53:53 +02:00
|
|
|
length = int(length)
|
|
|
|
summary = summary[:length] + " […]"
|
|
|
|
# summary = summary.strip().split('\n')
|
|
|
|
# summary = ["> " + line for line in summary]
|
|
|
|
# summary = "\n".join(summary)
|
|
|
|
else:
|
|
|
|
summary = '*** No summary ***'
|
|
|
|
link = result[2]
|
2024-06-16 10:55:22 +02:00
|
|
|
link = Url.remove_tracking_parameters(link)
|
|
|
|
link = await Url.replace_hostname(link, "link") or link
|
2024-06-13 17:53:53 +02:00
|
|
|
feed_id = result[4]
|
|
|
|
# news_item = ("\n{}\n{}\n{} [{}]\n").format(str(title), str(link),
|
|
|
|
# str(feed_title), str(ix))
|
2024-07-07 11:57:10 +02:00
|
|
|
formatting = Config.get_setting_value(self, jid, 'formatting')
|
2024-06-13 17:53:53 +02:00
|
|
|
news_item = formatting.format(feed_title=feed_title,
|
|
|
|
title=title,
|
|
|
|
summary=summary,
|
|
|
|
link=link,
|
|
|
|
ix=ix,
|
|
|
|
feed_id=feed_id)
|
|
|
|
# news_item = news_item.replace('\\n', '\n')
|
|
|
|
return news_item
|
|
|
|
|
|
|
|
|
|
|
|
class XmppChatTask:
|
|
|
|
|
|
|
|
|
|
|
|
async def task_message(self, jid_bare):
|
|
|
|
db_file = config.get_pathname_to_database(jid_bare)
|
|
|
|
if jid_bare not in self.settings:
|
2024-07-07 11:57:10 +02:00
|
|
|
Config.add_settings_jid(self, jid_bare, db_file)
|
2024-06-13 17:53:53 +02:00
|
|
|
while True:
|
2024-07-07 11:57:10 +02:00
|
|
|
update_interval = Config.get_setting_value(self, jid_bare, 'interval')
|
2024-06-13 17:53:53 +02:00
|
|
|
update_interval = 60 * int(update_interval)
|
|
|
|
last_update_time = sqlite.get_last_update_time(db_file)
|
|
|
|
if last_update_time:
|
|
|
|
last_update_time = float(last_update_time)
|
|
|
|
diff = time.time() - last_update_time
|
|
|
|
if diff < update_interval:
|
|
|
|
next_update_time = update_interval - diff
|
|
|
|
await asyncio.sleep(next_update_time) # FIXME!
|
|
|
|
|
|
|
|
# print("jid :", jid, "\n"
|
|
|
|
# "time :", time.time(), "\n"
|
|
|
|
# "last_update_time :", last_update_time, "\n"
|
|
|
|
# "difference :", diff, "\n"
|
|
|
|
# "update interval :", update_interval, "\n"
|
|
|
|
# "next_update_time :", next_update_time, "\n"
|
|
|
|
# )
|
|
|
|
|
|
|
|
# elif diff > val:
|
|
|
|
# next_update_time = val
|
|
|
|
await sqlite.update_last_update_time(db_file)
|
|
|
|
else:
|
|
|
|
await sqlite.set_last_update_time(db_file)
|
|
|
|
await XmppChatAction.send_unread_items(self, jid_bare)
|
|
|
|
|
|
|
|
|
|
|
|
def restart_task(self, jid_bare):
|
|
|
|
if jid_bare == self.boundjid.bare:
|
|
|
|
return
|
|
|
|
if jid_bare not in self.task_manager:
|
|
|
|
self.task_manager[jid_bare] = {}
|
|
|
|
logger.info('Creating new task manager for JID {}'.format(jid_bare))
|
|
|
|
logger.info('Stopping task "interval" for JID {}'.format(jid_bare))
|
|
|
|
try:
|
|
|
|
self.task_manager[jid_bare]['interval'].cancel()
|
|
|
|
except:
|
|
|
|
logger.info('No task "interval" for JID {} (XmppChatTask.task_message)'
|
|
|
|
.format(jid_bare))
|
|
|
|
logger.info('Starting tasks "interval" for JID {}'.format(jid_bare))
|
|
|
|
self.task_manager[jid_bare]['interval'] = asyncio.create_task(
|
2024-09-11 08:48:27 +02:00
|
|
|
XmppChatTask.task_message(self, jid_bare))
|