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.



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

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) {|session| ... } ⇒ Object

Yields:

  • (session)


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

def register(session)
  yield session if block_given?
  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



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

def reset!
  sessions.clear
end

Instance Method Details

#away(away_message) ⇒ Object



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

def away(away_message)
  @away_message = away_message
end

#away?Boolean

Returns:

  • (Boolean)


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

def away?
  !@away_message.nil?
end

#backObject



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

def back
  @away_message = nil
end

#broadcast(command, *args) ⇒ Object



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

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

#channel?Boolean

Returns:

  • (Boolean)


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

def channel?
  false
end

#deliver(message_type, session, options) ⇒ Object



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

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

#destroyObject



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

def destroy
  self.class.delete(nickname)
end

#find(name) ⇒ Object



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

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

#hostnameObject



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

def hostname
  Hector.server_name
end

#nameObject



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

def name
  nickname
end

#realnameObject



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

def realname
  nickname
end

#receive(request) ⇒ Object



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

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

#rename(new_nickname) ⇒ Object



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

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

#respond_with(command, *args) ⇒ Object



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

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



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

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

#usernameObject



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

def username
  "~#{nickname}"
end

#whoObject



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

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