]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/nickserv.rb
fix usage :/
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / nickserv.rb
1 # automatically lookup nicks in @registry and identify when asked
2
3 class NickServPlugin < Plugin
4   
5   def help(plugin, topic="")
6     case topic
7     when ""
8       return "nickserv plugin: handles nickserv protected IRC nicks. topics password, register, identify, listnicks"
9     when "password"
10       return "nickserv password <nick> <passwd>: remember the password for nick <nick> and use it to identify in future"
11     when "register"
12       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"
13     when "identify"
14       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"
15     when "listnicks"
16       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"
17     end
18   end
19   
20   def genpasswd
21     # generate a random password
22     passwd = ""
23     8.times do
24       passwd += (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr
25     end
26     return passwd
27   end
28
29   def initialize
30     super
31     # this plugin only wants to store strings!
32     class << @registry
33       def store(val)
34         val
35       end
36       def restore(val)
37         val
38       end
39     end
40   end
41
42   def password(m, params)
43     @registry[params[:nick]] = params[:passwd]
44     m.okay
45   end
46   def nick_register(m, params)
47     passwd = params[:passwd] ? params[:passwd] : genpasswd
48     message = "REGISTER #{passwd}"
49     message += " #{params[:email]}" if params[:email]
50     @bot.sendmsg "PRIVMSG", "NickServ", message
51     @registry[@bot.nick] = passwd
52     m.okay
53   end
54   def listnicks(m, params)
55     if @registry.length > 0
56       @registry.each {|k,v|
57         @bot.say m.sourcenick, "#{k} => #{v}"
58       }
59     else
60       m.reply "none known"
61     end
62   end
63   def identify(m, params)
64     if @registry.has_key?(@bot.nick)
65       @bot.sendmsg "PRIVMSG", "NickServ", "IDENTIFY #{@registry[@bot.nick]}"
66       m.okay
67     else
68       m.reply "I dunno the nickserv password for the nickname #{@bot.nick} :("
69     end
70   end
71   
72   def listen(m)
73     return unless(m.kind_of? NoticeMessage)
74
75     if (m.sourcenick == "NickServ" && m.message =~ /This nickname is owned by someone else/)
76       debug "nickserv asked us to identify for nick #{@bot.nick}"
77       if @registry.has_key?(@bot.nick)
78         @bot.sendmsg "PRIVMSG", "NickServ", "IDENTIFY " + @registry[@bot.nick]
79       end
80     end
81   end
82
83 end
84 plugin = NickServPlugin.new
85 plugin.map 'nickserv password :nick :passwd', :action => "password"
86 plugin.map 'nickserv register :passwd :email', :action => 'nick_register',
87            :defaults => {:passwd => false, :email => false}
88 plugin.map 'nickserv listnicks', :action => "listnicks"
89 plugin.map 'nickserv identify', :action => "identify"