Class: Atheme::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/atheme/session.rb

Constant Summary collapse

DEFAULT_CONNECTION_SETTINGS =

Connection settings which are used on default

{
  protocol: 'http',
  hostname: 'localhost',
  port: '8080'
}
DEFAULT_IP =

Default IP for connections if noone is specified on login

'127.0.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Session

Returns a new instance of Session.

Yields:

  • (_self)

Yield Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#call(*args) ⇒ Object

Send raw XMLRPC calls to the Atheme API



72
73
74
75
76
77
# File 'lib/atheme/session.rb', line 72

def call(*args)
  @server ||= connect_client
  return @server.call(*args)
rescue
  return Atheme::Error.new
end

#hostnameObject



100
101
102
# File 'lib/atheme/session.rb', line 100

def hostname
  @hostname || DEFAULT_CONNECTION_SETTINGS[:hostname]
end

#hostname=(h) ⇒ Object



88
89
90
# File 'lib/atheme/session.rb', line 88

def hostname=(h)
  @hostname = h
end

#logged_in?Boolean

Returns true if we are currently logged in and the cookie is a valid one; false otherwise.

Returns:

  • (Boolean)


57
58
59
60
61
62
# 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) ⇒ Object

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



30
31
32
33
34
35
36
37
38
# File 'lib/atheme/session.rb', line 30

def (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

#logoutObject

Logs out from the current session if previously logged in. Always returns true



49
50
51
52
53
54
# 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

#myselfObject

Returns an Atheme::User object of the current user who is logged in Returns false, if noone is currently logged in



66
67
68
69
# File 'lib/atheme/session.rb', line 66

def myself
  return false unless logged_in?
  self.nickserv.info(@user)
end

#portObject



104
105
106
# File 'lib/atheme/session.rb', line 104

def port
  @port || DEFAULT_CONNECTION_SETTINGS[:port]
end

#port=(p) ⇒ Object



92
93
94
# File 'lib/atheme/session.rb', line 92

def port=(p)
  @port = p.to_i
end

#protocolObject



96
97
98
# File 'lib/atheme/session.rb', line 96

def protocol
  @protocol || DEFAULT_CONNECTION_SETTINGS[:protocol]
end

#protocol=(p) ⇒ Object



84
85
86
# File 'lib/atheme/session.rb', line 84

def protocol=(p)
  @protocol = p
end

#relogin(cookie, user, ip = DEFAULT_IP) ⇒ Object

Relogin into the services using a previously created cookie and the associated username.



42
43
44
45
# 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) ⇒ Object

Shorthand method for service-calls through the Atheme API



80
81
82
# File 'lib/atheme/session.rb', line 80

def service_call(service, method, *args)
  self.call("atheme.command", @cookie, @user, @ip, service, method, *args)
end