]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/quakeauth.rb
plugin(search): fix search and gcalc, closes #28, #29
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / quakeauth.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: QuakeNet Auth Plugin
5 #
6 # Author:: Raine Virta <rane@kapsi.fi>
7 # Copyright:: (C) 2008 Raine Virta
8 # License:: GPL v2
9 #
10 # Automatically auths with Q on QuakeNet servers
11
12 class QPlugin < Plugin
13
14   def help(plugin, topic="")
15     case topic
16     when ""
17       return "qauth plugin: handles Q auths. topics set, identify, register"
18     when "set"
19       return "qauth set <user> <password>: set the Q user and password and use it to identify in future"
20     when "identify"
21       return "qauth identify: identify with Q (if user and auth are set)"
22     when "register"
23       return "qauth register <email>: register with Q, an email on how to proceed will be sent to the email address you provide"
24     end
25   end
26
27   def initialize
28     super
29     # this plugin only wants to store strings!
30     class << @registry
31       def store(val)
32         val
33       end
34       def restore(val)
35         val
36       end
37     end
38     @source = nil
39   end
40
41   def set(m, params)
42     @registry['quakenet.user'] = params[:nick]
43     @registry['quakenet.auth'] = params[:password]
44     m.okay
45   end
46
47   def connect
48     identify(nil, {}) if on_quakenet?
49   end
50
51   def identify(m, params)
52     @source = m.replyto if m
53     @registry['quakenet.auth'] = params[:password] if params[:password]
54
55     if @registry.has_key?('quakenet.user') && @registry.has_key?('quakenet.auth')
56       user = @registry['quakenet.user']
57       pass = @registry['quakenet.auth']
58
59       debug "authing with Q using #{user} #{pass}"
60       msg_q "auth #{user} #{pass}"
61     else
62       m.reply "not configured, try 'qauth set :nick :password' or 'qauth register :email'" if m
63     end
64   end
65
66   def notice(m)
67     if m.source.user == "TheQBot" && m.source.host = "CServe.quakenet.org"
68       case m.message
69       when /a user with that name already exists/i
70         @bot.say @source, "user with my name already exists, identify if it belongs to you"
71       when /created successfully/
72         @registry['quakenet.user'] = @bot.nick
73         @bot.say @source, "an email on how to proceed should have been sent to #{@email} -- 'qauth identify <password>' next"
74       when /too many accounts exist from this email address/i
75         @bot.say @source, "too many accounts on that email address"
76       when /registration service is unavailable/
77         @bot.say @source, "the registration service is unavailable, try again later"
78       when /password incorrect/
79         @bot.say @source, "username or password incorrect" if @source
80       when /you are now logged in/i
81         @bot.say @source, "authed successfully" if @source
82         @bot.plugins.delegate('identified')
83       when /auth is not available/
84         @bot.say @source, "already authed" if @source
85       end
86     end
87   end
88
89   def register_nick(m, params)
90     # check nick for invalid characters
91     if @bot.nick =~ /[`~\^\[\]{}|_\\]/
92       m.reply "for me to be able to register, my nick cannot have any of the following characters: `~^[]{}|_\\"
93       return
94     end
95
96     @email  = params[:email]
97     @source = m.replyto
98
99     msg_q "hello #{@email} #{@email}"
100   end
101
102   def msg_q(message)
103     @bot.say "Q@CServe.quakenet.org", message if on_quakenet?
104   end
105
106   def on_quakenet?
107     @bot.server.hostname.split(".")[-2] == "quakenet"
108   end
109 end
110
111 plugin = QPlugin.new
112 plugin.map 'qauth set :nick :password', :action => "set"
113 plugin.map 'qauth identify [:password]', :action => "identify"
114 plugin.map 'qauth register :email', :action => "register_nick"
115
116 plugin.default_auth('*', false)