-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirccat.py
54 lines (42 loc) · 1.61 KB
/
irccat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
"""
irccat.py - Phenny "irccat" Module
Copyright 2011, Richard Bateman
Licensed under the New BSD License.
Written to be used in the #firebreath IRC channel: http://www.firebreath.org
"""
import SocketServer, thread
from SocketServer import ThreadingMixIn, StreamRequestHandler
shared_data = {}
class IrcCatListener(ThreadingMixIn, StreamRequestHandler):
def handle(self):
phenny = shared_data["phenny"]
msg = self.rfile.readline().strip()
print "Handling communication"
print "Received", msg
dest, msg = self.splitMsg(msg)
if not dest:
dest = phenny.channels
print "Channels:", dest
for chan in dest:
if chan[0] == "#" and chan not in phenny.channels:
print "Someone tried to make me say something in a channel I'm not in!"
else:
phenny.msg(chan, msg)
def splitMsg(self, message):
if message[0] not in ('#', '@'):
return [], message
dest, message = message.split(" ", 1)
dest = [x.strip().strip("@") for x in dest.split(",")]
return dest, message
def startListener():
print "-- irccat thread started"
HOST, PORT = shared_data["phenny"].config.irccat_host, shared_data["phenny"].config.irccat_port
server = SocketServer.TCPServer((HOST, PORT), IrcCatListener)
print "-- starting irccat server on %s:%s" % (HOST, PORT)
server.serve_forever()
print "closing thread?"
def setup(phenny):
print "-- setting up irccat..."
shared_data["phenny"] = phenny
thread.start_new_thread(startListener, tuple())