]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/nickserv.rb
Plugin header boilerplating.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / nickserv.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Nickserv management plugin for rbot
5 #
6 # Author:: Tom Gilbert (giblet) <tom@linuxbrit.co.uk>
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 #
9 # Copyright:: (C) 2002-2005 Tom Gilbert
10 # Copyright:: (C) 2006 Tom Gilbert, Giuseppe Bilotta
11 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
12 #
13 # Automatically lookup nicks in @registry and identify when asked
14 #
15 # Takes over proper nick if required and nick is registered
16
17 # TODO:: allow custom IDENTIFY and GHOST names
18
19 class NickServPlugin < Plugin
20   
21   BotConfig.register BotConfigStringValue.new('nickserv.name',
22     :default => "nickserv", :requires_restart => false,
23     :desc => "Name of the nick server (all lowercase)")
24   BotConfig.register BotConfigStringValue.new('nickserv.ident_request',
25     :default => "IDENTIFY", :requires_restart => false,
26     :on_change => Proc.new { |bot, v| bot.plugins.delegate "set_ident_request", v },
27     :desc => "String to look for to see if the nick server is asking us to identify")
28   BotConfig.register BotConfigStringValue.new('nickserv.nick_avail',
29     :default => "not (currently )?online|killed|recovered|disconnesso|libero",
30     :requires_restart => false,
31     :on_change => Proc.new { |bot, v| bot.plugins.delegate "set_nick_avail", v },
32     :desc => "String to look for to see if the nick server is informing us that our nick is now available")
33   BotConfig.register BotConfigBooleanValue.new('nickserv.wants_nick',
34     :default => false, :requires_restart => false,
35     :desc => "Set to false if the nick server doesn't expect the nick as a parameter in the identify command")
36   BotConfig.register BotConfigIntegerValue.new('nickserv.wait',
37     :default => 30, :validate => Proc.new { |v| v > 0 }, :requires_restart => false,
38     :desc => "Seconds to wait after sending a message to nickserv, e.g. after ghosting")
39
40   def help(plugin, topic="")
41     case topic
42     when ""
43       return "nickserv plugin: handles nickserv protected IRC nicks. topics password, register, identify, listnicks"
44     when "password"
45       return "nickserv password [<nick>] <passwd>: remember the password for nick <nick> and use it to identify in future"
46     when "register"
47       return "nickserv register [<password> [<email>]]: register the current nick, choosing a random password unless <password> is supplied - current nick must not already be registered for this to work. Also specify email if required by your services"
48     when "identify"
49       return "nickserv identify: identify with nickserv - shouldn't be needed - bot should identify with nickserv immediately on request - however this could be useful after splits or service disruptions, or when you just set the password for the current nick"
50     when "listnicks"
51       return "nickserv listnicks: lists nicknames and associated password the bot knows about - you will need config level auth access to do this one and it will reply by privmsg only"
52     end
53   end
54   
55   def genpasswd
56     return Irc::Auth.random_password
57   end
58
59   def set_ident_request(val)
60     @ident_request = Regexp.new(val)
61   end
62
63   def set_nick_avail(val)
64     @nick_avail = Regexp.new(val)
65   end
66
67   def initialize
68     super
69     # this plugin only wants to store strings!
70     class << @registry
71       def store(val)
72         val
73       end
74       def restore(val)
75         val
76       end
77     end
78     set_ident_request(@bot.config['nickserv.ident_request'])
79     set_nick_avail(@bot.config['nickserv.nick_avail'])
80   end
81
82   # Returns the nickserv name
83   def ns_nick
84     @bot.config['nickserv.name']
85   end
86
87   # say something to nickserv
88   def ns_say(msg)
89     @bot.say ns_nick, msg
90   end
91
92   def password(m, params)
93     nick = params[:nick] || @bot.nick
94     passwd = params[:passwd]
95     if nick == @bot.nick
96       ns_say "SET PASSWORD #{passwd}"
97     else
98       m.reply "I'm only changing this in my database, I won't inform #{ns_nick} of the change"
99     end
100     @registry[nick] = passwd
101     m.okay
102   end
103
104   def nick_register(m, params)
105     passwd = params[:passwd] ? params[:passwd] : genpasswd
106     message = "REGISTER #{passwd}"
107     message += " #{params[:email]}" if params[:email]
108     ns_say message
109     @registry[@bot.nick] = passwd
110     m.okay
111   end
112
113   def listnicks(m, params)
114     if @registry.length > 0
115       @registry.each {|k,v|
116         @bot.say m.sourcenick, "#{k} => #{v}"
117       }
118     else
119       m.reply "none known"
120     end
121   end
122
123   def do_identify(nick=@bot.nick)
124     if @registry.has_key?(nick)
125       if @bot.config['nickserv.wants_nick']
126         ns_say "IDENTIFY #{nick} #{@registry[nick]}"
127       else
128         if nick == @bot.nick
129           ns_say "IDENTIFY #{@registry[nick]}"
130         else
131           # We cannot identify for different nicks if we can't use the nickname ...
132           return false
133         end
134       end
135       return true
136     end
137     return nil
138   end
139
140   def identify(m, params)
141     ided = do_identify
142     case ided
143     when true
144       m.okay
145     when false
146       m.reply "I cannot identify for a this nick"
147     when nil
148       m.reply "I dunno the nickserv password for the nickname #{@bot.nick} :("
149     else
150       m.reply "uh ... something went wrong ..."
151     end
152   end
153   
154   def connect
155     do_identify
156   end
157   
158   def nicktaken(nick)
159     if @registry.has_key?(nick)
160       ns_say "GHOST #{nick} #{@registry[nick]}"
161     end
162   end
163
164   def listen(m)
165     return unless(m.kind_of? NoticeMessage)
166     return unless m.source.downcase == ns_nick.downcase
167
168     case m.message
169     when @ident_request
170       debug "nickserv asked us to identify for nick #{@bot.nick}"
171       do_identify
172     when @nick_avail
173       debug "our nick seems to be free now"
174       @bot.nickchg @bot.config['irc.nick']
175     end
176   end
177
178 end
179 plugin = NickServPlugin.new
180 plugin.map 'nickserv password [:nick] :passwd', :action => "password"
181 plugin.map 'nickserv register :passwd :email', :action => 'nick_register',
182            :defaults => {:passwd => false, :email => false}
183 plugin.map 'nickserv listnicks', :action => "listnicks"
184 plugin.map 'nickserv identify', :action => "identify"
185
186 plugin.default_auth('*', false)
187