-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook_irc
executable file
·88 lines (74 loc) · 2.41 KB
/
hook_irc
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /usr/bin/env python
import sys
server = "irc.worldforge.org"
port = 6667
nick = "Trurl"
channel = "#coders"
new_topic = "[[ http://trurl.worldforge.org/ " + sys.argv[1] + " ]]"
try:
old_topic = open('hook_irc.topic', 'r').read()
except:
old_topic = ""
if new_topic != old_topic:
open('hook_irc.topic', 'w').write(new_topic)
import irclib, re, socket, time
try:
socket.getaddrinfo(server, port)
except:
time.sleep(1)
class DebugClient(irclib.SimpleIRCClient):
def _dispatcher(self, c, e):
"""[Internal]"""
m = "on_" + e.eventtype()
print m
irclib.SimpleIRCClient._dispatcher(self, c, e)
client = DebugClient()
client.connect(server, port, nick)
#client.connection.join("#trurl")
#client.connection.privmsg("#trurl", "Foo")
client.connection.join(channel)
#client.connection.privmsg(channel, "Foo")
def update_topic(conn, current_topic):
full_topic = ''
if re.search(r'\[\[.*\]\]', current_topic):
full_topic = re.sub(r'\[\[.*\]\]', new_topic, current_topic)
else:
if current_topic == '':
full_topic = new_topic
else:
full_topic = current_topic + ' ' + new_topic
if current_topic != full_topic:
client.connection.topic(channel, full_topic)
else:
conn.close()
sys.exit()
# print "current topic:", current_topic
# print "full topic:", full_topic
topic = None
def on_topic(conn, event):
global topic
if event.target() == nick and topic == None:
topic = event.arguments()[1]
update_topic(conn, topic)
else:
if topic != None and event.target() == channel:
conn.close()
sys.exit()
def on_notopic(conn, event):
global topic
if event.target() == nick and topic == None:
topic = ''
update_topic(conn, topic)
#client.on_topicinfo = on_topicinfo
client.on_topic = on_topic
client.on_currenttopic = on_topic
client.on_notopic = on_notopic
client.connection.topic(channel)
#client.connection.topic(channel)
import signal
def signal_exit(a, b):
sys.stderr.write("hook_irc: timeout\n")
sys.exit(1)
signal.signal(signal.SIGALRM, signal_exit)
signal.alarm(60)
client.start()