Added a daily automatic save.
[halbot.git] / connection.py
blobf3988447b477f2d4172008e469443f1aae9ab312
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, ("Hal", where, "Flooding."))
47 hal.connection.userhost([user])
49 last_messages = {}
50 #@safe
51 def got_msg(conn, event):
52 for user in my_users:
53 if irclib.mask_matches(event.source(), user):
54 if my_users[user] == perms.ban:
55 return
56 msg = event.arguments()[0]
57 who = irclib.nm_to_n(event.source())
58 if who.count("Serv"):
59 return
60 (command, args) = resize(msg.split(" ", 1),2)
61 if command and command[0] == "$":
62 command = command[1:]
63 maybe_save()
65 if event.eventtype() == 'privmsg':
66 where = private
67 else:
68 where = event.target()
69 last_message_info = last_messages.setdefault(where, ["",0])
70 if last_message_info[0] == msg:
71 last_message_info[1] += 1
72 if last_message_info[1] > 3:
73 flood_protect(event.source(), where, last_message_info[1] == 4)
74 else:
75 if last_message_info[1] > 3:
76 hal.connection.privmsg(where, "D.A.V.E. complete. Have a nice day!")
77 last_message_info[0] = msg
78 last_message_info[1] = 1
80 if msg.count("http://"):
81 import html
82 got_command(who, where, "title implicit", html.extract_url(msg))
83 if msg[0] != "$":
84 if nick_re.match(msg):
85 (me, command, args) = resize(msg.split(" ", 2),3)
86 if command in commands:
87 pass
88 elif is_re.search(msg):
89 command = "do is"
90 args = is_re.split(msg.split(" ", 1)[1], 1)
91 maybe_save()
92 else:
93 command = "factoid implicit"
94 args = msg.split(" ", 1)[1]
95 else:
96 if command.lower() == "forget":
97 command = "forget"
98 elif is_re.search(msg):
99 command = "do is"
100 args = is_re.split(msg, 1)
101 maybe_save()
102 else:
103 command = "factoid implicit"
104 args = msg
105 got_command(who, where, command, args)
106 got_msg = safe(got_msg)
107 irc.add_global_handler("privmsg", got_msg)
108 irc.add_global_handler("pubmsg", got_msg)
110 def debug_console():
111 while DEBUG:
112 cmd = sys.stdin.readline().strip()
113 if cmd:
114 hal.connection.send_raw(cmd)
116 def process_events():
117 irc.process_forever()