Mercurial > repo
comparison a/d/DCPUToolBot-master/irc.py @ 2768:730e97409041
<ThatOtherPerson> mv d a
author | HackBot |
---|---|
date | Mon, 22 Apr 2013 15:44:16 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2767:2aa9e7b74c84 | 2768:730e97409041 |
---|---|
1 import socket | |
2 import re | |
3 import threading | |
4 import sys | |
5 | |
6 command_handlers = [] | |
7 privmsg_handlers = [] | |
8 msgtome_handlers = [] | |
9 | |
10 def command(command, params): | |
11 print "Command: (\"" + command + "\", \"" + params + ")" | |
12 msg = command + " " + params | |
13 print msg | |
14 server.sendall(msg + "\r\n") | |
15 | |
16 def privmsg(nickIn, chan, msg): | |
17 | |
18 lines = msg.split("\n") | |
19 if len(lines) > 1: | |
20 for line in lines: | |
21 privmsg(nickIn, chan, line) | |
22 elif msg != "": | |
23 response = "" | |
24 | |
25 if chan == nick: | |
26 response += nickIn + " :" + msg | |
27 else: | |
28 response += chan + " :" + nickIn + ": " + msg | |
29 | |
30 command("PRIVMSG", response) | |
31 | |
32 def handlePing(nick, user, host, chan, params): | |
33 print params | |
34 command('PONG', params) | |
35 | |
36 def handleMsgToMe(nick, user, host, chan, params): | |
37 for regex, callback in msgtome_handlers: | |
38 matches = regex.match(params) | |
39 if matches: | |
40 callback(nick, user, host, chan, params) | |
41 | |
42 def handlePrivmsg(nickIn, user, host, chan, params): | |
43 for regex, callback in privmsg_handlers: | |
44 matches = regex.match(params) | |
45 if matches: | |
46 callback(nickIn, user, host, chan, matches) | |
47 | |
48 global nick | |
49 matches = re.match("^" + nick + "[:, ]?(.*)", params) | |
50 if matches: | |
51 handleMsgToMe(nickIn, user, host, chan, matches.group(1)) | |
52 | |
53 if chan == nick: | |
54 handleMsgToMe(nickIn, user, host, chan, params) | |
55 | |
56 class EventHandler(threading.Thread): | |
57 def run(self): | |
58 while True: | |
59 try: | |
60 message = server.recv(4096) | |
61 if message == '': | |
62 server.close() | |
63 | |
64 handleCommand(message) | |
65 except Exception as e: | |
66 print e.message | |
67 | |
68 def connect(host, port, nickIn="TestBot", password="", name="dcpubot", realname="DCPU Bot"): | |
69 global nick | |
70 nick = nickIn | |
71 | |
72 global server | |
73 server = socket.create_connection((host, port)) | |
74 | |
75 server.sendall("PASS " + password + "\r\n") | |
76 server.sendall("NICK " + nick + "\r\n") | |
77 server.sendall("USER " + name + " 0 * :" + realname + "\r\n") | |
78 | |
79 onCommand('PING', handlePing) | |
80 onCommand('PRIVMSG', handlePrivmsg) | |
81 | |
82 eventHandler = EventHandler() | |
83 eventHandler.start() | |
84 | |
85 def join(channels): | |
86 print channels | |
87 if type(channels) is str: | |
88 print "JOIN " + channels | |
89 server.sendall("JOIN " + channels + "\r\n") | |
90 else: | |
91 for channel in channels: | |
92 print "JOIN " + channel | |
93 server.sendall("JOIN " + channel + "\r\n") | |
94 | |
95 message_re = re.compile("^((:([^!@ ]+)(!([^@ ]+))?(@([^ ]+))? ?)?([^ ]+)?)? ?((?!:)[^ ]*)[^:]*(:(.*))?") | |
96 | |
97 def handleCommand(message): | |
98 print "Message: " + message | |
99 message_data = message_re.match(message) | |
100 | |
101 if message_data: | |
102 | |
103 nick = message_data.group(3) | |
104 user = message_data.group(5) | |
105 host = message_data.group(7) | |
106 command = message_data.group(8) | |
107 chan = message_data.group(9) | |
108 params = message_data.group(11) | |
109 | |
110 for com, callback in command_handlers: | |
111 if com == command: | |
112 callback(nick, user, host, chan, params) | |
113 else: | |
114 print "Message could not be parsed: " + message | |
115 | |
116 def onCommand(command, callback): | |
117 global command_handlers | |
118 command_handlers.append((command, callback)) | |
119 | |
120 def onPrivmsg(reg, callback): | |
121 global privmsg_handlers | |
122 regex = re.compile(reg) | |
123 privmsg_handlers.append((regex, callback)) | |
124 | |
125 def onMsgToMe(reg, callback): | |
126 global msgtome_handlers | |
127 regex = re.compile(reg) | |
128 msgtome_handlers.append((regex, callback)) |