~ubuntu-branches/ubuntu/precise/gozerbot/precise

« back to all changes in this revision

Viewing changes to gozerplugs/plugs/udp.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2008-06-02 19:26:39 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080602192639-3rn65nx4q1sgd6sy
Tags: 0.8.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#
3
3
#
4
4
 
5
 
""" implement listenudp thread """
 
5
"""
 
6
the bot has the capability to listen for udp packets which it will use
 
7
to /msg a given nick or channel.
 
8
 
 
9
1) udp config 
 
10
 
 
11
edit the following section in you gozerdata/config
 
12
 
 
13
::
 
14
 
 
15
  # udp
 
16
  udp = 1 # set to 1 to enable
 
17
  udphost = 'localhost'
 
18
  udpport = 5500
 
19
  udpallow = ['127.0.0.1', ]
 
20
  udpallowednicks = ['#gozerbot', 'dunker']
 
21
  udppassword = 'mekker'
 
22
  udpseed = "blablablablablaz" # needs to be 16 chars wide
 
23
 
 
24
udpallow is set to the ip from which udp packets are accepted .. 
 
25
udpallowednicks are nicks/channels the bot is allowed to send messages to
 
26
and udppassword is passed along with the message. set udpseed if you want to
 
27
have your messages encrypted.
 
28
 
 
29
2) limiter
 
30
 
 
31
on IRC the bot's /msg to a user/channel are limited to 1 per 3 seconds so the
 
32
bot will not excessflood on the server. you can use partyudp if you need no 
 
33
delay between sent messages, this will use dcc chat to deliver the message.
 
34
on jabber bots there is no delay
 
35
 
 
36
3) toudp
 
37
 
 
38
::
 
39
 
 
40
  # files/toudp.py
 
41
  
 
42
  use this script to pipeline a programs output to the bot
 
43
  
 
44
  example: tail -f /var/log/httpd-access.log | ./todup.py
 
45
"""
 
46
 
6
47
 
7
48
__copyright__ = 'this file is in the public domain'
8
49
 
47
88
        self.loggers = []
48
89
 
49
90
    def _outloop(self):
50
 
        rlog(10, 'udp', 'starting outloop')
 
91
        rlog(5, 'udp', 'starting outloop')
51
92
        while not self.stop:
52
93
            (printto, txt) = self.outqueue.get()
53
94
            if self.stop:
54
95
                return
55
96
            self.dosay(printto, txt)
56
 
        rlog(10, 'udp', 'stopping outloop')
 
97
        rlog(5, 'udp', 'stopping outloop')
57
98
 
58
99
    def _handleloop(self):
59
100
        while not self.stop:
65
106
            self._handle(input, addr)
66
107
            if config['udpsleep']:
67
108
                time.sleep(config['udpsleep'] or 0.001)
68
 
        rlog(10, 'udp', 'shutting down udplistener')
 
109
        rlog(5, 'udp', 'shutting down udplistener')
69
110
 
70
111
    def _listen(self):
71
112
        """ listen for udp messages .. /msg via bot"""
72
113
        if not config['udp']:
73
 
            rlog(0, 'udp', 'udp not enabled')
74
114
            return
75
115
        try:
76
116
            fleet.startok.wait()
 
117
            bot = fleet.getmainbot()
 
118
            if not bot:
 
119
                rlog(10, 'udp', "can't find main bot .. not starting")
 
120
                return
77
121
            self.sock.bind((config['udphost'], config['udpport']))
78
122
            rlog(10, 'udp', 'udp listening on %s %s' % (config['udphost'], \
79
123
config['udpport']))
84
128
            self.stop = 1
85
129
            return
86
130
        # loop on listening udp socket
87
 
        bot = fleet.getmainbot()
88
 
        if not bot:
89
 
            rlog(10, 'udp', "can't find main bot")
90
 
            return
91
131
        bot.connectok.wait()
92
132
        while not self.stop:
93
133
            try:
111
151
            if self.stop:
112
152
                break
113
153
            self.queue.put((input, addr))
114
 
        rlog(10, 'udp', 'shutting down main loop')
 
154
        rlog(5, 'udp', 'shutting down main loop')
115
155
 
116
156
    def _handle(self, input, addr):
117
157
        if config['udpseed']:
165
205
        for i in self.loggers:
166
206
            i.log(printto, txt)
167
207
 
168
 
udplistener = Udplistener()
 
208
if config['udp']:
 
209
    udplistener = Udplistener()
169
210
 
170
 
if config['udpseed']:
 
211
if config['udp'] and config['udpseed']:
171
212
    crypt = rijndael(config['udpseed'])
172
213
 
173
214
def init():
174
215
    """ init the plugin """
175
 
    start_new_thread(udplistener._listen, ())
176
 
    start_new_thread(udplistener._handleloop, ())
177
 
    start_new_thread(udplistener._outloop, ())
 
216
    if config['udp']:
 
217
        start_new_thread(udplistener._listen, ())
 
218
        start_new_thread(udplistener._handleloop, ())
 
219
        start_new_thread(udplistener._outloop, ())
178
220
    return 1
179
221
    
180
222
def shutdown():
181
223
    """ shutdown the plugin """
182
 
    udplistener.stop = 1
183
 
    udplistener.outqueue.put_nowait((None, None))
184
 
    udplistener.queue.put_nowait((None, None))
 
224
    if config['udp']:
 
225
        udplistener.stop = 1
 
226
        udplistener.outqueue.put_nowait((None, None))
 
227
        udplistener.queue.put_nowait((None, None))
185
228
    return 1
186
229
 
187
230
if config['dbenable'] and config['udpdblog']: