irc.py

Mon, 17 Nov 2014 22:22:05 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 17 Nov 2014 22:22:05 +0200
changeset 89
ec70a118f6cd
parent 81
ce5d27463356
child 92
da291d9426ea
permissions
-rw-r--r--

- ..

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

mercurial