]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/nickserv.rb
Fix a problem with not rejoining channel after disconnect and nick taken, because...
[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 # 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"
6
7 class NickServPlugin < Plugin
8   
9   BotConfig.register BotConfigStringValue.new('nickserv.name',
10     :default => "NickServ", :requires_restart => false,
11     :desc => "Name of the nick server")
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 BotConfigIntegerValue.new('nickserv.wait',
17     :default => 30, :validate => Proc.new { |v| v > 0 }, :requires_restart => false,
18     :desc => "Seconds to wait after sending a message to nickserv, e.g. after ghosting")
19
20   def help(plugin, topic="")
21     case topic
22     when ""
23       return "nickserv plugin: handles nickserv protected IRC nicks. topics password, register, identify, listnicks"
24     when "password"
25       return "nickserv password <nick> <passwd>: remember the password for nick <nick> and use it to identify in future"
26     when "register"
27       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"
28     when "identify"
29       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"
30     when "listnicks"
31       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"
32     end
33   end
34   
35   def genpasswd
36     # generate a random password
37     passwd = ""
38     8.times do
39       passwd += (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr
40     end
41     return passwd
42   end
43
44   def set_ident_request(val)
45     @ident_request = Regexp.new(val)
46   end
47
48   def initialize
49     super
50     # this plugin only wants to store strings!
51     class << @registry
52       def store(val)
53         val
54       end
55       def restore(val)
56         val
57       end
58     end
59     set_ident_request(@bot.config['nickserv.ident_request'])
60   end
61
62   def password(m, params)
63     @registry[params[:nick]] = params[:passwd]
64     m.okay
65   end
66
67   def nick_register(m, params)
68     passwd = params[:passwd] ? params[:passwd] : genpasswd
69     message = "REGISTER #{passwd}"
70     message += " #{params[:email]}" if params[:email]
71     @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], message
72     @registry[@bot.nick] = passwd
73     m.okay
74   end
75
76   def listnicks(m, params)
77     if @registry.length > 0
78       @registry.each {|k,v|
79         @bot.say m.sourcenick, "#{k} => #{v}"
80       }
81     else
82       m.reply "none known"
83     end
84   end
85
86   def do_identify(nick=@bot.nick)
87     if @registry.has_key?(nick)
88       @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], "IDENTIFY #{nick} #{@registry[nick]}"
89       return true
90     end
91     return false
92   end
93
94   def identify(m, params)
95     if do_identify
96       m.okay
97     else
98       m.reply "I dunno the nickserv password for the nickname #{@bot.nick} :("
99     end
100   end
101   
102   def connect
103     do_identify
104   end
105   
106   def nicktaken(nick)
107     if @registry.has_key?(nick)
108       @bot.sendmsg "PRIVMSG", @bot.config['nickserv.name'], "GHOST #{nick} #{@registry[@bot.nick]}"
109       do_identify nick
110       sleep @bot.config['nickserv.wait']
111       @bot.nickchg nick
112       # We need to wait after changing nick, otherwise the server
113       # might refuse to execute further commangs, e.g. subsequent JOIN
114       # commands until the nick has changed.
115       sleep @bot.config['nickserv.wait']
116     end
117   end
118   
119   def listen(m)
120     return unless(m.kind_of? NoticeMessage)
121
122     if (m.sourcenick == @bot.config['nickserv.name'] && m.message =~ @ident_request)
123       debug "nickserv asked us to identify for nick #{@bot.nick}"
124       do_identify
125     end
126   end
127
128 end
129 plugin = NickServPlugin.new
130 plugin.map 'nickserv password :nick :passwd', :action => "password"
131 plugin.map 'nickserv register :passwd :email', :action => 'nick_register',
132            :defaults => {:passwd => false, :email => false}
133 plugin.map 'nickserv listnicks', :action => "listnicks"
134 plugin.map 'nickserv identify', :action => "identify"