]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/nickserv.rb
2a40bae5ff633ac48cd7799e2b63e0c0fc19ccee
[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[nick] = passwd
46       @bot.okay m.replyto
47     when (/^register\s*(.*)\s*$/)
48       passwd = $1
49       @bot.sendmsg "PRIVMSG", "NickServ", "REGISTER " + passwd
50       @bot.okay m.replyto
51     when (/^listnicks$/)
52       if @bot.auth.allow?("config", m.source, m.replyto)
53         if @registry.length > 0
54           @registry.each {|k,v|
55             @bot.say m.sourcenick, "#{k} => #{v}"
56           }
57         else
58           m.reply "none known"
59         end
60       end
61     when (/^identify$/)
62       if @registry.has_key?(@bot.nick)
63         @bot.sendmsg "PRIVMSG", "NickServ", "IDENTIFY " + @registry[@bot.nick]
64         @bot.okay m.replyto
65       else
66         m.reply "I dunno the nickserv password for the nickname #{@bot.nick} :("
67       end
68     end
69   end
70   
71   def listen(m)
72     return unless(m.kind_of? NoticeMessage)
73
74     if (m.sourcenick == "NickServ" && m.message =~ /This nickname is owned by someone else/)
75       puts "nickserv asked us to identify for nick #{@bot.nick}"
76       if @registry.has_key?(@bot.nick)
77         @bot.sendmsg "PRIVMSG", "NickServ", "IDENTIFY " + @registry[@bot.nick]
78       end
79     end
80   end
81
82   def genpasswd
83     # generate a random password
84     passwd = ""
85     8.times do
86       passwd += (rand(26) + (rand(2) == 0 ? 65 : 97) ).chr
87     end
88     return passwd
89   end
90 end
91 plugin = NickServPlugin.new
92 plugin.register("nickserv")