Class: Yaic::Client
- Inherits:
-
Object
- Object
- Yaic::Client
- Defined in:
- lib/yaic/client.rb
Constant Summary collapse
- STALE_TIMEOUT =
180- DEFAULT_CONNECT_TIMEOUT =
30- DEFAULT_OPERATION_TIMEOUT =
10- EVENT_MAP =
{ "PRIVMSG" => :message, "NOTICE" => :notice, "JOIN" => :join, "PART" => :part, "QUIT" => :quit, "KICK" => :kick, "NICK" => :nick, "TOPIC" => :topic, "MODE" => :mode }.freeze
- PREFIX_MODES =
{ "@" => :op, "+" => :voice, "%" => :halfop, "~" => :owner, "&" => :admin }.freeze
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #channels ⇒ Object
- #connect(timeout: DEFAULT_CONNECT_TIMEOUT) ⇒ Object
- #connected? ⇒ Boolean
- #connection_stale? ⇒ Boolean
- #disconnect ⇒ Object
- #handle_message(message) ⇒ Object
-
#initialize(port:, host: nil, nick: nil, user: nil, realname: nil, password: nil, ssl: false, server: nil, nickname: nil, username: nil, verbose: false) ⇒ Client
constructor
A new instance of Client.
- #isupport ⇒ Object
- #join(channel, key = nil, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
- #kick(channel, nick, reason = nil) ⇒ Object
- #last_received_at ⇒ Object
- #mode(target, modes = nil, *args) ⇒ Object
- #names(channel) ⇒ Object
- #nick(new_nick = nil, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
- #notice(target, text) ⇒ Object
- #off(event_type) ⇒ Object
- #on(event_type, &block) ⇒ Object
- #part(channel, reason = nil, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
- #privmsg(target, text) ⇒ Object (also: #msg)
- #quit(reason = nil) ⇒ Object
- #raw(command) ⇒ Object
- #state ⇒ Object
- #topic(channel, new_topic = nil) ⇒ Object
- #who(mask, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
- #whois(nick, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
Constructor Details
#initialize(port:, host: nil, nick: nil, user: nil, realname: nil, password: nil, ssl: false, server: nil, nickname: nil, username: nil, verbose: false) ⇒ Client
Returns a new instance of Client.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/yaic/client.rb', line 47 def initialize(port:, host: nil, nick: nil, user: nil, realname: nil, password: nil, ssl: false, server: nil, nickname: nil, username: nil, verbose: false) @server = server || host @port = port @nick = nickname || nick @user = username || user || @nick @realname = realname || @nick @password = password @ssl = ssl @verbose = verbose @socket = nil @state = :disconnected @isupport = {} @nick_attempts = 0 @last_received_at = nil @handlers = {} @channels = {} @pending_names = {} @pending_whois = {} @pending_whois_complete = {} @pending_who_results = {} @pending_who_complete = {} @read_thread = nil @monitor = Monitor.new end |
Instance Attribute Details
#server ⇒ Object (readonly)
Returns the value of attribute server.
29 30 31 |
# File 'lib/yaic/client.rb', line 29 def server @server end |
Instance Method Details
#channels ⇒ Object
43 44 45 |
# File 'lib/yaic/client.rb', line 43 def channels @monitor.synchronize { @channels.dup } end |
#connect(timeout: DEFAULT_CONNECT_TIMEOUT) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/yaic/client.rb', line 73 def connect(timeout: DEFAULT_CONNECT_TIMEOUT) log "Connecting to #{@server}:#{@port}#{" (SSL)" if @ssl}..." sock = @monitor.synchronize do @socket ||= Socket.new(@server, @port, ssl: @ssl) end sock.connect send_registration set_state(:registering) start_read_loop wait_until(timeout: timeout) { connected? } log "Connected" end |
#connected? ⇒ Boolean
86 87 88 |
# File 'lib/yaic/client.rb', line 86 def connected? @monitor.synchronize { @state == :connected } end |
#connection_stale? ⇒ Boolean
153 154 155 156 157 158 |
# File 'lib/yaic/client.rb', line 153 def connection_stale? @monitor.synchronize do return false if @last_received_at.nil? Time.now - @last_received_at > STALE_TIMEOUT end end |
#disconnect ⇒ Object
90 91 92 93 94 |
# File 'lib/yaic/client.rb', line 90 def disconnect socket&.disconnect set_state(:disconnected) @read_thread&.join(1) end |
#handle_message(message) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/yaic/client.rb', line 96 def () @monitor.synchronize { @last_received_at = Time.now } case .command when "PING" handle_ping() when "001" handle_rpl_welcome() when "005" handle_rpl_isupport() when "433" handle_err_nicknameinuse() when "JOIN" handle_join() when "PART" handle_part() when "QUIT" handle_quit() when "NICK" handle_nick() when "KICK" handle_kick() when "TOPIC" handle_topic() when "332" handle_rpl_topic() when "333" handle_rpl_topicwhotime() when "353" handle_rpl_namreply() when "366" handle_rpl_endofnames() when "MODE" handle_mode() when "352" handle_rpl_whoreply() when "315" handle_rpl_endofwho() when "311" handle_rpl_whoisuser() when "319" handle_rpl_whoischannels() when "312" handle_rpl_whoisserver() when "317" handle_rpl_whoisidle() when "330" handle_rpl_whoisaccount() when "301" handle_rpl_away() when "318" handle_rpl_endofwhois() end emit_events() end |
#isupport ⇒ Object
35 36 37 |
# File 'lib/yaic/client.rb', line 35 def isupport @monitor.synchronize { @isupport.dup } end |
#join(channel, key = nil, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/yaic/client.rb', line 187 def join(channel, key = nil, timeout: DEFAULT_OPERATION_TIMEOUT) log "Joining #{channel}..." params = key ? [channel, key] : [channel] = Message.new(command: "JOIN", params: params) socket.write(.to_s) wait_until(timeout: timeout) { channel_joined?(channel) } log "Joined #{channel}" end |
#kick(channel, nick, reason = nil) ⇒ Object
232 233 234 235 236 |
# File 'lib/yaic/client.rb', line 232 def kick(channel, nick, reason = nil) params = reason ? [channel, nick, reason] : [channel, nick] = Message.new(command: "KICK", params: params) socket.write(.to_s) end |
#last_received_at ⇒ Object
39 40 41 |
# File 'lib/yaic/client.rb', line 39 def last_received_at @monitor.synchronize { @last_received_at } end |
#mode(target, modes = nil, *args) ⇒ Object
243 244 245 246 247 248 249 |
# File 'lib/yaic/client.rb', line 243 def mode(target, modes = nil, *args) params = [target] params << modes if modes params.concat(args) unless args.empty? = Message.new(command: "MODE", params: params) socket.write(.to_s) end |
#names(channel) ⇒ Object
238 239 240 241 |
# File 'lib/yaic/client.rb', line 238 def names(channel) = Message.new(command: "NAMES", params: [channel]) socket.write(.to_s) end |
#nick(new_nick = nil, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
217 218 219 220 221 222 223 224 |
# File 'lib/yaic/client.rb', line 217 def nick(new_nick = nil, timeout: DEFAULT_OPERATION_TIMEOUT) return @monitor.synchronize { @nick } if new_nick.nil? old_nick = @monitor.synchronize { @nick } = Message.new(command: "NICK", params: [new_nick]) socket.write(.to_s) wait_until(timeout: timeout) { @monitor.synchronize { @nick } != old_nick } end |
#notice(target, text) ⇒ Object
182 183 184 185 |
# File 'lib/yaic/client.rb', line 182 def notice(target, text) = Message.new(command: "NOTICE", params: [target, text]) socket.write(.to_s) end |
#off(event_type) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/yaic/client.rb', line 168 def off(event_type) @monitor.synchronize do @handlers.delete(event_type) end self end |
#on(event_type, &block) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/yaic/client.rb', line 160 def on(event_type, &block) @monitor.synchronize do @handlers[event_type] ||= [] @handlers[event_type] << block end self end |
#part(channel, reason = nil, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
196 197 198 199 200 201 202 203 |
# File 'lib/yaic/client.rb', line 196 def part(channel, reason = nil, timeout: DEFAULT_OPERATION_TIMEOUT) log "Parting #{channel}..." params = reason ? [channel, reason] : [channel] = Message.new(command: "PART", params: params) socket.write(.to_s) wait_until(timeout: timeout) { !channel_joined?(channel) } log "Parted #{channel}" end |
#privmsg(target, text) ⇒ Object Also known as: msg
175 176 177 178 |
# File 'lib/yaic/client.rb', line 175 def privmsg(target, text) = Message.new(command: "PRIVMSG", params: [target, text]) socket.write(.to_s) end |
#quit(reason = nil) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/yaic/client.rb', line 205 def quit(reason = nil) params = reason ? [reason] : [] = Message.new(command: "QUIT", params: params) socket.write(.to_s) @monitor.synchronize { @channels.clear } set_state(:disconnected) @read_thread&.join(5) socket&.disconnect emit(:disconnect, nil) log "Disconnected" end |
#raw(command) ⇒ Object
290 291 292 |
# File 'lib/yaic/client.rb', line 290 def raw(command) socket.write(command) end |
#state ⇒ Object
31 32 33 |
# File 'lib/yaic/client.rb', line 31 def state @monitor.synchronize { @state } end |
#topic(channel, new_topic = nil) ⇒ Object
226 227 228 229 230 |
# File 'lib/yaic/client.rb', line 226 def topic(channel, new_topic = nil) params = new_topic.nil? ? [channel] : [channel, new_topic] = Message.new(command: "TOPIC", params: params) socket.write(.to_s) end |
#who(mask, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/yaic/client.rb', line 251 def who(mask, timeout: DEFAULT_OPERATION_TIMEOUT) log "Sending WHO #{mask}..." @monitor.synchronize do @pending_who_results[mask] = [] @pending_who_complete[mask] = false end = Message.new(command: "WHO", params: [mask]) socket.write(.to_s) wait_until(timeout: timeout) { @monitor.synchronize { @pending_who_complete[mask] } } @monitor.synchronize do results = @pending_who_results.delete(mask) log "WHO complete (#{results.size} results)" results end ensure @monitor.synchronize { @pending_who_complete.delete(mask) } end |
#whois(nick, timeout: DEFAULT_OPERATION_TIMEOUT) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/yaic/client.rb', line 272 def whois(nick, timeout: DEFAULT_OPERATION_TIMEOUT) log "Sending WHOIS #{nick}..." @monitor.synchronize { @pending_whois_complete[nick] = false } = Message.new(command: "WHOIS", params: [nick]) socket.write(.to_s) wait_until(timeout: timeout) { @monitor.synchronize { @pending_whois_complete[nick] } } @monitor.synchronize do result = @pending_whois.delete(nick) log "WHOIS complete" result end ensure @monitor.synchronize { @pending_whois_complete.delete(nick) } end |