diff --git a/xmpp_tg/message_handlers.py b/xmpp_tg/message_handlers.py index 0c46828..82cef95 100644 --- a/xmpp_tg/message_handlers.py +++ b/xmpp_tg/message_handlers.py @@ -35,7 +35,7 @@ class MessageHandler(): xmpp.send_message(mto=self.replyto, mtype=self.type, mbody=text) def debug(self, *args, **kwargs): - """nolistnolist Show debug info""" + """nolist Show debug info""" return pprint.pformat(self.__dict__) def help(self, *args, **kwargs): @@ -75,7 +75,7 @@ class MessageHandler(): docstring = "No description available" if docstring.startswith("nolist "): continue - reply += "\n!{command} - {doc}".format(method, docstring.split("\n")[0] + reply += "\n!{command} - {doc}".format(method, docstring.split("\n")[0]) return reply else: method = getattr(self,self.arguments[0]) @@ -362,3 +362,74 @@ class ChatCommandHandler(MessageHandler): else: del(self.tg_dialogs[hndl.jid]['messages'][hndl.tg_id]) self.tg_connections[hndl.jid].invoke( DeleteMessagesRequest([msg_id], revoke = True) ) + +class GroupchatCommandHandler(MessageHandler): + _helptext = "Available Groupchat commands:" + + def __init__(self, msg): + super(GroupchatCommandHandler, self).__init__(msg) + if self._command.startswith("s/"): + self._handler = self._replace + + def leave(hndl, self): + """Leave group or channel""" + tg_id = int(hndl.msg['to'].node[1:]) + if tg_id in self.tg_dialogs[hndl.jid]['supergroups']: + peer = InputPeerChannel(tg_id, self.tg_dialogs[hndl.jid]['supergroups'][tg_id].access_hash) + self.tg_connections[hndl.jid].invoke(LeaveChannelRequest(peer)) + self.tg_connections[hndl.jid].invoke(DeleteHistoryRequest(peer, max_id = 0, just_clear = None)) + c_jid = get_contact_jid(self.tg_dialogs[hndl.jid]['supergroups'][tg_id], self.boundjid.bare) + self.send_presence(pto =hndl.jid, pfrom = c_jid, ptype ='unavailable') + self.send_presence(pto =hndl.jid, pfrom = c_jid, ptype ='unsubscribed') + self.send_presence(pto =hndl.jid, pfrom = c_jid, ptype ='unsubscribe') + if tg_id in self.tg_dialogs[hndl.jid]['groups']: + self.tg_connections[hndl.jid].invoke(DeleteChatUserRequest(tg_id, self.tg_connections[hndl.jid].me)) + self.tg_connections[hndl.jid].invoke(DeleteHistoryRequest(InputPeerChat(tg_id), max_id = 0, just_clear = None)) + c_jid = get_contact_jid(self.tg_dialogs[hndl.jid]['groups'][tg_id], self.boundjid.bare) + self.send_presence(pto =hndl.jid, pfrom = c_jid, ptype ='unavailable') + self.send_presence(pto =hndl.jid, pfrom = c_jid, ptype ='unsubscribed') + self.send_presence(pto =hndl.jid, pfrom = c_jid, ptype ='unsubscribe') + + def invite(hndl, self): + """Invite user to group""" + tg_id = int(hndl.msg['to'].node[1:]) + if tg_id in self.tg_dialogs[hndl.jid]['supergroups']: + invited_user = self.tg_connections[[hndl.jid]].get_entity(parsed[1]) + if type(invited_user) == User: + self.tg_connections[[hndl.jid]].invoke(EditBannedRequest(InputPeerChannel(tg_id, self.tg_dialogs[[hndl.jid]]['supergroups'][tg_id].access_hash), invited_user, ChannelBannedRights(until_date=None, view_messages=False))) + self.tg_connections[[hndl.jid]].invoke(InviteToChannelRequest(InputPeerChannel(tg_id, self.tg_dialogs[[hndl.jid]]['supergroups'][tg_id].access_hash), [invited_user])) + if tg_id in self.tg_dialogs[[hndl.jid]]['groups']: + invited_user = self.tg_connections[[hndl.jid]].get_entity(parsed[1]) + if type(invited_user) == User: + self.tg_connections[[hndl.jid]].invoke(AddChatUserRequest(tg_id, invited_user, 0)) + + def kick(hndl, self): + """Kick user""" + tg_id = int(hndl.msg['to'].node[1:]) + if tg_id in self.tg_dialogs[hndl.jid]['supergroups']: + kicked_user = self.tg_connections[hndl.jid].get_entity(parsed[1]) + if type(kicked_user) == User: + self.tg_connections[hndl.jid].invoke(EditBannedRequest(InputPeerChannel(tg_id, self.tg_dialogs[hndl.jid]['supergroups'][tg_id].access_hash), kicked_user, ChannelBannedRights(until_date=None, view_messages=True))) + if tg_id in self.tg_dialogs[hndl.jid]['groups']: + kicked_user = self.tg_connections[hndl.jid].get_entity(parsed[1]) + if type(kicked_user) == User: + self.tg_connections[hndl.jid].invoke(DeleteChatUserRequest(tg_id, kicked_user)) + + def _replace(hndl, self): + tg_id = int(hndl.msg['to'].node[1:]) + peer = InputPeerChannel(tg_id, self.tg_dialogs[hndl.jid]['supergroups'][tg_id].access_hash) if tg_id in self.tg_dialogs[ + hndl.jid]['supergroups'] else InputPeerChat(tg_id) + + msg_id, edited = self.edit_message(hndl.jid, tg_id, iq['body']) + if not edited: return + + # and send it + if edited != '' and edited != ' ': + self.tg_dialogs[hndl.jid]['messages'][tg_id]["body"] = edited + self.tg_connections[hndl.jid].invoke(EditMessageRequest(peer, msg_id, message = edited)) + else: + del(self.tg_dialogs[hndl.jid]['messages'][tg_id]) + if isinstance(peer, InputPeerChannel): + self.tg_connections[hndl.jid].invoke(DeleteMessagesChannel(peer, [msg_id])) + else: + self.tg_connections[hndl.jid].invoke(DeleteMessagesRequest([msg_id], revoke = True))