2022-08-18 13:16:56 +02:00
|
|
|
from mastodonbot import Mastodonbot
|
|
|
|
from ejabberdapi import Ejabberd
|
|
|
|
from mastodon import MastodonNetworkError
|
|
|
|
import sys
|
2024-04-03 13:22:39 +02:00
|
|
|
import humanize
|
|
|
|
import datetime as dt
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
# 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:
|
|
|
|
|
2022-08-21 10:52:08 +02:00
|
|
|
account_exists = ejabberd.check_account(mention.acct, ejabberd.local_vhost)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
if not account_exists:
|
|
|
|
|
|
|
|
password = ejabberd.generate_pass()
|
|
|
|
|
2022-08-21 10:52:08 +02:00
|
|
|
is_registered, text = ejabberd.register(mention.acct, ejabberd.local_vhost, password)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
if is_registered:
|
|
|
|
|
2022-08-21 10:52:08 +02:00
|
|
|
post = f"@{mention.acct} {bot.registerok_str}\n\n{bot.user_str} {mention.acct}@{ejabberd.local_vhost}\n"
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2022-08-21 10:52:08 +02:00
|
|
|
post += f"{bot.password_str} {password}\n{bot.server_str} {ejabberd.local_vhost}"
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2022-08-21 10:52:08 +02:00
|
|
|
is_unregistered, is_admin = ejabberd.unregister(mention.acct, ejabberd.local_vhost)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
if is_unregistered:
|
|
|
|
|
2022-08-21 10:52:08 +02:00
|
|
|
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')
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2022-08-21 10:52:08 +02:00
|
|
|
post = f'@{mention.acct}, {bot.stats_title_str} {ejabberd.local_vhost}:\n\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
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'
|
|
|
|
|
2024-04-03 13:22:39 +02:00
|
|
|
post += f'{bot.uptime_str} {humanize.naturaldelta(dt.timedelta(seconds=stats.uptimeseconds))}\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2022-08-21 10:52:08 +02:00
|
|
|
sessions = ejabberd.user_sessions_info(mention.acct, ejabberd.local_vhost)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
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)
|