Class: Hector::Session

Direct Known Subclasses

Service, UserSession

Constant Summary collapse

SESSIONS =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands::Invite

#on_invite

Methods included from Commands::Away

#on_away

Methods included from Commands::Whois

#on_whois, #respond_to_whois_for, #whois

Methods included from Commands::Who

#on_who, #sessions_for_who

Methods included from Commands::Topic

#on_topic, #respond_to_topic

Methods included from Commands::Realname

#on_realname

Methods included from Commands::Quit

#on_quit

Methods included from Commands::Privmsg

#on_privmsg

Methods included from Commands::Pong

#on_pong

Methods included from Commands::Ping

#on_ping

Methods included from Commands::Part

#on_part

Methods included from Commands::Notice

#on_notice

Methods included from Commands::Nick

#on_nick

Methods included from Commands::Names

#on_names, #respond_to_names

Methods included from Commands::Mode

#on_mode

Methods included from Commands::Join

#on_join

Methods included from Concerns::Presence

#channels, #destroy_presence, included, #initialize_presence, #peer_sessions, #seconds_idle, #touch_presence

Methods included from Concerns::KeepAlive

#destroy_keep_alive, #initialize_keep_alive, #on_heartbeat

Constructor Details

#initialize(nickname) ⇒ Session

Returns a new instance of Session.



86
87
88
# File 'lib/hector/session.rb', line 86

def initialize(nickname)
  @nickname = nickname
end

Instance Attribute Details

#away_messageObject (readonly)

Returns the value of attribute away_message.



23
24
25
# File 'lib/hector/session.rb', line 23

def away_message
  @away_message
end

#nicknameObject (readonly)

Returns the value of attribute nickname.



23
24
25
# File 'lib/hector/session.rb', line 23

def nickname
  @nickname
end

#requestObject (readonly)

Returns the value of attribute request.



23
24
25
# File 'lib/hector/session.rb', line 23

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



23
24
25
# File 'lib/hector/session.rb', line 23

def response
  @response
end

Class Method Details

.allObject



28
29
30
# File 'lib/hector/session.rb', line 28

def all
  sessions.values.grep(self)
end

.broadcast_to(sessions, command, *args) ⇒ Object



55
56
57
58
59
60
# File 'lib/hector/session.rb', line 55

def broadcast_to(sessions, command, *args)
  except = args.last.delete(:except) if args.last.is_a?(Hash)
  sessions.each do |session|
    session.respond_with(command, *args) unless session == except
  end
end

.delete(nickname) ⇒ Object



51
52
53
# File 'lib/hector/session.rb', line 51

def delete(nickname)
  sessions.delete(normalize(nickname))
end

.find(nickname) ⇒ Object



36
37
38
# File 'lib/hector/session.rb', line 36

def find(nickname)
  sessions[normalize(nickname)]
end

.nicknamesObject



32
33
34
# File 'lib/hector/session.rb', line 32

def nicknames
  sessions.keys
end

.normalize(nickname) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/hector/session.rb', line 62

def normalize(nickname)
  nickname.force_encoding("UTF-8") if nickname.respond_to?(:force_encoding)
  if nickname =~ /^[\p{L}\p{M}\p{N}\p{So}\p{Co}\w][\p{L}\p{M}\p{N}\p{So}\p{Co}\p{P}\w\|\-]{0,15}$/u
    nickname.downcase
  else
    raise ErroneousNickname, nickname
  end
end

.register(session) ⇒ Object



71
72
73
74
# File 'lib/hector/session.rb', line 71

def register(session)
  sessions[normalize(session.nickname)] = session
  session
end

.rename(from, to) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/hector/session.rb', line 40

def rename(from, to)
  if find(to)
    raise NicknameInUse, to
  else
    find(from).tap do |session|
      delete(from)
      sessions[normalize(to)] = session
    end
  end
end

.reset!Object



76
77
78
# File 'lib/hector/session.rb', line 76

def reset!
  sessions.clear
end

Instance Method Details

#away(away_message) ⇒ Object



148
149
150
# File 'lib/hector/session.rb', line 148

def away(away_message)
  @away_message = away_message
end

#away?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/hector/session.rb', line 152

def away?
  !@away_message.nil?
end

#backObject



156
157
158
# File 'lib/hector/session.rb', line 156

def back
  @away_message = nil
end

#broadcast(command, *args) ⇒ Object



90
91
92
# File 'lib/hector/session.rb', line 90

def broadcast(command, *args)
  Session.broadcast_to(peer_sessions, command, *args)
end

#channel?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/hector/session.rb', line 94

def channel?
  false
end

#deliver(message_type, session, options) ⇒ Object



98
99
100
# File 'lib/hector/session.rb', line 98

def deliver(message_type, session, options)
  respond_with(message_type, nickname, options)
end

#destroyObject



102
103
104
# File 'lib/hector/session.rb', line 102

def destroy
  self.class.delete(nickname)
end

#find(name) ⇒ Object



106
107
108
109
110
# File 'lib/hector/session.rb', line 106

def find(name)
  destination_klass_for(name).find(name).tap do |destination|
    raise NoSuchNickOrChannel, name unless destination
  end
end

#hostnameObject



112
113
114
# File 'lib/hector/session.rb', line 112

def hostname
  Hector.server_name
end

#nameObject



116
117
118
# File 'lib/hector/session.rb', line 116

def name
  nickname
end

#realnameObject



120
121
122
# File 'lib/hector/session.rb', line 120

def realname
  nickname
end

#receive(request) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/hector/session.rb', line 124

def receive(request)
  @request = request
  if respond_to?(@request.event_name)
    send(@request.event_name)
  end
ensure
  @request = nil
end

#rename(new_nickname) ⇒ Object



133
134
135
136
# File 'lib/hector/session.rb', line 133

def rename(new_nickname)
  Session.rename(nickname, new_nickname)
  @nickname = new_nickname
end

#respond_with(command, *args) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/hector/session.rb', line 138

def respond_with(command, *args)
  @response = command.is_a?(Response) ? command : Response.new(command, *preprocess_args(args))
  if respond_to?(@response.event_name)
    send(@response.event_name)
  end
  @response
ensure
  @response = nil
end

#sourceObject



160
161
162
# File 'lib/hector/session.rb', line 160

def source
  "#{nickname}!#{username}@#{hostname}"
end

#usernameObject



164
165
166
# File 'lib/hector/session.rb', line 164

def username
  "~#{nickname}"
end

#whoObject



168
169
170
# File 'lib/hector/session.rb', line 168

def who
  "#{identity.username} #{Hector.server_name} #{Hector.server_name} #{nickname} H :0 #{realname}"
end