]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/nickserv.rb
Nickserv fixes
[user/henk/code/ruby/rbot.git] / 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>]: register the current nick, choosing a random password unless <password> is supplied - current nick must not already be registered for this to work"
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 initialize
21     super
22     # this plugin only wants to store strings!
23     class << @registry
24       def store(val)
25         val
26       end
27       def restore(val)
28         val
29       end
30     end
31   end
32   
33   def privmsg(m)
34     return unless m.params
35     
36     case m.params
37     when (/^password\s*(\S*)\s*(.*)$/)
38       nick = $1
39       passwd = $2
40       @registry[nick] = passwd
41       @bot.okay m.replyto
42     when (/^register$/)
43       passwd = genpasswd
44       @bot.sendmsg "PRIVMSG", "NickServ", "REGISTER " + passwd
45       @registry[@bot.nick] = passwd
46       @bot.okay m.replyto
47     when (/^register\s*(.*)\s*$/)
48       passwd = $1
49       @bot.sendmsg "PRIVMSG", "NickServ", "REGISTER " + passwd
50       @registry[@bot.nick] = passwd
51       @bot.okay m.replyto
52     when (/^listnicks$/)
53       if @bot.auth.allow?("config", m.source, m.replyto)
54         if @registry.length > 0
55           @registry.each {|k,v|
56             @bot.say m.sourcenick, "#{k} => #{v}"
57           }
58         else
59           m.reply "none known"
60         end
61       end
62     when (/^identify$/)
63       if @registry.has_key?(@bot.nick)
64         @bot.sendmsg "PRIVMSG", "NickServ", "IDENTIFY " + @registry[@bot.nick]
65         @bot.okay m.replyto
66       else
67         m.reply "I dunno the nickserv password for the nickname #{@bot.nick} :("
68       end
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       puts "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   def genpasswd
84     # generate a random password
85     passwd = ""
86     8.times do
87       passwd += (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr
88     end
89     return passwd
90   end
91 end
92 plugin = NickServPlugin.new
93 plugin.register("nickserv")