~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gozerbot/xmpp/wait.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/xmpp/wait.py
 
2
#
 
3
#
 
4
 
 
5
""" wait for ircevent based on ircevent.CMND """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
# gozerbot imports
 
10
from gozerbot.utils.log import rlog
 
11
from gozerbot.utils.locking import lockdec
 
12
import gozerbot.threads.thr as thr
 
13
 
 
14
# xmpp imports
 
15
from gozerbot.wait import Wait
 
16
 
 
17
# basic imports
 
18
import time, thread
 
19
 
 
20
# locks
 
21
waitlock = thread.allocate_lock()
 
22
locked = lockdec(waitlock)
 
23
 
 
24
class XMPPWait(Wait):
 
25
 
 
26
    """ wait object for jabber messages. """
 
27
 
 
28
    def register(self, catch, queue, timeout=15):
 
29
 
 
30
        """ register wait for privmsg. """
 
31
 
 
32
        rlog(1, 'xmpp-wait', 'registering for %s' % catch)
 
33
        self.ticket += 1
 
34
        self.waitlist.append((catch, queue, self.ticket))
 
35
        if timeout:
 
36
            thr.start_new_thread(self.dotimeout, (timeout, self.ticket))
 
37
        return self.ticket
 
38
 
 
39
    def check(self, msg):
 
40
 
 
41
        """ check if <msg> is waited for. """
 
42
 
 
43
        for teller in range(len(self.waitlist)-1, -1, -1):
 
44
            i = self.waitlist[teller]
 
45
            if i[0] == msg.userhost:
 
46
                msg.ticket = i[2]
 
47
                i[1].put_nowait(msg)
 
48
                self.delete(msg.ticket)
 
49
                rlog(10, 'xmpp-wait', 'got response for %s' % i[0])
 
50
                msg.isresponse = 1
 
51
 
 
52
    @locked
 
53
    def delete(self, ticket):
 
54
 
 
55
        """ delete wait item with ticket nr. """
 
56
 
 
57
        for itemnr in range(len(self.waitlist)-1, -1, -1):
 
58
            item = self.waitlist[itemnr]
 
59
            if item[2] == ticket:
 
60
                item[1].put_nowait(None)
 
61
                try:
 
62
                    del self.waitlist[itemnr]
 
63
                    rlog(1, 'xmpp-wait', 'deleted ' + str(ticket))
 
64
                except IndexError:
 
65
                    pass
 
66
                return 1
 
67
 
 
68
class XMPPErrorWait(XMPPWait):
 
69
 
 
70
    """ wait for jabber errors. """
 
71
 
 
72
    def check(self, msg):
 
73
 
 
74
        """ check if <msg> is waited for. """
 
75
 
 
76
        if not msg.type == 'error':
 
77
            return
 
78
 
 
79
        errorcode = msg.error
 
80
 
 
81
        for teller in range(len(self.waitlist)-1, -1, -1):
 
82
            i = self.waitlist[teller]
 
83
            if i[0] == 'ALL' or i[0] == errorcode:
 
84
                msg.error = msg.error
 
85
                msg.ticket = i[2]
 
86
                i[1].put_nowait(msg)
 
87
                self.delete(msg.ticket)
 
88
                rlog(10,'xmpp-errorwait','got error response for %s' % i[0])