irc.py

Sat, 11 Apr 2015 21:02:09 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 11 Apr 2015 21:02:09 +0300
changeset 121
ac07779f788d
parent 120
9880bb697149
child 123
aeb0d0788869
permissions
-rw-r--r--

- reworked mercurial repository handling, removed hardcoded values

73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 import asyncore
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 import socket
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 import sys
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 import re
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 import modulecore as ModuleCore
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 import traceback
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 from configfile import Config
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 import bt as Bt
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 import hgpoll as HgPoll
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 CLIF_CONNECTED = (1 << 1)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12
119
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
13 CtcpChar = '\001'
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
14 BoldChar = '\002'
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
15 ColorChar = '\003'
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
16
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 all_clients = []
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 class RestartError (Exception):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 def __init__ (self, value):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 self.message = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 def __str__ (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 return self.message
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 #
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 # Prints a line to log channel(s)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 #
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 def broadcast (line):
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 92
diff changeset
29 print line
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 for client in all_clients:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 if not client.flags & CLIF_CONNECTED:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 continue
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 for channel in client.channels:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 if channel.get_value ('logchannel', default=False):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 client.write ("PRIVMSG %s :%s" % (channel.get_value ('name'), line))
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 class logical_exception (Exception):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 def __init__ (self, value):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 self.value = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 def __str__ (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 return self.value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
44 def get_client (name):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
45 for client in all_clients:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
46 if client.name == name:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
47 return client
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
48
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
49 raise ValueError ('no such client %s' % name)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
50
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 #
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 # Main IRC client class
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 #
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 class irc_client (asyncore.dispatcher):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 def __init__ (self, cfg, flags):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 global all_clients
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 self.name = cfg.get_value ('name')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 self.host = cfg.get_value ('address')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 self.port = cfg.get_value ('port', default=6667)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 self.password = cfg.get_value ('password', default='')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 self.channels = cfg.get_nodelist ('channels')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 self.flags = flags
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 self.send_buffer = []
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 self.umode = cfg.get_value ('umode', default='')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 self.cfg = cfg
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 self.desired_name = Config.get_value ('nickname', default='cobalt')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 self.mynick = self.desired_name
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 self.verbose = Config.get_value ('verbose', default=False)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 self.commandprefix = Config.get_value ('commandprefix', default='.')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 all_clients.append (self)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 asyncore.dispatcher.__init__ (self)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 self.connect ((self.host, self.port))
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 def register_to_irc (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 ident = Config.get_value ('ident', default='cobalt')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 gecos = Config.get_value ('gecos', default='cobalt')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 self.write ("PASS %s" % self.password)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 self.write ("USER %s * * :%s" % (ident, gecos))
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 self.write ("NICK %s" % self.mynick)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 def handle_connect (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 print "Connected to [%s] %s:%d" % (self.name, self.host, self.port)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 self.register_to_irc()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 def write (self, utfdata):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 try:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 self.send_buffer.append ("%s" % utfdata.decode("utf-8","ignore").encode("ascii","ignore"))
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 except UnicodeEncodeError:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 pass
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 def handle_close (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 print "Connection to [%s] %s:%d terminated." % (self.name, self.host, self.port)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 self.close()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 def handle_write (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 self.send_all_now()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 def readable (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 return True
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 def writable (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 return len (self.send_buffer) > 0
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 def send_all_now (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 for line in self.send_buffer:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 if self.verbose:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 print "[%s] <- %s" % (self.name, line)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 self.send ("%s\n" % line)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 self.send_buffer = []
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 def handle_read (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 lines = self.recv (4096).splitlines()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 for utfline in lines:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 try:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 line = utfline.decode("utf-8","ignore").encode("ascii","ignore")
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 except UnicodeDecodeError:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 continue
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 if self.verbose:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 print "[%s] -> %s" % (self.name, line)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 if line.startswith ("PING :"):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 self.write ("PONG :%s" % line[6:])
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 words = line.split(" ")
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 if len(words) >= 2:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 if words[1] == "001":
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 self.flags |= CLIF_CONNECTED
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 umode = self.cfg.get_value ('umode', '')
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
132
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 if umode != '':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 self.write ('MODE %s %s' % (self.mynick, self.cfg.get_value ('umode', '')))
120
9880bb697149 - added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
135
9880bb697149 - added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
136 for channel in self.channels:
9880bb697149 - added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 119
diff changeset
137 self.write ("JOIN %s %s" % (channel.get_value ('name'), channel.get_value ('password', default='')))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 elif words[1] == "PRIVMSG":
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 self.handle_privmsg (line)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 elif words[1] == 'QUIT':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) QUIT')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 match = rex.match (line)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 # Try reclaim our nickname if possible
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 if match and match.group(1) == self.desired_name:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 self.mynick = self.desired_name
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147 self.write ("NICK %s" % self.mynick)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 elif words[1] == "433":
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 #:irc.localhost 433 * cobalt :Nickname is already in use.
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 self.mynick += self.cfg.get_value ('conflictsuffix', default='`')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 self.write ("NICK " + self.mynick)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 # Check for new issues on the bugtracker
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 Bt.poll()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 # Check for new commits in the repositories
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 HgPoll.poll()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 #
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 # Handle a PRIVMSG line from the IRC server
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 #
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 def handle_privmsg (self, line):
112
cdafc1a0544e - stuff!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 94
diff changeset
164 rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.*)$')
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 match = rex.match (line)
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
166
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167 if not match:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 broadcast ("Recieved bad PRIVMSG: %s" % line)
112
cdafc1a0544e - stuff!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 94
diff changeset
169 return
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
170
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 sender = match.group (1)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 user = match.group (2)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 host = match.group (3)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 channel = match.group (4)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 message = match.group (5)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 replyto = channel if channel != self.mynick else sender
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
177
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 # Check for command.
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 if len(message) >= 2 and message[0] == self.commandprefix and message[1] != self.commandprefix:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 stuff = message[1:].split(' ')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 command = stuff[0]
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 args = stuff[1:]
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
183 self.handle_command (sender, user, host, channel, replyto, command, args, message)
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 return
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
185
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
186 if channel != self.mynick:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
187 ModuleCore.call_hook (bot=self, hookname='chanmsg', channel=channel, sender=sender,
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
188 ident=user, host=host, message=message, replyto=replyto)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
189 else:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
190 ModuleCore.call_hook (bot=self, hookname='querymsg', sender=sender, ident=user,
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
191 host=host, message=message, replyto=replyto)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
192
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
193 ModuleCore.call_hook (bot=self, hookname='privmsg', target=channel, sender=sender,
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
194 ident=user, host=host, message=message, replyto=replyto)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
195
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 Bt.process_message (self, line, replyto)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 def add_irc_channel (self, channame):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 for channel in self.channels:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 if channel.get_value ('name').upper() == channame.upper():
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 return
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
202
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 channel = self.cfg.append_nodelist ('channels')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 channel.set_value ('name', channame)
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
205 self.channels = self.cfg.get_nodelist ('channels')
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 self.write ('JOIN ' + channame)
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
207 self.cfg.save()
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
208
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 def remove_irc_channel (self, channame):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 for channel in self.channels:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 if channel.get_value ('name') == channame:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 self.channels.remove (channel)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 break
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
214 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 return
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
216
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 self.write ('PART ' + channame)
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
218 self.cfg.save()
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
219
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
220 def handle_command (self, sender, ident, host, target, replyto, command, args, message):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
221 kvargs = {'sender': sender, 'ident': ident, 'host': host, 'target': target,
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 117
diff changeset
222 'replyto': replyto, 'cmdname': command, 'message': message}
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
223
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224 try:
117
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 112
diff changeset
225 ModuleCore.call_command (self, **kvargs)
6c0609395889 - added a confirm system, probably useful in the future
Teemu Piippo <crimsondusk64@gmail.com>
parents: 112
diff changeset
226 return
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 except ModuleCore.CommandError as e:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 lines = str (e).split ('\n')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
229 self.privmsg (replyto, 'error: %s' % lines[0])
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
230
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231 for line in lines[1:]:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
232 self.privmsg (replyto, ' ' + line)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233 return
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
234
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 def handle_error(self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 raise RestartError (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
237
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 def restart(self):
81
ce5d27463356 - fixed bad RestartError construction
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
239 raise RestartError('')
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 def privmsg (self, channel, msg):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242 self.write ("PRIVMSG %s :%s" % (channel, msg))
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
243
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 def close_connection (self, message):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 if self.flags & CLIF_CONNECTED:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246 self.write ("QUIT :" + message)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 self.send_all_now()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 self.close()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250 def quit_irc (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 self.close_connection ('Leaving')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253 def exceptdie (self):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254 self.close_connection ('Caught exception')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256 def keyboardinterrupt (self):
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 81
diff changeset
257 self.close_connection ('KeyboardInterrupt')

mercurial