From 40bbd00ebb91837905dd4f1094548c576e870a93 Mon Sep 17 00:00:00 2001 From: spla Date: Thu, 18 Aug 2022 13:16:56 +0200 Subject: [PATCH] Added requerimentx.txt and the listen bot, xmpp.py --- requirements.txt | 2 + xmpp.py | 136 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 requirements.txt create mode 100644 xmpp.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bdfcd87 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Mastodon.py +unidecode diff --git a/xmpp.py b/xmpp.py new file mode 100644 index 0000000..6747f5c --- /dev/null +++ b/xmpp.py @@ -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)