pixcat/pixcat.py
2023-05-15 16:19:29 +02:00

228 lines
5.2 KiB
Python

from app.libraries.pixelfed import Pixelfed
from app.libraries.setup import Setup
from mastodon import Mastodon
import subprocess
import re
import os
import sys
import os.path
import string
import secrets
import requests
import pdb
def cleanhtml(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
def unescape(s):
s = s.replace("&apos;", "'")
return s
def generate_email():
alphabet = string.ascii_letters + string.digits
while True:
email_address = ''.join(secrets.choice(alphabet) for i in range(6))
if (any(c.islower() for c in email_address)
and any(c.isupper() for c in email_address)
and sum(c.isdigit() for c in email_address) >= 3):
break
email_address = email_address + '@' + setup.mastodon_hostname
return email_address
def generate_pass():
alphabet = string.ascii_letters + string.digits
while True:
password = ''.join(secrets.choice(alphabet) for i in range(10))
if (any(c.islower() for c in password)
and any(c.isupper() for c in password)
and sum(c.isdigit() for c in password) >= 3):
break
return password
def get_data(notif):
notification_id = notif.id
if notif.type != 'mention':
print(f'dismissing notification {notification_id}')
mastodon.notifications_dismiss(notification_id)
return
account_id = notif.account.id
username = notif.account.acct
if '@' in username:
print(f'dismissing notification {notification_id}')
mastodon.notifications_dismiss(notification_id)
return
status_id = notif.status.id
text = notif.status.content
visibility = notif.status.visibility
reply, query_word = replying(text)
if reply:
if query_word == 'registre':
email = generate_email()
user_pass = generate_pass()
#response = pixelfed.pre_flight_check()
#response = pixelfed.email_verification(email, username)
#registered, response = pixelfed.in_app_registration(email, username, user_pass)
registered = register(username, email, user_pass)
if registered:
toot_text = f'@{username} registrat amb èxit!\n\n'
toot_text += f'instància Pixelfed: {pixelfed.api_base_url} \n'
toot_text += f'usuari: {email}\n'
toot_text += f'contrasenya: {user_pass}\n\n'
toot_text += "nota: aquesta contrasenya s'ha generat aleatòriament però l'hauràs de canviar "
toot_text += f"desprès d'iniciar sessió a {pixelfed.api_base_url}\n\n"
toot_text += f"Compte! a {pixelfed.api_base_url} tens assignada una adreça de correu aleatoria i falsa, sota el domini mastodont.cat. "
toot_text += f"L'hauràs de canviar a {pixelfed.api_base_url}/settings/email per a poder rebre notificacions per correu electrònic."
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility='direct')
mastodon.notifications_dismiss(notification_id)
else:
toot_text = f'@{username} error en el registre!\n\n'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
mastodon.notifications_dismiss(notification_id)
else:
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
mastodon.notifications_dismiss(notification_id)
def replying(text):
reply = False
content = cleanhtml(text)
content = unescape(content)
try:
start = content.index("@")
end = content.index(" ")
if len(content) > end:
content = content[0: start:] + content[end +1::]
cleanit = content.count('@')
i = 0
while i < cleanit :
start = content.rfind("@")
end = len(content)
content = content[0: start:] + content[end +1::]
i += 1
content = content.lower()
query_word = content
if query_word == 'registre':
reply = True
except:
pass
return (reply, query_word)
def register(username, email, password):
result = False
try:
pixelfed_new_user = subprocess.run([
"/usr/bin/php",
"artisan",
"user:create",
f"--name={username}",
f"--username={username}",
f"--email={email}",
f"--password={password}",
"--confirm_email=true"
],
cwd=pixelfed.pixelfed_path,
check=True
)
if pixelfed_new_user.returncode == 0:
result = True
except subprocess.CalledProcessError as err:
pass
return result
###############################################################################
# main
if __name__ == '__main__':
setup = Setup()
pixelfed = Pixelfed()
mastodon = Mastodon(
access_token = setup.mastodon_app_token,
api_base_url= setup.mastodon_hostname
)
notifications = mastodon.notifications()
for notif in notifications:
get_data(notif)