This file is indexed.

/usr/lib/python2.7/dist-packages/gplugs/udp.py is in gozerbot 0.99.1-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# plugs/udp.py
#
#

"""
    the bot has the capability to listen for udp packets which it will use
    to /msg a given nick or channel.

    1) setup

        * do !reload udp to enable the udp plugin
        * call !udp-cfgsave to generate a config file in gozerdata/plugs/udp/config
        * edit this file .. esp. look at the udpallowednicks list
        * run ./bin/gozerbot-udp -s to generate clientside config file "udp-send"
        * edit this file
        * test with:

        ::

            echo "YOOO" | ./bin/gozerbot-udp

    2) limiter

        on IRC the bot's /msg to a user/channel are limited to 1 per 3 seconds so the
        bot will not excessflood on the server. you can use partyudp if you need no 
        delay between sent messages, this will use dcc chat to deliver the message.
        on jabber bots there is no delay

"""


__copyright__ = 'this file is in the public domain'

# IMPORT SECTION

from gozerbot.fleet import fleet
from gozerbot.generic import rlog, handle_exception, strippedtxt, lockdec
from gozerbot.config import config
from gozerbot.plughelp import plughelp
from gozerbot.partyline import partyline
from gozerbot.threads.thr import start_new_thread
from gozerbot.contrib.rijndael import rijndael
from gozerbot.persist.persistconfig import PersistConfig

import socket, re, time, Queue

# END IMPORT

plughelp.add('udp' , 'run the udp listen thread')

# VARS SECTION

cfg = PersistConfig()
cfg.define('udp', 1) # set to 0 to disnable
cfg.define('udpparty', 0)
cfg.define('udpipv6', 0)
cfg.define('udpmasks', ['192.168*', ])
cfg.define('udphost', "localhost")
cfg.define('udpport', 5500)
cfg.define('udpallow', ["127.0.0.1", ])
cfg.define('udpallowednicks', ["#gozerbot", "dunker"])
cfg.define('udppassword', "mekker", exposed=False)
cfg.define('udpseed', "blablablablablaz", exposed=False) # needs to be 16 chars wide
cfg.define('udpstrip', 1) # strip all chars < char(32)
cfg.define('udpsleep', 0) # sleep in sendloop .. can be used to delay pack
cfg.define('udpdblog', 0)
cfg.define('udpbots', [cfg['udpbot'] or 'default', ])

# END VARS

def _inmask(addr):

    """ check if addr matches a mask. """

    if not cfg['udpmasks']:
        return False
    for i in cfg['udpmasks']:
        i = i.replace('*', '.*')
        if re.match(i, addr):
            return True

class Udplistener(object):

    """ 
        listen for udp messages.

    """

    def __init__(self):
        self.outqueue = Queue.Queue()
        self.queue = Queue.Queue()
        self.stop = 0
        if cfg['udpipv6']:
            self.sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
        else:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        try:
            self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        except:
            pass
        self.sock.setblocking(1)
        self.sock.settimeout(1)
        self.loggers = []

    def _outloop(self):

        """ 
            loop controling the rate of outputted messages.

            .. literalinclude:: ../../gozerplugs/udp.py
                :pyobject: Udplistener._outloop

        """

        rlog(5, 'udp', 'starting outloop')
        while not self.stop:
            (printto, txt) = self.outqueue.get()
            if self.stop:
                return
            self.dosay(printto, txt)
        rlog(5, 'udp', 'stopping outloop')

    def _handleloop(self):
        while not self.stop:
            (input, addr) = self.queue.get()
            if not input or not addr:
                continue
            if self.stop:
                break                
            self.handle(input, addr)
            if cfg['udpsleep']:
                time.sleep(cfg['udpsleep'] or 0.01)
        rlog(5, 'udp', 'shutting down udplistener')

    def _listen(self):
        """ listen for udp messages .. /msg via bot"""
        if not cfg['udp']:
            return
        for botname in cfg['udpbots']:
            if not fleet.byname(botname):
                rlog(10, 'udp', "can't find %s bot" % botname)
                
        try:
            fleet.startok.wait(5)
            self.sock.bind((cfg['udphost'], cfg['udpport']))
            rlog(10, 'udp', 'udp listening on %s %s' % (cfg['udphost'], \
cfg['udpport']))
            self.stop = 0
        except IOError:
            handle_exception()
            self.sock = None
            self.stop = 1
            return
        # loop on listening udp socket
        while not self.stop:
            try:
                input, addr = self.sock.recvfrom(64000)
            except socket.timeout:
                continue
            except Exception, ex:
                try:
                    (errno, errstr) = ex
                except ValueError:
                    errno = 0
                    errstr = str(ex)
                if errno == 4:
                    rlog(10, self.name, str(ex))
                    break
                if errno == 35:
                    continue
                else:
                    handle_exception()
                    break
            if self.stop:
                break
            self.queue.put((input, addr))
        rlog(5, 'udp', 'shutting down main loop')

    def handle(self, input, addr):

        """ 
            handle an incoming udp packet. 

            :param input: txt in udp packet
            :type input: string
            :param addr: address info of udp packet
            :type add: (host, port) tuple

            .. literalinclude:: ../../gozerplugs/udp.py
                :pyobject: Udplistener.handle
        """

        if cfg['udpseed']:
            data = ""
            for i in range(len(input)/16):
                try:
                    data += crypt.decrypt(input[i*16:i*16+16])
                except Exception, ex:
                    rlog(10, 'udp', "can't decrypt: %s" % str(ex))
                    data = input
                    break
        else:
            data = input
        if cfg['udpstrip']:
            data = strippedtxt(data)
        # check if udp is enabled and source ip is in udpallow list
        if cfg['udp'] and (addr[0] in cfg['udpallow'] or \
_inmask(addr[0])):
            # get printto and passwd data
            header = re.search('(\S+) (\S+) (.*)', data)
            if header:
                # check password
                if header.group(1) == cfg['udppassword']:
                    printto = header.group(2)    # is the nick/channel
                    # check if printto is in allowednicks
                    if not printto in cfg['udpallowednicks']:
                        rlog(10, 'udp', "udp denied %s" % printto )
                        return
                    rlog(0, 'udp', str(addr[0]) +  " udp allowed")
                    text = header.group(3)    # is the text
                    self.say(printto, text)
                else:
                    rlog(10, 'udp', "can't match udppasswd from " + \
str(addr))
            else:
                rlog(10, 'udp', "can't match udp from " + str(addr[0]))
        else:
            rlog(10, 'udp', 'denied udp from ' + str(addr[0]))

    def say(self, printto, txt):

        """ 
            send txt to printto. 
 
            .. literalinclude:: ../../gozerplugs/udp.py
                :pyobject: Udplistener.say

        """

        self.outqueue.put((printto, txt))

    def dosay(self, printto, txt):

        """ 
            send txt to printto .. do some checks. 

           .. literalinclude:: ../../gozerplugs/udp.py
                :pyobject: Udplistener.dosay

        """

        if cfg['udpparty'] and partyline.is_on(printto):
            partyline.say_nick(printto, txt)
            return
        if not cfg['udpbots']:
            bots = [cfg['udpbot'], ]
        else:
            bots = cfg['udpbots']
        for botname in bots:
            bot = fleet.byname(botname)
            if not bot:
                rlog(10, 'udp', "can't find %s bot in fleet" % botname)
                continue
            #if not bot.jabber and not cfg['nolimiter']:
            #    time.sleep(3)
            bot.connectok.wait()
            bot.say(printto, txt)
            for i in self.loggers:
                i.log(printto, txt)

# the udplistener object
if cfg['udp']:
    udplistener = Udplistener()

# initialize crypt object if udpseed is set in config
if cfg['udp'] and cfg['udpseed']:
    crypt = rijndael(cfg['udpseed'])

def init():

    """ 
        init the udp plugin. 

        .. literalinclude:: ../../gozerplugs/udp.py
            :pyobject: init

    """

    if cfg['udp']:
        start_new_thread(udplistener._listen, ())
        start_new_thread(udplistener._handleloop, ())
        start_new_thread(udplistener._outloop, ())
    return 1
    
def shutdown():

    """ 
        shutdown the udp plugin.

        .. literalinclude:: ../../gozerplugs/udp.py
            :pyobject: init

    """

    if cfg['udp'] and udplistener:
        udplistener.stop = 1
        udplistener.outqueue.put_nowait((None, None))
        udplistener.queue.put_nowait((None, None))
    return 1

if cfg['udp'] and cfg['udpdblog']:

    from gozerbot.database.db import Db

    class Udpdblog:

        """ log udp data to database. """

        # see tables/udplog for table definition and add udpdblog = 1 to 
        # the config file
        db = Db()

        def log(self, printto, txt):

            """ do the actual logging. """

            try:
                res = self.db.execute("""INSERT into udplog(time,printto,txt)
values(%s,%s,%s) """, (time.time(), printto, txt))
            except Exception, ex:
                rlog(10, 'udp', 'failed to log to db: %s' % str(ex))
            return res

    udplistener.loggers.append(Udpdblog())
    rlog(10, 'udp', 'registered database udp logger')