summaryrefslogtreecommitdiffstats
path: root/.graveyard/punani/bot/__init__.py
blob: 4944bba09e062ba987ed43a7deb4d1dfbba7f5b8 (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
#!/usr/bin/python
from Queue import Queue
from SocketServer import BaseRequestHandler, ThreadingTCPServer
from threading import Thread
from time import sleep, strftime, strptime

from ircbot import SingleServerIRCBot
from irclib import nm_to_n


class PunaniRequestHandler(BaseRequestHandler):
    """Handler for Punani messages."""

    def handle(self):
        try:
            msg = self.request.recv(1024).strip()
        except ValueError:
            msg = 'Invalid message.'
        else:
            self.server.queue.put((self.client_address, msg))
        print ('%s:%d' % self.client_address), str(msg)


class PunaniReceiveServer(ThreadingTCPServer):
    """UDP server that waits for Punani messages."""

    def __init__(self):
        ThreadingTCPServer.__init__(self,
                ('127.0.0.1', 5555),
                PunaniRequestHandler)
        self.queue = Queue()


class PunaniBot(SingleServerIRCBot):

    def __init__(self, server_list, channel_list, nickname='punani-ircbot',
            realname='Bob Ross'):
        self.reconnection_interval = 30
        SingleServerIRCBot.__init__(self, server_list, nickname, realname)
        self.channel_list = channel_list

    def on_welcome(self, conn, event):
        """Join channels after connect."""
        print 'Connected to %s:%d.' % conn.socket.getsockname()
        for channel, key in self.channel_list:
            conn.join(channel, key)

    def on_nicknameinuse(self, conn, event):
        """Choose another nickname if conflicting."""
        self._nickname += '_'
        conn.nick(self._nickname)

    def on_ctcp(self, conn, event):
        """Answer CTCP PING and VERSION queries."""
        whonick = nm_to_n(event.source())
        message = event.arguments()[0].lower()
        if message == 'version':
            conn.notice(whonick, 'Punani2irc')
        elif message == 'ping':
            conn.pong(whonick)

    def on_privmsg(self, conn, event):
        """React on private messages.

        Die, for example.
        """
        whonick = nm_to_n(event.source())
        message = event.arguments()[0]
        if message == 'die!':
            print 'Shutting down as requested by %s...' % whonick
            self.die('Shutting down.')

    def say(self, msg):
        """Say message to channels."""
        for channel, key in self.channel_list:
            self.connection.privmsg(channel, msg)


def process_queue(announce_callback, queue, delay=2):
    """Process received messages in queue."""
    while True:
        sleep(delay)
        try:
            addr, msg = queue.get()
        except Empty:
            continue
        #do something with the addr?
        announce_callback(str(msg))
if __name__ == '__main__':
    # Set IRC connection parameters.
    irc_servers = [('supernode', 6667)]
    irc_channels = [('#retiolum', '')]

    # Prepare and start IRC bot.
    bot = PunaniBot(irc_servers, irc_channels)
    t = Thread(target=bot.start)
    t.daemon = True
    t.start()
    announce = bot.say

    receiver = PunaniReceiveServer()
    t = Thread(target=process_queue, args=(announce, receiver.queue))
    t.daemon = True
    t.start()
    receiver.serve_forever()