cobalt.py

Tue, 13 May 2014 23:29:37 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Tue, 13 May 2014 23:29:37 +0300
changeset 1
29c7e9d13a30
parent 0
4904744b40eb
child 2
1a24dd2d598e
permissions
-rwxr-xr-x

- fixed up exception handling, no longer connects to every possible connection, rather uses the autoconnect config entry

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
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 from suds.xsd.doctor import Import
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 from suds.xsd.doctor import ImportDoctor
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 from suds.client import Client
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 try:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 with open ('cobalt.json', 'r') as fp:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 g_config = json.loads (fp.read())
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 except IOError as e:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 print 'couldn\'t open cobalt.json: %s' % e
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 quit()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 g_admins = g_config['admins']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 g_mynick = g_config['nickname']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 # SOAP stuff
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 suds_import = Import ('http://schemas.xmlsoap.org/soap/encoding/', \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 'http://schemas.xmlsoap.org/soap/encoding/')
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 suds_client = Client ('https://zandronum.com/tracker/api/soap/mantisconnect.php?wsdl', \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 plugins=[ImportDoctor (suds_import)])
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 # irc_client flags
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 CLIF_CONTROL = (1 << 0)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 CLIF_CONNECTED = (1 << 1)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 # List of all clients
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 g_clients = []
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 class channel (object):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 name = ""
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 password = ""
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 def __init__ (self, name):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 self.name = name
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 # Prints a line to control channel(s)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 def control (line):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 for client in g_clients:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 if client.flags & (CLIF_CONTROL|CLIF_CONNECTED) == (CLIF_CONTROL|CLIF_CONNECTED):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 client.write ("PRIVMSG %s :%s" % (client.channels[0]['name'], line))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 # Exception handling
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 #
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 def handle_exception(excType, excValue, trace):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 excepterm (traceback.format_exception(excType, excValue, trace))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 def excepterm(data):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 for segment in data:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 for line in segment.splitlines():
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 print line
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 control (line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 for client in g_clients:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 client.exceptdie()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 quit()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 sys.excepthook = handle_exception
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
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
102 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
103 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
104 raise logical_exception (".%s requires admin access" % command)
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105
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
106 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
107 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
108 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
109 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
110 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
111
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
112 #
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
113 # 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
114 #
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 class irc_client (asyncore.dispatcher):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 def __init__ (self, cfg, flags):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 self.name = cfg['name']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 self.host = cfg['address']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 self.port = cfg['port']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 self.password = cfg['password'] if 'password' in cfg else ''
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 self.channels = cfg['channels']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 self.flags = flags
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 self.send_buffer = list()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 self.umode = cfg['umode'] if 'umode' in cfg else ''
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 self.cfg = cfg
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 self.mynick = ''
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 g_clients.append (self)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 asyncore.dispatcher.__init__ (self)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 self.connect ((self.host, self.port))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 def handle_connect (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 nick = self.cfg['nickname'] if 'nickname' in self.cfg else g_config['nickname']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 ident = self.cfg['ident'] if 'ident' in self.cfg else g_config['ident']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 gecos = self.cfg['gecos'] if 'gecos' in self.cfg else g_config['gecos']
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 self.mynick = nick
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 print "Connected to [%s] %s:%d" % (self.name, self.host, self.port)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 if 'password' in self.cfg:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 self.write ("PASS %s" % self.cfg['password'])
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 self.write ("USER %s * * :%s" % (ident, gecos))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 self.write ("NICK %s" % nick)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 def write (self, data):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 self.send_buffer.append ("%s" % data)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 def handle_close (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 print "Connection to [%s] %s:%d terminated." % (self.name, self.host, self.port)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 self.close()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 def handle_write (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 self.send_all_now()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 def readable (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 return True
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 def writable (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 return len (self.send_buffer) > 0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 def send_all_now (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 for line in self.send_buffer:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 print "[%s] <- %s" % (self.name, line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 self.send ("%s\n" % line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 self.send_buffer = []
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 def handle_read (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 lines = self.recv (4096).splitlines()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 for line in lines:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 print "[%s] -> %s" % (self.name, line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 if line.startswith ("PING :"):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 self.write ("PONG :%s" % line[6:])
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 continue
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 words = line.split(" ")
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 if len(words) >= 2 and words[1] == "001":
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 self.flags |= CLIF_CONNECTED
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 for channel in self.cfg['channels']:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 self.write ("JOIN %s %s" % (channel['name'], channel['password'] if 'password' in channel else ''))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 if 'umode' in self.cfg:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 self.write ('MODE %s %s' % (self.mynick, self.cfg['umode']))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 if len(words) >= 2 and words[1] == "PRIVMSG":
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 self.handle_privmsg (line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 def handle_privmsg (self, line):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)$')
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 match = rex.match (line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 if match:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 sender = match.group (1)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 user = match.group (2)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 host = match.group (3)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 channel = match.group (4)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 message = match.group (5)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 replyto = channel if channel != g_mynick else sender
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 # Check for tracker url in the message
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 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
200 http_match = http_regex.match (line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 # Check for command.
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 if len(message) >= 2 and message[0] == '.' and message[1] != '.':
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 stuff = message[1:].split(' ')
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 command = stuff[0]
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 args = stuff[1:]
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 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
208 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
209 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
210 self.privmsg (replyto, "error: %s" % msg)
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 elif http_match:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 self.get_ticket_data (replyto, http_match.group (2), False)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 else:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 control ("Recieved bad PRIVMSG: %s" % line)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 def get_ticket_data (self, replyto, ticket, withlink):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 data = {}
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 try:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 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
220 except Exception, e:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 self.privmsg (replyto, "Failed to get info for issue %s: %s" % (ticket, `e`))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223 if data:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 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
225 (ticket, \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 data.summary, \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 data.reporter.name, \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 data.handler.name if hasattr (data, 'handler') else "nobody", \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 data.status.name, \
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 data.resolution.name))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 if withlink:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 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
234
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
235 def handle_command (self, sender, ident, host, replyto, command, args):
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 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
237 check_admin (sender, ident, host, command)
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 self.write (" ".join (args))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239 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
240 check_admin (sender, ident, host, command)
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 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
242 raise logical_exception ("usage: .%s <target> <message...>" % command)
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243 self.privmsg (args[0], " ".join (args[1:]))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 elif command == "ticket":
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 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
246 raise logical_exception ("usage: .%s <ticket>" % command)
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 self.get_ticket_data (replyto, args[0], True)
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 else:
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 raise logical_exception ("unknown command `.%s`" % command)
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 def handle_error(self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254 def privmsg (self, channel, msg):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255 self.write ("PRIVMSG %s :%s" % (channel, msg))
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
257 def exceptdie (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
258 if self.flags & CLIF_CONNECTED:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
259 self.write ("QUIT :Caught exception")
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
260 self.send_all_now()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
261 self.close()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
263 def keyboardinterrupt (self):
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264 if self.flags & CLIF_CONNECTED:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
265 self.write ("QUIT :KeyboardInterrupt")
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
266 self.send_all_now()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
267 self.close()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
268
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
269 #
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
270 # 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
271 #
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
272 try:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273 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
274 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
275 irc_client (conndata, CLIF_CONTROL if conndata['control'] else 0)
0
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
276 asyncore.loop()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
277 except KeyboardInterrupt:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
278 for client in g_clients:
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
279 client.keyboardinterrupt()
4904744b40eb - initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
280 quit()

mercurial