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