Added requerimentx.txt and the listen bot, xmpp.py

This commit is contained in:
spla 2022-08-18 13:16:56 +02:00
pare cf10f4818d
commit 40bbd00ebb
S'han modificat 2 arxius amb 138 adicions i 0 eliminacions

2
requirements.txt Normal file
Veure arxiu

@ -0,0 +1,2 @@
Mastodon.py
unidecode

136
xmpp.py Normal file
Veure arxiu

@ -0,0 +1,136 @@
from mastodonbot import Mastodonbot
from ejabberdapi import Ejabberd
from mastodon import MastodonNetworkError
import sys
# main
if __name__ == '__main__':
bot = Mastodonbot()
ejabberd = Ejabberd()
try:
notifications = bot.mastodon.notifications()
except MastodonNetworkError as net_error:
sys.exit(net_error)
for notif in notifications:
if notif.type != 'mention':
print(f"Dismissing notification id {notif.id}")
bot.mastodon.notifications_dismiss(notif.id)
else:
mention = bot.get_data(notif)
if mention.reply and '@' not in mention.acct:
if mention.question == bot.register_str:
account_exists = ejabberd.check_account(mention.acct, bot.mastodon_hostname)
if not account_exists:
password = ejabberd.generate_pass()
is_registered, text = ejabberd.register(mention.acct, bot.mastodon_hostname, password)
if is_registered:
post = f"@{mention.acct} {bot.registerok_str}\n\n{bot.user_str} {mention.acct}@{bot.mastodon_hostname}\n"
post += f"{bot.password_str} {password}\n{bot.server_str} {bot.mastodon_hostname}"
bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility='direct')
else:
bot.mastodon.status_post(f'@{mention.acct}, {text}', in_reply_to_id=mention.status_id, visibility='direct')
else:
bot.mastodon.status_post(f'@{mention.acct}, {bot.account_exists_str}', in_reply_to_id=mention.status_id, visibility='direct')
elif mention.question == bot.unregister_str:
is_unregistered, is_admin = ejabberd.unregister(mention.acct, bot.mastodon_hostname)
if is_unregistered:
bot.mastodon.status_post(f"@{mention.acct}, {bot.xmpp_account_str} {mention.acct}@{bot.mastodon_hostname}: {bot.deleted_str}", in_reply_to_id=mention.status_id, visibility='direct')
elif not is_unregistered:
if is_admin:
bot.mastodon.status_post(f'@{mention.acct}, {bot.notdeleteadmin_str}', in_reply_to_id=mention.status_id, visibility='direct')
elif mention.question == bot.stats_str:
stats = ejabberd.stats()
post = f'@{mention.acct}, {bot.stats_title_str} {ejabberd._Ejabberd__local_vhost}:\n\n'
post += f'{bot.registered_users_str} {stats.registeredusers}\n'
post += f'{bot.users_online_str} {stats.onlineusers}\n'
post += f'{bot.node_users_str} {stats.onlineusersnode}\n'
post += f'{bot.uptime_str} {stats.uptimeseconds}\n'
post += f'{bot.processes_str} {stats.processes}\n'
bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility=mention.visibility)
elif mention.question == bot.status_str:
status = ejabberd.status()
post = f'@{mention.acct}, {status}\n'
bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility=mention.visibility)
elif mention.question == bot.user_sessions_info_str:
sessions = ejabberd.user_sessions_info(mention.acct, bot.mastodon_hostname)
post = f'@{mention.acct}, {bot.current_sessions_str}\n\n'
if len(sessions) != 0:
post += f'{bot.sessions_connection_str} {sessions.connection}\n'
post += f'{bot.sessions_ip_str} {sessions.ip}\n'
post += f'{bot.sessions_port_str} {sessions.port}\n'
post += f'{bot.sessions_priority_str} {sessions.priority}\n'
post += f'{bot.sessions_node_str} {sessions.node}\n'
post += f'{bot.sessions_uptime_str} {sessions.uptime}\n'
post += f'{bot.sessions_status_str} {sessions.status}\n'
post += f'{bot.sessions_resource_str} {sessions.resource}\n'
post += f'{bot.sessions_statustext_str} {sessions.statustext}\n'
bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility='direct')
else:
post += '0'
bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility=mention.visibility)
print(f"Dismissing notification id {mention.id}")
bot.mastodon.notifications_dismiss(mention.id)
else:
print(f"Dismissing notification id {mention.id}")
bot.mastodon.notifications_dismiss(mention.id)