Class: IRCSocket
- Inherits:
-
Object
- Object
- IRCSocket
- Defined in:
- lib/irc-socket.rb
Overview
Author
-
Lee Jarvis - [email protected]
Description
IRCSocket is an IRC wrapper around a TCPSocket. It implements all of the major commands laid out in RFC 2812. All these commands are available as instance methods of an IRCSocket Object.
Example
irc = IRCSocket.new('irc.freenode.org')
irc.connect
if irc.connected?
irc.nick "HulkHogan"
irc.user "Hulk", 0, "*", "I am Hulk Hogan"
while line = irc.read
# Join a channel after MOTD
if line.split[1] == '376'
irc.join "#mychannel"
end
puts "Received: #{line}"
end
end
Block Form
IRCSocket.new('irc.freenode.org') do |irc|
irc.nick "SpongeBob"
irc.user "Spongey", 0, "*", "Square Pants"
puts irc.read
end
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
The port our socket is connected on.
-
#server ⇒ Object
readonly
The server our socket is connected to.
-
#socket ⇒ Object
readonly
The TCPSocket instance.
Class Method Summary collapse
-
.open(server, port = 6667, ssl = false) ⇒ Object
Creates a new IRCSocket and automatically connects.
Instance Method Summary collapse
-
#away(message = nil) ⇒ Object
Send AWAY command.
-
#close ⇒ Object
Close our socket instance.
-
#connect ⇒ Object
Connect to an IRC server, returns true on a successful connection, or raises otherwise.
-
#connected? ⇒ Boolean
(also: #connected)
Check if our socket is alive and connected to an IRC server.
-
#info(target = nil) ⇒ Object
Send INFO command.
-
#initialize(server, port = 6667, ssl = false) ⇒ IRCSocket
constructor
Create a new IRCSocket to connect to
server
onport
. -
#invite(nickname, channel) ⇒ Object
Send INVITE command.
-
#join(channel, password = nil) ⇒ Object
Send JOIN command - Join a channel with given password.
-
#kick(channel, user, comment = nil) ⇒ Object
Send KICK command.
-
#kill(user, message) ⇒ Object
Send KILL command.
-
#list(*channels) ⇒ Object
Send LIST command.
-
#mode(channel, *modes) ⇒ Object
Send the MODE command.
-
#motd(target = nil) ⇒ Object
Send MOTD command.
-
#names(*channels) ⇒ Object
Send NAMES command.
-
#nick(nickname) ⇒ Object
Send NICK command.
-
#notice(target, message) ⇒ Object
Send NOTICE command.
-
#oper(name, password) ⇒ Object
Send OPER command.
-
#part(channel, message = nil) ⇒ Object
Send PART command.
-
#pass(password) ⇒ Object
Send PASS command.
-
#ping(server) ⇒ Object
Send PING command.
-
#pong(server) ⇒ Object
Send PONG command.
-
#privmsg(target, message) ⇒ Object
Send PRIVMSG command.
-
#quit(message = nil) ⇒ Object
Send QUIT command.
-
#read(chompstr = "\r\n") ⇒ Object
Read the next line in from the server.
-
#squery(target, message) ⇒ Object
Send SQUERY command.
-
#stats(*params) ⇒ Object
Send STATS command.
-
#time(target = nil) ⇒ Object
Send TIME command.
-
#topic(channel, topic = nil) ⇒ Object
Send TOPIC command.
-
#user(user, mode, unused, realname) ⇒ Object
Send USER command.
-
#userhost(*users) ⇒ Object
Send USERHOST command.
-
#users(target = nil) ⇒ Object
Send USERS command.
-
#version(target = nil) ⇒ Object
Send VERSION command.
-
#who(*params) ⇒ Object
Send WHO command.
-
#whois(*params) ⇒ Object
Send WHOIS command.
-
#whowas(*params) ⇒ Object
Send WHOWAS command.
-
#write(data) ⇒ Object
Write to our Socket and append CRLF.
Constructor Details
#initialize(server, port = 6667, ssl = false) ⇒ IRCSocket
Create a new IRCSocket to connect to server
on port
. Defaults to port 6667. If an optional code block is given, it will be passed an instance of the IRCSocket.
NOTE: Using the block form does not mean the socket will send the applicable QUIT command to leave the IRC server. You must send this yourself.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/irc-socket.rb', line 67 def initialize(server, port=6667, ssl=false) @server = server @port = port @ssl = ssl @socket = nil @connected = false if block_given? connect yield self end end |
Instance Attribute Details
#port ⇒ Object (readonly)
The port our socket is connected on
43 44 45 |
# File 'lib/irc-socket.rb', line 43 def port @port end |
#server ⇒ Object (readonly)
The server our socket is connected to
40 41 42 |
# File 'lib/irc-socket.rb', line 40 def server @server end |
#socket ⇒ Object (readonly)
The TCPSocket instance
46 47 48 |
# File 'lib/irc-socket.rb', line 46 def socket @socket end |
Class Method Details
.open(server, port = 6667, ssl = false) ⇒ Object
Creates a new IRCSocket and automatically connects
Example
irc = IRCSocket.open('irc.freenode.org')
while data = irc.read
puts data
end
56 57 58 59 60 |
# File 'lib/irc-socket.rb', line 56 def self.open(server, port=6667, ssl=false) irc = new(server, port, ssl) irc.connect irc end |
Instance Method Details
#away(message = nil) ⇒ Object
Send AWAY command
281 282 283 |
# File 'lib/irc-socket.rb', line 281 def away(=nil) raw("AWAY", ) end |
#close ⇒ Object
Close our socket instance
296 297 298 |
# File 'lib/irc-socket.rb', line 296 def close @socket.close if connected? end |
#connect ⇒ Object
Connect to an IRC server, returns true on a successful connection, or raises otherwise
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/irc-socket.rb', line 89 def connect socket = TCPSocket.new(server, port) if @ssl require 'openssl' ssl = OpenSSL::SSL::SSLContext.new ssl.verify_mode = OpenSSL::SSL::VERIFY_NONE @socket = OpenSSL::SSL::SSLSocket.new(socket, ssl) @socket.sync = true @socket.connect else @socket = socket end rescue Interrupt raise rescue Exception raise else @connected = true end |
#connected? ⇒ Boolean Also known as: connected
Check if our socket is alive and connected to an IRC server
82 83 84 |
# File 'lib/irc-socket.rb', line 82 def connected? @connected end |
#info(target = nil) ⇒ Object
Send INFO command
241 242 243 |
# File 'lib/irc-socket.rb', line 241 def info(target=nil) write_optional("INFO", target) end |
#invite(nickname, channel) ⇒ Object
Send INVITE command
201 202 203 |
# File 'lib/irc-socket.rb', line 201 def invite(nickname, channel) write("INVITE #{nickname} #{channel}") end |
#join(channel, password = nil) ⇒ Object
Send JOIN command - Join a channel with given password
176 177 178 |
# File 'lib/irc-socket.rb', line 176 def join(channel, password=nil) write_optional("JOIN #{channel}", password) end |
#kick(channel, user, comment = nil) ⇒ Object
Send KICK command
206 207 208 |
# File 'lib/irc-socket.rb', line 206 def kick(channel, user, comment=nil) raw("KICK", channel, user, comment) end |
#kill(user, message) ⇒ Object
Send KILL command
266 267 268 |
# File 'lib/irc-socket.rb', line 266 def kill(user, ) write("KILL #{user} :#{}") end |
#list(*channels) ⇒ Object
Send LIST command
196 197 198 |
# File 'lib/irc-socket.rb', line 196 def list(*channels) write("LIST #{channels.join(',') unless channels.empty?}") end |
#mode(channel, *modes) ⇒ Object
Send the MODE command. Should probably implement a better way of doing this
166 167 168 |
# File 'lib/irc-socket.rb', line 166 def mode(channel, *modes) write("MODE #{channel} #{modes.join(' ')}") end |
#motd(target = nil) ⇒ Object
Send MOTD command
221 222 223 |
# File 'lib/irc-socket.rb', line 221 def motd(target=nil) write_optional("MOTD", target) end |
#names(*channels) ⇒ Object
Send NAMES command
191 192 193 |
# File 'lib/irc-socket.rb', line 191 def names(*channels) write("NAMES #{channels.join(',') unless channels.empty?}") end |
#nick(nickname) ⇒ Object
Send NICK command
150 151 152 |
# File 'lib/irc-socket.rb', line 150 def nick(nickname) write("NICK #{nickname}") end |
#notice(target, message) ⇒ Object
Send NOTICE command
216 217 218 |
# File 'lib/irc-socket.rb', line 216 def notice(target, ) write("NOTICE #{target} :#{}") end |
#oper(name, password) ⇒ Object
Send OPER command
160 161 162 |
# File 'lib/irc-socket.rb', line 160 def oper(name, password) write("OPER #{name} #{password}") end |
#part(channel, message = nil) ⇒ Object
Send PART command
181 182 183 |
# File 'lib/irc-socket.rb', line 181 def part(channel, =nil) raw("PART", channel, ) end |
#pass(password) ⇒ Object
Send PASS command
145 146 147 |
# File 'lib/irc-socket.rb', line 145 def pass(password) write("PASS #{password}") end |
#ping(server) ⇒ Object
Send PING command
271 272 273 |
# File 'lib/irc-socket.rb', line 271 def ping(server) write("PING #{server}") end |
#pong(server) ⇒ Object
Send PONG command
276 277 278 |
# File 'lib/irc-socket.rb', line 276 def pong(server) write("PONG #{server}") end |
#privmsg(target, message) ⇒ Object
Send PRIVMSG command
211 212 213 |
# File 'lib/irc-socket.rb', line 211 def privmsg(target, ) write("PRIVMSG #{target} :#{}") end |
#quit(message = nil) ⇒ Object
Send QUIT command
171 172 173 |
# File 'lib/irc-socket.rb', line 171 def quit(=nil) raw("QUIT", ) end |
#read(chompstr = "\r\n") ⇒ Object
Read the next line in from the server. If no arguments are passed the line will have the CRLF chomp’ed. Returns nil if no data could be read
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/irc-socket.rb', line 113 def read(chompstr="\r\n") if data = @socket.gets("\r\n") data.chomp!(chompstr) if chompstr data else nil end rescue IOError nil end |
#squery(target, message) ⇒ Object
Send SQUERY command
246 247 248 |
# File 'lib/irc-socket.rb', line 246 def squery(target, ) write("SQUERY #{target} :#{}") end |
#stats(*params) ⇒ Object
Send STATS command
231 232 233 |
# File 'lib/irc-socket.rb', line 231 def stats(*params) write_optional("STATS", params) end |
#time(target = nil) ⇒ Object
Send TIME command
236 237 238 |
# File 'lib/irc-socket.rb', line 236 def time(target=nil) write_optional("TIME", target) end |
#topic(channel, topic = nil) ⇒ Object
Send TOPIC command
186 187 188 |
# File 'lib/irc-socket.rb', line 186 def topic(channel, topic=nil) raw("TOPIC", channel, topic) end |
#user(user, mode, unused, realname) ⇒ Object
Send USER command
155 156 157 |
# File 'lib/irc-socket.rb', line 155 def user(user, mode, unused, realname) write("USER #{user} #{mode} #{unused} :#{realname}") end |
#userhost(*users) ⇒ Object
Send USERHOST command
291 292 293 |
# File 'lib/irc-socket.rb', line 291 def userhost(*users) write("USERHOST #{users.join(' ')}") end |
#users(target = nil) ⇒ Object
Send USERS command
286 287 288 |
# File 'lib/irc-socket.rb', line 286 def users(target=nil) write_optional("USERS", target) end |
#version(target = nil) ⇒ Object
Send VERSION command
226 227 228 |
# File 'lib/irc-socket.rb', line 226 def version(target=nil) write_optional("VERSION", target) end |
#who(*params) ⇒ Object
Send WHO command
251 252 253 |
# File 'lib/irc-socket.rb', line 251 def who(*params) write_optional("WHO", params) end |
#whois(*params) ⇒ Object
Send WHOIS command
256 257 258 |
# File 'lib/irc-socket.rb', line 256 def whois(*params) write_optional("WHOIS", params) end |
#whowas(*params) ⇒ Object
Send WHOWAS command
261 262 263 |
# File 'lib/irc-socket.rb', line 261 def whowas(*params) write_optional("WHOWAS", params) end |
#write(data) ⇒ Object
Write to our Socket and append CRLF
125 126 127 128 129 |
# File 'lib/irc-socket.rb', line 125 def write(data) @socket.write(data + "\r\n") rescue IOError raise end |