]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/quakeauth.rb
10257adf099e78d067b45ef3cfab66510f65267f
[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   end
39
40   def set(m, params)
41     @registry['quakenet.user'] = params[:nick]
42     @registry['quakenet.auth'] = params[:password]
43     m.okay
44   end
45   
46   def connect
47     identify(nil, nil)
48   end
49
50   def identify(m, params)
51     @source = m.replyto
52     @registry['quakenet.auth'] = params[:password] if params[:password]
53
54     if @registry.has_key?('quakenet.user') && @registry.has_key?('quakenet.auth')
55       user = @registry['quakenet.user']
56       pass = @registry['quakenet.auth']
57
58       debug "authing with Q using #{user} #{pass}"
59       msg_q "auth #{user} #{pass}"
60     else
61       m.reply "not configured, try 'qauth set :nick :password' or 'qauth register :email'" if m
62     end
63   end
64
65   def notice(m)
66     if m.source.user == "TheQBot" && m.source.host = "CServe.quakenet.org"
67       case m.message
68       when /a user with that name already exists/i
69         @bot.say @source, "user with my name already exists, identify if it belongs to you"
70       when /created successfully/
71         @registry['quakenet.user'] = @bot.nick
72         @bot.say @source, "an email on how to proceed should have been sent to #{@email} -- 'qauth identify <password>' next"
73       when /too many accounts exist from this email address/i
74         @bot.say @source, "too many accounts on that email address"
75       when /registration service is unavailable/
76         @bot.say @source, "the registration service is unavailable, try again later"
77       when /password incorrect/
78         @bot.say @source, "username or password incorrect" if @source
79       when /you are now logged in/i
80         @bot.say @source, "authed successfully" if @source
81       when /auth is not available/
82         @bot.say @source, "already authed" if @source
83       end
84     end
85   end
86
87   def register_nick(m, params)
88     # check nick for invalid characters
89     if @bot.nick =~ /[`~\^\[\]{}|_\\]/
90       m.reply "for me to be able to register, my nick cannot have any of the following characters: `~^[]{}|_\\"
91       return
92     end
93
94     @email  = params[:email]
95     @source = m.replyto
96
97     msg_q "hello #{@email} #{@email}"
98   end
99
100   def msg_q(message)
101     @bot.say "Q@CServe.quakenet.org", message if on_quakenet?
102   end
103
104   def on_quakenet?
105     @bot.server.hostname.split(".")[-2] == "quakenet"
106   end
107 end
108
109 plugin = QPlugin.new
110 plugin.map 'qauth set :nick :password', :action => "set"
111 plugin.map 'qauth identify [:password]', :action => "identify"
112 plugin.map 'qauth register :email', :action => "register_nick"
113
114 plugin.default_auth('*', false)