~ubuntu-branches/ubuntu/natty/mago/natty

« back to all changes in this revision

Viewing changes to mago/application/pidgin/xmpp_utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python -u
2
 
 
3
 
import gobject, gtk
4
 
import sys
5
 
from time import sleep
6
 
import traceback
7
 
 
8
 
from pyxmpp.all import JID,Iq,Presence,Message,StreamError
9
 
from pyxmpp.jabber.client import JabberClient
10
 
from pyxmpp.jabber.clientstream import LegacyAuthenticationError
11
 
 
12
 
from pyxmpp.jabber.simple import xmpp_do
13
 
from pyxmpp.jabber.register import Register
14
 
 
15
 
class ClientXMPP(JabberClient):
16
 
    def __init__(self, jid, password, debug=False):
17
 
        if isinstance(jid, str) or isinstance(jid, unicode):
18
 
            jid = JID(jid)
19
 
 
20
 
        self.main_loop = gobject.MainLoop()
21
 
        self.is_connected = False
22
 
        self.messages = []
23
 
        self._exception = None
24
 
        self.debug = debug
25
 
 
26
 
        self.name = jid.node
27
 
        self.email = jid.as_unicode()
28
 
 
29
 
        # if bare JID is provided add a resource -- it is required
30
 
        if not jid.resource:
31
 
            jid=JID(jid.node, jid.domain, "UDT")
32
 
 
33
 
        # setup client with provided connection information
34
 
        # and identity data
35
 
        JabberClient.__init__(self, jid, password,
36
 
                disco_name="PyXMPP example: echo bot", disco_type="bot")
37
 
 
38
 
    def connected(self):
39
 
        self.is_connected = True
40
 
        JabberClient.connected(self)
41
 
 
42
 
    def disconnected(self):
43
 
        self.is_connected = False
44
 
        JabberClient.disconnected(self)
45
 
 
46
 
    def process_registration_form(self, stanza, form):
47
 
        if not 'FORM_TYPE' in form or \
48
 
                'jabber:iq:register' not in form['FORM_TYPE'].values:
49
 
            raise RuntimeError, "Unknown form type: %r %r" % (form, form['FORM_TYPE'])
50
 
        for field in form:
51
 
            if field.name == u"username":
52
 
                field.value = self.jid.node
53
 
            elif field.name == u"password":
54
 
                field.value = self.password
55
 
            elif field.name == u"name":
56
 
                field.value = self.name
57
 
            elif field.name == u"email":
58
 
                field.value = self.email
59
 
            elif field.required:
60
 
                raise RuntimeError, "Unsupported required registration form field %r" % (field.name,)
61
 
        self.submit_registration_form(form)
62
 
 
63
 
 
64
 
    def stream_state_changed(self,state,arg):
65
 
        if self.debug:
66
 
            print "- %s %r" % (state,arg)
67
 
 
68
 
    def session_started(self):
69
 
        JabberClient.session_started(self)
70
 
 
71
 
        # set up handler for <message stanza>
72
 
        self.stream.set_message_handler("normal",self.message)
73
 
 
74
 
    def message(self,stanza):
75
 
        if stanza.get_type() == "chat":
76
 
            self.messages.append((stanza.get_from().as_unicode(), 
77
 
                                  stanza.get_subject(), 
78
 
                                  stanza.get_body()))
79
 
 
80
 
    def loop_iter(self, timeout=1):
81
 
        if self.stream:
82
 
            try:
83
 
                self.stream.loop_iter(timeout)
84
 
            except Exception, e:
85
 
                self._exception = sys.exc_info()
86
 
                self.main_loop.quit()
87
 
        return True
88
 
 
89
 
    def loop(self):
90
 
        self.main_loop.run()
91
 
        if self._exception:
92
 
            exceptionType, exceptionValue, exceptionTraceback = self._exception
93
 
            #traceback.print_exception(exceptionType, exceptionValue, 
94
 
            #                          exceptionTraceback, file=sys.stdout)
95
 
            e = exceptionValue
96
 
            self._exception = None
97
 
            raise e
98
 
 
99
 
    def match_messages(self, jid, subject, body):
100
 
        matched = []
101
 
        for mjid, msubject, mbody in self.messages:
102
 
            if jid is not None and jid != mjid:
103
 
                continue
104
 
            if subject is not None and subject != msubject:
105
 
                continue
106
 
            if body is not None and body != mbody:
107
 
                continue
108
 
            matched.append((mjid, msubject, mbody))
109
 
 
110
 
        return matched
111
 
 
112
 
    def flush_messages(self):
113
 
        while True:
114
 
            try:
115
 
                self.messages.pop(0)
116
 
            except IndexError:
117
 
                break
118
 
        
119
 
 
120
 
    def send_message(self, userid, body, subject=''):
121
 
        m=Message(
122
 
            to_jid=unicode(userid),
123
 
            from_jid=self.jid.as_unicode(),
124
 
            stanza_type="chat",
125
 
            subject=unicode(subject),
126
 
            body=unicode(body))
127
 
        self.stream.send(m)
128
 
 
129
 
def unregister(jid, passwd):
130
 
    if not jid.resource:
131
 
        jid=JID(jid.node, jid.domain, "remover")
132
 
        
133
 
    def _remove(stream):
134
 
        iq = Iq(from_jid=jid, to_jid=jid.domain, stanza_type="set")
135
 
        r = Register()
136
 
        r.remove = True
137
 
        iq.set_content(r)
138
 
        stream.send(iq)
139
 
 
140
 
    xmpp_do(jid, passwd, _remove)
141
 
 
142
 
def register(jid, passwd):
143
 
    b = Buddy(JID(jid), passwd)
144
 
    try:
145
 
        b.connect(True)
146
 
    except LegacyAuthenticationError:
147
 
        pass
148
 
    finally:
149
 
        b.disconnect()
150
 
 
151
 
if __name__ == "__main__":
152
 
    b = Buddy(JID(sys.argv[1]), sys.argv[2])
153
 
    b.connect()
154
 
    print 'connected?'
155
 
    #b.client.loop()
156
 
    while True:
157
 
        msg = b.wait_for_message(timeout=1)
158
 
        if msg:
159
 
            print msg
160
 
 
161
 
    #sleep(1)
162
 
    #print b.wait_for_message(timeout=5)
163
 
    #b.disconnect()