1 import irclib
, ircbot
, time
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
12 hal
= ircbot
.SingleServerIRCBot([(server
, port
)], nick
, "Halbot")
13 hal
.connect(server
, port
, nick
)
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
)
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
)
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
):
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
])
52 def got_msg(conn
, event
):
54 if irclib
.mask_matches(event
.source(), user
):
55 if my_users
[user
] == perms
.ban
:
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())
63 (command
, args
) = resize(msg
.split(" ", 1),2)
64 if command
and command
[0] == "$":
68 if event
.eventtype() == 'privmsg':
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)
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://"):
85 got_command(who
, where
, "title implicit", html
.extract_url(msg
))
87 if nick_re
.match(msg
):
88 (me
, command
, args
) = resize(msg
.split(" ", 2),3)
89 if command
in commands
:
91 elif is_re
.search(msg
):
93 args
= is_re
.split(msg
.split(" ", 1)[1], 1)
96 command
= "factoid implicit"
97 args
= msg
.split(" ", 1)[1]
99 if command
.lower() == "forget":
101 elif is_re
.search(msg
):
103 args
= is_re
.split(msg
, 1)
106 command
= "factoid implicit"
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":
116 got_ctcp
= safe(got_ctcp
)
117 irc
.add_global_handler("ctcp", got_ctcp
)
121 cmd
= sys
.stdin
.readline().strip()
123 hal
.connection
.send_raw(cmd
)
125 def process_events():
126 irc
.process_forever()