summaryrefslogtreecommitdiffstats
path: root/wmd/irc.py
blob: dd99f5c6b26d0bb7807b25dc01abb82d1818905f (plain) (blame)
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
import socket

from wmd import parser

import settings

class IRC(object):

    def __init__(self, server=None, nick=None, name=None, port=6667):
        """
        IRC connection library that needs at least server, nick and name
        """
        self.server = server
        self.nick = nick
        self.name = name
        self.port = port
        self.actions = dict()

        self.load_actions()

    def connect(self):
        """
        Connects to the irc server.
        """
        self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.irc.connect((self.server, self.port))
        self.log(self.irc.recv(4096))
        self.irc.send('NICK ' + self.nick + '\r\n')
        self.irc.send('USER ' + self.name + ' 8 * :Warmachine\r\n')

    def join(self, chan):
        """
        Joins a channel
        """
        self.rawsend('JOIN ' + chan)

    def log(self, string):
        """
        Takes care of log formating.
        """
        print 'log: ' + string

    def privmsg(self, dest, msg):
        """
        send a privmsg to dest
        """
        self.rawsend('PRIVMSG %s :%s' % (dest, msg))

    def rawsend(self, command):
        """
        Sends commands straight to the irc server.
        """
        self.irc.send(command + '\r\n')

    def load_actions(self):
        """
        Loads the actions internally.
        """
        for action in settings.ACTIONS:
            self.load_action(action)

    def load_action(self, path):
        """
        Loads the provided action
        """
        module_name, class_name = path.rsplit('.', 1)
        module = __import__(module_name, globals(), locals(), [class_name], -1)
        classz = getattr(module, class_name)
        self.actions[class_name] = classz()

    def __call__(self, *args, **kwargs):
        """
        Main Event Loop that parses generic commands
        """
        while True:
            data = self.irc.recv(4096)

            if data == '':
                continue

            # Buffering for MOTD.
            if data[-1] != '\n':
                data = data + self.irc.recv(4096)

            # For dynamic loading, we have to add to the actions dictionary after we're through looping. Maybe there
            # are some other cases to use the post_loop_commands
            for line in data.split('\r\n'):
                obj_data = parser.IRCParse(line)
                #pass to action handlers here...
                if (obj_data.prefix == '') and (obj_data.command == '') and (obj_data.params == ''):
                    continue

                print "<- " + obj_data.prefix + "~" + obj_data.command + "~" + obj_data.params

                modules_to_load = []
                for plugin_name in self.actions:
                    retval = self.actions[plugin_name].recv_msg(self, obj_data)
                    if type(retval) == type(dict()):
                        if retval.has_key('load'):
                            modules_to_load.append(retval['load'])
                    elif type(retval) == type('str is str'):
                        self.rawsend(retval)

                for module in modules_to_load:
                    self.load_action(module)