Class: Sponge::IRC::Client
- Inherits:
-
Object
- Object
- Sponge::IRC::Client
- Defined in:
- lib/sponge/irc/client.rb
Overview
Author
-
Lee Jarvis - [email protected]
Description
TODO
Usage
Minimal Example
client = Sponge::IRC::Client.new do
server "irc.freenode.org"
nickname "MyNick"
end
client.run
Mapping a Listener
client = Sponge::IRC::Client.new do
server "irc.freenode.org"
nickname "MyNick"
realname "Mr Sponge Bot"
end
# Join a channel once we've seen end of MOTD
client.on(376) do |irc, |
irc.join "#mychan"
end
client.on(:JOIN) do |irc, |
.reply "Hi #{.nick}. Welcome to #{.channel}!"
end
client.run
Direct Known Subclasses
Defined Under Namespace
Classes: OptionsDSL
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Client configuration options.
-
#irc ⇒ Object
readonly
The current IRC::Socket this Client is using.
-
#listeners ⇒ Object
readonly
Our Listeners Object.
Instance Method Summary collapse
-
#connect ⇒ Object
Start our connection.
-
#initialize(server = nil, opts = {}, &blk) ⇒ Client
constructor
A new instance of Client.
-
#on(*commands, &blk) ⇒ Object
Map listeners to an IRC command.
-
#process(data) ⇒ Object
Sugar for IRC::Parser#parse.
-
#quit ⇒ Object
Quit our IRC socket and exit the Client.
-
#run ⇒ Object
Reads the next line from the IRC server whilst we’re connected.
-
#set_nick(new) ⇒ Object
Change clients nickname.
Constructor Details
#initialize(server = nil, opts = {}, &blk) ⇒ Client
Returns a new instance of Client.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sponge/irc/client.rb', line 47 def initialize(server=nil, opts={}, &blk) config = opts.merge(OptionsDSL.new(server, &blk).) config = OpenStruct.new(opts.merge(config)) @config = config config.port ||= 6667 raise(ArgumentError, "No server supplied") unless config.server @irc = IRC::Socket.new(config.server, config.port) @irc.client = self @parser = IRC::Parser.new(self) @listeners = IRC::Listeners.new(self) setup_username end |
Instance Attribute Details
#config ⇒ Object (readonly)
Client configuration options
42 43 44 |
# File 'lib/sponge/irc/client.rb', line 42 def config @config end |
#irc ⇒ Object (readonly)
The current IRC::Socket this Client is using
39 40 41 |
# File 'lib/sponge/irc/client.rb', line 39 def irc @irc end |
#listeners ⇒ Object (readonly)
Our Listeners Object
45 46 47 |
# File 'lib/sponge/irc/client.rb', line 45 def listeners @listeners end |
Instance Method Details
#connect ⇒ Object
Start our connection
64 65 66 67 68 69 70 71 72 |
# File 'lib/sponge/irc/client.rb', line 64 def connect puts "Connecting to #{irc.server} on #{irc.port}" irc.connect unless irc.connected? irc.nick(config.nickname) irc.user(config.username, config.hostname, "*", config.realname) puts "Connected" if irc.connected? end |
#on(*commands, &blk) ⇒ Object
Map listeners to an IRC command. For example:
client.on("376") do |irc, |
irc.join "#mychan"
end
client.on(:JOIN) do |irc, |
.reply "Hi, #{.nick}. Welcome to #{.channel}"
end
-
irc
- The Clients Sponge::IRC::Socket -
message
- The current Sponge::IRC::Message
86 87 88 |
# File 'lib/sponge/irc/client.rb', line 86 def on(*commands, &blk) @listeners.add(*commands, &blk) end |
#process(data) ⇒ Object
Sugar for IRC::Parser#parse
97 98 99 100 |
# File 'lib/sponge/irc/client.rb', line 97 def process(data) = @parser.parse(data) @listeners.handle() if end |
#quit ⇒ Object
Quit our IRC socket and exit the Client
110 111 112 113 |
# File 'lib/sponge/irc/client.rb', line 110 def quit @irc.quit exit end |
#run ⇒ Object
Reads the next line from the IRC server whilst we’re connected. Sends the line to #process
104 105 106 107 |
# File 'lib/sponge/irc/client.rb', line 104 def run connect unless irc.connected? process(irc.read) while irc.connected? end |
#set_nick(new) ⇒ Object
Change clients nickname
91 92 93 94 |
# File 'lib/sponge/irc/client.rb', line 91 def set_nick(new) config.nickname = new @irc.nick(new) end |