Hal will now attempt to become an oper on connect.
[halbot.git] / connection.py
blob0122f826dc3647e66a39acf291cd9c85817a3a95
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
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)
22 irc = hal.ircobj
23 irc.add_global_handler("whoisuser", whois.got_whois)
24 irc.add_global_handler("nosuchnick", whois.got_whois)
25 irc.add_global_handler("307", whois.got_registered)
26 irc.add_global_handler("whoischannels", whois.got_channels)
27 irc.add_global_handler("whoisoperator", whois.got_servop)
28 irc.add_global_handler("endofwhois", whois.got_whoend)
30 #@safe
31 def got_invite(conn, event):
32 who = irclib.nm_to_n(event.source())
33 where = event.arguments()[0]
34 got_command(who, where, "do join")
35 got_invite = safe(got_invite)
36 irc.add_global_handler("invite", got_invite)
38 #@safe
39 def got_msg(conn, event):
40 for user in my_users:
41 if irclib.mask_matches(event.source(), user):
42 if my_users[user] == perms.ban:
43 return
44 msg = event.arguments()[0]
45 who = irclib.nm_to_n(event.source())
46 (command, args) = resize(msg.split(" ", 1),2)
47 if command and command[0] == "$":
48 command = command[1:]
50 if event.eventtype() == 'privmsg':
51 where = private
52 else:
53 where = event.target()
54 if msg.count("http://"):
55 import html
56 got_command(who, where, "title implicit", html.extract_url(msg))
57 if msg[0] != "$":
58 if nick_re.match(msg):
59 (me, command, args) = resize(msg.split(" ", 2),3)
60 if command in commands:
61 pass
62 elif is_re.search(msg):
63 command = "do is"
64 args = is_re.split(msg.split(" ", 1)[1], 1)
65 else:
66 command = "factoid implicit"
67 args = msg.split(" ", 1)[1]
68 else:
69 if command.lower() == "forget":
70 command = "forget"
71 elif is_re.search(msg):
72 command = "do is"
73 args = is_re.split(msg, 1)
74 else:
75 command = "factoid implicit"
76 args = msg
77 got_command(who, where, command, args)
78 got_msg = safe(got_msg)
79 irc.add_global_handler("privmsg", got_msg)
80 irc.add_global_handler("pubmsg", got_msg)
82 def debug_console():
83 while DEBUG:
84 cmd = sys.stdin.readline().strip()
85 if cmd:
86 hal.connection.send_raw(cmd)
88 def process_events():
89 irc.process_forever(None)