multiuserchat.py 2.58 KB
Newer Older
1
# Copyright 2010-2011, Florent Le Coz <louiz@louiz.org>
2 3 4 5 6 7 8 9 10 11 12 13 14

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation version 3 of the License.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

15 16 17 18 19
"""
Implementation of the XEP-0045: Multi-User Chat.
Add some facilities that are not available on the XEP_0045
sleek plugin
"""
20

21
import sleekxmpp
22

23
from xml.etree import cElementTree as ET
24

25
def send_private_message(xmpp, jid, line):
26
    """
27
    Send a private message
28
    """
29
    xmpp.send_message(mto=jid, mbody=line, mtype='chat')
30

31 32 33 34
def send_groupchat_message(xmpp, jid, line):
    """
    Send a message to the groupchat
    """
35
    xmpp.send_message(mto=jid, mbody=line, mtype='groupchat')
36

37 38 39 40
def change_show(xmpp, jid, own_nick, show, status):
    """
    Change our 'Show'
    """
41
    pres = xmpp.make_presence(pto='%s/%s' % (jid, own_nick))
42
    if show: # if show is None, don't put a <show /> tag. It means "available"
43 44 45 46 47 48 49 50 51
        pres['type'] = show
    if status:
        pres['status'] = status
    pres.send()

def change_subject(xmpp, jid, subject):
    """
    Change the room subject
    """
52
    msg = xmpp.make_message(jid)
53 54 55
    msg['type'] = 'groupchat'
    msg['subject'] = subject
    msg.send()
56

57 58 59 60
def change_nick(xmpp, jid, nick):
    """
    Change our own nick in a room
    """
61
    xmpp.make_presence(pto='%s/%s' % (jid, nick)).send()
62

63
def join_groupchat(xmpp, jid, nick, passwd=''):
64 65 66
    """
    Join the groupchat
    """
67
    xmpp.plugin['xep_0045'].joinMUC(jid, nick, maxhistory=None, password=passwd)
68

69 70 71 72 73
def leave_groupchat(xmpp, jid, own_nick, msg):
    """
    Leave the groupchat
    """
    xmpp.plugin['xep_0045'].leaveMUC(jid, own_nick, msg)
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
def eject_user(xmpp, jid, nick, reason):
    """
    (try to) Eject an user from the room
    """
    iq = xmpp.makeIqSet()
    query = ET.Element('{http://jabber.org/protocol/muc#admin}query')
    item = ET.Element('{http://jabber.org/protocol/muc#admin}item', {'nick':nick, 'role':'none'})
    if reason:
        reason_el = ET.Element('{http://jabber.org/protocol/muc#admin}reason')
        reason_el.text = reason
        item.append(reason_el)
    query.append(item)
    iq.append(query)
    iq['to'] = jid
    return iq.send()