xmppbot.py/xmpp.py
2024-04-03 13:22:39 +02:00

138 líneas
5 KiB
Python

from mastodonbot import Mastodonbot
from ejabberdapi import Ejabberd
from mastodon import MastodonNetworkError
import sys
import humanize
import datetime as dt
# 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, ejabberd.local_vhost)
if not account_exists:
password = ejabberd.generate_pass()
is_registered, text = ejabberd.register(mention.acct, ejabberd.local_vhost, password)
if is_registered:
post = f"@{mention.acct} {bot.registerok_str}\n\n{bot.user_str} {mention.acct}@{ejabberd.local_vhost}\n"
post += f"{bot.password_str} {password}\n{bot.server_str} {ejabberd.local_vhost}"
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, ejabberd.local_vhost)
if is_unregistered:
bot.mastodon.status_post(f"@{mention.acct}, {bot.xmpp_account_str} {mention.acct}@{ejabberd.local_vhost}: {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.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} {humanize.naturaldelta(dt.timedelta(seconds=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, ejabberd.local_vhost)
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)