xmpp/xmpp.py

137 lines
4.9 KiB
Python

from akkomabot import Akkomabot
from ejabberdapi import Ejabberd, EjabberdAPIConfigError
from akkoma import AkkomaNetworkError
import sys
# main
if __name__ == '__main__':
bot = Akkomabot()
ejabberd = Ejabberd()
try:
notifications = bot.akkoma.notifications()
except AkkomaNetworkError as net_error:
sys.exit(net_error)
for notif in notifications:
if notif.type != 'mention':
print(f"Dismissing notification id {notif.id}")
bot.akkoma.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.akkoma_hostname)
if not account_exists:
password = ejabberd.generate_pass()
is_registered, text = ejabberd.register(mention.acct, bot.akkoma_hostname, password)
if is_registered:
post = f"@{mention.acct} {bot.registerok_str}\n\n{bot.user_str} {mention.acct}@{bot.akkoma_hostname}\n"
post += f"{bot.password_str} {password}\n{bot.server_str} {bot.akkoma_hostname}"
bot.akkoma.status_post(post, in_reply_to_id=mention.status_id, visibility='direct')
else:
bot.akkoma.status_post(f'@{mention.acct}, {text}', in_reply_to_id=mention.status_id, visibility='direct')
else:
bot.akkoma.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.akkoma_hostname)
if is_unregistered:
bot.akkoma.status_post(f"@{mention.acct}, {bot.xmpp_account_str} {mention.acct}@{bot.akkoma_hostname}: {bot.deleted_str}", in_reply_to_id=mention.status_id, visibility='direct')
elif not is_unregistered:
if is_admin:
bot.akkoma.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} {bot.akkoma_hostname}:\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.akkoma.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.akkoma.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.akkoma_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.akkoma.status_post(post, in_reply_to_id=mention.status_id, visibility='direct')
else:
post += '0'
bot.akkoma.status_post(post, in_reply_to_id=mention.status_id, visibility=mention.visibility)
print(f"Dismissing notification id {mention.id}")
bot.akkoma.notifications_dismiss(mention.id)
else:
print(f"Dismissing notification id {mention.id}")
bot.akkoma.notifications_dismiss(mention.id)