2024-01-02 12:42:41 +01: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?
|
|
|
|
See: case _ if message_lowercase.startswith("add"):
|
|
|
|
|
|
|
|
2) If subscription is inadequate (see state.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
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2024-01-13 18:17:43 +01:00
|
|
|
import slixfeed.fetch as fetch
|
2024-01-09 13:34:10 +01:00
|
|
|
import logging
|
2024-01-02 12:42:41 +01:00
|
|
|
import os
|
2024-01-04 02:16:24 +01:00
|
|
|
import slixfeed.action as action
|
2024-01-02 12:42:41 +01:00
|
|
|
from slixfeed.config import (
|
|
|
|
add_to_list,
|
2024-01-06 23:03:08 +01:00
|
|
|
get_default_data_directory,
|
2024-01-04 13:38:22 +01:00
|
|
|
get_value_default,
|
2024-01-02 12:42:41 +01:00
|
|
|
get_value,
|
|
|
|
get_pathname_to_database,
|
|
|
|
remove_from_list)
|
2024-01-03 11:37:33 +01:00
|
|
|
from slixfeed.datetime import current_time, timestamp
|
2024-01-02 12:42:41 +01:00
|
|
|
import slixfeed.sqlite as sqlite
|
|
|
|
import slixfeed.task as task
|
|
|
|
import slixfeed.url as uri
|
|
|
|
import slixfeed.xmpp.bookmark as bookmark
|
|
|
|
import slixfeed.xmpp.muc as groupchat
|
2024-01-09 16:53:19 +01:00
|
|
|
import slixfeed.xmpp.manual as manual
|
2024-01-03 11:37:33 +01:00
|
|
|
import slixfeed.xmpp.upload as upload
|
2024-01-03 13:17:39 +01:00
|
|
|
from slixfeed.xmpp.utility import jid_type
|
2024-01-02 12:42:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def event(self, event):
|
|
|
|
"""
|
|
|
|
Process the session_start event.
|
|
|
|
|
|
|
|
Typical actions for the session_start event are
|
|
|
|
requesting the roster and broadcasting an initial
|
|
|
|
presence stanza.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
event -- An empty dictionary. The session_start
|
|
|
|
event does not provide any additional
|
|
|
|
data.
|
|
|
|
"""
|
|
|
|
self.send_presence()
|
|
|
|
await self.get_roster()
|
|
|
|
|
|
|
|
# for task in main_task:
|
|
|
|
# task.cancel()
|
|
|
|
|
|
|
|
# Deprecated in favour of event "presence_available"
|
|
|
|
# if not main_task:
|
|
|
|
# await select_file()
|
|
|
|
|
|
|
|
|
|
|
|
async def message(self, message):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
message : str
|
|
|
|
The received message stanza. See the documentation
|
|
|
|
for stanza objects and the Message stanza to see
|
|
|
|
how it may be used.
|
|
|
|
"""
|
|
|
|
if message["type"] in ("chat", "groupchat", "normal"):
|
|
|
|
jid = message["from"].bare
|
2024-01-09 13:34:10 +01:00
|
|
|
message_text = " ".join(message["body"].split())
|
|
|
|
|
2024-01-14 13:46:38 +01:00
|
|
|
if (message["type"] == "groupchat" and
|
|
|
|
message['muc']['nick'] == self.nick):
|
|
|
|
return
|
|
|
|
|
|
|
|
# NOTE This is an exceptional case in which we treat
|
2024-01-09 13:34:10 +01:00
|
|
|
# type groupchat the same as type chat.
|
2024-01-10 22:40:12 +01:00
|
|
|
if (message_text.lower().startswith("http") and
|
2024-01-09 13:34:10 +01:00
|
|
|
message_text.lower().endswith(".opml")):
|
|
|
|
url = message_text
|
|
|
|
await task.clean_tasks_xmpp(
|
|
|
|
jid, ["status"])
|
|
|
|
status_type = "dnd"
|
|
|
|
status_message = (
|
|
|
|
"📥️ Procesing request to import feeds ..."
|
|
|
|
)
|
|
|
|
send_status_message(
|
|
|
|
self, jid, status_type, status_message)
|
|
|
|
db_file = get_pathname_to_database(jid)
|
|
|
|
count = await action.import_opml(db_file, url)
|
|
|
|
if count:
|
|
|
|
response = (
|
2024-01-10 21:06:56 +01:00
|
|
|
"Successfully imported {} feeds."
|
2024-01-09 13:34:10 +01:00
|
|
|
).format(count)
|
|
|
|
else:
|
|
|
|
response = (
|
|
|
|
"OPML file was not imported."
|
|
|
|
)
|
|
|
|
await task.clean_tasks_xmpp(
|
|
|
|
jid, ["status"])
|
|
|
|
await task.start_tasks_xmpp(
|
|
|
|
self, jid, ["status"])
|
|
|
|
send_reply_message(self, message, response)
|
2024-01-10 21:06:56 +01:00
|
|
|
return
|
2024-01-09 13:34:10 +01:00
|
|
|
|
|
|
|
|
2024-01-02 12:42:41 +01:00
|
|
|
if message["type"] == "groupchat":
|
|
|
|
# nick = message["from"][message["from"].index("/")+1:]
|
2024-01-14 13:46:38 +01:00
|
|
|
# nick = str(message["from"])
|
|
|
|
# nick = nick[nick.index("/")+1:]
|
2024-01-02 12:42:41 +01:00
|
|
|
if (message['muc']['nick'] == self.nick or
|
|
|
|
not message["body"].startswith("!")):
|
|
|
|
return
|
|
|
|
# token = await initdb(
|
|
|
|
# jid,
|
|
|
|
# get_settings_value,
|
|
|
|
# "token"
|
|
|
|
# )
|
|
|
|
# if token == "accepted":
|
|
|
|
# operator = await initdb(
|
|
|
|
# jid,
|
|
|
|
# get_settings_value,
|
|
|
|
# "masters"
|
|
|
|
# )
|
|
|
|
# if operator:
|
|
|
|
# if nick not in operator:
|
|
|
|
# return
|
|
|
|
# approved = False
|
|
|
|
jid_full = str(message["from"])
|
|
|
|
role = self.plugin['xep_0045'].get_jid_property(
|
|
|
|
jid,
|
|
|
|
jid_full[jid_full.index("/")+1:],
|
|
|
|
"role")
|
|
|
|
if role != "moderator":
|
|
|
|
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,
|
|
|
|
# get_settings_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"
|
2024-01-06 23:03:08 +01:00
|
|
|
# db_dir = get_default_data_directory()
|
2024-01-02 12:42:41 +01:00
|
|
|
# os.chdir(db_dir)
|
|
|
|
# if jid + ".db" not in os.listdir():
|
|
|
|
# await task_jid(jid)
|
|
|
|
|
|
|
|
# await compose.message(self, jid, message)
|
|
|
|
|
|
|
|
if message["type"] == "groupchat":
|
|
|
|
message_text = message_text[1:]
|
|
|
|
message_lowercase = message_text.lower()
|
|
|
|
|
2024-01-09 13:34:10 +01:00
|
|
|
logging.debug(
|
|
|
|
[str(message["from"]), ":", message_text])
|
|
|
|
response = None
|
2024-01-02 12:42:41 +01:00
|
|
|
match message_lowercase:
|
2024-01-02 13:52:37 +01:00
|
|
|
# case "breakpoint":
|
|
|
|
# if jid == get_value("accounts", "XMPP", "operator"):
|
|
|
|
# breakpoint()
|
2024-01-09 13:34:10 +01:00
|
|
|
# print("task_manager[jid]")
|
|
|
|
# print(task_manager[jid])
|
|
|
|
# await self.get_roster()
|
|
|
|
# print("roster 1")
|
|
|
|
# print(self.client_roster)
|
|
|
|
# print("roster 2")
|
|
|
|
# print(self.client_roster.keys())
|
|
|
|
# print("jid")
|
|
|
|
# print(jid)
|
2024-01-02 13:52:37 +01:00
|
|
|
# else:
|
|
|
|
# response = (
|
|
|
|
# "This action is restricted. "
|
|
|
|
# "Type: breakpoint."
|
|
|
|
# )
|
|
|
|
# send_reply_message(self, message, response)
|
2024-01-02 12:42:41 +01:00
|
|
|
case "commands":
|
2024-01-09 16:53:19 +01:00
|
|
|
response = manual.print_cmd()
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case "help":
|
2024-01-09 16:53:19 +01:00
|
|
|
response = manual.print_help()
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case "info":
|
2024-01-09 16:53:19 +01:00
|
|
|
response = manual.print_info()
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase in [
|
|
|
|
"greetings", "hallo", "hello", "hey",
|
|
|
|
"hi", "hola", "holla", "hollo"]:
|
|
|
|
response = (
|
|
|
|
"Greeting!\n"
|
|
|
|
"I'm Slixfeed, an RSS News Bot!\n"
|
|
|
|
"Send \"help\" for instructions.\n"
|
|
|
|
)
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
|
|
|
|
# case _ if message_lowercase.startswith("activate"):
|
|
|
|
# if message["type"] == "groupchat":
|
|
|
|
# acode = message[9:]
|
|
|
|
# token = await initdb(
|
|
|
|
# jid,
|
|
|
|
# get_settings_value,
|
|
|
|
# "token"
|
|
|
|
# )
|
|
|
|
# if int(acode) == token:
|
|
|
|
# await initdb(
|
|
|
|
# jid,
|
2024-01-14 13:46:38 +01:00
|
|
|
# update_settings_value,
|
2024-01-02 12:42:41 +01:00
|
|
|
# ["masters", nick]
|
|
|
|
# )
|
|
|
|
# await initdb(
|
|
|
|
# jid,
|
2024-01-14 13:46:38 +01:00
|
|
|
# update_settings_value,
|
2024-01-02 12:42:41 +01:00
|
|
|
# ["token", "accepted"]
|
|
|
|
# )
|
|
|
|
# response = "{}, your are in command.".format(nick)
|
|
|
|
# else:
|
|
|
|
# response = "Activation code is not valid."
|
|
|
|
# else:
|
|
|
|
# response = "This command is valid for groupchat only."
|
|
|
|
case _ if message_lowercase.startswith("add"):
|
2024-01-04 02:16:24 +01:00
|
|
|
# Add given feed without validity check.
|
2024-01-02 12:42:41 +01:00
|
|
|
message_text = message_text[4:]
|
|
|
|
url = message_text.split(" ")[0]
|
|
|
|
title = " ".join(message_text.split(" ")[1:])
|
|
|
|
if url.startswith("http"):
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-06 23:03:08 +01:00
|
|
|
exist = await sqlite.get_feed_id_and_name(
|
|
|
|
db_file, url)
|
2024-01-04 02:16:24 +01:00
|
|
|
if not exist:
|
2024-01-06 23:03:08 +01:00
|
|
|
await sqlite.insert_feed(
|
|
|
|
db_file, url, title)
|
|
|
|
await action.scan(db_file, url)
|
2024-01-04 13:38:22 +01:00
|
|
|
old = (
|
|
|
|
await sqlite.get_settings_value(db_file, "old")
|
|
|
|
) or (
|
|
|
|
get_value_default("settings", "Settings", "old")
|
|
|
|
)
|
2024-01-04 02:16:24 +01:00
|
|
|
if old:
|
2024-01-04 13:38:22 +01:00
|
|
|
await task.clean_tasks_xmpp(
|
|
|
|
jid, ["status"])
|
2024-01-04 02:16:24 +01:00
|
|
|
# await send_status(jid)
|
2024-01-04 13:38:22 +01:00
|
|
|
await task.start_tasks_xmpp(
|
|
|
|
self, jid, ["status"])
|
2024-01-04 02:16:24 +01:00
|
|
|
else:
|
2024-01-06 23:03:08 +01:00
|
|
|
await sqlite.mark_feed_as_read(
|
2024-01-04 13:38:22 +01:00
|
|
|
db_file, url)
|
2024-01-04 02:16:24 +01:00
|
|
|
response = (
|
|
|
|
"> {}\nNews source has been "
|
|
|
|
"added to subscription list."
|
|
|
|
).format(url)
|
2024-01-02 12:42:41 +01:00
|
|
|
else:
|
2024-01-04 02:16:24 +01:00
|
|
|
ix = exist[0]
|
|
|
|
name = exist[1]
|
|
|
|
response = (
|
|
|
|
"> {}\nNews source \"{}\" is already "
|
|
|
|
"listed in the subscription list at "
|
2024-01-09 13:34:10 +01:00
|
|
|
"index {}"
|
|
|
|
).format(url, name, ix)
|
2024-01-02 12:42:41 +01:00
|
|
|
else:
|
|
|
|
response = "Missing URL."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("allow +"):
|
|
|
|
key = "filter-" + message_text[:5]
|
|
|
|
val = message_text[7:]
|
|
|
|
if val:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-04 14:58:06 +01:00
|
|
|
keywords = await sqlite.get_filters_value(
|
|
|
|
db_file, key)
|
|
|
|
val = await add_to_list(
|
|
|
|
val, keywords)
|
2024-01-14 13:46:38 +01:00
|
|
|
if await sqlite.get_filters_value(db_file, key):
|
|
|
|
await sqlite.update_filters_value(
|
|
|
|
db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_filters_value(db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"Approved keywords\n"
|
|
|
|
"```\n{}\n```"
|
|
|
|
).format(val)
|
|
|
|
else:
|
|
|
|
response = "Missing keywords."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("allow -"):
|
|
|
|
key = "filter-" + message_text[:5]
|
|
|
|
val = message_text[7:]
|
|
|
|
if val:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-04 14:58:06 +01:00
|
|
|
keywords = await sqlite.get_filters_value(
|
|
|
|
db_file, key)
|
|
|
|
val = await remove_from_list(
|
|
|
|
val, keywords)
|
2024-01-14 13:46:38 +01:00
|
|
|
if await sqlite.get_filters_value(db_file, key):
|
|
|
|
await sqlite.update_filters_value(
|
|
|
|
db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_filters_value(db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"Approved keywords\n"
|
|
|
|
"```\n{}\n```"
|
|
|
|
).format(val)
|
|
|
|
else:
|
|
|
|
response = "Missing keywords."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("archive"):
|
|
|
|
key = message_text[:7]
|
|
|
|
val = message_text[8:]
|
|
|
|
if val:
|
|
|
|
try:
|
|
|
|
if int(val) > 500:
|
|
|
|
response = "Value may not be greater than 500."
|
|
|
|
else:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-14 13:46:38 +01:00
|
|
|
if await sqlite.get_settings_value(
|
|
|
|
db_file, [key, val]):
|
|
|
|
await sqlite.update_settings_value(
|
|
|
|
db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_settings_value(
|
|
|
|
db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"Maximum archived items has been set to {}."
|
|
|
|
).format(val)
|
|
|
|
except:
|
|
|
|
response = "Enter a numeric value only."
|
|
|
|
else:
|
|
|
|
response = "Missing value."
|
|
|
|
send_reply_message(self, message, response)
|
2024-01-13 18:17:43 +01:00
|
|
|
case _ if message_lowercase.startswith("bookmark -"):
|
2024-01-02 12:42:41 +01:00
|
|
|
if jid == get_value("accounts", "XMPP", "operator"):
|
|
|
|
muc_jid = message_text[11:]
|
|
|
|
await bookmark.remove(self, muc_jid)
|
|
|
|
response = (
|
|
|
|
"Groupchat {} has been removed from bookmarks."
|
|
|
|
).format(muc_jid)
|
|
|
|
else:
|
|
|
|
response = (
|
2024-01-02 13:12:29 +01:00
|
|
|
"This action is restricted. "
|
2024-01-02 12:42:41 +01:00
|
|
|
"Type: removing bookmarks."
|
|
|
|
)
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case "bookmarks":
|
2024-01-04 14:58:06 +01:00
|
|
|
if jid == get_value(
|
|
|
|
"accounts", "XMPP", "operator"):
|
2024-01-06 23:03:08 +01:00
|
|
|
response = await action.list_bookmarks(self)
|
2024-01-02 13:12:29 +01:00
|
|
|
else:
|
|
|
|
response = (
|
|
|
|
"This action is restricted. "
|
|
|
|
"Type: viewing bookmarks."
|
|
|
|
)
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("deny +"):
|
|
|
|
key = "filter-" + message_text[:4]
|
|
|
|
val = message_text[6:]
|
|
|
|
if val:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-04 14:58:06 +01:00
|
|
|
keywords = await sqlite.get_filters_value(
|
|
|
|
db_file, key)
|
|
|
|
val = await add_to_list(
|
|
|
|
val, keywords)
|
2024-01-14 13:46:38 +01:00
|
|
|
if await sqlite.get_filters_value(db_file, key):
|
|
|
|
await sqlite.update_filters_value(
|
|
|
|
db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_filters_value(db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"Rejected keywords\n"
|
|
|
|
"```\n{}\n```"
|
|
|
|
).format(val)
|
|
|
|
else:
|
|
|
|
response = "Missing keywords."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("deny -"):
|
|
|
|
key = "filter-" + message_text[:4]
|
|
|
|
val = message_text[6:]
|
|
|
|
if val:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-04 14:58:06 +01:00
|
|
|
keywords = await sqlite.get_filters_value(
|
|
|
|
db_file, key)
|
|
|
|
val = await remove_from_list(
|
|
|
|
val, keywords)
|
2024-01-14 13:46:38 +01:00
|
|
|
if await sqlite.get_filters_value(db_file, key):
|
|
|
|
await sqlite.update_filters_value(
|
|
|
|
db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_filters_value(db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"Rejected keywords\n"
|
|
|
|
"```\n{}\n```"
|
|
|
|
).format(val)
|
|
|
|
else:
|
|
|
|
response = "Missing keywords."
|
|
|
|
send_reply_message(self, message, response)
|
2024-01-13 18:17:43 +01:00
|
|
|
case _ if message_lowercase.startswith("export"):
|
2024-01-09 23:36:16 +01:00
|
|
|
ex = message_text[7:]
|
|
|
|
if ex in ("opml", "html", "md", "xbel"):
|
2024-01-03 13:17:39 +01:00
|
|
|
status_type = "dnd"
|
|
|
|
status_message = (
|
2024-01-03 16:04:01 +01:00
|
|
|
"📤️ Procesing request to export feeds into {} ..."
|
2024-01-10 21:06:56 +01:00
|
|
|
).format(ex)
|
2024-01-03 13:17:39 +01:00
|
|
|
send_status_message(
|
|
|
|
self, jid, status_type, status_message)
|
2024-01-06 23:03:08 +01:00
|
|
|
data_dir = get_default_data_directory()
|
2024-01-03 13:17:39 +01:00
|
|
|
if not os.path.isdir(data_dir):
|
|
|
|
os.mkdir(data_dir)
|
2024-01-10 21:06:56 +01:00
|
|
|
if not os.path.isdir(data_dir + '/' + ex):
|
|
|
|
os.mkdir(data_dir + '/' + ex)
|
2024-01-03 13:17:39 +01:00
|
|
|
filename = os.path.join(
|
2024-01-10 21:06:56 +01:00
|
|
|
data_dir, ex, "slixfeed_" + timestamp() + "." + ex)
|
2024-01-03 13:17:39 +01:00
|
|
|
db_file = get_pathname_to_database(jid)
|
|
|
|
results = await sqlite.get_feeds(db_file)
|
2024-01-10 21:06:56 +01:00
|
|
|
match ex:
|
2024-01-03 13:17:39 +01:00
|
|
|
case "html":
|
|
|
|
response = "Not yet implemented."
|
|
|
|
case "md":
|
2024-01-06 23:03:08 +01:00
|
|
|
action.export_to_markdown(
|
2024-01-03 13:17:39 +01:00
|
|
|
jid, filename, results)
|
|
|
|
case "opml":
|
2024-01-06 23:03:08 +01:00
|
|
|
action.export_to_opml(
|
2024-01-03 13:17:39 +01:00
|
|
|
jid, filename, results)
|
|
|
|
case "xbel":
|
|
|
|
response = "Not yet implemented."
|
|
|
|
url = await upload.start(self, jid, filename)
|
2024-01-09 23:36:16 +01:00
|
|
|
# response = (
|
|
|
|
# "Feeds exported successfully to {}.\n{}"
|
2024-01-10 21:06:56 +01:00
|
|
|
# ).format(ex, url)
|
2024-01-03 11:37:33 +01:00
|
|
|
# send_oob_reply_message(message, url, response)
|
2024-01-06 23:03:08 +01:00
|
|
|
await send_oob_message(
|
|
|
|
self, jid, url)
|
|
|
|
await task.start_tasks_xmpp(
|
|
|
|
self, jid, ["status"])
|
2024-01-03 13:17:39 +01:00
|
|
|
else:
|
|
|
|
response = "Unsupported filetype."
|
2024-01-06 23:03:08 +01:00
|
|
|
send_reply_message(self, message, response)
|
2024-01-03 16:04:01 +01:00
|
|
|
case _ if (message_lowercase.startswith("gemini:") or
|
|
|
|
message_lowercase.startswith("gopher:")):
|
2024-01-02 12:42:41 +01:00
|
|
|
response = "Gemini and Gopher are not supported yet."
|
|
|
|
send_reply_message(self, message, response)
|
2024-01-09 16:53:19 +01:00
|
|
|
# TODO xHTML, HTMLZ, Markdown, MHTML, PDF, TXT
|
2024-01-13 18:17:43 +01:00
|
|
|
case _ if (message_lowercase.startswith("get")):
|
2024-01-09 16:53:19 +01:00
|
|
|
message_text = message_text[4:]
|
2024-01-09 23:36:16 +01:00
|
|
|
ix_url = message_text.split(" ")[0]
|
|
|
|
ext = " ".join(message_text.split(" ")[1:])
|
|
|
|
ext = ext if ext else 'pdf'
|
2024-01-11 11:55:42 +01:00
|
|
|
url = None
|
|
|
|
status = None
|
2024-01-09 23:36:16 +01:00
|
|
|
if ext in ("html", "md", "pdf"):
|
|
|
|
status_type = "dnd"
|
|
|
|
status_message = (
|
2024-01-13 18:17:43 +01:00
|
|
|
"📃️ Procesing request to produce {} document..."
|
|
|
|
).format(ext.upper())
|
2024-01-09 23:36:16 +01:00
|
|
|
send_status_message(
|
|
|
|
self, jid, status_type, status_message)
|
|
|
|
db_file = get_pathname_to_database(jid)
|
|
|
|
data_dir = get_default_data_directory()
|
|
|
|
if ix_url:
|
|
|
|
if not os.path.isdir(data_dir):
|
|
|
|
os.mkdir(data_dir)
|
|
|
|
if not os.path.isdir(data_dir + '/readability'):
|
|
|
|
os.mkdir(data_dir + '/readability')
|
|
|
|
try:
|
|
|
|
ix = int(ix_url)
|
|
|
|
try:
|
|
|
|
url = sqlite.get_entry_url(db_file, ix)
|
|
|
|
except:
|
2024-01-13 18:17:43 +01:00
|
|
|
response = "No entry with Id {}".format(ix)
|
2024-01-09 23:36:16 +01:00
|
|
|
except:
|
|
|
|
url = ix_url
|
2024-01-11 11:55:42 +01:00
|
|
|
if url:
|
|
|
|
url = uri.remove_tracking_parameters(url)
|
|
|
|
url = (uri.replace_hostname(url, "link")) or url
|
2024-01-13 18:17:43 +01:00
|
|
|
result = await fetch.http(url)
|
|
|
|
data = result[0]
|
|
|
|
code = result[1]
|
|
|
|
if data:
|
|
|
|
title = action.get_document_title(data)
|
|
|
|
title = title.strip().lower()
|
|
|
|
for i in (" ", "-"):
|
|
|
|
title = title.replace(i, "_")
|
|
|
|
for i in ("?", "'", "!"):
|
|
|
|
title = title.replace(i, "")
|
|
|
|
filename = os.path.join(
|
|
|
|
data_dir, "readability",
|
|
|
|
title + "_" + timestamp() + "." + ext)
|
|
|
|
error = action.generate_document(
|
|
|
|
data, url, ext, filename)
|
|
|
|
if status:
|
|
|
|
response = (
|
|
|
|
"Failed to export {}. Reason: {}"
|
|
|
|
).format(ext.upper(), error)
|
|
|
|
else:
|
|
|
|
url = await upload.start(self, jid, filename)
|
|
|
|
await send_oob_message(self, jid, url)
|
2024-01-11 11:55:42 +01:00
|
|
|
else:
|
2024-01-13 18:17:43 +01:00
|
|
|
response = (
|
|
|
|
"Failed to fetch {}. Reason: {}"
|
|
|
|
).format(url, code)
|
2024-01-11 11:55:42 +01:00
|
|
|
await task.start_tasks_xmpp(
|
|
|
|
self, jid, ["status"])
|
2024-01-09 16:53:19 +01:00
|
|
|
else:
|
2024-01-09 23:36:16 +01:00
|
|
|
response = "Missing entry Id."
|
2024-01-09 16:53:19 +01:00
|
|
|
else:
|
2024-01-09 23:36:16 +01:00
|
|
|
response = "Unsupported filetype."
|
2024-01-09 16:53:19 +01:00
|
|
|
if response:
|
2024-01-11 11:55:42 +01:00
|
|
|
print(response)
|
2024-01-09 16:53:19 +01:00
|
|
|
send_reply_message(self, message, response)
|
2024-01-09 13:34:10 +01:00
|
|
|
# case _ if (message_lowercase.startswith("http")) and(
|
|
|
|
# message_lowercase.endswith(".opml")):
|
|
|
|
# url = message_text
|
|
|
|
# await task.clean_tasks_xmpp(
|
|
|
|
# jid, ["status"])
|
|
|
|
# status_type = "dnd"
|
|
|
|
# status_message = (
|
|
|
|
# "📥️ Procesing request to import feeds ..."
|
|
|
|
# )
|
|
|
|
# send_status_message(
|
|
|
|
# self, jid, status_type, status_message)
|
|
|
|
# db_file = get_pathname_to_database(jid)
|
|
|
|
# count = await action.import_opml(db_file, url)
|
|
|
|
# if count:
|
|
|
|
# response = (
|
2024-01-10 21:06:56 +01:00
|
|
|
# "Successfully imported {} feeds."
|
2024-01-09 13:34:10 +01:00
|
|
|
# ).format(count)
|
|
|
|
# else:
|
|
|
|
# response = (
|
|
|
|
# "OPML file was not imported."
|
|
|
|
# )
|
|
|
|
# await task.clean_tasks_xmpp(
|
|
|
|
# jid, ["status"])
|
|
|
|
# await task.start_tasks_xmpp(
|
|
|
|
# self, jid, ["status"])
|
|
|
|
# send_reply_message(self, message, response)
|
2024-01-02 12:42:41 +01:00
|
|
|
case _ if (message_lowercase.startswith("http") or
|
2024-01-03 16:04:01 +01:00
|
|
|
message_lowercase.startswith("feed:")):
|
2024-01-02 12:42:41 +01:00
|
|
|
url = message_text
|
2024-01-06 23:03:08 +01:00
|
|
|
await task.clean_tasks_xmpp(
|
|
|
|
jid, ["status"])
|
2024-01-02 12:42:41 +01:00
|
|
|
status_type = "dnd"
|
|
|
|
status_message = (
|
|
|
|
"📫️ Processing request to fetch data from {}"
|
|
|
|
).format(url)
|
2024-01-06 23:03:08 +01:00
|
|
|
send_status_message(
|
|
|
|
self, jid, status_type, status_message)
|
2024-01-02 12:42:41 +01:00
|
|
|
if url.startswith("feed:"):
|
|
|
|
url = uri.feed_to_http(url)
|
|
|
|
url = (uri.replace_hostname(url, "feed")) or url
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-10 21:06:56 +01:00
|
|
|
try:
|
|
|
|
response = await action.add_feed(
|
|
|
|
db_file, url)
|
|
|
|
await task.clean_tasks_xmpp(
|
|
|
|
jid, ["status"])
|
|
|
|
await task.start_tasks_xmpp(
|
|
|
|
self, jid, ["status"])
|
|
|
|
except:
|
|
|
|
response = (
|
|
|
|
"> {}\nNews source is in the process "
|
|
|
|
"of being added to the subscription "
|
|
|
|
"list.".format(url)
|
|
|
|
)
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("feeds"):
|
|
|
|
query = message_text[6:]
|
|
|
|
if query:
|
|
|
|
if len(query) > 3:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-09 13:34:10 +01:00
|
|
|
result = await sqlite.search_feeds(
|
|
|
|
db_file, query)
|
|
|
|
response = action.list_feeds_by_query(
|
|
|
|
query, result)
|
2024-01-02 12:42:41 +01:00
|
|
|
else:
|
|
|
|
response = (
|
|
|
|
"Enter at least 4 characters to search"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
|
|
|
result = await sqlite.get_feeds(db_file)
|
2024-01-06 23:03:08 +01:00
|
|
|
response = action.list_feeds(result)
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case "goodbye":
|
|
|
|
if message["type"] == "groupchat":
|
|
|
|
await groupchat.leave(self, jid)
|
|
|
|
else:
|
|
|
|
response = "This command is valid for groupchat only."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("interval"):
|
|
|
|
# FIXME
|
|
|
|
# The following error occurs only upon first attempt to set interval.
|
|
|
|
# /usr/lib/python3.11/asyncio/events.py:73: RuntimeWarning: coroutine 'Slixfeed.send_update' was never awaited
|
|
|
|
# self._args = None
|
|
|
|
# RuntimeWarning: Enable tracemalloc to get the object allocation traceback
|
|
|
|
key = message_text[:8]
|
|
|
|
val = message_text[9:]
|
|
|
|
if val:
|
|
|
|
# response = (
|
|
|
|
# "Updates will be sent every {} minutes."
|
|
|
|
# ).format(response)
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-14 13:46:38 +01:00
|
|
|
if await sqlite.get_settings_value(db_file, key):
|
|
|
|
await sqlite.update_settings_value(db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_settings_value(db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
# NOTE Perhaps this should be replaced
|
|
|
|
# by functions clean and start
|
|
|
|
await task.refresh_task(
|
|
|
|
self, jid, task.send_update, key, val)
|
|
|
|
response = (
|
|
|
|
"Updates will be sent every {} minutes."
|
|
|
|
).format(val)
|
|
|
|
else:
|
|
|
|
response = "Missing value."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("join"):
|
|
|
|
muc_jid = uri.check_xmpp_uri(message_text[5:])
|
|
|
|
if muc_jid:
|
|
|
|
# TODO probe JID and confirm it's a groupchat
|
|
|
|
await groupchat.join(self, jid, muc_jid)
|
|
|
|
response = (
|
|
|
|
"Joined groupchat {}"
|
2024-01-09 13:34:10 +01:00
|
|
|
).format(message_text)
|
2024-01-02 12:42:41 +01:00
|
|
|
else:
|
|
|
|
response = (
|
|
|
|
"> {}\nXMPP URI is not valid."
|
2024-01-09 13:34:10 +01:00
|
|
|
).format(message_text)
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("length"):
|
|
|
|
key = message_text[:6]
|
|
|
|
val = message_text[7:]
|
|
|
|
if val:
|
|
|
|
try:
|
|
|
|
val = int(val)
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-14 13:46:38 +01:00
|
|
|
if await sqlite.get_settings_value(
|
|
|
|
db_file, [key, val]):
|
|
|
|
await sqlite.update_settings_value(
|
|
|
|
db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_settings_value(
|
|
|
|
db_file, [key, val])
|
2024-01-04 14:58:06 +01:00
|
|
|
if val == 0: # if not val:
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"Summary length limit is disabled."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
response = (
|
|
|
|
"Summary maximum length "
|
|
|
|
"is set to {} characters."
|
|
|
|
).format(val)
|
|
|
|
except:
|
|
|
|
response = "Enter a numeric value only."
|
|
|
|
else:
|
|
|
|
response = "Missing value."
|
|
|
|
# case _ if message_lowercase.startswith("mastership"):
|
|
|
|
# key = message_text[:7]
|
|
|
|
# val = message_text[11:]
|
|
|
|
# if val:
|
|
|
|
# names = await initdb(
|
|
|
|
# jid,
|
|
|
|
# get_settings_value,
|
|
|
|
# key
|
|
|
|
# )
|
|
|
|
# val = await add_to_list(
|
|
|
|
# val,
|
|
|
|
# names
|
|
|
|
# )
|
|
|
|
# await initdb(
|
|
|
|
# jid,
|
2024-01-14 13:46:38 +01:00
|
|
|
# update_settings_valuevv,
|
2024-01-02 12:42:41 +01:00
|
|
|
# [key, val]
|
|
|
|
# )
|
|
|
|
# response = (
|
|
|
|
# "Operators\n"
|
|
|
|
# "```\n{}\n```"
|
|
|
|
# ).format(val)
|
|
|
|
# else:
|
|
|
|
# response = "Missing value."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case "new":
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-14 13:46:38 +01:00
|
|
|
key = "old"
|
|
|
|
val = 0
|
|
|
|
if await sqlite.get_settings_value(db_file, key):
|
|
|
|
await sqlite.update_settings_value(db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_settings_value(db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"Only new items of newly added feeds will be sent."
|
|
|
|
)
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
# TODO Will you add support for number of messages?
|
|
|
|
case "next":
|
|
|
|
# num = message_text[5:]
|
2024-01-04 14:58:06 +01:00
|
|
|
await task.clean_tasks_xmpp(
|
|
|
|
jid, ["interval", "status"])
|
|
|
|
await task.start_tasks_xmpp(
|
|
|
|
self, jid, ["interval", "status"])
|
2024-01-02 12:42:41 +01:00
|
|
|
# await refresh_task(
|
|
|
|
# self,
|
|
|
|
# jid,
|
|
|
|
# send_update,
|
|
|
|
# "interval",
|
|
|
|
# num
|
|
|
|
# )
|
|
|
|
# await refresh_task(
|
|
|
|
# self,
|
|
|
|
# jid,
|
|
|
|
# send_status,
|
|
|
|
# "status",
|
|
|
|
# 20
|
|
|
|
# )
|
|
|
|
# await refresh_task(jid, key, val)
|
|
|
|
case "old":
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-14 13:46:38 +01:00
|
|
|
key = "old"
|
|
|
|
val = 1
|
|
|
|
if await sqlite.get_settings_value(db_file, key):
|
|
|
|
await sqlite.update_settings_value(db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_settings_value(db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"All items of newly added feeds will be sent."
|
|
|
|
)
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("quantum"):
|
|
|
|
key = message_text[:7]
|
|
|
|
val = message_text[8:]
|
|
|
|
if val:
|
|
|
|
try:
|
|
|
|
val = int(val)
|
|
|
|
# response = (
|
|
|
|
# "Every update will contain {} news items."
|
|
|
|
# ).format(response)
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-14 13:46:38 +01:00
|
|
|
a = await sqlite.get_settings_value(db_file, key)
|
|
|
|
print(a)
|
|
|
|
print(key)
|
|
|
|
print(val)
|
|
|
|
print(a)
|
|
|
|
print(a)
|
|
|
|
print(a)
|
|
|
|
if await sqlite.get_settings_value(
|
|
|
|
db_file, key):
|
|
|
|
await sqlite.update_settings_value(
|
|
|
|
db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_settings_value(
|
|
|
|
db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"Next update will contain {} news items."
|
|
|
|
).format(val)
|
|
|
|
except:
|
|
|
|
response = "Enter a numeric value only."
|
|
|
|
else:
|
|
|
|
response = "Missing value."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case "random":
|
|
|
|
# TODO /questions/2279706/select-random-row-from-a-sqlite-table
|
|
|
|
# NOTE sqlitehandler.get_entry_unread
|
|
|
|
response = "Updates will be sent by random order."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("read"):
|
|
|
|
data = message_text[5:]
|
|
|
|
data = data.split()
|
|
|
|
url = data[0]
|
2024-01-04 02:16:24 +01:00
|
|
|
await task.clean_tasks_xmpp(
|
|
|
|
jid, ["status"])
|
2024-01-02 12:42:41 +01:00
|
|
|
status_type = "dnd"
|
|
|
|
status_message = (
|
|
|
|
"📫️ Processing request to fetch data from {}"
|
|
|
|
).format(url)
|
2024-01-06 23:03:08 +01:00
|
|
|
send_status_message(
|
|
|
|
self, jid, status_type, status_message)
|
2024-01-02 12:42:41 +01:00
|
|
|
if url.startswith("feed:"):
|
|
|
|
url = uri.feed_to_http(url)
|
|
|
|
url = (uri.replace_hostname(url, "feed")) or url
|
|
|
|
match len(data):
|
|
|
|
case 1:
|
|
|
|
if url.startswith("http"):
|
2024-01-04 02:16:24 +01:00
|
|
|
response = await action.view_feed(url)
|
2024-01-02 12:42:41 +01:00
|
|
|
else:
|
|
|
|
response = "Missing URL."
|
|
|
|
case 2:
|
|
|
|
num = data[1]
|
|
|
|
if url.startswith("http"):
|
2024-01-04 02:16:24 +01:00
|
|
|
response = await action.view_entry(url, num)
|
2024-01-02 12:42:41 +01:00
|
|
|
else:
|
|
|
|
response = "Missing URL."
|
|
|
|
case _:
|
|
|
|
response = (
|
|
|
|
"Enter command as follows:\n"
|
|
|
|
"`read <url>` or `read <url> <number>`\n"
|
|
|
|
"URL must not contain white space."
|
|
|
|
)
|
|
|
|
send_reply_message(self, message, response)
|
2024-01-04 14:58:06 +01:00
|
|
|
await task.start_tasks_xmpp(
|
|
|
|
self, jid, ["status"])
|
2024-01-02 12:42:41 +01:00
|
|
|
case _ if message_lowercase.startswith("recent"):
|
|
|
|
num = message_text[7:]
|
|
|
|
if num:
|
|
|
|
try:
|
|
|
|
num = int(num)
|
|
|
|
if num < 1 or num > 50:
|
|
|
|
response = "Value must be ranged from 1 to 50."
|
|
|
|
else:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
|
|
|
result = await sqlite.last_entries(db_file, num)
|
2024-01-06 23:03:08 +01:00
|
|
|
response = action.list_last_entries(result, num)
|
2024-01-02 12:42:41 +01:00
|
|
|
except:
|
|
|
|
response = "Enter a numeric value only."
|
|
|
|
else:
|
|
|
|
response = "Missing value."
|
|
|
|
send_reply_message(self, message, response)
|
2024-01-13 18:17:43 +01:00
|
|
|
case _ if message_lowercase.startswith("remove"):
|
2024-01-06 23:03:08 +01:00
|
|
|
ix_url = message_text[7:]
|
|
|
|
if ix_url:
|
2024-01-02 12:42:41 +01:00
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-03 16:04:01 +01:00
|
|
|
try:
|
2024-01-06 23:03:08 +01:00
|
|
|
ix = int(ix_url)
|
|
|
|
try:
|
|
|
|
url = await sqlite.remove_feed_by_index(
|
|
|
|
db_file, ix)
|
|
|
|
response = (
|
|
|
|
"> {}\nNews source {} has been removed "
|
2024-01-09 13:34:10 +01:00
|
|
|
"from subscription list."
|
|
|
|
).format(url, ix)
|
2024-01-06 23:03:08 +01:00
|
|
|
except:
|
|
|
|
response = (
|
2024-01-09 13:34:10 +01:00
|
|
|
"No news source with ID {}."
|
|
|
|
).format(ix)
|
2024-01-03 16:04:01 +01:00
|
|
|
except:
|
2024-01-06 23:03:08 +01:00
|
|
|
url = ix_url
|
|
|
|
await sqlite.remove_feed_by_url(db_file, url)
|
2024-01-04 02:16:24 +01:00
|
|
|
response = (
|
2024-01-06 23:03:08 +01:00
|
|
|
"> {}\nNews source has been removed "
|
2024-01-09 13:34:10 +01:00
|
|
|
"from subscription list."
|
|
|
|
).format(url)
|
2024-01-06 23:03:08 +01:00
|
|
|
# await refresh_task(
|
|
|
|
# self,
|
|
|
|
# jid,
|
|
|
|
# send_status,
|
|
|
|
# "status",
|
|
|
|
# 20
|
|
|
|
# )
|
|
|
|
await task.clean_tasks_xmpp(jid, ["status"])
|
|
|
|
await task.start_tasks_xmpp(self, jid, ["status"])
|
2024-01-02 12:42:41 +01:00
|
|
|
else:
|
|
|
|
response = "Missing feed ID."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("reset"):
|
2024-01-06 23:03:08 +01:00
|
|
|
url = message_text[6:]
|
2024-01-02 12:42:41 +01:00
|
|
|
await task.clean_tasks_xmpp(jid, ["status"])
|
|
|
|
status_type = "dnd"
|
|
|
|
status_message = "📫️ Marking entries as read..."
|
2024-01-04 02:16:24 +01:00
|
|
|
send_status_message(
|
|
|
|
self, jid, status_type, status_message)
|
2024-01-06 23:03:08 +01:00
|
|
|
if url:
|
2024-01-02 12:42:41 +01:00
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-06 23:03:08 +01:00
|
|
|
await sqlite.mark_feed_as_read(
|
|
|
|
db_file, url)
|
2024-01-02 12:42:41 +01:00
|
|
|
response = (
|
|
|
|
"All entries of {} have been "
|
2024-01-06 23:03:08 +01:00
|
|
|
"marked as read.".format(url)
|
2024-01-02 12:42:41 +01:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
|
|
|
await sqlite.mark_all_as_read(db_file)
|
|
|
|
response = "All entries have been marked as read."
|
|
|
|
send_reply_message(self, message, response)
|
2024-01-04 14:58:06 +01:00
|
|
|
await task.start_tasks_xmpp(
|
|
|
|
self, jid, ["status"])
|
2024-01-02 12:42:41 +01:00
|
|
|
case _ if message_lowercase.startswith("search"):
|
|
|
|
query = message_text[7:]
|
|
|
|
if query:
|
|
|
|
if len(query) > 1:
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-04 14:58:06 +01:00
|
|
|
results = await sqlite.search_entries(
|
|
|
|
db_file, query)
|
2024-01-06 23:03:08 +01:00
|
|
|
response = action.list_search_results(
|
2024-01-04 14:58:06 +01:00
|
|
|
query, results)
|
2024-01-02 12:42:41 +01:00
|
|
|
else:
|
|
|
|
response = (
|
|
|
|
"Enter at least 2 characters to search"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
response = "Missing search query."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case "start":
|
|
|
|
# response = "Updates are enabled."
|
|
|
|
key = "enabled"
|
|
|
|
val = 1
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-14 13:46:38 +01:00
|
|
|
if await sqlite.get_settings_value(db_file, key):
|
|
|
|
await sqlite.update_settings_value(db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_settings_value(db_file, [key, val])
|
2024-01-02 12:42:41 +01:00
|
|
|
# asyncio.create_task(task_jid(self, jid))
|
2024-01-04 02:16:24 +01:00
|
|
|
await task.start_tasks_xmpp(
|
|
|
|
self, jid, ["interval", "status", "check"])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = "Updates are enabled."
|
|
|
|
# print(current_time(), "task_manager[jid]")
|
|
|
|
# print(task_manager[jid])
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case "stats":
|
|
|
|
db_file = get_pathname_to_database(jid)
|
|
|
|
result = await sqlite.statistics(db_file)
|
2024-01-06 23:03:08 +01:00
|
|
|
response = action.list_statistics(result)
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
2024-01-03 16:04:01 +01:00
|
|
|
case _ if message_lowercase.startswith("disable "):
|
|
|
|
ix = message_text[8:]
|
|
|
|
db_file = get_pathname_to_database(jid)
|
|
|
|
try:
|
|
|
|
await sqlite.set_enabled_status(db_file, ix, 0)
|
|
|
|
response = (
|
|
|
|
"Updates are now disabled for news source {}."
|
|
|
|
).format(ix)
|
|
|
|
except:
|
|
|
|
response = "No news source with ID {}.".format(ix)
|
|
|
|
send_reply_message(self, message, response)
|
2024-01-13 18:17:43 +01:00
|
|
|
case _ if message_lowercase.startswith("enable"):
|
2024-01-02 12:42:41 +01:00
|
|
|
ix = message_text[7:]
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-03 16:04:01 +01:00
|
|
|
try:
|
|
|
|
await sqlite.set_enabled_status(db_file, ix, 1)
|
|
|
|
response = (
|
2024-01-10 21:06:56 +01:00
|
|
|
"Updates are now enabled for news source {}."
|
2024-01-03 16:04:01 +01:00
|
|
|
).format(ix)
|
|
|
|
except:
|
|
|
|
response = "No news source with ID {}.".format(ix)
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case "stop":
|
|
|
|
# FIXME
|
|
|
|
# The following error occurs only upon first attempt to stop.
|
|
|
|
# /usr/lib/python3.11/asyncio/events.py:73: RuntimeWarning: coroutine 'Slixfeed.send_update' was never awaited
|
|
|
|
# self._args = None
|
|
|
|
# RuntimeWarning: Enable tracemalloc to get the object allocation traceback
|
|
|
|
# response = "Updates are disabled."
|
|
|
|
# try:
|
|
|
|
# # task_manager[jid]["check"].cancel()
|
|
|
|
# # task_manager[jid]["status"].cancel()
|
|
|
|
# task_manager[jid]["interval"].cancel()
|
|
|
|
# key = "enabled"
|
|
|
|
# val = 0
|
|
|
|
# response = await initdb(
|
|
|
|
# jid,
|
2024-01-14 13:46:38 +01:00
|
|
|
# update_settings_value,
|
2024-01-02 12:42:41 +01:00
|
|
|
# [key, val]
|
|
|
|
# )
|
|
|
|
# except:
|
|
|
|
# response = "Updates are already disabled."
|
|
|
|
# # print("Updates are already disabled. Nothing to do.")
|
|
|
|
# # await send_status(jid)
|
|
|
|
key = "enabled"
|
|
|
|
val = 0
|
|
|
|
db_file = get_pathname_to_database(jid)
|
2024-01-14 13:46:38 +01:00
|
|
|
if await sqlite.get_settings_value(db_file, key):
|
|
|
|
await sqlite.update_settings_value(db_file, [key, val])
|
|
|
|
else:
|
|
|
|
await sqlite.set_settings_value(db_file, [key, val])
|
2024-01-04 02:16:24 +01:00
|
|
|
await task.clean_tasks_xmpp(
|
|
|
|
jid, ["interval", "status"])
|
2024-01-02 12:42:41 +01:00
|
|
|
response = "Updates are disabled."
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
status_type = "xa"
|
2024-01-04 02:16:24 +01:00
|
|
|
status_message = (
|
|
|
|
"💡️ Send \"Start\" to receive Jabber updates")
|
|
|
|
send_status_message(
|
|
|
|
self, jid, status_type, status_message)
|
2024-01-02 12:42:41 +01:00
|
|
|
case "support":
|
|
|
|
# TODO Send an invitation.
|
|
|
|
response = (
|
2024-01-04 14:58:06 +01:00
|
|
|
"Join xmpp:slixfeed@chat.woodpeckersnest.space?join"
|
|
|
|
)
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _ if message_lowercase.startswith("xmpp:"):
|
|
|
|
muc_jid = uri.check_xmpp_uri(message_text)
|
|
|
|
if muc_jid:
|
|
|
|
# TODO probe JID and confirm it's a groupchat
|
|
|
|
await groupchat.join(self, jid, muc_jid)
|
|
|
|
response = (
|
|
|
|
"Joined groupchat {}"
|
2024-01-09 13:34:10 +01:00
|
|
|
).format(message_text)
|
2024-01-02 12:42:41 +01:00
|
|
|
else:
|
|
|
|
response = (
|
|
|
|
"> {}\nXMPP URI is not valid."
|
2024-01-09 13:34:10 +01:00
|
|
|
).format(message_text)
|
2024-01-02 12:42:41 +01:00
|
|
|
send_reply_message(self, message, response)
|
|
|
|
case _:
|
|
|
|
response = (
|
|
|
|
"Unknown command. "
|
|
|
|
"Press \"help\" for list of commands"
|
|
|
|
)
|
|
|
|
send_reply_message(self, message, response)
|
|
|
|
# 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()
|
|
|
|
|
2024-01-02 19:11:36 +01:00
|
|
|
if not response: response = "EMPTY MESSAGE - ACTION ONLY"
|
2024-01-06 23:03:08 +01:00
|
|
|
data_dir = get_default_data_directory()
|
2024-01-03 11:37:33 +01:00
|
|
|
if not os.path.isdir(data_dir):
|
|
|
|
os.mkdir(data_dir)
|
2024-01-03 13:30:52 +01:00
|
|
|
if not os.path.isdir(data_dir + '/logs/'):
|
|
|
|
os.mkdir(data_dir + '/logs/')
|
2024-01-06 23:03:08 +01:00
|
|
|
action.log_to_markdown(
|
2024-01-03 11:37:33 +01:00
|
|
|
current_time(), os.path.join(data_dir, "logs", jid),
|
2024-01-02 12:42:41 +01:00
|
|
|
jid, message_text)
|
2024-01-06 23:03:08 +01:00
|
|
|
action.log_to_markdown(
|
2024-01-03 11:37:33 +01:00
|
|
|
current_time(), os.path.join(data_dir, "logs", jid),
|
2024-01-02 12:42:41 +01:00
|
|
|
self.boundjid.bare, response)
|
|
|
|
|
|
|
|
|
|
|
|
def send_status_message(self, jid, status_type, status_message):
|
|
|
|
self.send_presence(
|
|
|
|
pshow=status_type,
|
|
|
|
pstatus=status_message,
|
|
|
|
pto=jid)
|
|
|
|
|
|
|
|
|
|
|
|
def send_reply_message(self, message, response):
|
|
|
|
message.reply(response).send()
|
|
|
|
|
|
|
|
|
2024-01-04 14:58:06 +01:00
|
|
|
# TODO Solve this function
|
2024-01-03 11:37:33 +01:00
|
|
|
def send_oob_reply_message(message, url, response):
|
|
|
|
reply = message.reply(response)
|
|
|
|
reply['oob']['url'] = url
|
|
|
|
reply.send()
|
|
|
|
|
|
|
|
|
2024-01-03 13:17:39 +01:00
|
|
|
async def send_oob_message(self, jid, url):
|
|
|
|
chat_type = await jid_type(self, jid)
|
2024-01-03 11:37:33 +01:00
|
|
|
html = (
|
|
|
|
f'<body xmlns="http://www.w3.org/1999/xhtml">'
|
|
|
|
f'<a href="{url}">{url}</a></body>')
|
2024-01-04 14:58:06 +01:00
|
|
|
message = self.make_message(
|
|
|
|
mto=jid,
|
|
|
|
mbody=url,
|
|
|
|
mhtml=html,
|
|
|
|
mtype=chat_type
|
|
|
|
)
|
2024-01-03 11:37:33 +01:00
|
|
|
message['oob']['url'] = url
|
|
|
|
message.send()
|
|
|
|
|
|
|
|
|
2024-01-02 12:42:41 +01:00
|
|
|
# def greet(self, jid, chat_type="chat"):
|
|
|
|
# messages = [
|
|
|
|
# "Greetings!",
|
|
|
|
# "I'm {}, the news anchor.".format(self.nick),
|
|
|
|
# "My job is to bring you the latest news "
|
|
|
|
# "from sources you provide me with.",
|
|
|
|
# "You may always reach me via "
|
|
|
|
# "xmpp:{}?message".format(self.boundjid.bare)
|
|
|
|
# ]
|
|
|
|
# for message in messages:
|
|
|
|
# self.send_message(
|
|
|
|
# mto=jid,
|
|
|
|
# mbody=message,
|
|
|
|
# mtype=chat_type
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
|
|
def greet(self, jid, chat_type="chat"):
|
|
|
|
message = (
|
|
|
|
"Greetings!\n"
|
|
|
|
"I'm {}, the news anchor.\n"
|
|
|
|
"My job is to bring you the latest "
|
|
|
|
"news from sources you provide me with.\n"
|
|
|
|
"You may always reach me via xmpp:{}?message").format(
|
|
|
|
self.nick,
|
|
|
|
self.boundjid.bare
|
|
|
|
)
|
|
|
|
self.send_message(
|
|
|
|
mto=jid,
|
|
|
|
mbody=message,
|
|
|
|
mtype=chat_type
|
|
|
|
)
|
|
|
|
|