cobalt.py

Fri, 13 Jun 2014 00:25:52 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Fri, 13 Jun 2014 00:25:52 +0300
changeset 7
438dc247ceb9
parent 6
2c6265a99d32
child 8
dd467db4b18e
permissions
-rwxr-xr-x

- handle nickname shenanigans: handle 433 message and try reclaim nickname if someone who has taken it (possibly our own ghost) disconnected

0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 '''
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 Copyright 2014 Santeri Piippo
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 All rights reserved.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 Redistribution and use in source and binary forms, with or without
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 modification, are permitted provided that the following conditions
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 are met:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 1. Redistributions of source code must retain the above copyright
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 notice, this list of conditions and the following disclaimer.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 2. Redistributions in binary form must reproduce the above copyright
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 notice, this list of conditions and the following disclaimer in the
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 documentation and/or other materials provided with the distribution.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 3. The name of the author may not be used to endorse or promote products
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 derived from this software without specific prior written permission.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 '''
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 import asyncore
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 import socket
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 import time
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 import sys
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 import traceback
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 import re
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 from suds.xsd.doctor import Import
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 from suds.xsd.doctor import ImportDoctor
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 from suds.client import Client
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 try:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 with open ('cobalt.json', 'r') as fp:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 g_config = json.loads (fp.read())
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 except IOError as e:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 print 'couldn\'t open cobalt.json: %s' % e
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 quit()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 g_admins = g_config['admins']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 g_mynick = g_config['nickname']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 # SOAP stuff
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 # irc_client flags
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 CLIF_CONTROL = (1 << 0)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 CLIF_CONNECTED = (1 << 1)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 # List of all clients
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 g_clients = []
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 class channel (object):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 name = ""
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 password = ""
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 def __init__ (self, name):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 self.name = name
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 # Prints a line to control channel(s)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 def control (line):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 for client in g_clients:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 if client.flags & (CLIF_CONTROL|CLIF_CONNECTED) == (CLIF_CONTROL|CLIF_CONNECTED):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 client.write ("PRIVMSG %s :%s" % (client.channels[0]['name'], line))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 # Exception handling
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 def handle_exception(excType, excValue, trace):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 excepterm (traceback.format_exception(excType, excValue, trace))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 def excepterm(data):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 for segment in data:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 for line in segment.splitlines():
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 print line
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 control (line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 sys.excepthook = handle_exception
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 class irc_client (asyncore.dispatcher):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 def __init__ (self, cfg, flags):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 self.name = cfg['name']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 self.host = cfg['address']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 self.port = cfg['port']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 self.password = cfg['password'] if 'password' in cfg else ''
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 self.channels = cfg['channels']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 self.flags = flags
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 self.send_buffer = list()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 self.umode = cfg['umode'] if 'umode' in cfg else ''
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 self.cfg = cfg
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 g_clients.append (self)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 asyncore.dispatcher.__init__ (self)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 self.connect ((self.host, self.port))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 ident = self.cfg['ident'] if 'ident' in self.cfg else g_config['ident']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 gecos = self.cfg['gecos'] if 'gecos' in self.cfg else g_config['gecos']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 if 'password' in self.cfg:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 self.write ("PASS %s" % self.cfg['password'])
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 def write (self, data):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 self.send_buffer.append ("%s" % data)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 def handle_close (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 print "Connection to [%s] %s:%d terminated." % (self.name, self.host, self.port)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 self.close()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 def handle_write (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 self.send_all_now()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 def readable (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 return True
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 def writable (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 return len (self.send_buffer) > 0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 def send_all_now (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 for line in self.send_buffer:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 print "[%s] <- %s" % (self.name, line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 self.send ("%s\n" % line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 self.send_buffer = []
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 def handle_read (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 print "[%s] -> %s" % (self.name, line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 if line.startswith ("PING :"):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 self.write ("PONG :%s" % line[6:])
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 continue
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 def handle_privmsg (self, line):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)$')
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 match = rex.match (line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 if match:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 sender = match.group (1)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 user = match.group (2)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 host = match.group (3)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 channel = match.group (4)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 message = match.group (5)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 replyto = channel if channel != g_mynick else sender
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 # Check for tracker url in the message
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 http_regex = re.compile (r'.*http(s?)://%s/view\.php\?id=([0-9]+).*' % g_config['trackerurl'])
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 http_match = http_regex.match (line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 # Check for command.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 if len(message) >= 2 and message[0] == '.' and message[1] != '.':
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 stuff = message[1:].split(' ')
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246 command = stuff[0]
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 args = stuff[1:]
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 elif http_match:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253 self.get_ticket_data (replyto, http_match.group (2), False)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254 else:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255 control ("Recieved bad PRIVMSG: %s" % line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
265 data = {}
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
266 try:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
267 data = suds_client.service.mc_issue_get (g_config['trackeruser'], g_config ['trackerpassword'], ticket)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
268 except Exception, e:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
269 self.privmsg (replyto, "Failed to get info for issue %s: %s" % (ticket, `e`))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
270
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
271 if data:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
272 self.privmsg (replyto, "Issue %s: %s: Reporter: %s, assigned to: %s, status: %s (%s)" % \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273 (ticket, \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
276 data.handler.name if hasattr (data, 'handler') else "nobody", \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
277 data.status.name, \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
278 data.resolution.name))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
279
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
280 if withlink:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
281 self.privmsg (replyto, "Read all about it here: https://%s/view.php?id=%s" % (g_config['trackerurl'], ticket))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
290 self.write (" ".join (args))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
2c6265a99d32 - added .die
Santeri Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
354 elif command == 'die':
2c6265a99d32 - added .die
Santeri Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
355 check_admin (sender, ident, host, command)
2c6265a99d32 - added .die
Santeri Piippo <crimsondusk64@gmail.com>
parents: 5
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
359
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
360 def handle_error(self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
361 excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
362
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
363 def privmsg (self, channel, msg):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
364 self.write ("PRIVMSG %s :%s" % (channel, msg))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
372 def exceptdie (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
373 if self.flags & CLIF_CONNECTED:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
374 self.write ("QUIT :Caught exception")
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
375 self.send_all_now()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
376 self.close()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
377
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
378 def keyboardinterrupt (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
379 if self.flags & CLIF_CONNECTED:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
380 self.write ("QUIT :KeyboardInterrupt")
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
381 self.send_all_now()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
382 self.close()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
387 try:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
391 asyncore.loop()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
392 except KeyboardInterrupt:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
393 for client in g_clients:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
394 client.keyboardinterrupt()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
395 quit()

mercurial