Class: Hector::Session
- Inherits:
-
Object
show all
- Includes:
- Commands::Away, Commands::Invite, Commands::Join, Commands::Mode, Commands::Names, Commands::Nick, Commands::Notice, Commands::Part, Commands::Ping, Commands::Pong, Commands::Privmsg, Commands::Quit, Commands::Realname, Commands::Topic, Commands::Who, Commands::Whois, Concerns::KeepAlive, Concerns::Presence
- Defined in:
- lib/hector/session.rb
Constant Summary
collapse
- SESSIONS =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#on_invite
#on_away
#on_whois, #respond_to_whois_for, #whois
#on_who, #sessions_for_who
#on_topic, #respond_to_topic
#on_realname
#on_quit
#on_privmsg
#on_pong
#on_ping
#on_part
#on_notice
#on_nick
#on_names, #respond_to_names
#on_mode
#on_join
#channels, #destroy_presence, included, #initialize_presence, #peer_sessions, #seconds_idle, #touch_presence
#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_message ⇒ Object
Returns the value of attribute away_message.
23
24
25
|
# File 'lib/hector/session.rb', line 23
def away_message
@away_message
end
|
#nickname ⇒ Object
Returns the value of attribute nickname.
23
24
25
|
# File 'lib/hector/session.rb', line 23
def nickname
@nickname
end
|
#request ⇒ Object
Returns the value of attribute request.
23
24
25
|
# File 'lib/hector/session.rb', line 23
def request
@request
end
|
#response ⇒ Object
Returns the value of attribute response.
23
24
25
|
# File 'lib/hector/session.rb', line 23
def response
@response
end
|
Class Method Details
.all ⇒ Object
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
|
.nicknames ⇒ Object
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
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
153
154
155
|
# File 'lib/hector/session.rb', line 153
def away?
!@away_message.nil?
end
|
#back ⇒ Object
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
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
|
#destroy ⇒ Object
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
|
#hostname ⇒ Object
113
114
115
|
# File 'lib/hector/session.rb', line 113
def hostname
Hector.server_name
end
|
#name ⇒ Object
117
118
119
|
# File 'lib/hector/session.rb', line 117
def name
nickname
end
|
#realname ⇒ Object
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
|
#source ⇒ Object
161
162
163
|
# File 'lib/hector/session.rb', line 161
def source
"#{nickname}!#{username}@#{hostname}"
end
|
#username ⇒ Object
165
166
167
|
# File 'lib/hector/session.rb', line 165
def username
"~#{nickname}"
end
|