irc.py

Mon, 12 Jan 2015 02:44:56 -0500

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 12 Jan 2015 02:44:56 -0500
changeset 116
9e483447654b
parent 112
cdafc1a0544e
child 117
6c0609395889
permissions
-rw-r--r--

- moved mod_hgpoll to mod_hg

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

mercurial