New feature! ejabberd's users_sessions_info endpoint!

This commit is contained in:
spla 2022-08-10 20:27:23 +02:00
pare e00c96263b
commit 3fdca1b99b
S'han modificat 3 arxius amb 84 adicions i 1 eliminacions

Veure arxiu

@ -256,6 +256,17 @@ class Akkomabot:
self.uptime_str = self.__get_parameter("uptime_str", lang_file_path)
self.processes_str = self.__get_parameter("processes_str", lang_file_path)
self.account_exists_str = self.__get_parameter("account_exists_str", lang_file_path)
self.user_sessions_info_str = self.__get_parameter("user_sessions_info_str", lang_file_path)
self.current_sessions_str = self.__get_parameter("current_sessions_str", lang_file_path)
self.sessions_connection_str = self.__get_parameter("sessions_connection_str", lang_file_path)
self.sessions_ip_str = self.__get_parameter("sessions_ip_str", lang_file_path)
self.sessions_port_str = self.__get_parameter("sessions_port_str", lang_file_path)
self.sessions_priority_str = self.__get_parameter("sessions_priority_str", lang_file_path)
self.sessions_node_str = self.__get_parameter("sessions_node_str", lang_file_path)
self.sessions_uptime_str = self.__get_parameter("sessions_uptime_str", lang_file_path)
self.sessions_status_str = self.__get_parameter("sessions_status_str", lang_file_path)
self.sessions_resource_str = self.__get_parameter("sessions_resource_str", lang_file_path)
self.sessions_statustext_str = self.__get_parameter("sessions_statustext_str", lang_file_path)
@staticmethod
def __modify_file(self, file_name, pattern,value=""):
@ -357,7 +368,7 @@ class Akkomabot:
#keyword_length = 8
keyword = question
if keyword == self.register_str or keyword == self.unregister_str or keyword == self.stats_str or self.status_str:
if keyword == self.register_str or keyword == self.unregister_str or keyword == self.stats_str or self.status_str or user_sessions_info_str:
keyword_length = len(keyword)

Veure arxiu

@ -168,6 +168,52 @@ class Ejabberd:
return result
def user_sessions_info(self, username, host):
temp_dict = {}
sessions_dict = {}
data = {'user':username,
'host':self.__local_vhost,
}
endpoint = self.__api_base_url + '/api/user_sessions_info?'
response = self.__api_request(endpoint, data)
i = 0
while i < len(response.json()):
temp_dict['connection'] = response.json()[i]['connection']
temp_dict['ip'] = response.json()[i]['ip']
temp_dict['port'] = response.json()[i]['port']
temp_dict['priority'] = response.json()[i]['priority']
temp_dict['node'] = response.json()[i]['node']
temp_dict['uptime'] = response.json()[i]['uptime']
temp_dict['status'] = response.json()[i]['status']
temp_dict['resource'] = response.json()[i]['resource']
temp_dict['statustext'] = response.json()[i]['statustext']
if len(sessions_dict) > 0:
ds = [temp_dict, sessions_dict]
sessions_temp = {}
for k in temp_dict.keys():
sessions_temp[k] = tuple(sessions_temp[k] for sessions_temp in ds)
else:
sessions_dict = temp_dict.copy()
sessions_temp = sessions_dict.copy()
i += 1
sessions = self.__json_allow_dict_attrs(sessions_temp)
return sessions
def __api_request(self, endpoint, data):
try:

26
xmpp.py
Veure arxiu

@ -99,6 +99,32 @@ if __name__ == '__main__':
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=mention.visibility)
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)