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