summaryrefslogtreecommitdiffstats
path: root/warmachine.py
blob: 0a358fc637cc0423cae4f4f60065e025b13dc97c (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
import socket
import sys
from actions.ActionMap import *
from passiveactions.PassiveActionMap import *
from conf.users import *
from wmd import parser
class irc:

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

    def setServer(self, server):
        """
        Sets the servername.
        """
        self.server = server
    def setNick(self, nick):
        """
        Sets the nickname.
        """
        self.nick = nick

    def setName(self, name):
        """
        Sets the name.
        """
        self.name = name

    def setPort(self, port):
        """
        Sets the port number to connect to.
        """
        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 MainLoop(self):
        """
        Main Event Loop that parses generic commands
        """
        while True:
            data = self.irc.recv(4096)
            #obj_data = parser.ircparse(data)

            # Passive Actions
            try:
                for key in passiveactions.keys():
                    pa = passiveactions[key].getAction(data, user)
                    if pa:
                        self.send(pa)
            # Direct Actions
                if data.find(self.nick + ':') != -1:
                    curuser = data[1:data.index('!')]
                    if curuser in user:
                        input = data.split()
                        for key in actions.keys():
                            if data.find(key) != -1:
                                self.send(actions[key].getAction(data))
                                break
                    else:
                        input = data.split()
                        self.send('PRIVMSG ' + input[2] + ' :' + curuser + ': stop bothering me jerk.')
            except Exception,e:
                print "Action failed"
                print e


if __name__ == '__main__':
    i = irc('irc.efnet.org', 'warmachine', 'omgident')
    i.connect()
    i.join('#zzq')
    i.MainLoop()