class Atheme::Session
Constants
- DEFAULT_CONNECTION_SETTINGS
Connection settings which are used on default
- DEFAULT_IP
Default IP for connections if noone is specified on login
Public Class Methods
# File lib/atheme/session.rb, line 13 def initialize(opts={}) opts.each_key do |k| if self.respond_to?("#{k}=") self.send("#{k}=", opts[k]) else raise ArgumentError, "Argument '#{k}' is not allowed" end end yield self if block_given? @cookie, @user, @ip = '.', '.', DEFAULT_IP end
Public Instance Methods
Send raw XMLRPC calls to the Atheme
API
# File lib/atheme/session.rb, line 72 def call(*args) @server ||= connect_client return @server.call(*args) rescue return Atheme::Error.new end
# File lib/atheme/session.rb, line 100 def hostname @hostname || DEFAULT_CONNECTION_SETTINGS[:hostname] end
# File lib/atheme/session.rb, line 88 def hostname=(h) @hostname = h end
Returns true if we are currently logged in and the cookie is a valid one; false otherwise.
# File lib/atheme/session.rb, line 57 def logged_in? return false if @cookie.nil? || @cookie=='.' #need to call a dummy command to decide if the cookie is still valid #as we think that we are a valid user, /ns info 'self' should succeed self.nickserv.info(@user).success? end
Login with an username and passwortd into atheme. The creditials are the same as in IRC. IP is optional and only for logging purposes, defaults to 127.0.0.1. Returns a cookie on success, an Atheme::Error
otherwise
# File lib/atheme/session.rb, line 30 def login(user, password, ip=DEFAULT_IP) cookie_or_error = self.call("atheme.login", user, password, ip) if cookie_or_error.kind_of?(String) @cookie, @user, @ip = cookie_or_error, user, ip else # should be Atheme::Error @cookie, @user, @ip = '.', '.', DEFAULT_IP end return cookie_or_error end
Logs out from the current session if previously logged in. Always returns true
# File lib/atheme/session.rb, line 49 def logout return true unless logged_in? self.call("atheme.logout", @cookie, @user, @ip) @cookie, @user, @ip = '.', '.', DEFAULT_IP true end
Returns an Atheme::User
object of the current user who is logged in Returns false, if noone is currently logged in
# File lib/atheme/session.rb, line 66 def myself return false unless logged_in? self.nickserv.info(@user) end
# File lib/atheme/session.rb, line 104 def port @port || DEFAULT_CONNECTION_SETTINGS[:port] end
# File lib/atheme/session.rb, line 92 def port=(p) @port = p.to_i end
# File lib/atheme/session.rb, line 96 def protocol @protocol || DEFAULT_CONNECTION_SETTINGS[:protocol] end
# File lib/atheme/session.rb, line 84 def protocol=(p) @protocol = p end
Relogin into the services using a previously created cookie and the associated username.
# File lib/atheme/session.rb, line 42 def relogin(cookie, user, ip=DEFAULT_IP) @cookie, @user, @ip = cookie, user, ip logged_in? end
Shorthand method for service-calls through the Atheme
API
# File lib/atheme/session.rb, line 80 def service_call(service, method, *args) self.call("atheme.command", @cookie, @user, @ip, service, method, *args) end
Private Instance Methods
# File lib/atheme/session.rb, line 110 def connect_client @server = XMLRPC::Client.new2("#{self.protocol}://#{self.hostname}:#{self.port}/xmlrpc") end