Caught signals that would otherwise be fatal and saveless.
[halbot.git] / connection.py
blobb76993a1f15e29e3a18e0f9260d8b4d6fb276a44
1 import irclib, ircbot, time
3 import whois, perms
4 from globals import DEBUG, nick, server, port, autojoin_channels, commands, \
5 my_users, private, password, kill_channels, maybe_save
6 from safety import safe
7 from common import resize
8 from factoids import nick_re, is_re
9 from basic_commands import got_command
10 irclib.DEBUG = DEBUG
12 hal = ircbot.SingleServerIRCBot([(server, port)], nick, "Halbot")
13 hal.connect(server, port, nick)
14 time.sleep(.1)
15 if password:
16 hal.connection.privmsg("NickServ", "identify " + password)
17 hal.connection.send_raw("OPER Hal " + password)
18 hal.connection.send_raw("UMODE -hs")
19 for channel in autojoin_channels:
20 hal.connection.join(channel)
21 for channel in kill_channels:
22 hal.connection.join(channel)
24 irc = hal.ircobj
25 irc.add_global_handler("whoisuser", whois.got_whois)
26 irc.add_global_handler("nosuchnick", whois.got_whois)
27 irc.add_global_handler("307", whois.got_registered)
28 irc.add_global_handler("whoischannels", whois.got_channels)
29 irc.add_global_handler("whoisoperator", whois.got_servop)
30 irc.add_global_handler("endofwhois", whois.got_whoend)
31 irc.add_global_handler("userhost", whois.got_userhost)
33 #@safe
34 def got_invite(conn, event):
35 who = irclib.nm_to_n(event.source())
36 where = event.arguments()[0]
37 got_command(who, where, "do join")
38 got_invite = safe(got_invite)
39 irc.add_global_handler("invite", got_invite)
41 def flood_protect(who, where, is_first):
42 if is_first:
43 hal.connection.privmsg(where, "The Definite Annoyance eViction Engine (D.A.V.E.) project is proud to present: Flood protection!")
44 from panic import do_kickban
45 user = irclib.nm_to_n(who)
46 whois.userhost_callbacks[user] = (do_kickban,
47 ("Hal", where, "Flooding.", False))
48 hal.connection.userhost([user])
50 last_messages = {}
51 #@safe
52 def got_msg(conn, event):
53 for user in my_users:
54 if irclib.mask_matches(event.source(), user):
55 if my_users[user] == perms.ban:
56 return
57 msg = event.arguments()[0]
58 if msg == "ACTION" and len(event.arguments()) > 1:
59 msg = "<action>" + event.arguments()[1]
60 who = irclib.nm_to_n(event.source())
61 if who.count("Serv"):
62 return
63 (command, args) = resize(msg.split(" ", 1),2)
64 if command and command[0] == "$":
65 command = command[1:]
66 maybe_save()
68 if event.eventtype() == 'privmsg':
69 where = private
70 else:
71 where = event.target()
72 last_message_info = last_messages.setdefault(where, ["",0])
73 if last_message_info[0] == msg:
74 last_message_info[1] += 1
75 if last_message_info[1] > 3:
76 flood_protect(event.source(), where, last_message_info[1] == 4)
77 else:
78 if last_message_info[1] > 3:
79 hal.connection.privmsg(where, "D.A.V.E. complete. Have a nice day!")
80 last_message_info[0] = msg
81 last_message_info[1] = 1
83 if msg.count("http://"):
84 import html
85 got_command(who, where, "title implicit", html.extract_url(msg))
86 if msg[0] != "$":
87 if nick_re.match(msg):
88 (me, command, args) = resize(msg.split(" ", 2),3)
89 if command in commands:
90 pass
91 elif is_re.search(msg):
92 command = "do is"
93 args = is_re.split(msg.split(" ", 1)[1], 1)
94 maybe_save()
95 else:
96 command = "factoid implicit"
97 args = msg.split(" ", 1)[1]
98 else:
99 if command.lower() == "forget":
100 command = "forget"
101 elif is_re.search(msg):
102 command = "do is"
103 args = is_re.split(msg, 1)
104 maybe_save()
105 else:
106 command = "factoid implicit"
107 args = msg
108 got_command(who, where, command, args)
109 got_msg = safe(got_msg)
110 irc.add_global_handler("privmsg", got_msg)
111 irc.add_global_handler("pubmsg", got_msg)
113 def got_ctcp(conn, event):
114 if event.arguments()[0] == "ACTION":
115 got_msg(conn, event)
116 got_ctcp = safe(got_ctcp)
117 irc.add_global_handler("ctcp", got_ctcp)
119 def debug_console():
120 while DEBUG:
121 cmd = sys.stdin.readline().strip()
122 if cmd:
123 hal.connection.send_raw(cmd)
125 def process_events():
126 irc.process_forever()