Class: IRC::Connection
- Inherits:
-
Object
- Object
- IRC::Connection
- Defined in:
- lib/irc/connection.rb
Instance Method Summary collapse
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #disconnected? ⇒ Boolean
-
#initialize(host = nil, port = 6667) ⇒ Connection
constructor
A new instance of Connection.
- #join(*channels) ⇒ Object
- #listen ⇒ Object
- #mutex ⇒ Object
- #nick(_nick, realname = nil) ⇒ Object
- #part(*channels) ⇒ Object
- #pong(value) ⇒ Object
- #privmsg(content, *recipients) ⇒ Object
- #quit(message = nil) ⇒ Object
- #socket ⇒ Object
- #write(*strings) ⇒ Object
Constructor Details
#initialize(host = nil, port = 6667) ⇒ Connection
Returns a new instance of Connection.
6 7 8 9 |
# File 'lib/irc/connection.rb', line 6 def initialize host = nil, port = 6667 @host = host @port = port end |
Instance Method Details
#connected? ⇒ Boolean
19 20 21 |
# File 'lib/irc/connection.rb', line 19 def connected? !disconnected? end |
#disconnect ⇒ Object
68 69 70 71 |
# File 'lib/irc/connection.rb', line 68 def disconnect quit socket.close end |
#disconnected? ⇒ Boolean
15 16 17 |
# File 'lib/irc/connection.rb', line 15 def disconnected? !@socket || @socket.closed? end |
#join(*channels) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/irc/connection.rb', line 32 def join *channels channels.flatten.each do |channel| @channel = channel write "JOIN #{channel}" end end |
#listen ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/irc/connection.rb', line 78 def listen unless @listening loop do @listening = true socket.lines "\r\n" do |line| = Message.new(line, self) Callback.handle end @listening = false end end end |
#mutex ⇒ Object
11 12 13 |
# File 'lib/irc/connection.rb', line 11 def mutex @mutex ||= Mutex.new end |
#nick(_nick, realname = nil) ⇒ Object
45 46 47 48 49 |
# File 'lib/irc/connection.rb', line 45 def nick _nick, realname = nil realname ||= _nick write "NICK #{_nick}" write "USER #{_nick} localhost #{@host} :#{realname}" end |
#part(*channels) ⇒ Object
39 40 41 42 43 |
# File 'lib/irc/connection.rb', line 39 def part *channels channels.flatten.each do |channel| write "PART #{channel}" end end |
#pong(value) ⇒ Object
64 65 66 |
# File 'lib/irc/connection.rb', line 64 def pong value write "PONG #{value}" end |
#privmsg(content, *recipients) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/irc/connection.rb', line 51 def privmsg content, *recipients recipients = @channel unless recipients.any? recipients = [recipients].flatten.join(',') msg = "PRIVMSG #{recipients} :#{content}" write msg.slice!(0,500) privmsg msg, recipients if msg != '' end |
#quit(message = nil) ⇒ Object
60 61 62 |
# File 'lib/irc/connection.rb', line 60 def quit = nil write "QUIT :#{message}" end |
#socket ⇒ Object
73 74 75 |
# File 'lib/irc/connection.rb', line 73 def socket @socket ||= TCPSocket.open(@host, @port) end |
#write(*strings) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/irc/connection.rb', line 23 def write *strings # mutex do strings.each do |string| puts '-> ' + string socket.print string + "\r\n" end # end end |