summaryrefslogtreecommitdiffstats
path: root/wmd/irc.py
blob: 68a53848c3b5a71588a22495409602d9c1218a2c (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
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
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

        # the structure
        #   TODO: Refactor
        # self.actions["module_name"][0]: the actual module. saved to reload if needed
        # self.actions["module_name"][1]: dictionary. key = class name, value = class object
        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)

        if self.actions.has_key(module_name):
            module = self.actions[module_name][0]
        else:
            try:
                module = __import__(module_name, globals(), locals(), [class_name], -1)
            except ImportError:
                self.log("Error loading module: %s" %(path,))
                return

        if not self.actions.has_key(module_name):
            self.actions[module_name] = []
            self.actions[module_name].insert(0, module) # Save the module so it can be reloaded later
            self.actions[module_name].insert(1, dict())
        elif class_name in self.actions[module_name][1]:
            self.log("Class already loaded")
            return

        try:
            classz = getattr(module, class_name)
        except AttributeError:
            self.log("Class does not exist in module")
            return

        self.actions[module_name][1][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 = []
                modules_to_unload = []
                for module_name in self.actions:
                    for class_name in self.actions[module_name][1]:
                        retval = self.actions[module_name][1][class_name].recv_msg(self, obj_data)

                        if type(retval) == type(dict()):
                            if retval.has_key('load'):
                                modules_to_load.append(retval['load'])
                            if retval.has_key('unload'):
                                modules_to_unload.append(retval['unload'])
                            elif type(retval) == type('str is str'):
                                self.rawsend(retval)

                            for module in modules_to_load:
                                self.load_action(module)
                            #for module in modules_to_unload:
                            #    if self.actions.has_key(module):
                            #        del(self.actions[module])