Changed some vars visibility and Fix #3

master
spla 4 months ago
parent 0e854876ad
commit da93574d73
  1. 46
      ejabberdapi.py
  2. 16
      xmpp.py

@ -34,14 +34,14 @@ class Ejabberd:
if is_setup:
self.__api_base_url = self.__get_parameter("api_base_url", self.__ejabberd_config_path)
self.__local_vhost = self.__get_parameter("local_vhost", self.__ejabberd_config_path)
self.__admin_account = self.__get_parameter("admin_account", self.__ejabberd_config_path)
self.__admin_pass = self.__get_parameter("admin_pass", self.__ejabberd_config_path)
self.api_base_url = self.__get_parameter("api_base_url", self.__ejabberd_config_path)
self.local_vhost = self.__get_parameter("local_vhost", self.__ejabberd_config_path)
self.admin_account = self.__get_parameter("admin_account", self.__ejabberd_config_path)
self.admin_pass = self.__get_parameter("admin_pass", self.__ejabberd_config_path)
else:
self.__api_base_url, self.__local_vhost, self.__admin_account, self.__admin_pass = self.setup(self)
self.api_base_url, self.local_vhost, self.admin_account, self.admin_pass = self.setup(self)
def generate_pass(self):
@ -61,10 +61,10 @@ class Ejabberd:
def check_account(self, username, host):
data = {'user':username,
'host':self.__local_vhost,
'host':self.local_vhost,
}
endpoint = self.__api_base_url + '/api/check_account?'
endpoint = self.api_base_url + '/api/check_account?'
response = self.__api_request(endpoint, data)
@ -79,11 +79,11 @@ class Ejabberd:
if not account_exists:
data = {'user':username,
'host':self.__local_vhost,
'host':self.local_vhost,
'password':user_password,
}
endpoint = self.__api_base_url + '/api/register?'
endpoint = self.api_base_url + '/api/register?'
response = self.__api_request(endpoint, data)
@ -111,17 +111,17 @@ class Ejabberd:
is_admin = False
if username+'@'+host == self.__admin_account:
if username == self.admin_account.replace('@'+self.local_vhost, ''):
is_admin = True
return (is_unregistered, is_admin)
data = {'user':username,
'host':self.__local_vhost,
'host':self.local_vhost,
}
endpoint = self.__api_base_url + '/api/unregister?'
endpoint = self.api_base_url + '/api/unregister?'
response = self.__api_request(endpoint, data)
@ -143,7 +143,7 @@ class Ejabberd:
"name": name
}
endpoint = self.__api_base_url + '/api/stats?'
endpoint = self.api_base_url + '/api/stats?'
response = self.__api_request(endpoint, data)
@ -160,7 +160,7 @@ class Ejabberd:
data = {
}
endpoint = self.__api_base_url + '/api/status?'
endpoint = self.api_base_url + '/api/status?'
response = self.__api_request(endpoint, data)
@ -175,10 +175,10 @@ class Ejabberd:
sessions_dict = {}
data = {'user':username,
'host':self.__local_vhost,
'host':self.local_vhost,
}
endpoint = self.__api_base_url + '/api/user_sessions_info?'
endpoint = self.api_base_url + '/api/user_sessions_info?'
response = self.__api_request(endpoint, data)
@ -223,7 +223,7 @@ class Ejabberd:
try:
response = requests.post(url = endpoint, json = data, auth=(self._Ejabberd__admin_account, self._Ejabberd__admin_pass))
response = requests.post(url = endpoint, json = data, auth=(self.admin_account, self.admin_pass))
except Exception as e:
@ -296,10 +296,10 @@ class Ejabberd:
if not os.path.exists('secrets'):
os.makedirs('secrets')
self.__api_base_url = input("api_base_url, in ex. 'http://127.0.0.1:5280': ")
self.__local_vhost = input("local_vhost, in ex. 'ejabberd.server': ")
self.__admin_account = input("admin_account, in ex. 'admin@ejabberd.server': ")
self.__admin_pass = getpass.getpass("admin_pass, in ex. 'my_very_hard_secret_pass': ")
self.api_base_url = input("api_base_url, in ex. 'http://127.0.0.1:5280': ")
self.local_vhost = input("local_vhost, in ex. 'ejabberd.server': ")
self.admin_account = input("admin_account, in ex. 'admin@ejabberd.server': ")
self.admin_pass = getpass.getpass("admin_pass, in ex. 'my_very_hard_secret_pass': ")
if not os.path.exists(self.__ejabberd_config_path):
with open(self.__ejabberd_config_path, 'w'): pass
@ -307,9 +307,9 @@ class Ejabberd:
with open(self.__ejabberd_config_path, 'a') as the_file:
print("Writing ejabberd secrets parameters to " + self.__ejabberd_config_path)
the_file.write(f'api_base_url: {self.__api_base_url}\n'+f'local_vhost: {self.__local_vhost}\n'+f'admin_account: {self.__admin_account}\n'+f'admin_pass: {self.__admin_pass}\n')
the_file.write(f'api_base_url: {self.api_base_url}\n'+f'local_vhost: {self.local_vhost}\n'+f'admin_account: {self.admin_account}\n'+f'admin_pass: {self.admin_pass}\n')
return (self.__api_base_url, self.__local_vhost, self.__admin_account, self.__admin_pass)
return (self.api_base_url, self.local_vhost, self.admin_account, self.admin_pass)
@staticmethod
def __get_parameter(parameter, file_path ):

@ -35,19 +35,19 @@ if __name__ == '__main__':
if mention.question == bot.register_str:
account_exists = ejabberd.check_account(mention.acct, bot.mastodon_hostname)
account_exists = ejabberd.check_account(mention.acct, ejabberd.local_vhost)
if not account_exists:
password = ejabberd.generate_pass()
is_registered, text = ejabberd.register(mention.acct, bot.mastodon_hostname, password)
is_registered, text = ejabberd.register(mention.acct, ejabberd.local_vhost, password)
if is_registered:
post = f"@{mention.acct} {bot.registerok_str}\n\n{bot.user_str} {mention.acct}@{bot.mastodon_hostname}\n"
post = f"@{mention.acct} {bot.registerok_str}\n\n{bot.user_str} {mention.acct}@{ejabberd.local_vhost}\n"
post += f"{bot.password_str} {password}\n{bot.server_str} {bot.mastodon_hostname}"
post += f"{bot.password_str} {password}\n{bot.server_str} {ejabberd.local_vhost}"
bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility='direct')
@ -61,11 +61,11 @@ if __name__ == '__main__':
elif mention.question == bot.unregister_str:
is_unregistered, is_admin = ejabberd.unregister(mention.acct, bot.mastodon_hostname)
is_unregistered, is_admin = ejabberd.unregister(mention.acct, ejabberd.local_vhost)
if is_unregistered:
bot.mastodon.status_post(f"@{mention.acct}, {bot.xmpp_account_str} {mention.acct}@{bot.mastodon_hostname}: {bot.deleted_str}", in_reply_to_id=mention.status_id, visibility='direct')
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')
elif not is_unregistered:
@ -77,7 +77,7 @@ if __name__ == '__main__':
stats = ejabberd.stats()
post = f'@{mention.acct}, {bot.stats_title_str} {ejabberd._Ejabberd__local_vhost}:\n\n'
post = f'@{mention.acct}, {bot.stats_title_str} {ejabberd.local_vhost}:\n\n'
post += f'{bot.registered_users_str} {stats.registeredusers}\n'
@ -101,7 +101,7 @@ if __name__ == '__main__':
elif mention.question == bot.user_sessions_info_str:
sessions = ejabberd.user_sessions_info(mention.acct, bot.mastodon_hostname)
sessions = ejabberd.user_sessions_info(mention.acct, ejabberd.local_vhost)
post = f'@{mention.acct}, {bot.current_sessions_str}\n\n'

Loading…
Cancel
Save