# Slixmpp: The Slick XMPP Library# Copyright (C) 2011 Nathanael C. Fritz, Dalek# This file is part of Slixmpp.# See the file LICENSE for copying permission.importloggingfromtypingimportOptionalimportslixmppfromslixmppimportMessage,JIDfromslixmpp.pluginsimportBasePluginfromslixmpp.xmlstreamimportregister_stanza_pluginfromslixmpp.xmlstream.handlerimportCallbackfromslixmpp.xmlstream.matcherimportStanzaPathfromslixmpp.plugins.xep_0249importInvite,stanzalog=logging.getLogger(__name__)
[docs]classXEP_0249(BasePlugin):""" XEP-0249: Direct MUC Invitations """name='xep_0249'description='XEP-0249: Direct MUC Invitations'dependencies={'xep_0030'}stanza=stanzadefplugin_init(self):self.xmpp.register_handler(Callback('Direct MUC Invitations',StanzaPath('message/groupchat_invite'),self._handle_invite))register_stanza_plugin(Message,Invite)defplugin_end(self):self.xmpp['xep_0030'].del_feature(feature=Invite.namespace)self.xmpp.remove_handler('Direct MUC Invitations')defsession_bind(self,jid):self.xmpp['xep_0030'].add_feature(Invite.namespace)def_handle_invite(self,msg:Message):""" Raise an event for all invitations received. """log.debug("Received direct muc invitation from %s to room %s",msg['from'],msg['groupchat_invite']['jid'])self.xmpp.event('groupchat_direct_invite',msg)
[docs]defsend_invitation(self,jid:JID,roomjid:JID,password:Optional[str]=None,reason:Optional[str]=None,*,mfrom:Optional[JID]=None):""" Send a direct MUC invitation to an XMPP entity. :param JID jid: The JID of the entity that will receive the invitation :param JID roomjid: the address of the groupchat room to be joined :param str password: a password needed for entry into a password-protected room (OPTIONAL). :param str reason: a human-readable purpose for the invitation (OPTIONAL). """msg=self.xmpp.Message()msg['to']=jidifmfromisnotNone:msg['from']=mfrommsg['groupchat_invite']['jid']=roomjidifpasswordisnotNone:msg['groupchat_invite']['password']=passwordifreasonisnotNone:msg['groupchat_invite']['reason']=reasonreturnmsg.send()