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 # TODO instead of nickserv.wait it would be ideal if we could just
5 # set up "don't send further commands until you receive this particular message"
7 class NickServPlugin < Plugin
9 BotConfig.register BotConfigStringValue.new('nickserv.name',
10 :default => "nickserv", :requires_restart => false,
11 :desc => "Name of the nick server (all lowercase)")
12 BotConfig.register BotConfigStringValue.new('nickserv.ident_request',
13 :default => "IDENTIFY", :requires_restart => false,
14 :on_change => Proc.new { |bot, v| bot.plugins.delegate "set_ident_request", v },
15 :desc => "String to look for to see if the nick server is asking us to identify")
16 BotConfig.register BotConfigBooleanValue.new('nickserv.wants_nick',
17 :default => true, :requires_restart => false,
18 :desc => "Set to false if the nick server doesn't expect the nick as a parameter in the identify command")
19 BotConfig.register BotConfigIntegerValue.new('nickserv.wait',
20 :default => 30, :validate => Proc.new { |v| v > 0 }, :requires_restart => false,
21 :desc => "Seconds to wait after sending a message to nickserv, e.g. after ghosting")
23 def help(plugin, topic="")
26 return "nickserv plugin: handles nickserv protected IRC nicks. topics password, register, identify, listnicks"
28 return "nickserv password <nick> <passwd>: remember the password for nick <nick> and use it to identify in future"
30 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"
32 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"
34 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"
39 # generate a random password
42 passwd += (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr
47 def set_ident_request(val)
48 @ident_request = Regexp.new(val)
53 # this plugin only wants to store strings!
62 set_ident_request(@bot.config['nickserv.ident_request'])
65 def password(m, params)
66 @registry[params[:nick]] = params[:passwd]
70 def nick_register(m, params)
71 passwd = params[:passwd] ? params[:passwd] : genpasswd
72 message = "REGISTER #{passwd}"
73 message += " #{params[:email]}" if params[:email]
74 @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], message
75 @registry[@bot.nick] = passwd
79 def listnicks(m, params)
80 if @registry.length > 0
82 @bot.say m.sourcenick, "#{k} => #{v}"
89 def do_identify(nick=@bot.nick)
90 if @registry.has_key?(nick)
91 if @bot.config['nickserv.wants_nick']
92 @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], "IDENTIFY #{nick} #{@registry[nick]}"
95 @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], "IDENTIFY #{@registry[nick]}"
97 # We cannot identify for different nicks if we can't use the nickname ...
106 def identify(m, params)
110 m.reply "I dunno the nickserv password for the nickname #{@bot.nick} :("
119 if @registry.has_key?(nick)
120 @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], "GHOST #{nick} #{@registry[nick]}"
122 sleep @bot.config['nickserv.wait']
124 # We need to wait after changing nick, otherwise the server
125 # might refuse to execute further commangs, e.g. subsequent JOIN
126 # commands until the nick has changed.
127 sleep @bot.config['nickserv.wait']
129 debug "Failed to identify for nick #{nick}, cannot take over"
135 return unless(m.kind_of? NoticeMessage)
137 if (m.sourcenick == @bot.config['nickserv.name'] && m.message =~ @ident_request)
138 debug "nickserv asked us to identify for nick #{@bot.nick}"
144 plugin = NickServPlugin.new
145 plugin.map 'nickserv password :nick :passwd', :action => "password"
146 plugin.map 'nickserv register :passwd :email', :action => 'nick_register',
147 :defaults => {:passwd => false, :email => false}
148 plugin.map 'nickserv listnicks', :action => "listnicks"
149 plugin.map 'nickserv identify', :action => "identify"