*quietly adds a file that should have been in the tree since 9839a*
[halbot.git] / whois.py
bloba36ebe67163536c4686608609cf0c155905ff251
1 from __future__ import generators
2 from sets import Set
4 import irclib
6 import perms
7 from common import multiunion, ircdict_to_set
8 from safety import safe
10 def voice_or_better(channel):
11 from connection import hal
12 if channel in hal.channels:
13 chan = hal.channels[channel]
14 dicts = (chan.voiceddict, chan.hopdict, chan.operdict, chan.admindict,
15 chan.ownerdict)
16 return multiunion(*[ircdict_to_set(d) for d in dicts])
17 else:
18 return Set()
20 def callback(who, info):
21 raise ValueError, "Whois callback never got replaced."
23 class Whois(object):
24 registered = False
25 ircop = False
26 channel_perms = None
27 def __init__(self):
28 self.channel_perms = {}
30 whois_info = {}
32 #@safe
33 def got_whois(conn, event):
34 user = event.arguments()[0]
35 whois_info[user] = Whois()
36 got_whois = safe(got_whois)
38 #@safe
39 def got_registered(conn, event):
40 args = event.arguments()
41 if len(args) == 2 and args[1] == "is a registered nick":
42 user = args[0]
43 whois_info[user].registered = True
44 got_registered = safe(got_registered)
46 #@safe
47 def got_channels(conn, event):
48 args = event.arguments()
49 if len(args) == 2:
50 user = args[0]
51 channels = args[1].split()
52 for c in channels:
53 if c[0] != "#":
54 whois_info[user].channel_perms[c[1:]] = perms.Perms.from_symbol(c[0])
55 else:
56 whois_info[user].channel_perms[c] = perms.present
57 got_channels = safe(got_channels)
59 #@safe
60 def got_servop(conn, event):
61 args = event.arguments()
62 if len(args) == 2:
63 user = args[0]
64 whois_info[user].ircop = True
65 got_servop = safe(got_servop)
67 #@safe
68 def got_whoend(conn, event):
69 who = event.arguments()[0]
70 callback(who, whois_info[who])
71 if False:
72 user = whois_info[who]
73 print "Who info for %s:" % who
74 print "Registered: %s" % user.registered
75 print "IRCop: %s" % user.ircop
76 p = user.channel_perms
77 perms = "".join([("%s: %s, " % (k, p[k])) for k in p])[:-2]
78 print "Channel info: %s" % perms
79 got_whoend = safe(got_whoend)
81 userhost_callbacks = {}
83 #@safe
84 def got_userhost(conn, event):
85 raw = event.arguments()[0]
86 nm = raw.strip().replace("=+", "!").replace("=-", "!")
87 nick = irclib.nm_to_n(nm)
88 if nick in userhost_callbacks:
89 func, extra_args = userhost_callbacks[nick]
90 func(nm, *extra_args)
91 got_userhost = safe(got_userhost)