Class: Sponge::IRC::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/sponge/irc/socket.rb

Overview

Author

Description

Sponge::IRC::Socket is a simple wrapper around TCPSocket. It provides simple functionality to interact with IRC servers.

Note that when creating a new IRC::Socket, I generally assign the instance to a variable named irc throughout the documentation.

Synopsis

irc = Sponge::IRC::Socket.open('irc.2600.net')
irc.nick "SpongeBot"
irc.user "SpongeBot", "sponge", "sponge", "Sponge IRC bot"
irc.join "#mychan"
irc.privmsg "#mychan", "Hello!"
irc.part "#mychan"
irc.quit "Quitting.."

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, port = 6667) ⇒ Socket

Creates a new IRC::Socket. This doesn’t automatically connect. If you want to do that use IRC::Socket.open



43
44
45
46
47
48
49
# File 'lib/sponge/irc/socket.rb', line 43

def initialize(server, port=6667)
  @server = server
  @port = port
  
  @client = nil
  @connected = false
end

Instance Attribute Details

#clientObject

The Client using this Socket (if one at all)



26
27
28
# File 'lib/sponge/irc/socket.rb', line 26

def client
  @client
end

#portObject (readonly)

The port our socket is connected on



32
33
34
# File 'lib/sponge/irc/socket.rb', line 32

def port
  @port
end

#serverObject (readonly)

The server address we’re connected to



29
30
31
# File 'lib/sponge/irc/socket.rb', line 29

def server
  @server
end

Class Method Details

.open(server, port = 6667) ⇒ Object

Create a new IRC::Socket and connect to it



35
36
37
38
39
# File 'lib/sponge/irc/socket.rb', line 35

def self.open(server, port=6667)
  sock = new(server, port)
  sock.connect
  sock
end

Instance Method Details

#away(message = nil) ⇒ Object

Set ourselves as being away, (or not-away if a message is not given)



152
153
154
# File 'lib/sponge/irc/socket.rb', line 152

def away(message=nil)
  write("AWAY#{message ? ':'+message : ''}")
end

#closeObject

Close our socket



101
102
103
104
# File 'lib/sponge/irc/socket.rb', line 101

def close
  @socket.close
rescue IOError
end

#connectObject

Connect to our IRC server if we haven’t already connected



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sponge/irc/socket.rb', line 52

def connect
  return if connected?
  
  @socket = TCPSocket.new(server, port)
rescue Interrupt
  raise
rescue Exception
  raise
else
  @connected = true
end

#connected?Boolean

Check if our Socket is currently connected to a server

Returns:

  • (Boolean)


65
66
67
# File 'lib/sponge/irc/socket.rb', line 65

def connected?
  @connected
end

#join(channel, password = nil) ⇒ Object

Join a channel, using a password is necessary



127
128
129
# File 'lib/sponge/irc/socket.rb', line 127

def join(channel, password=nil)
  write("JOIN #{channel}#{password ? password : ''}")
end

#kick(channel, nick, reason = "") ⇒ Object

Kick nick from channel



142
143
144
# File 'lib/sponge/irc/socket.rb', line 142

def kick(channel, nick, reason="")
  write("KICK #{channel} #{nick} :#{reason}")
end

#nick(nick) ⇒ Object

Change our nickname



112
113
114
# File 'lib/sponge/irc/socket.rb', line 112

def nick(nick)
  write("NICK #{nick}")
end

#notice(recipient, message) ⇒ Object

Send a NOTICE to a specific user or channel



122
123
124
# File 'lib/sponge/irc/socket.rb', line 122

def notice(recipient, message)
  write("NOTICE #{recipient} :#{message}")
end

#part(*channels) ⇒ Object

Part a channel(s)



132
133
134
# File 'lib/sponge/irc/socket.rb', line 132

def part(*channels)
  write("PART #{channels.join(',')}")
end

#pong(server) ⇒ Object

Send a PONG command



147
148
149
# File 'lib/sponge/irc/socket.rb', line 147

def pong(server)
  write("PONG #{server}")
end

#privmsg(recipient, message) ⇒ Object

Send a PRIVMSG to a specific user or channel



117
118
119
# File 'lib/sponge/irc/socket.rb', line 117

def privmsg(recipient, message)
  write("PRIVMSG #{recipient} :#{message}")
end

#quit(reason = "Cya!") ⇒ Object

Send the QUIT command to our IRC server This also closes our IRC::Socket



158
159
160
161
# File 'lib/sponge/irc/socket.rb', line 158

def quit(reason="Cya!")
  write("QUIT :#{reason}")
  close
end

#readObject

Read the next line from the server. Chomps the trailing CR-LF Returns nil if there are issues with reading from the Socket. This method also safely shuts down our IRC::Socket by sending the QUIT command before exiting, it then closes the socket and exits the program.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sponge/irc/socket.rb', line 81

def read
  if data = @socket.gets
    data.chomp("\r\n")
  else
    @connected = false
    nil
  end
rescue Interrupt
  puts "\nInterrupted. Shutting down safely."
  if connected?
    quit("Client Interrupted")
    close
    exit 1
  end
rescue IOError
  @connected = false
  nil
end

#topic(channel, topic) ⇒ Object

Set the topic on a given channel



137
138
139
# File 'lib/sponge/irc/socket.rb', line 137

def topic(channel, topic)
  write("TOPIC #{channel} :#{topic}")
end

#user(user, host, server, real) ⇒ Object

Send the USER command, usually to log into the server



107
108
109
# File 'lib/sponge/irc/socket.rb', line 107

def user(user, host, server, real)
  write("USER #{user} #{host} #{server} :#{real}")
end

#write(data) ⇒ Object

Send a message to our server, appending CR-LF



70
71
72
73
74
# File 'lib/sponge/irc/socket.rb', line 70

def write(data)
  @socket.print(data + "\r\n")
rescue IOError
  raise
end