2023-12-28 15:50:23 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
TODO
|
|
|
|
|
|
|
|
1) Send message to inviter that bot has joined to groupchat.
|
|
|
|
|
|
|
|
2) If groupchat requires captcha, send the consequent message.
|
|
|
|
|
|
|
|
3) If groupchat error is received, send that error message to inviter.
|
|
|
|
|
2024-02-04 18:08:12 +01:00
|
|
|
FIXME
|
|
|
|
|
|
|
|
1) Save name of groupchat instead of jid as name
|
2024-01-26 12:34:07 +01:00
|
|
|
|
2023-12-28 15:50:23 +01:00
|
|
|
"""
|
2024-01-03 11:37:33 +01:00
|
|
|
import logging
|
2023-12-28 15:50:23 +01:00
|
|
|
|
2024-02-07 01:26:42 +01:00
|
|
|
class XmppGroupchat:
|
2023-12-28 15:50:23 +01:00
|
|
|
|
2024-02-07 01:26:42 +01:00
|
|
|
# async def accept_muc_invite(self, message, ctr=None):
|
|
|
|
# # if isinstance(message, str):
|
|
|
|
# if not ctr:
|
|
|
|
# ctr = message["from"].bare
|
|
|
|
# jid = message['groupchat_invite']['jid']
|
|
|
|
# else:
|
|
|
|
# jid = message
|
2024-02-14 18:09:54 +01:00
|
|
|
def accept_invitation(self, message):
|
2024-02-07 01:26:42 +01:00
|
|
|
# operator muc_chat
|
|
|
|
inviter = message["from"].bare
|
2024-02-14 18:09:54 +01:00
|
|
|
jid = message['groupchat_invite']['jid']
|
|
|
|
self.join(self, inviter, jid)
|
2024-02-07 01:26:42 +01:00
|
|
|
|
|
|
|
|
2024-02-14 18:09:54 +01:00
|
|
|
def autojoin(self, bookmarks):
|
|
|
|
conferences = bookmarks["private"]["bookmarks"]["conferences"]
|
2024-02-07 01:26:42 +01:00
|
|
|
for conference in conferences:
|
2024-02-14 18:09:54 +01:00
|
|
|
if conference["jid"] and conference["autojoin"]:
|
|
|
|
if not conference["nick"]:
|
|
|
|
conference["nick"] = self.alias
|
|
|
|
logging.error('Alias (i.e. Nicknname) is missing for '
|
|
|
|
'bookmark {}'.format(conference['name']))
|
2024-03-12 18:13:01 +01:00
|
|
|
# jid_from = str(self.boundjid) if self.is_component else None
|
2024-02-14 18:09:54 +01:00
|
|
|
self.plugin['xep_0045'].join_muc(conference["jid"],
|
2024-02-07 01:26:42 +01:00
|
|
|
conference["nick"],
|
2024-03-12 18:13:01 +01:00
|
|
|
# pfrom=jid_from,
|
2024-02-07 01:26:42 +01:00
|
|
|
# If a room password is needed, use:
|
|
|
|
# password=the_room_password,
|
|
|
|
)
|
|
|
|
logging.info('Autojoin groupchat\n'
|
|
|
|
'Name : {}\n'
|
|
|
|
'JID : {}\n'
|
|
|
|
'Alias : {}\n'
|
|
|
|
.format(conference["name"],
|
2024-02-14 18:09:54 +01:00
|
|
|
conference["jid"],
|
2024-02-07 01:26:42 +01:00
|
|
|
conference["nick"]))
|
2024-02-14 18:09:54 +01:00
|
|
|
elif not conference["jid"]:
|
|
|
|
logging.error('JID is missing for bookmark {}'
|
|
|
|
.format(conference['name']))
|
2024-02-07 01:26:42 +01:00
|
|
|
|
|
|
|
|
2024-02-14 18:09:54 +01:00
|
|
|
def join(self, inviter, jid):
|
2024-02-07 01:26:42 +01:00
|
|
|
# token = await initdb(
|
|
|
|
# muc_jid,
|
2024-02-16 13:12:06 +01:00
|
|
|
# sqlite.get_setting_value,
|
2024-02-07 01:26:42 +01:00
|
|
|
# "token"
|
|
|
|
# )
|
|
|
|
# if token != "accepted":
|
|
|
|
# token = randrange(10000, 99999)
|
|
|
|
# await initdb(
|
|
|
|
# muc_jid,
|
2024-02-16 13:12:06 +01:00
|
|
|
# sqlite.update_setting_value,
|
2024-02-07 01:26:42 +01:00
|
|
|
# ["token", token]
|
|
|
|
# )
|
|
|
|
# self.send_message(
|
|
|
|
# mto=inviter,
|
|
|
|
# mfrom=self.boundjid.bare,
|
|
|
|
# mbody=(
|
|
|
|
# "Send activation token {} to groupchat xmpp:{}?join."
|
|
|
|
# ).format(token, muc_jid)
|
|
|
|
# )
|
|
|
|
logging.info('Joining groupchat\n'
|
|
|
|
'JID : {}\n'
|
|
|
|
'Inviter : {}\n'
|
2024-02-14 18:09:54 +01:00
|
|
|
.format(jid, inviter))
|
2024-03-12 18:13:01 +01:00
|
|
|
jid_from = str(self.boundjid) if self.is_component else None
|
2024-02-14 18:09:54 +01:00
|
|
|
self.plugin['xep_0045'].join_muc(jid,
|
2024-02-07 01:26:42 +01:00
|
|
|
self.alias,
|
2024-03-12 18:13:01 +01:00
|
|
|
pfrom=jid_from
|
2024-02-07 01:26:42 +01:00
|
|
|
# If a room password is needed, use:
|
|
|
|
# password=the_room_password,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-14 18:09:54 +01:00
|
|
|
def leave(self, jid):
|
2024-02-12 20:01:28 +01:00
|
|
|
message = ('This news bot will now leave this groupchat.\n'
|
|
|
|
'The JID of this news bot is xmpp:{}?message'
|
2024-02-14 18:09:54 +01:00
|
|
|
.format(self.boundjid.bare))
|
2024-02-12 20:01:28 +01:00
|
|
|
status_message = ('This bot has left the group. '
|
|
|
|
'It can be reached directly via {}'
|
2024-02-14 18:09:54 +01:00
|
|
|
.format(self.boundjid.bare))
|
|
|
|
self.send_message(mto=jid,
|
|
|
|
mfrom=self.boundjid,
|
2024-02-12 20:01:28 +01:00
|
|
|
mbody=message,
|
|
|
|
mtype='groupchat')
|
2024-02-14 18:09:54 +01:00
|
|
|
self.plugin['xep_0045'].leave_muc(jid,
|
2024-02-07 01:26:42 +01:00
|
|
|
self.alias,
|
2024-02-12 20:01:28 +01:00
|
|
|
status_message,
|
2024-02-14 18:09:54 +01:00
|
|
|
self.boundjid)
|