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

new(opts={}) { |self| ... } click to toggle source
# 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

call(*args) click to toggle source

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
hostname() click to toggle source
# File lib/atheme/session.rb, line 100
def hostname
  @hostname || DEFAULT_CONNECTION_SETTINGS[:hostname]
end
hostname=(h) click to toggle source
# File lib/atheme/session.rb, line 88
def hostname=(h)
  @hostname = h
end
logged_in?() click to toggle source

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(user, password, ip=DEFAULT_IP) click to toggle source

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
logout() click to toggle source

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
myself() click to toggle source

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
port() click to toggle source
# File lib/atheme/session.rb, line 104
def port
  @port || DEFAULT_CONNECTION_SETTINGS[:port]
end
port=(p) click to toggle source
# File lib/atheme/session.rb, line 92
def port=(p)
  @port = p.to_i
end
protocol() click to toggle source
# File lib/atheme/session.rb, line 96
def protocol
  @protocol || DEFAULT_CONNECTION_SETTINGS[:protocol]
end
protocol=(p) click to toggle source
# File lib/atheme/session.rb, line 84
def protocol=(p)
  @protocol = p
end
relogin(cookie, user, ip=DEFAULT_IP) click to toggle source

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
service_call(service, method, *args) click to toggle source

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

connect_client() click to toggle source
# File lib/atheme/session.rb, line 110
def connect_client
  @server = XMLRPC::Client.new2("#{self.protocol}://#{self.hostname}:#{self.port}/xmlrpc")
end