2009-06-03 22:56:51 +00:00
|
|
|
"""
|
2010-07-20 15:19:49 +00:00
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz
|
|
|
|
This file is part of SleekXMPP.
|
2010-11-06 05:28:59 +00:00
|
|
|
|
2010-07-20 15:19:49 +00:00
|
|
|
See the file LICENSE for copying permission.
|
2009-06-03 22:56:51 +00:00
|
|
|
"""
|
|
|
|
from __future__ import with_statement
|
2012-03-12 05:36:37 +00:00
|
|
|
|
2009-06-03 22:56:51 +00:00
|
|
|
import logging
|
2012-03-12 05:36:37 +00:00
|
|
|
|
|
|
|
from sleekxmpp import Presence
|
|
|
|
from sleekxmpp.plugins import BasePlugin, register_plugin
|
|
|
|
from sleekxmpp.xmlstream import register_stanza_plugin, ElementBase, JID, ET
|
|
|
|
from sleekxmpp.xmlstream.handler.callback import Callback
|
|
|
|
from sleekxmpp.xmlstream.matcher.xpath import MatchXPath
|
|
|
|
from sleekxmpp.xmlstream.matcher.xmlmask import MatchXMLMask
|
2011-10-04 14:36:52 +00:00
|
|
|
from sleekxmpp.exceptions import IqError, IqTimeout
|
2009-12-17 01:54:22 +00:00
|
|
|
|
2010-11-06 05:28:59 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2009-12-17 01:54:22 +00:00
|
|
|
class MUCPresence(ElementBase):
|
2010-12-16 21:18:49 +00:00
|
|
|
name = 'x'
|
|
|
|
namespace = 'http://jabber.org/protocol/muc#user'
|
|
|
|
plugin_attrib = 'muc'
|
|
|
|
interfaces = set(('affiliation', 'role', 'jid', 'nick', 'room'))
|
|
|
|
affiliations = set(('', ))
|
|
|
|
roles = set(('', ))
|
|
|
|
|
|
|
|
def getXMLItem(self):
|
|
|
|
item = self.xml.find('{http://jabber.org/protocol/muc#user}item')
|
|
|
|
if item is None:
|
|
|
|
item = ET.Element('{http://jabber.org/protocol/muc#user}item')
|
|
|
|
self.xml.append(item)
|
|
|
|
return item
|
|
|
|
|
|
|
|
def getAffiliation(self):
|
|
|
|
#TODO if no affilation, set it to the default and return default
|
|
|
|
item = self.getXMLItem()
|
|
|
|
return item.get('affiliation', '')
|
|
|
|
|
|
|
|
def setAffiliation(self, value):
|
|
|
|
item = self.getXMLItem()
|
|
|
|
#TODO check for valid affiliation
|
|
|
|
item.attrib['affiliation'] = value
|
|
|
|
return self
|
|
|
|
|
|
|
|
def delAffiliation(self):
|
|
|
|
item = self.getXMLItem()
|
|
|
|
#TODO set default affiliation
|
|
|
|
if 'affiliation' in item.attrib: del item.attrib['affiliation']
|
|
|
|
return self
|
|
|
|
|
|
|
|
def getJid(self):
|
|
|
|
item = self.getXMLItem()
|
|
|
|
return JID(item.get('jid', ''))
|
|
|
|
|
|
|
|
def setJid(self, value):
|
|
|
|
item = self.getXMLItem()
|
|
|
|
if not isinstance(value, str):
|
|
|
|
value = str(value)
|
|
|
|
item.attrib['jid'] = value
|
|
|
|
return self
|
|
|
|
|
|
|
|
def delJid(self):
|
|
|
|
item = self.getXMLItem()
|
|
|
|
if 'jid' in item.attrib: del item.attrib['jid']
|
|
|
|
return self
|
|
|
|
|
|
|
|
def getRole(self):
|
|
|
|
item = self.getXMLItem()
|
|
|
|
#TODO get default role, set default role if none
|
|
|
|
return item.get('role', '')
|
|
|
|
|
|
|
|
def setRole(self, value):
|
|
|
|
item = self.getXMLItem()
|
|
|
|
#TODO check for valid role
|
|
|
|
item.attrib['role'] = value
|
|
|
|
return self
|
|
|
|
|
|
|
|
def delRole(self):
|
|
|
|
item = self.getXMLItem()
|
|
|
|
#TODO set default role
|
|
|
|
if 'role' in item.attrib: del item.attrib['role']
|
|
|
|
return self
|
|
|
|
|
|
|
|
def getNick(self):
|
|
|
|
return self.parent()['from'].resource
|
|
|
|
|
|
|
|
def getRoom(self):
|
|
|
|
return self.parent()['from'].bare
|
|
|
|
|
|
|
|
def setNick(self, value):
|
|
|
|
log.warning("Cannot set nick through mucpresence plugin.")
|
|
|
|
return self
|
|
|
|
|
|
|
|
def setRoom(self, value):
|
|
|
|
log.warning("Cannot set room through mucpresence plugin.")
|
|
|
|
return self
|
|
|
|
|
|
|
|
def delNick(self):
|
|
|
|
log.warning("Cannot delete nick through mucpresence plugin.")
|
|
|
|
return self
|
|
|
|
|
|
|
|
def delRoom(self):
|
|
|
|
log.warning("Cannot delete room through mucpresence plugin.")
|
|
|
|
return self
|
2009-06-03 22:56:51 +00:00
|
|
|
|
2012-03-12 05:36:37 +00:00
|
|
|
|
|
|
|
class XEP_0045(BasePlugin):
|
|
|
|
|
2010-12-16 21:18:49 +00:00
|
|
|
"""
|
2012-03-12 05:36:37 +00:00
|
|
|
Implements XEP-0045 Multi-User Chat
|
2010-12-16 21:18:49 +00:00
|
|
|
"""
|
|
|
|
|
2012-03-12 05:36:37 +00:00
|
|
|
name = 'xep_0045'
|
|
|
|
description = 'XEP-0045: Multi-User Chat'
|
|
|
|
dependencies = set(['xep_0030', 'xep_0004'])
|
|
|
|
|
2010-12-16 21:18:49 +00:00
|
|
|
def plugin_init(self):
|
|
|
|
self.rooms = {}
|
|
|
|
self.ourNicks = {}
|
|
|
|
self.xep = '0045'
|
|
|
|
# load MUC support in presence stanzas
|
2012-03-12 05:36:37 +00:00
|
|
|
register_stanza_plugin(Presence, MUCPresence)
|
2013-02-21 21:48:03 +00:00
|
|
|
self.xmpp.register_handler(Callback('MUCPresence', MatchXMLMask("<presence xmlns='%s' />" % self.xmpp.default_ns), self.handle_groupchat_presence))
|
|
|
|
self.xmpp.register_handler(Callback('MUCMessage', MatchXMLMask("<message xmlns='%s' type='groupchat'><body/></message>" % self.xmpp.default_ns), self.handle_groupchat_message))
|
|
|
|
self.xmpp.register_handler(Callback('MUCSubject', MatchXMLMask("<message xmlns='%s' type='groupchat'><subject/></message>" % self.xmpp.default_ns), self.handle_groupchat_subject))
|
|
|
|
self.xmpp.register_handler(Callback('MUCConfig', MatchXMLMask("<message xmlns='%s' type='groupchat'><x xmlns='http://jabber.org/protocol/muc#user'><status/></x></message>" % self.xmpp.default_ns), self.handle_config_change))
|
|
|
|
self.xmpp.register_handler(Callback('MUCInvite', MatchXPath("{%s}message/{%s}x/{%s}invite" % (
|
2012-04-04 05:41:37 +00:00
|
|
|
self.xmpp.default_ns,
|
|
|
|
'http://jabber.org/protocol/muc#user',
|
|
|
|
'http://jabber.org/protocol/muc#user')), self.handle_groupchat_invite))
|
2010-12-16 21:18:49 +00:00
|
|
|
|
|
|
|
def handle_groupchat_invite(self, inv):
|
|
|
|
""" Handle an invite into a muc.
|
|
|
|
"""
|
2012-07-26 18:53:07 +00:00
|
|
|
logging.debug("MUC invite to %s from %s: %s", inv['to'], inv["from"], inv)
|
2010-12-16 21:18:49 +00:00
|
|
|
if inv['from'] not in self.rooms.keys():
|
|
|
|
self.xmpp.event("groupchat_invite", inv)
|
|
|
|
|
2012-05-14 23:10:22 +00:00
|
|
|
def handle_config_change(self, msg):
|
|
|
|
"""Handle a MUC configuration change (with status code)."""
|
|
|
|
self.xmpp.event('groupchat_config_status', msg)
|
|
|
|
self.xmpp.event('muc::%s::config_status' % msg['from'].bare , msg)
|
|
|
|
|
2010-12-16 21:18:49 +00:00
|
|
|
def handle_groupchat_presence(self, pr):
|
|
|
|
""" Handle a presence in a muc.
|
|
|
|
"""
|
|
|
|
got_offline = False
|
|
|
|
got_online = False
|
|
|
|
if pr['muc']['room'] not in self.rooms.keys():
|
|
|
|
return
|
|
|
|
entry = pr['muc'].getStanzaValues()
|
|
|
|
entry['show'] = pr['show']
|
|
|
|
entry['status'] = pr['status']
|
2012-08-08 02:33:17 +00:00
|
|
|
entry['alt_nick'] = pr['nick']
|
2010-12-16 21:18:49 +00:00
|
|
|
if pr['type'] == 'unavailable':
|
|
|
|
if entry['nick'] in self.rooms[entry['room']]:
|
|
|
|
del self.rooms[entry['room']][entry['nick']]
|
|
|
|
got_offline = True
|
|
|
|
else:
|
|
|
|
if entry['nick'] not in self.rooms[entry['room']]:
|
|
|
|
got_online = True
|
|
|
|
self.rooms[entry['room']][entry['nick']] = entry
|
2011-11-19 20:07:57 +00:00
|
|
|
log.debug("MUC presence from %s/%s : %s", entry['room'],entry['nick'], entry)
|
2010-12-16 21:18:49 +00:00
|
|
|
self.xmpp.event("groupchat_presence", pr)
|
|
|
|
self.xmpp.event("muc::%s::presence" % entry['room'], pr)
|
|
|
|
if got_offline:
|
|
|
|
self.xmpp.event("muc::%s::got_offline" % entry['room'], pr)
|
|
|
|
if got_online:
|
|
|
|
self.xmpp.event("muc::%s::got_online" % entry['room'], pr)
|
|
|
|
|
|
|
|
def handle_groupchat_message(self, msg):
|
|
|
|
""" Handle a message event in a muc.
|
|
|
|
"""
|
|
|
|
self.xmpp.event('groupchat_message', msg)
|
|
|
|
self.xmpp.event("muc::%s::message" % msg['from'].bare, msg)
|
|
|
|
|
|
|
|
def handle_groupchat_subject(self, msg):
|
|
|
|
""" Handle a message coming from a muc indicating
|
|
|
|
a change of subject (or announcing it when joining the room)
|
|
|
|
"""
|
|
|
|
self.xmpp.event('groupchat_subject', msg)
|
|
|
|
|
|
|
|
def jidInRoom(self, room, jid):
|
|
|
|
for nick in self.rooms[room]:
|
|
|
|
entry = self.rooms[room][nick]
|
|
|
|
if entry is not None and entry['jid'].full == jid:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def getNick(self, room, jid):
|
|
|
|
for nick in self.rooms[room]:
|
|
|
|
entry = self.rooms[room][nick]
|
|
|
|
if entry is not None and entry['jid'].full == jid:
|
|
|
|
return nick
|
|
|
|
|
|
|
|
def configureRoom(self, room, form=None, ifrom=None):
|
|
|
|
if form is None:
|
2013-02-10 19:47:58 +00:00
|
|
|
form = self.getRoomConfig(room, ifrom=ifrom)
|
2010-12-16 21:18:49 +00:00
|
|
|
iq = self.xmpp.makeIqSet()
|
|
|
|
iq['to'] = room
|
|
|
|
if ifrom is not None:
|
|
|
|
iq['from'] = ifrom
|
|
|
|
query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
|
|
|
|
form = form.getXML('submit')
|
|
|
|
query.append(form)
|
|
|
|
iq.append(query)
|
2011-08-18 04:30:47 +00:00
|
|
|
# For now, swallow errors to preserve existing API
|
|
|
|
try:
|
|
|
|
result = iq.send()
|
|
|
|
except IqError:
|
|
|
|
return False
|
|
|
|
except IqTimeout:
|
2010-12-16 21:18:49 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2011-10-10 15:31:03 +00:00
|
|
|
def joinMUC(self, room, nick, maxhistory="0", password='', wait=False, pstatus=None, pshow=None, pfrom=None):
|
2010-12-16 21:18:49 +00:00
|
|
|
""" Join the specified room, requesting 'maxhistory' lines of history.
|
|
|
|
"""
|
2011-10-10 15:31:03 +00:00
|
|
|
stanza = self.xmpp.makePresence(pto="%s/%s" % (room, nick), pstatus=pstatus, pshow=pshow, pfrom=pfrom)
|
2010-12-16 21:18:49 +00:00
|
|
|
x = ET.Element('{http://jabber.org/protocol/muc}x')
|
|
|
|
if password:
|
2012-10-05 15:58:04 +00:00
|
|
|
passelement = ET.Element('{http://jabber.org/protocol/muc}password')
|
2010-12-16 21:18:49 +00:00
|
|
|
passelement.text = password
|
|
|
|
x.append(passelement)
|
|
|
|
if maxhistory:
|
2012-10-05 15:58:04 +00:00
|
|
|
history = ET.Element('{http://jabber.org/protocol/muc}history')
|
2010-12-16 21:18:49 +00:00
|
|
|
if maxhistory == "0":
|
|
|
|
history.attrib['maxchars'] = maxhistory
|
|
|
|
else:
|
|
|
|
history.attrib['maxstanzas'] = maxhistory
|
|
|
|
x.append(history)
|
|
|
|
stanza.append(x)
|
|
|
|
if not wait:
|
|
|
|
self.xmpp.send(stanza)
|
|
|
|
else:
|
|
|
|
#wait for our own room presence back
|
|
|
|
expect = ET.Element("{%s}presence" % self.xmpp.default_ns, {'from':"%s/%s" % (room, nick)})
|
|
|
|
self.xmpp.send(stanza, expect)
|
|
|
|
self.rooms[room] = {}
|
|
|
|
self.ourNicks[room] = nick
|
|
|
|
|
|
|
|
def destroy(self, room, reason='', altroom = '', ifrom=None):
|
|
|
|
iq = self.xmpp.makeIqSet()
|
|
|
|
if ifrom is not None:
|
|
|
|
iq['from'] = ifrom
|
|
|
|
iq['to'] = room
|
|
|
|
query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
|
2012-10-05 15:58:04 +00:00
|
|
|
destroy = ET.Element('{http://jabber.org/protocol/muc#owner}destroy')
|
2010-12-16 21:18:49 +00:00
|
|
|
if altroom:
|
|
|
|
destroy.attrib['jid'] = altroom
|
2012-10-05 15:58:04 +00:00
|
|
|
xreason = ET.Element('{http://jabber.org/protocol/muc#owner}reason')
|
2010-12-16 21:18:49 +00:00
|
|
|
xreason.text = reason
|
|
|
|
destroy.append(xreason)
|
|
|
|
query.append(destroy)
|
|
|
|
iq.append(query)
|
2011-08-18 04:30:47 +00:00
|
|
|
# For now, swallow errors to preserve existing API
|
|
|
|
try:
|
|
|
|
r = iq.send()
|
|
|
|
except IqError:
|
|
|
|
return False
|
|
|
|
except IqTimeout:
|
2010-12-16 21:18:49 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2011-10-10 15:31:03 +00:00
|
|
|
def setAffiliation(self, room, jid=None, nick=None, affiliation='member', ifrom=None):
|
2010-12-16 21:18:49 +00:00
|
|
|
""" Change room affiliation."""
|
|
|
|
if affiliation not in ('outcast', 'member', 'admin', 'owner', 'none'):
|
|
|
|
raise TypeError
|
|
|
|
query = ET.Element('{http://jabber.org/protocol/muc#admin}query')
|
|
|
|
if nick is not None:
|
2012-10-05 15:58:04 +00:00
|
|
|
item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {'affiliation':affiliation, 'nick':nick})
|
2010-12-16 21:18:49 +00:00
|
|
|
else:
|
2012-10-05 15:58:04 +00:00
|
|
|
item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {'affiliation':affiliation, 'jid':jid})
|
2010-12-16 21:18:49 +00:00
|
|
|
query.append(item)
|
|
|
|
iq = self.xmpp.makeIqSet(query)
|
|
|
|
iq['to'] = room
|
2011-10-10 15:31:03 +00:00
|
|
|
iq['from'] = ifrom
|
2011-08-18 04:30:47 +00:00
|
|
|
# For now, swallow errors to preserve existing API
|
|
|
|
try:
|
|
|
|
result = iq.send()
|
|
|
|
except IqError:
|
|
|
|
return False
|
|
|
|
except IqTimeout:
|
|
|
|
return False
|
2010-12-16 21:18:49 +00:00
|
|
|
return True
|
|
|
|
|
2013-11-06 10:20:50 +00:00
|
|
|
def setRole(self, room, nick, role):
|
|
|
|
""" Change role property of a nick in a room.
|
|
|
|
Typically, roles are temporary (they last only as long as you are in the
|
|
|
|
room), whereas affiliations are permanent (they last across groupchat
|
|
|
|
sessions).
|
|
|
|
"""
|
|
|
|
if role not in ('outcast', 'member', 'admin', 'owner', 'none'):
|
|
|
|
raise TypeError
|
|
|
|
query = ET.Element('{http://jabber.org/protocol/muc#admin}query')
|
|
|
|
item = ET.Element('item', {'role':role, 'nick':nick})
|
|
|
|
query.append(item)
|
|
|
|
iq = self.xmpp.makeIqSet(query)
|
|
|
|
iq['to'] = room
|
|
|
|
result = iq.send()
|
|
|
|
if result is False or result['type'] != 'result':
|
|
|
|
raise ValueError
|
|
|
|
return True
|
|
|
|
|
2010-12-16 23:14:33 +00:00
|
|
|
def invite(self, room, jid, reason='', mfrom=''):
|
2010-12-16 21:18:49 +00:00
|
|
|
""" Invite a jid to a room."""
|
|
|
|
msg = self.xmpp.makeMessage(room)
|
2010-12-16 23:14:33 +00:00
|
|
|
msg['from'] = mfrom
|
2010-12-16 21:18:49 +00:00
|
|
|
x = ET.Element('{http://jabber.org/protocol/muc#user}x')
|
|
|
|
invite = ET.Element('{http://jabber.org/protocol/muc#user}invite', {'to': jid})
|
|
|
|
if reason:
|
2012-10-05 15:58:04 +00:00
|
|
|
rxml = ET.Element('{http://jabber.org/protocol/muc#user}reason')
|
2010-12-16 21:18:49 +00:00
|
|
|
rxml.text = reason
|
|
|
|
invite.append(rxml)
|
|
|
|
x.append(invite)
|
|
|
|
msg.append(x)
|
|
|
|
self.xmpp.send(msg)
|
|
|
|
|
2011-10-10 15:31:03 +00:00
|
|
|
def leaveMUC(self, room, nick, msg='', pfrom=None):
|
2010-12-16 21:18:49 +00:00
|
|
|
""" Leave the specified room.
|
|
|
|
"""
|
|
|
|
if msg:
|
2011-10-10 15:31:03 +00:00
|
|
|
self.xmpp.sendPresence(pshow='unavailable', pto="%s/%s" % (room, nick), pstatus=msg, pfrom=pfrom)
|
2010-12-16 21:18:49 +00:00
|
|
|
else:
|
2011-10-10 15:31:03 +00:00
|
|
|
self.xmpp.sendPresence(pshow='unavailable', pto="%s/%s" % (room, nick), pfrom=pfrom)
|
2010-12-16 21:18:49 +00:00
|
|
|
del self.rooms[room]
|
|
|
|
|
2010-12-16 23:14:33 +00:00
|
|
|
def getRoomConfig(self, room, ifrom=''):
|
2010-12-16 21:18:49 +00:00
|
|
|
iq = self.xmpp.makeIqGet('http://jabber.org/protocol/muc#owner')
|
|
|
|
iq['to'] = room
|
2010-12-16 23:14:33 +00:00
|
|
|
iq['from'] = ifrom
|
2011-08-18 04:30:47 +00:00
|
|
|
# For now, swallow errors to preserve existing API
|
|
|
|
try:
|
|
|
|
result = iq.send()
|
|
|
|
except IqError:
|
|
|
|
raise ValueError
|
|
|
|
except IqTimeout:
|
2010-12-16 21:18:49 +00:00
|
|
|
raise ValueError
|
|
|
|
form = result.xml.find('{http://jabber.org/protocol/muc#owner}query/{jabber:x:data}x')
|
|
|
|
if form is None:
|
|
|
|
raise ValueError
|
|
|
|
return self.xmpp.plugin['xep_0004'].buildForm(form)
|
|
|
|
|
2011-10-10 15:31:03 +00:00
|
|
|
def cancelConfig(self, room, ifrom=None):
|
2010-12-16 21:18:49 +00:00
|
|
|
query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
|
|
|
|
x = ET.Element('{jabber:x:data}x', type='cancel')
|
|
|
|
query.append(x)
|
|
|
|
iq = self.xmpp.makeIqSet(query)
|
2011-01-21 19:18:04 +00:00
|
|
|
iq['to'] = room
|
2011-10-10 15:31:03 +00:00
|
|
|
iq['from'] = ifrom
|
2010-12-16 21:18:49 +00:00
|
|
|
iq.send()
|
|
|
|
|
2010-12-16 23:14:33 +00:00
|
|
|
def setRoomConfig(self, room, config, ifrom=''):
|
2010-12-16 21:18:49 +00:00
|
|
|
query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
|
|
|
|
x = config.getXML('submit')
|
|
|
|
query.append(x)
|
|
|
|
iq = self.xmpp.makeIqSet(query)
|
|
|
|
iq['to'] = room
|
2010-12-16 23:14:33 +00:00
|
|
|
iq['from'] = ifrom
|
2010-12-16 21:18:49 +00:00
|
|
|
iq.send()
|
|
|
|
|
|
|
|
def getJoinedRooms(self):
|
|
|
|
return self.rooms.keys()
|
|
|
|
|
|
|
|
def getOurJidInRoom(self, roomJid):
|
|
|
|
""" Return the jid we're using in a room.
|
|
|
|
"""
|
|
|
|
return "%s/%s" % (roomJid, self.ourNicks[roomJid])
|
|
|
|
|
|
|
|
def getJidProperty(self, room, nick, jidProperty):
|
|
|
|
""" Get the property of a nick in a room, such as its 'jid' or 'affiliation'
|
|
|
|
If not found, return None.
|
|
|
|
"""
|
|
|
|
if room in self.rooms and nick in self.rooms[room] and jidProperty in self.rooms[room][nick]:
|
|
|
|
return self.rooms[room][nick][jidProperty]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def getRoster(self, room):
|
|
|
|
""" Get the list of nicks in a room.
|
|
|
|
"""
|
|
|
|
if room not in self.rooms.keys():
|
|
|
|
return None
|
|
|
|
return self.rooms[room].keys()
|
2012-03-12 05:36:37 +00:00
|
|
|
|
|
|
|
|
2012-03-12 16:56:48 +00:00
|
|
|
xep_0045 = XEP_0045
|
2012-03-12 05:36:37 +00:00
|
|
|
register_plugin(XEP_0045)
|