summaryrefslogtreecommitdiffstats
path: root/connection/xmpp.py
blob: 454f3df8b925d653b3377101f87744838065399d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from pyxmpp.all import JID, Message
from pyxmpp.streamtls import TLSSettings
from pyxmpp.client import Client
from pyxmpp.jabber.muc import *
from pyxmpp.interface import implements
from pyxmpp.interfaces import *


class MessageHandler:
    implements(IMessageHandlersProvider)
    
    def __init__(self, client):
        self.client = client
    
    def get_message_handlers(self):
        return [
            ("normal", self.message),
            ]
    
    def message(self, stanza):
        pass
    
class MucHandler(MucRoomHandler):
    def __init__(self, client):
        MucRoomHandler.__init__(self)
        self.client = client
    
    def reply(self, text):
        self.room_state.send_message(text)
    
    def get_topic(self):
        return self.room_state.subject
    
    def set_topic(self, topic):
        self.room_state.set_subject(topic)
    
    def message_received(self, user, stanza):
        if(user == None or user.same_as(self.room_state.me) or stanza.get_body() == None):
            return
        
        for mod in self.client.module_manager.modules.itervalues():
            mod.groupchat(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_body(), self)
    
    def user_joined(self, user, stanza):
        if(not self.room_state.joined or user.same_as(self.room_state.me)):
            return
        
        status = ''
        if stanza.get_status() != None:
            status = stanza.get_status()
        
        for mod in self.client.module_manager.modules.itervalues():
            mod.join(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self)

    def user_left(self, user, stanza):
        if(not self.room_state.joined or user.same_as(self.room_state.me)):
            return
        
        status = ''
        if stanza.get_status() != None:
            status = stanza.get_status()
        
        for mod in self.client.module_manager.modules.itervalues():
            mod.leave(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, stanza.get_show(), status, self)
    
    def subject_changed(self, user, stanza):
        topic = ''
        if stanza.get_subject() != None:
            topic = stanza.get_subject()
        
        for mod in self.client.module_manager.modules.itervalues():
            mod.topic(stanza.get_from().bare().as_unicode(), stanza.get_from().resource, topic, self)


class PresenceHandler(object):
    implements(IPresenceHandlersProvider)
    
    def __init__(self, client):
        self.client = client
    
    def get_presence_handlers(self):
        return [
            (None, self.presence),
            ("unavailable", self.presence),
            ("subscribe", self.presence_control),
            ("subscribed", self.presence_control),
            ("unsubscribe", self.presence_control),
            ("unsubscribed", self.presence_control),
            ]
    
    def presence(self,stanza):
        msg=u"%s has become " % (stanza.get_from())
        t=stanza.get_type()
        if t=="unavailable":
            msg+=u"unavailable"
        else:
            msg+=u"available"

        show=stanza.get_show()
        if show:
            msg+=u"(%s)" % (show,)

        status=stanza.get_status()
        if status:
            msg+=u": "+status
        print msg

    def presence_control(self,stanza):
        msg=unicode(stanza.get_from())
        t=stanza.get_type()
        if t=="subscribe":
            msg+=u" has requested presence subscription."
        elif t=="subscribed":
            msg+=u" has accepted our presence subscription request."
        elif t=="unsubscribe":
            msg+=u" has canceled his subscription of our."
        elif t=="unsubscribed":
            msg+=u" has canceled our subscription of his presence."

        print msg

        return stanza.make_accept_response()

class VersionHandler(object):
    implements(IIqHandlersProvider, IFeaturesProvider)

    def __init__(self, client):
        self.client = client

    def get_features(self):
        return ["jabber:iq:version"]

    def get_iq_get_handlers(self):
        return [
            ("query", "jabber:iq:version", self.get_version),
            ]

    def get_iq_set_handlers(self):
        return []

    def get_version(self,iq):
        iq=iq.make_result_response()
        q=iq.new_query("jabber:iq:version")
        q.newTextChild(q.ns(),"name","Curunir XMPP-Bot")
        q.newTextChild(q.ns(),"version","0.1")
        return iq


class XMPPConnection(Client):
    def __init__(self, config, modman):
        self.config = config
        self.module_manager = modman
        
        Client.__init__(self, JID(config['jid']), config['password'], tls_settings = TLSSettings(require = True, cacert_file = config['cacertfile']), keepalive = 60)
        
        self.interface_providers = [
            MessageHandler(self),
            PresenceHandler(self),
            VersionHandler(self)
            ]
    
    def authorized(self):
        Client.authorized(self)
        self.room_manager = MucRoomManager(self.stream)
        self.room_manager.set_handlers()
        
        for room in self.config['rooms']:
            self.room_manager.join(JID(room), self.config['nick'], MucHandler(self), history_maxstanzas=0)