*quietly adds a file that should have been in the tree since 9839a*
[halbot.git] / connection.py
blob9e9af25a1989d8c9a974319d591c80f8db2ad604
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
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])
48 print 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 who = irclib.nm_to_n(event.source())
59 if who.count("Serv"):
60 return
61 (command, args) = resize(msg.split(" ", 1),2)
62 if command and command[0] == "$":
63 command = command[1:]
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 else:
92 command = "factoid implicit"
93 args = msg.split(" ", 1)[1]
94 else:
95 if command.lower() == "forget":
96 command = "forget"
97 elif is_re.search(msg):
98 command = "do is"
99 args = is_re.split(msg, 1)
100 else:
101 command = "factoid implicit"
102 args = msg
103 got_command(who, where, command, args)
104 got_msg = safe(got_msg)
105 irc.add_global_handler("privmsg", got_msg)
106 irc.add_global_handler("pubmsg", got_msg)
108 def debug_console():
109 while DEBUG:
110 cmd = sys.stdin.readline().strip()
111 if cmd:
112 hal.connection.send_raw(cmd)
114 def process_events():
115 irc.process_forever()