mirror of
https://codeberg.org/sch/BukuBot
synced 2024-11-17 19:48:40 +01:00
d03839d578
Segregate code into modules.
182 lines
9.5 KiB
Python
182 lines
9.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from bukuxmpp.about import Documentation
|
|
from bukuxmpp.config import Configuration
|
|
|
|
try:
|
|
import tomllib
|
|
except:
|
|
import tomli as tomllib
|
|
|
|
|
|
class Chat:
|
|
|
|
def action(self, message):
|
|
"""
|
|
Process incoming message stanzas. Be aware that this also
|
|
includes MUC messages and error messages. It is usually
|
|
a good idea to check the messages's type before processing
|
|
or sending replies.
|
|
|
|
Arguments:
|
|
message -- The received message stanza. See the documentation
|
|
for stanza objects and the Message stanza to see
|
|
how it may be used.
|
|
"""
|
|
jid_bare = message['from'].bare
|
|
bookmarks_db = Configuration.init_db(jid_bare)
|
|
if message['type'] in ('chat', 'normal'):
|
|
message_text = " ".join(message['body'].split())
|
|
message_lowercase = message_text.lower()
|
|
match message_lowercase:
|
|
case 'help':
|
|
message_body = '```\n' + Documentation.commands() + '\n```'
|
|
case _ if message_lowercase.startswith('add '):
|
|
message_lowercase_split = message_lowercase[4:].split(' ')
|
|
link = message_lowercase_split[0]
|
|
tags = ' '.join(message_lowercase_split[1:])
|
|
tags = tags.replace(' ,', ',')
|
|
tags = tags.replace(', ', ',')
|
|
idx = bookmarks_db.get_rec_id(link)
|
|
if idx:
|
|
message_body = '*URL already exists.*'
|
|
else:
|
|
idx = bookmarks_db.add_rec(url=link, tags_in=tags)
|
|
message_body = ('*New bookmark has been added as {}.*'
|
|
.format(idx))
|
|
case _ if message_lowercase.startswith('id'):
|
|
idx = message_lowercase[2:]
|
|
result = bookmarks_db.get_rec_by_id(idx)
|
|
message_body = Chat.format_message(result, extended=True)
|
|
case 'last':
|
|
idx = bookmarks_db.get_max_id()
|
|
result = bookmarks_db.get_rec_by_id(idx)
|
|
message_body = Chat.format_message(result)
|
|
case _ if message_lowercase.startswith('search any '):
|
|
query = message_lowercase[11:]
|
|
query = query.split(' ')
|
|
results = bookmarks_db.searchdb(query,
|
|
all_keywords=False,
|
|
deep=True,
|
|
regex=False)
|
|
message_body = '*Results for query: {}*\n\n'.format(query)
|
|
for result in results:
|
|
message_body += Chat.format_message(result) + '\n\n'
|
|
message_body += '*Total of {} results*'.format(len(results))
|
|
case _ if message_lowercase.startswith('search '):
|
|
query = message_lowercase[7:]
|
|
query = query.split(' ')
|
|
results = bookmarks_db.searchdb(query,
|
|
all_keywords=True,
|
|
deep=True,
|
|
regex=False)
|
|
message_body = '*Results for query: {}*\n\n'.format(query)
|
|
for result in results:
|
|
message_body += Chat.format_message(result) + '\n\n'
|
|
message_body += '*Total of {} results*\n\n'.format(len(results))
|
|
# elif message.startswith('regex'):
|
|
# message_body = bookmark_regexp(message[7:])
|
|
case _ if message_lowercase.startswith('del '):
|
|
val = message_lowercase[4:]
|
|
try:
|
|
idx = int(val)
|
|
except:
|
|
idx = bookmarks_db.get_rec_id(val)
|
|
bookmark = bookmarks_db.get_rec_by_id(idx)
|
|
message_body = Chat.format_message(bookmark, extended=True) if bookmark else ''
|
|
result = bookmarks_db.delete_rec(idx)
|
|
if result:
|
|
message_body += '\n*Bookmark has been deleted.*'
|
|
else:
|
|
message_body += '\n*No action has been taken for index {}*'.format(idx)
|
|
case _ if message_lowercase.startswith('mod '):
|
|
message_lowercase_split = message_lowercase[4:].split(' ')
|
|
if len(message_lowercase_split) > 2:
|
|
arg = message_lowercase_split[0]
|
|
val = message_lowercase_split[1]
|
|
new = ' '.join(message_lowercase_split[2:])
|
|
message_body = ''
|
|
try:
|
|
idx = int(val)
|
|
except:
|
|
idx = bookmarks_db.get_rec_id(val)
|
|
match arg:
|
|
case 'name':
|
|
result = bookmarks_db.update_rec(idx, title_in=new)
|
|
case 'note':
|
|
result = bookmarks_db.update_rec(idx, desc=new)
|
|
case _:
|
|
result = None
|
|
message_body = ('*Invalid argument. '
|
|
'Must be "name" or "note".*\n')
|
|
bookmark = bookmarks_db.get_rec_by_id(idx)
|
|
message_body += Chat.format_message(bookmark, extended=True) if bookmark else ''
|
|
if result:
|
|
message_body += '\n*Bookmark has been deleted.*'
|
|
else:
|
|
message_body += '\n*No action has been taken for index {}*'.format(idx)
|
|
else:
|
|
message_body = ('Missing argument. '
|
|
'Require three arguments: '
|
|
'(1) "name" or "note"; '
|
|
'(2) <ID> or <URL>; '
|
|
'(3) <TEXT>.')
|
|
case _ if (message_lowercase.startswith('tag +') or
|
|
message_lowercase.startswith('tag -')):
|
|
message_lowercase_split = message_lowercase[4:].split(' ')
|
|
if len(message_lowercase_split) > 2:
|
|
arg = message_lowercase_split[0]
|
|
val = message_lowercase_split[1]
|
|
try:
|
|
idx = int(val)
|
|
except:
|
|
idx = bookmarks_db.get_rec_id(val)
|
|
# tag = ',' + ' '.join(message_lowercase_split[2:]) + ','
|
|
# tag = ' '.join(message_lowercase_split[2:])
|
|
tag = arg + ',' + ' '.join(message_lowercase_split[2:])
|
|
tag = tag.replace(' ,', ',')
|
|
tag = tag.replace(', ', ',')
|
|
result = bookmarks_db.update_rec(idx, tags_in=tag)
|
|
bookmark = bookmarks_db.get_rec_by_id(idx)
|
|
if result:
|
|
message_body = Chat.format_message(bookmark, extended=True) if bookmark else ''
|
|
message_body += '\n*Bookmark has been updated.*'
|
|
else:
|
|
message_body = '\n*No action has been taken for index {}*'.format(idx)
|
|
else:
|
|
message_body = ('Missing argument. '
|
|
'Require three arguments: '
|
|
'(1) + or - sign; '
|
|
'(2) <ID> or <URL>; '
|
|
'(3) <TAGS>.')
|
|
case _ if message_lowercase.startswith('tag'):
|
|
tag = message_lowercase[4:]
|
|
results = bookmarks_db.search_by_tag(tag)
|
|
message_body = '*Results for tag: {}*\n\n'.format(tag)
|
|
for result in results:
|
|
message_body += Chat.format_message(result) + '\n\n'
|
|
message_body += '*Total of {} results*'.format(len(results))
|
|
case _:
|
|
message_body = ('Unknown command. Send "help" for list '
|
|
'of commands.')
|
|
message.reply(message_body).send()
|
|
#message.reply("Thanks for sending\n%(body)s" % message).send()
|
|
|
|
def format_message(bookmark, extended=False):
|
|
# idx = bookmark.id
|
|
# url = bookmark.url
|
|
# name = bookmark.title if bookmark.title else 'Untitled'
|
|
# desc = bookmark.desc if bookmark.desc else 'No comment'
|
|
idx = bookmark[0]
|
|
url = bookmark[1]
|
|
name = bookmark[2] if bookmark[2] else 'Untitled'
|
|
desc = bookmark[4] if bookmark[4] else None
|
|
# rec = '\n 🔖️ {} [{}]\n 🔗️ {}\n 🏷️ {}'.format(title, index, link, tags)
|
|
if extended:
|
|
tags = '' if bookmark.tags_raw == ',' else ", ".join(bookmark.tags_raw.split(","))[2:-2]
|
|
tags = tags if tags else 'No tags'
|
|
message_body = ('{}. {}\n{}\n{}\n{}'.format(idx, name, url, desc, tags))
|
|
else:
|
|
message_body = ('{}. {}\n{}'.format(idx, name, url))
|
|
return message_body
|