From d53c18b4422c06110b26e5cd79b2073b611e6746 Mon Sep 17 00:00:00 2001 From: spla Date: Tue, 9 Aug 2022 11:16:26 +0200 Subject: [PATCH] New Feature! Added bot replies's lang support --- README.md | 3 +++ akkoma.py | 1 - akkomabot.py | 46 +++++++++++++++++++++++++++++++++++++++++----- ejabberdapi.py | 11 +++++------ xmpp.py | 33 +++++++++++++++++---------------- 5 files changed, 66 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index e31484c..6811e8c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ The bot will process any of the above keywords thanks to the wrapper for ejabber - admin_account: the ejabberd admin account, in exemple admin@ejabberd.server - admin_pass: ejabberd admin account password - Akkoma hostname: in ex. akkoma.host +- bot's replies language (ca or en) # Requirements @@ -32,6 +33,8 @@ Before running `python xmpp.py`: Enjoy! +## 9.8.2022 New feature! Added lang support! bot replies's lang is configured during setup. Only ca or en are actually supported. + diff --git a/akkoma.py b/akkoma.py index b0e77c7..1a130bd 100644 --- a/akkoma.py +++ b/akkoma.py @@ -16,7 +16,6 @@ import sys import six from decorator import decorate import hashlib -import pdb ### # Version check functions, including decorator and parser diff --git a/akkomabot.py b/akkomabot.py index 71a0a1f..cb804e1 100644 --- a/akkomabot.py +++ b/akkomabot.py @@ -6,7 +6,6 @@ import fileinput,re import os import sys import os.path -import pdb ### # Dict helper class. @@ -42,6 +41,12 @@ class Akkomabot: self.akkoma, self.akkoma_hostname = self.log_in(self) + bot_file_path = "config/config.txt" + + self.bot_lang = self.get_parameter("bot_lang" , bot_file_path) + + self.load_strings(self) + else: while(True): @@ -54,6 +59,12 @@ class Akkomabot: else: + bot_file_path = "config/config.txt" + + self.bot_lang = self.get_parameter("bot_lang" , bot_file_path) + + self.load_strings(self) + break @staticmethod @@ -102,9 +113,10 @@ class Akkomabot: sys.exit("Bye") - user_name = input("User name, ex. john? ") - user_password = getpass.getpass("User password? ") - app_name = input("App name? ") + user_name = input("Bot user name, ex. john? ") + user_password = getpass.getpass("Bot password? ") + app_name = input("Bot App name? ") + self.bot_lang = input("Bot replies lang (ca or en)? ") client_id, client_secret = Akkoma.create_app( app_name, @@ -221,6 +233,28 @@ class Akkomabot: print(f'{file_path} Missing parameter {parameter}') sys.exit(0) + @staticmethod + def load_strings(self): + + lang_file_path = f"app/locales/{self.bot_lang}.txt" + + self.register_str = self.get_parameter("register_str", lang_file_path) + self.unregister_str = self.get_parameter("unregister_str", lang_file_path) + self.stats_str = self.get_parameter("stats_str", lang_file_path) + self.registerok_str = self.get_parameter("registerok_str", lang_file_path) + self.user_str = self.get_parameter("user_str", lang_file_path) + self.password_str = self.get_parameter("password_str", lang_file_path) + self.server_str = self.get_parameter("server_str", lang_file_path) + self.xmpp_account_str = self.get_parameter("xmpp_account_str", lang_file_path) + self.notdeleteadmin_str = self.get_parameter("notdeleteadmin_str", lang_file_path) + self.deleted_str = self.get_parameter("deleted_str", lang_file_path) + self.stats_title_str = self.get_parameter("stats_title_str", lang_file_path) + self.registered_users_str = self.get_parameter("registered_users_str", lang_file_path) + self.users_online_str = self.get_parameter("users_online_str", lang_file_path) + self.node_users_str = self.get_parameter("node_users_str", lang_file_path) + self.uptime_str = self.get_parameter("uptime_str", lang_file_path) + self.processes_str = self.get_parameter("processes_str", lang_file_path) + @staticmethod def modify_file(self, file_name, pattern,value=""): @@ -253,6 +287,7 @@ class Akkomabot: the_file.write('akkoma_hostname: \n') print(f"adding parameter 'akkoma_hostname' to {self.config_filepath}") + print(f"adding parameter 'bot_lang' to {self.config_filepath}") @staticmethod def read_config_line(self): @@ -261,6 +296,7 @@ class Akkomabot: line = fp.readline() self.modify_file(self, self.config_filepath, "akkoma_hostname: ", value=self.akkoma_hostname) + self.modify_file(self, self.config_filepath, "bot_lang: ", value=self.bot_lang) def get_data(self, notif): @@ -319,7 +355,7 @@ class Akkomabot: #keyword_length = 8 keyword = question - if keyword == 'registre' or keyword == 'baixa' or keyword == 'info': + if keyword == self.register_str or keyword == self.unregister_str or keyword == self.stats_str: keyword_length = len(keyword) diff --git a/ejabberdapi.py b/ejabberdapi.py index 8febc78..4f38802 100644 --- a/ejabberdapi.py +++ b/ejabberdapi.py @@ -5,7 +5,6 @@ import string import getpass import secrets from collections import OrderedDict -import pdb ### # Dict helper class. @@ -86,11 +85,13 @@ class Ejabberd: is_unregistered = False + is_admin = False + if username+'@'+host == self.admin_account: - message = "ets l'admin, no puc esborrar el teu compte!" + is_admin = True - return (is_unregistered, message) + return (is_unregistered, is_admin) data = {'user':username, 'host':self.local_vhost, @@ -102,9 +103,7 @@ class Ejabberd: is_unregistered = response.ok - message = "eliminat amb èxit!" - - return (is_unregistered, message) + return (is_unregistered, is_admin) def stats(self): diff --git a/xmpp.py b/xmpp.py index 199fdaa..7aa07a0 100644 --- a/xmpp.py +++ b/xmpp.py @@ -1,6 +1,5 @@ from akkomabot import Akkomabot from ejabberdapi import Ejabberd -import pdb # main @@ -26,7 +25,7 @@ if __name__ == '__main__': if mention.reply and '@' not in mention.acct: - if mention.question == 'registre': + if mention.question == bot.register_str: password = ejabberd.generate_pass() @@ -34,9 +33,9 @@ if __name__ == '__main__': if is_registered: - post = f"@{mention.acct} compte xmpp registrat amb èxit!\n\nusuari: {mention.acct}@{bot.akkoma_hostname}\n" + post = f"@{mention.acct} {bot.registerok_str}\n\n{bot.user_str} {mention.acct}@{bot.akkoma_hostname}\n" - post += f"contrasenya: {password}\nservidor: {bot.akkoma_hostname}" + 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') @@ -44,33 +43,35 @@ if __name__ == '__main__': bot.akkoma.status_post(f'@{mention.acct}, {text}', in_reply_to_id=mention.status_id, visibility='direct') - elif mention.question == 'baixa': + elif mention.question == bot.unregister_str: - is_unregistered, message = ejabberd.unregister(mention.acct, bot.akkoma_hostname) + is_unregistered, is_admin = ejabberd.unregister(mention.acct, bot.akkoma_hostname) if is_unregistered: - bot.akkoma.status_post(f"@{mention.acct}, compte xmpp {mention.acct}@{bot.akkoma_hostname}: {message}", in_reply_to_id=mention.status_id, visibility='direct') + 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') - else: + elif not is_unregistered: - bot.akkoma.status_post(f'@{mention.acct}, {message}', in_reply_to_id=mention.status_id, visibility='direct') + if is_admin: - elif mention.question == 'info': + 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}, estadístiques del node #xmpp a {bot.akkoma_hostname}:\n\n' + post = f'@{mention.acct}, {bot.stats_title_str} {bot.akkoma_hostname}:\n\n' - post += f'usuaris registrats: {stats.registeredusers}\n' + post += f'{bot.registered_users_str} {stats.registeredusers}\n' - post += f'usuaris en línia: {stats.onlineusers}\n' + post += f'{bot.users_online_str} {stats.onlineusers}\n' - post += f'usuaris del node: {stats.onlineusersnode}\n' + post += f'{bot.node_users_str} {stats.onlineusersnode}\n' - post += f'temps en línia (uptime): {stats.uptimeseconds}\n' + post += f'{bot.uptime_str} {stats.uptimeseconds}\n' - post += f'processos: {stats.processes}\n' + post += f'{bot.processes_str} {stats.processes}\n' bot.akkoma.status_post(post, in_reply_to_id=mention.status_id, visibility=mention.visibility)