Fri, 13 Jun 2014 00:25:52 +0300
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
0 | 1 | #!/usr/bin/env python |
2 | ''' | |
3 | Copyright 2014 Santeri Piippo | |
4 | All rights reserved. | |
5 | ||
6 | Redistribution and use in source and binary forms, with or without | |
7 | modification, are permitted provided that the following conditions | |
8 | are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright | |
11 | notice, this list of conditions and the following disclaimer. | |
12 | 2. Redistributions in binary form must reproduce the above copyright | |
13 | notice, this list of conditions and the following disclaimer in the | |
14 | documentation and/or other materials provided with the distribution. | |
15 | 3. The name of the author may not be used to endorse or promote products | |
16 | derived from this software without specific prior written permission. | |
17 | ||
18 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
19 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
20 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
21 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
22 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
23 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
27 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | ''' | |
29 | ||
30 | import asyncore | |
31 | import socket | |
32 | import time | |
33 | import sys | |
34 | import traceback | |
35 | import re | |
36 | import json | |
2
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
37 | import urllib |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
38 | import urllib2 |
5
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
39 | import hgapi |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
40 | import os |
0 | 41 | from suds.xsd.doctor import Import |
42 | from suds.xsd.doctor import ImportDoctor | |
43 | from suds.client import Client | |
44 | ||
45 | try: | |
46 | with open ('cobalt.json', 'r') as fp: | |
47 | g_config = json.loads (fp.read()) | |
48 | except IOError as e: | |
49 | print 'couldn\'t open cobalt.json: %s' % e | |
50 | quit() | |
51 | ||
52 | g_admins = g_config['admins'] | |
53 | g_mynick = g_config['nickname'] | |
54 | ||
2
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
55 | g_idgamesSearchURL = 'http://www.doomworld.com/idgames/api/api.php?action=search&query=%s&type=title&sort=date&out=json' |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
56 | |
0 | 57 | # |
58 | # SOAP stuff | |
59 | # | |
3
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
60 | suds_active = False |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
61 | |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
62 | try: |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
63 | suds_import = Import ('http://schemas.xmlsoap.org/soap/encoding/', \ |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
64 | 'http://schemas.xmlsoap.org/soap/encoding/') |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
65 | suds_client = Client ('https://zandronum.com/tracker/api/soap/mantisconnect.php?wsdl', \ |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
66 | plugins=[ImportDoctor (suds_import)]) |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
67 | suds_active = True |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
68 | except Exception: |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
69 | pass |
0 | 70 | |
71 | # | |
72 | # irc_client flags | |
73 | # | |
74 | CLIF_CONTROL = (1 << 0) | |
75 | CLIF_CONNECTED = (1 << 1) | |
76 | ||
77 | # | |
78 | # List of all clients | |
79 | # | |
80 | g_clients = [] | |
81 | ||
82 | class channel (object): | |
83 | name = "" | |
84 | password = "" | |
85 | ||
86 | def __init__ (self, name): | |
87 | self.name = name | |
88 | ||
89 | # | |
90 | # Prints a line to control channel(s) | |
91 | # | |
92 | def control (line): | |
93 | for client in g_clients: | |
94 | if client.flags & (CLIF_CONTROL|CLIF_CONNECTED) == (CLIF_CONTROL|CLIF_CONNECTED): | |
95 | client.write ("PRIVMSG %s :%s" % (client.channels[0]['name'], line)) | |
96 | ||
97 | # | |
98 | # Exception handling | |
99 | # | |
100 | def handle_exception(excType, excValue, trace): | |
101 | excepterm (traceback.format_exception(excType, excValue, trace)) | |
102 | ||
103 | def excepterm(data): | |
104 | for segment in data: | |
105 | for line in segment.splitlines(): | |
106 | print line | |
107 | control (line) | |
108 | for client in g_clients: | |
5
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
109 | if len(data) > 0: |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
110 | client.exceptdie() |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
111 | else: |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
112 | client.quit_irc() |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
113 | restart_self() |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
114 | #quit() |
0 | 115 | |
116 | sys.excepthook = handle_exception | |
117 | ||
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
118 | def check_admin (sender, ident, host, command): |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
119 | if not "%s!%s@%s" % (sender, ident, host) in g_admins: |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
120 | raise logical_exception (".%s requires admin access" % command) |
0 | 121 | |
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
122 | class logical_exception (Exception): |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
123 | def __init__ (self, value): |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
124 | self.value = value |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
125 | def __str__ (self): |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
126 | return self.value |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
127 | |
5
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
128 | # from http://www.daniweb.com/software-development/python/code/260268/restart-your-python-program |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
129 | def restart_self(): |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
130 | python = sys.executable |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
131 | os.execl (python, python, * sys.argv) |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
132 | |
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
133 | # |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
134 | # Main IRC client class |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
135 | # |
0 | 136 | class irc_client (asyncore.dispatcher): |
137 | def __init__ (self, cfg, flags): | |
138 | self.name = cfg['name'] | |
139 | self.host = cfg['address'] | |
140 | self.port = cfg['port'] | |
141 | self.password = cfg['password'] if 'password' in cfg else '' | |
142 | self.channels = cfg['channels'] | |
143 | self.flags = flags | |
144 | self.send_buffer = list() | |
145 | self.umode = cfg['umode'] if 'umode' in cfg else '' | |
146 | self.cfg = cfg | |
147 | self.mynick = '' | |
7
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
148 | self.desired_name = self.cfg['nickname'] if 'nickname' in self.cfg else g_config['nickname'] |
0 | 149 | g_clients.append (self) |
150 | asyncore.dispatcher.__init__ (self) | |
151 | self.create_socket (socket.AF_INET, socket.SOCK_STREAM) | |
152 | self.connect ((self.host, self.port)) | |
153 | ||
7
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
154 | def register_to_irc (self): |
0 | 155 | ident = self.cfg['ident'] if 'ident' in self.cfg else g_config['ident'] |
156 | gecos = self.cfg['gecos'] if 'gecos' in self.cfg else g_config['gecos'] | |
157 | if 'password' in self.cfg: | |
158 | self.write ("PASS %s" % self.cfg['password']) | |
159 | self.write ("USER %s * * :%s" % (ident, gecos)) | |
7
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
160 | self.write ("NICK %s" % self.mynick) |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
161 | |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
162 | def handle_connect (self): |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
163 | self.mynick = self.desired_name |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
164 | print "Connected to [%s] %s:%d" % (self.name, self.host, self.port) |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
165 | self.register_to_irc() |
0 | 166 | |
167 | def write (self, data): | |
168 | self.send_buffer.append ("%s" % data) | |
169 | ||
170 | def handle_close (self): | |
171 | print "Connection to [%s] %s:%d terminated." % (self.name, self.host, self.port) | |
172 | self.close() | |
173 | ||
174 | def handle_write (self): | |
175 | self.send_all_now() | |
176 | ||
177 | def readable (self): | |
178 | return True | |
179 | ||
180 | def writable (self): | |
181 | return len (self.send_buffer) > 0 | |
182 | ||
183 | def send_all_now (self): | |
184 | for line in self.send_buffer: | |
185 | print "[%s] <- %s" % (self.name, line) | |
186 | self.send ("%s\n" % line) | |
187 | self.send_buffer = [] | |
188 | ||
189 | def handle_read (self): | |
190 | lines = self.recv (4096).splitlines() | |
2
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
191 | for utfline in lines: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
192 | line = utfline.decode("utf-8").encode("ascii","ignore") |
0 | 193 | print "[%s] -> %s" % (self.name, line) |
194 | ||
195 | if line.startswith ("PING :"): | |
196 | self.write ("PONG :%s" % line[6:]) | |
197 | continue | |
198 | ||
199 | words = line.split(" ") | |
7
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
200 | if len(words) >= 2: |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
201 | if words[1] == "001": |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
202 | self.flags |= CLIF_CONNECTED |
0 | 203 | |
7
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
204 | for channel in self.cfg['channels']: |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
205 | self.write ("JOIN %s %s" % (channel['name'], channel['password'] if 'password' in channel else '')) |
0 | 206 | |
7
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
207 | if 'umode' in self.cfg: |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
208 | self.write ('MODE %s %s' % (self.mynick, self.cfg['umode'])) |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
209 | elif words[1] == "PRIVMSG": |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
210 | self.handle_privmsg (line) |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
211 | elif words[1] == 'QUIT': |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
212 | rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) QUIT') |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
213 | match = rex.match (line) |
0 | 214 | |
7
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
215 | # Try reclaim our nickname if possible |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
216 | if match and match.group(1) == self.desired_name: |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
217 | self.mynick = self.desired_name |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
218 | self.write ("NICK %s" % self.mynick) |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
219 | elif words[1] == "433": |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
220 | #:irc.localhost 433 * cobalt :Nickname is already in use. |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
221 | self.mynick = '%s_' % self.mynick |
438dc247ceb9
- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected
Santeri Piippo <crimsondusk64@gmail.com>
parents:
6
diff
changeset
|
222 | self.write ("NICK %s" % self.mynick) |
0 | 223 | |
5
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
224 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
225 | # |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
226 | # Handle a PRIVMSG line from the IRC server |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
227 | # |
0 | 228 | def handle_privmsg (self, line): |
229 | rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)$') | |
230 | match = rex.match (line) | |
231 | if match: | |
232 | sender = match.group (1) | |
233 | user = match.group (2) | |
234 | host = match.group (3) | |
235 | channel = match.group (4) | |
236 | message = match.group (5) | |
237 | replyto = channel if channel != g_mynick else sender | |
238 | ||
239 | # Check for tracker url in the message | |
240 | http_regex = re.compile (r'.*http(s?)://%s/view\.php\?id=([0-9]+).*' % g_config['trackerurl']) | |
241 | http_match = http_regex.match (line) | |
242 | ||
243 | # Check for command. | |
244 | if len(message) >= 2 and message[0] == '.' and message[1] != '.': | |
245 | stuff = message[1:].split(' ') | |
246 | command = stuff[0] | |
247 | args = stuff[1:] | |
248 | try: | |
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
249 | self.handle_command (sender, user, host, replyto, command, args) |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
250 | except logical_exception as msg: |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
251 | self.privmsg (replyto, "error: %s" % msg) |
0 | 252 | elif http_match: |
253 | self.get_ticket_data (replyto, http_match.group (2), False) | |
254 | else: | |
255 | control ("Recieved bad PRIVMSG: %s" % line) | |
256 | ||
5
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
257 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
258 | # |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
259 | # Retrieve a ticket from mantisbt |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
260 | # |
0 | 261 | def get_ticket_data (self, replyto, ticket, withlink): |
3
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
262 | if suds_active == False: |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
263 | return |
53486417a8e5
- handle the case where the mantisbt is not available
Santeri Piippo <crimsondusk64@gmail.com>
parents:
2
diff
changeset
|
264 | |
0 | 265 | data = {} |
266 | try: | |
267 | data = suds_client.service.mc_issue_get (g_config['trackeruser'], g_config ['trackerpassword'], ticket) | |
268 | except Exception, e: | |
269 | self.privmsg (replyto, "Failed to get info for issue %s: %s" % (ticket, `e`)) | |
270 | ||
271 | if data: | |
272 | self.privmsg (replyto, "Issue %s: %s: Reporter: %s, assigned to: %s, status: %s (%s)" % \ | |
273 | (ticket, \ | |
274 | data.summary, \ | |
4
bd9508f8a10f
- don't crash if ticket data doesn't have a reporter name (can happen if the reporter is banned)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
3
diff
changeset
|
275 | data.reporter.name if hasattr (data.reporter, 'name') else "<unknown>", \ |
0 | 276 | data.handler.name if hasattr (data, 'handler') else "nobody", \ |
277 | data.status.name, \ | |
278 | data.resolution.name)) | |
279 | ||
280 | if withlink: | |
281 | self.privmsg (replyto, "Read all about it here: https://%s/view.php?id=%s" % (g_config['trackerurl'], ticket)) | |
282 | ||
5
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
283 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
284 | # |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
285 | # Process an IRC command |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
286 | # |
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
287 | def handle_command (self, sender, ident, host, replyto, command, args): |
0 | 288 | if command == "raw": |
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
289 | check_admin (sender, ident, host, command) |
0 | 290 | self.write (" ".join (args)) |
291 | elif command == "msg": | |
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
292 | check_admin (sender, ident, host, command) |
0 | 293 | if len(args) < 2: |
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
294 | raise logical_exception ("usage: .%s <target> <message...>" % command) |
0 | 295 | self.privmsg (args[0], " ".join (args[1:])) |
2
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
296 | elif command == 'ticket': |
0 | 297 | if len(args) != 1: |
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
298 | raise logical_exception ("usage: .%s <ticket>" % command) |
0 | 299 | self.get_ticket_data (replyto, args[0], True) |
2
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
300 | elif command == 'idgames': |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
301 | try: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
302 | if len(args) < 1: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
303 | raise logical_exception ('usage: .%s <keywords>' % command) |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
304 | |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
305 | url = g_idgamesSearchURL % urllib.quote (" ".join (args[0:])) |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
306 | response = urllib2.urlopen (url).read() |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
307 | data = json.loads (response) |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
308 | |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
309 | if 'content' in data and 'file' in data['content']: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
310 | if type (data['content']['file']) is list: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
311 | files = data['content']['file'] |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
312 | else: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
313 | files = [data['content']['file']] |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
314 | |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
315 | i = 0 |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
316 | for filedata in files: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
317 | if i >= 5: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
318 | break |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
319 | |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
320 | self.privmsg (replyto, '- %s: \'%s\' by \'%s\', rating: %s: %s' % \ |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
321 | (filedata['filename'], filedata['title'], filedata['author'], filedata['rating'], filedata['url'])) |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
322 | |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
323 | i += 1 |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
324 | self.privmsg (replyto, "(%d / %d results posted)" % (i, len(files))) |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
325 | elif 'warning' in data and 'message' in data['warning']: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
326 | raise logical_exception (data['warning']['message']) |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
327 | elif 'error' in data and 'message' in data['error']: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
328 | raise logical_exception (data['error']['message']) |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
329 | else: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
330 | raise logical_exception ("Incomplete JSON response from doomworld.com/idgames") |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
331 | except logical_exception as e: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
332 | raise e |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
333 | except Exception as e: |
1a24dd2d598e
- added a basic /idgames search
Santeri Piippo <crimsondusk64@gmail.com>
parents:
1
diff
changeset
|
334 | raise logical_exception ('Search failed: %s' % `e`) |
5
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
335 | elif command == 'restart': |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
336 | check_admin (sender, ident, host, command) |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
337 | excepterm('') |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
338 | elif command == 'update': |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
339 | check_admin (sender, ident, host, command) |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
340 | |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
341 | try: |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
342 | repo = hgapi.Repo ('.') |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
343 | r1 = repo.hg_id() |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
344 | repo.hg_pull() |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
345 | repo.hg_update('tip', True) |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
346 | r2 = repo.hg_id() |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
347 | if r1 != r2: |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
348 | self.privmsg (replyto, 'Updated to %s, restarting...' % r2) |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
349 | excepterm('') |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
350 | else: |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
351 | self.privmsg (replyto, 'Up to date at %s.' % r2) |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
352 | except hgapi.HgException as e: |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
353 | raise logical_exception ('Search failed: %s' % `e`) |
6 | 354 | elif command == 'die': |
355 | check_admin (sender, ident, host, command) | |
356 | quit() | |
5
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
357 | # else: |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
358 | # raise logical_exception ("unknown command `.%s`" % command) |
0 | 359 | |
360 | def handle_error(self): | |
361 | excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) | |
362 | ||
363 | def privmsg (self, channel, msg): | |
364 | self.write ("PRIVMSG %s :%s" % (channel, msg)) | |
365 | ||
5
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
366 | def quit_irc (self): |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
367 | if self.flags & CLIF_CONNECTED: |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
368 | self.write ("QUIT :Leaving") |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
369 | self.send_all_now() |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
370 | self.close() |
b6d2b7de0a6d
- added .restart and .update
Santeri Piippo <crimsondusk64@gmail.com>
parents:
4
diff
changeset
|
371 | |
0 | 372 | def exceptdie (self): |
373 | if self.flags & CLIF_CONNECTED: | |
374 | self.write ("QUIT :Caught exception") | |
375 | self.send_all_now() | |
376 | self.close() | |
377 | ||
378 | def keyboardinterrupt (self): | |
379 | if self.flags & CLIF_CONNECTED: | |
380 | self.write ("QUIT :KeyboardInterrupt") | |
381 | self.send_all_now() | |
382 | self.close() | |
383 | ||
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
384 | # |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
385 | # Main procedure: |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
386 | # |
0 | 387 | try: |
388 | for conndata in g_config['connections']: | |
1
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
389 | if conndata['name'] in g_config['autoconnect']: |
29c7e9d13a30
- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
390 | irc_client (conndata, CLIF_CONTROL if conndata['control'] else 0) |
0 | 391 | asyncore.loop() |
392 | except KeyboardInterrupt: | |
393 | for client in g_clients: | |
394 | client.keyboardinterrupt() | |
395 | quit() |