poezio/src/multiuserchat.py

178 lines
5.4 KiB
Python
Raw Normal View History

2011-09-06 00:45:53 +00:00
# Copyright 2010-2011 Florent Le Coz <louiz@louiz.org>
#
# This file is part of Poezio.
#
# Poezio is free software: you can redistribute it and/or modify
2011-09-11 15:10:05 +00:00
# it under the terms of the zlib license. See the COPYING file.
2010-01-10 20:14:17 +00:00
2010-08-31 23:11:02 +00:00
"""
Implementation of the XEP-0045: Multi-User Chat.
Add some facilities that are not available on the XEP_0045
sleek plugin
"""
2010-01-10 20:14:17 +00:00
from gettext import gettext as _
2010-08-31 23:11:02 +00:00
from xml.etree import cElementTree as ET
2010-02-14 03:51:03 +00:00
from common import safeJID
import logging
log = logging.getLogger(__name__)
NS_MUC_ADMIN = 'http://jabber.org/protocol/muc#admin'
NS_MUC_OWNER = 'http://jabber.org/protocol/muc#owner'
def destroy_room(xmpp, room, reason='', altroom=''):
"""
destroy a room
"""
room = safeJID(room)
if not room:
return False
iq = xmpp.make_iq_set()
iq['to'] = room
query = ET.Element('{%s}query' % NS_MUC_OWNER)
destroy = ET.Element('{%s}destroy' % NS_MUC_OWNER)
if altroom:
destroy.attrib['jid'] = altroom
if reason:
xreason = ET.Element('{%s}reason' % NS_MUC_OWNER)
xreason.text = reason
destroy.append(xreason)
query.append(destroy)
iq.append(query)
def callback(iq):
if not iq or iq['type'] == 'error':
xmpp.core.information(_('Unable to destroy room %s') % room,
_('Info'))
else:
xmpp.core.information(_('Room %s destroyed') % room, _('Info'))
iq.send(block=False, callback=callback)
return True
2010-08-31 23:11:02 +00:00
def send_private_message(xmpp, jid, line):
2010-02-13 15:18:39 +00:00
"""
2010-08-31 23:11:02 +00:00
Send a private message
2010-02-13 15:18:39 +00:00
"""
jid = safeJID(jid)
2010-11-19 03:44:43 +00:00
xmpp.send_message(mto=jid, mbody=line, mtype='chat')
2010-02-13 15:18:39 +00:00
2010-08-31 23:11:02 +00:00
def send_groupchat_message(xmpp, jid, line):
"""
Send a message to the groupchat
"""
jid = safeJID(jid)
2010-11-19 03:44:43 +00:00
xmpp.send_message(mto=jid, mbody=line, mtype='groupchat')
2010-01-10 20:14:17 +00:00
2010-08-31 23:11:02 +00:00
def change_show(xmpp, jid, own_nick, show, status):
"""
Change our 'Show'
"""
jid = safeJID(jid)
2010-11-19 03:44:43 +00:00
pres = xmpp.make_presence(pto='%s/%s' % (jid, own_nick))
2010-11-15 11:59:09 +00:00
if show: # if show is None, don't put a <show /> tag. It means "available"
2010-08-31 23:11:02 +00:00
pres['type'] = show
if status:
pres['status'] = status
pres.send()
def change_subject(xmpp, jid, subject):
"""
Change the room subject
"""
jid = safeJID(jid)
2010-11-19 03:44:43 +00:00
msg = xmpp.make_message(jid)
2010-08-31 23:11:02 +00:00
msg['type'] = 'groupchat'
msg['subject'] = subject
msg.send()
2010-01-10 20:14:17 +00:00
2013-05-09 11:13:11 +00:00
def change_nick(core, jid, nick, status=None, show=None):
2010-08-31 23:11:02 +00:00
"""
Change our own nick in a room
"""
2013-05-09 11:13:11 +00:00
xmpp = core.xmpp
presence = xmpp.make_presence(pshow=show, pstatus=status, pto=safeJID('%s/%s' % (jid, nick)))
core.events.trigger('changing_nick', presence)
presence.send()
2010-01-31 03:07:30 +00:00
def join_groupchat(core, jid, nick, passwd='', maxhistory=None, status=None, show=None, seconds=None):
xmpp = core.xmpp
stanza = xmpp.makePresence(pto='%s/%s' % (jid, nick), pstatus=status, pshow=show)
x = ET.Element('{http://jabber.org/protocol/muc}x')
if passwd:
passelement = ET.Element('password')
passelement.text = passwd
x.append(passelement)
if seconds is not None:
history = ET.Element('{http://jabber.org/protocol/muc}history')
2012-02-29 19:31:46 +00:00
history.attrib['seconds'] = str(seconds)
x.append(history)
stanza.append(x)
core.events.trigger('joining_muc', stanza)
to = stanza["to"]
stanza.send()
xmpp.plugin['xep_0045'].rooms[jid] = {}
xmpp.plugin['xep_0045'].ourNicks[jid] = to.resource
2010-02-14 03:51:03 +00:00
2010-08-31 23:11:02 +00:00
def leave_groupchat(xmpp, jid, own_nick, msg):
"""
Leave the groupchat
"""
jid = safeJID(jid)
try:
xmpp.plugin['xep_0045'].leaveMUC(jid, own_nick, msg)
except KeyError:
log.debug("muc.leave_groupchat: could not leave the room %s",
jid, exc_info=True)
2010-02-14 03:51:03 +00:00
def set_user_role(xmpp, jid, nick, reason, role, callback=None):
2010-08-31 23:11:02 +00:00
"""
(try to) Set the role of a MUC user
(role = 'none': eject user)
2010-08-31 23:11:02 +00:00
"""
jid = safeJID(jid)
2010-08-31 23:11:02 +00:00
iq = xmpp.makeIqSet()
query = ET.Element('{%s}query' % NS_MUC_ADMIN)
item = ET.Element('{%s}item' % NS_MUC_ADMIN, {'nick':nick, 'role':role})
2010-08-31 23:11:02 +00:00
if reason:
reason_el = ET.Element('{%s}reason' % NS_MUC_ADMIN)
2010-08-31 23:11:02 +00:00
reason_el.text = reason
item.append(reason_el)
query.append(item)
iq.append(query)
iq['to'] = jid
if callback:
return iq.send(block=False, callback=callback)
2011-10-04 16:56:02 +00:00
try:
return iq.send()
2011-10-04 16:56:02 +00:00
except Exception as e:
return e.iq
def set_user_affiliation(xmpp, muc_jid, affiliation, nick=None, jid=None, reason=None, callback=None):
"""
(try to) Set the affiliation of a MUC user
"""
muc_jid = safeJID(muc_jid)
query = ET.Element('{http://jabber.org/protocol/muc#admin}query')
if nick:
item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {'affiliation':affiliation, 'nick':nick})
else:
item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {'affiliation':affiliation, 'jid':str(jid)})
2013-05-10 22:33:06 +00:00
if reason:
reason_item = ET.Element('{http://jabber.org/protocol/muc#admin}reason')
reason_item.text = reason
item.append(reason_item)
query.append(item)
iq = xmpp.makeIqSet(query)
iq['to'] = muc_jid
if callback:
return iq.send(block=False, callback=callback)
try:
return xmpp.plugin['xep_0045'].setAffiliation(str(muc_jid), str(jid) if jid else None, nick, affiliation)
except:
import traceback
log.debug('Error setting the affiliation: %s', traceback.format_exc())
return False