Class: Sponge::IRC::Client

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

Overview

Author

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, message|
  irc.join "#mychan"
end

client.on(:JOIN) do |irc, message|
  message.reply "Hi #{message.nick}. Welcome to #{message.channel}!"
end

client.run

Direct Known Subclasses

Bot

Defined Under Namespace

Classes: OptionsDSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = nil, opts = {}, &blk) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


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).options)
  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

#configObject (readonly)

Client configuration options



42
43
44
# File 'lib/sponge/irc/client.rb', line 42

def config
  @config
end

#ircObject (readonly)

The current IRC::Socket this Client is using



39
40
41
# File 'lib/sponge/irc/client.rb', line 39

def irc
  @irc
end

#listenersObject (readonly)

Our Listeners Object



45
46
47
# File 'lib/sponge/irc/client.rb', line 45

def listeners
  @listeners
end

Instance Method Details

#connectObject

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, message|
  irc.join "#mychan"
end

client.on(:JOIN) do |irc, message|
  message.reply "Hi, #{message.nick}. Welcome to #{message.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)
  message = @parser.parse(data)
  @listeners.handle(message) if message
end

#quitObject

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

#runObject

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