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