2024-04-03 19:08:28 +02:00
|
|
|
from app.libraries.setup import Setup
|
|
|
|
from app.libraries.mentions import Mentions
|
|
|
|
from app.libraries.ejabberdapi import Ejabberd
|
|
|
|
from app.libraries.strings import Strings
|
|
|
|
from mastodon import Mastodon, StreamListener
|
2022-08-18 13:16:56 +02:00
|
|
|
import sys
|
2024-04-03 13:22:39 +02:00
|
|
|
import humanize
|
|
|
|
import datetime as dt
|
2024-04-03 19:08:28 +02:00
|
|
|
import pdb
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
def check_activity(username):
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
active = False
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
account_id = mastodon.account_lookup(username).id
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
act_st = mastodon.account_statuses(account_id)
|
|
|
|
|
|
|
|
active_post = 0
|
|
|
|
|
|
|
|
for status in act_st:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
if status.created_at.replace(tzinfo=None) > datetime.now() - timedelta(days=30):
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
active_post += 1
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
if active_post >= 3:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
active = True
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
return active
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
class Listener(StreamListener):
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
def on_notification(self, notification):
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
if notification.type != 'mention':
|
|
|
|
|
|
|
|
return
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
qry = Mentions(mastodon)
|
|
|
|
|
|
|
|
reply, query_word, username, status_id, visibility = qry.get_data(notification)
|
|
|
|
|
|
|
|
if reply:
|
|
|
|
|
|
|
|
if query_word == strings.register_str:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
active = check_activity(username)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
if active:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
account_exists = ejabberd.check_account(username, ejabberd.local_vhost)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
if not account_exists:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
password = ejabberd.generate_pass()
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
is_registered, text = ejabberd.register(username, ejabberd.local_vhost, password)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
if is_registered:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post = f"@{username} {strings.registerok_str}\n\n{strings.user_str} {username}@{ejabberd.local_vhost}\n"
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post += f"{strings.password_str} {password}\n{strings.server_str} {ejabberd.local_vhost}"
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon.status_post(post, in_reply_to_id=status_id, visibility='direct')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
mastodon.status_post(f'@{username}, {text}', in_reply_to_id=status_id, visibility='direct')
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon.status_post(f'@{username}, {strings.account_exists_str}', in_reply_to_id=status_id, visibility='direct')
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
toot_text = f'@{username},\n'
|
|
|
|
|
|
|
|
toot_text += 'no hi ha activitat en el darrer mes, el registre no és pot dur a terme.'
|
|
|
|
|
|
|
|
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
elif query_word == strings.unregister_str:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
is_unregistered, is_admin = ejabberd.unregister(username, ejabberd.local_vhost)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
if is_unregistered:
|
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon.status_post(f"@{username}, {strings.xmpp_account_str} {username}@{ejabberd.local_vhost}: {strings.deleted_str}", in_reply_to_id=status_id, visibility='direct')
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
elif not is_unregistered:
|
|
|
|
|
|
|
|
if is_admin:
|
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon.status_post(f'@{username}, {strings.notdeleteadmin_str}', in_reply_to_id=status_id, visibility='direct')
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
elif query_word == strings.stats_str:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
stats = ejabberd.stats()
|
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post = f'@{username}, {strings.stats_title_str} {ejabberd.local_vhost}:\n\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post += f'{strings.registered_users_str} {stats.registeredusers}\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post += f'{strings.users_online_str} {stats.onlineusers}\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post += f'{strings.node_users_str} {stats.onlineusersnode}\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post += f'{strings.uptime_str} {humanize.naturaldelta(dt.timedelta(seconds=stats.uptimeseconds))}\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post += f'{strings.processes_str} {stats.processes}\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon.status_post(post, in_reply_to_id=status_id, visibility=visibility)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
elif query_word == strings.status_str:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
status = ejabberd.status()
|
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post = f'@{username}, {status}\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon.status_post(post, in_reply_to_id=status_id, visibility=visibility)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
elif query_word == strings.user_sessions_info_str:
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
sessions = ejabberd.user_sessions_info(username, ejabberd.local_vhost)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post = f'@{username}, {strings.current_sessions_str}\n\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
if len(sessions) != 0:
|
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
post += f'{strings.sessions_connection_str} {sessions.connection}\n'
|
|
|
|
post += f'{strings.sessions_ip_str} {sessions.ip}\n'
|
|
|
|
post += f'{strings.sessions_port_str} {sessions.port}\n'
|
|
|
|
post += f'{strings.sessions_priority_str} {sessions.priority}\n'
|
|
|
|
post += f'{strings.sessions_node_str} {sessions.node}\n'
|
|
|
|
post += f'{strings.sessions_uptime_str} {sessions.uptime}\n'
|
|
|
|
post += f'{strings.sessions_status_str} {sessions.status}\n'
|
|
|
|
post += f'{strings.sessions_resource_str} {sessions.resource}\n'
|
|
|
|
post += f'{strings.sessions_statustext_str} {sessions.statustext}\n'
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon.status_post(post, in_reply_to_id=status_id, visibility='direct')
|
2022-08-18 13:16:56 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
post += '0'
|
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon.status_post(post, in_reply_to_id=status_id, visibility=visibility)
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
# main
|
|
|
|
if __name__ == '__main__':
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
setup = Setup()
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon = Mastodon(
|
|
|
|
access_token = setup.mastodon_app_token,
|
|
|
|
api_base_url= setup.mastodon_hostname
|
|
|
|
)
|
|
|
|
|
|
|
|
ejabberd = Ejabberd()
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
strings = Strings()
|
2022-08-18 13:16:56 +02:00
|
|
|
|
2024-04-03 19:08:28 +02:00
|
|
|
mastodon.stream_user(Listener())
|