1
0
Fork 0
forked from sch/KaikOut
KaikOut/kaikout/xmpp/groupchat.py
2024-11-21 21:15:48 +02:00

52 lines
2.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from kaikout.xmpp.message import XmppMessage
from kaikout.xmpp.muc import XmppMuc
from kaikout.xmpp.status import XmppStatus
from kaikout.utilities import Toml
from kaikout.log import Logger, Message
import random
logger = Logger(__name__)
class XmppGroupchat:
async def join(self, room):
result = await XmppMuc.join(self, room)
if result == 'ban':
message_body = '{} is banned from {}'.format(self.alias, room)
jid_bare = message['from'].bare
XmppMessage.send(self, jid_bare, message_body, 'chat')
logger.warning(message_body)
elif result == 'conflict':
while result == 'conflict':
number = str(random.randrange(1000, 5000))
print(f'Conflict. Atempting to join to {room} as {self.alias} #{number}')
result = await XmppMuc.join(self, room, f'{self.alias} #{number}')
else:
#self.bookmarks.append({'jid' : room, 'lang' : '', 'pass' : ''})
if room not in self.bookmarks: self.bookmarks.append(room)
Toml.save_file(self.filename_bookmarks, self.data_bookmarks)
return result
async def autojoin(self):
mucs_joined = []
for room in self.bookmarks:
alias = self.alias
print(f'Joining to MUC {room} ...')
#Message.printer(f'Joining to MUC {room} ...')
result = await XmppMuc.join(self, room)
if result == 'ban':
if room in self.bookmarks: self.bookmarks.remove(room)
Toml.save_file(self.filename_bookmarks, self.data_bookmarks)
logger.warning(f'{alias} is banned from {room}')
logger.warning(f'Groupchat {room} has been removed from bookmarks')
elif result == 'conflict':
while result == 'conflict':
number = str(random.randrange(1000, 5000))
print(f'Conflict. Atempting to join to {room} as {self.alias} #{number}')
result = await XmppMuc.join(self, room, f'{alias} #{number}')
else:
mucs_joined.append(room)
return mucs_joined