]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/chanserv.rb
plugin(imdb): changed base url
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / chanserv.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Chanserv management plugin for rbot
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 #
8 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
9 #
10 # ChanServ interface functionality.
11 #
12 # Currently, it only uses Chanserv to automatically op/hop/voice itself or others on channels where it can.
13 #
14 # TODO:: improve documentation, it really sucks as of now
15
16 class ChanServPlugin < Plugin
17
18   Config.register Config::StringValue.new('chanserv.name',
19     :default => "chanserv", :requires_restart => false,
20     :desc => "Name of the chan server (all lowercase)")
21
22   def help(plugin, topic="")
23     case topic
24     when ""
25       return "chanserv plugin: interface the bot with the chanserv; topics: add, rm, list"
26     when "add"
27       return "chanserv add +status+ +channel+ [+nick+] => the bot will tell chanserv to set the status of +nick+ on +channel+ to +status+"
28     when "rm"
29       return "chanserv rm +status+ +channel+ [+nick+] => the bot will not tell chanserv to set the status of +nick+ on +channel+ to +status+ anymore"
30     when "list"
31       return "chanserv list => list current chanserv status modifiers"
32     end
33   end
34
35   # Returns the chanserv name
36   def cs_nick
37     @bot.config['chanserv.name']
38   end
39
40   # say something to chanserv
41   def cs_say(msg)
42     @bot.say cs_nick, msg
43   end
44
45   def cs_add(m, params)
46     status = params[:status].downcase
47     ch = params[:channel].downcase
48     who = params[:nick].downcase rescue ''
49     as = @registry[:auto_status] || Array.new
50     as << [status, ch, who]
51     debug as.inspect
52     @registry[:auto_status] = as
53     m.okay
54   end
55
56   def cs_rm(m, params)
57     status = params[:status].downcase
58     ch = params[:channel].downcase
59     who = params[:nick].downcase rescue ''
60     as = @registry[:auto_status]
61     unless as
62       m.reply "No chanserv entries known!"
63       return
64     end
65     as.delete [status, ch, who]
66     debug as.inspect
67     @registry[:auto_status] = as
68     m.okay
69   end
70
71   def cs_list(m, params)
72     unless @registry[:auto_status]
73       m.reply "No chanserv entries known!"
74       return
75     end
76     @registry[:auto_status].each { |status, ch, pre_who|
77       who = pre_who.empty? ? "me (yes, me)" : pre_who
78       m.reply "chanserv should #{status} #{who} on #{ch}"
79     }
80
81   end
82
83   def join(m)
84     return unless @registry[:auto_status]
85     @registry[:auto_status].each { |status, ch, pre_who|
86       who = pre_who.empty? ? @bot.nick : pre_who
87       if m.channel.downcase == ch.downcase
88         if who.downcase == m.source.downcase
89           cs_say "#{status} #{ch} #{who}"
90         end
91       end
92     }
93   end
94
95   def nick(m)
96     return unless @registry[:auto_status]
97     is_on = m.server.channels.inject(ChannelList.new) { |list, ch|
98       list << ch if ch.users.include?(m.source)
99       list
100     }
101     is_on.each { |channel|
102       @registry[:auto_status].each { |status, ch, pre_who|
103         who = pre_who.empty? ? @bot.nick : pre_who
104         if channel.downcase == ch.downcase
105           if who.downcase == m.source.downcase
106             cs_say "#{status} #{ch} #{who}"
107           end
108         end
109       }
110     }
111   end
112
113   # This method gets delegated by the NickServ plugin after successfull identification
114   def identified
115     return unless @registry[:auto_status]
116     @registry[:auto_status].each { |status, ch, who|
117       if who.empty?
118         cs_say "#{status} #{ch} #{@bot.nick}"
119       end
120     }
121   end
122
123
124 end
125
126 plugin = ChanServPlugin.new
127 plugin.map 'chanserv add :status :channel [:nick]', :action => :cs_add
128 plugin.map 'chanserv rm :status :channel [:nick]', :action => :cs_rm
129 plugin.map 'chanserv list', :action => :cs_list
130
131 plugin.default_auth('*', false)
132