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

    def locaActions(self):
        """
        Loads the actions internally.
        """
        pass

    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.send('JOIN ' + chan)

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

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

    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 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
                try:
                    for key in passiveactions.keys():
                        pa = passiveactions[key].getAction(obj_data, user)
                        if pa:
                            self.send(pa)

                    if obj_data.params.find(self.nick + ':') > -1 or obj_data.params.find(':!') > -1:
                        curuser = obj_data.getUsername()
                        if curuser not in user:
                            continue
                        for key in actions.keys():
                            if obj_data.params.find(key) > -1:
                                self.send(actions[key].getAction(obj_data))

                except Exception,e:
                    print "Action failed"
                    print e