Class: Smirc::Client

Inherits:
Object
  • Object
show all
Includes:
Eventful
Defined in:
lib/smirc/client.rb

Overview

A Simple Irc Client Events:

<tt>:connected</tt>
  Fires when a connection has successfully been made

<tt>:message</tt> |irc, channel, message, user|
  Fires when a PRIVMSG is sent from the server

<tt>:log</tt> |irc, message|
  Fires when message is logged by the client

TODO:

* Implement Queueing (based on Isaac gem)
* Add :error event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ Client

Configuration options

  • :server

  • :nickname

  • :realname

  • :port

  • :password



27
28
29
30
31
32
33
# File 'lib/smirc/client.rb', line 27

def initialize(configuration={})
  @configuration = Mash.new(configuration)
  @queue = Smirc::Queue.new

  @queue.on(:lock) { send("PING :#{@configuration.server}\r\n") }
  @queue.on(:send) { |q, msg| send(msg) }
end

Instance Attribute Details

#queueObject

Returns the value of attribute queue.



19
20
21
# File 'lib/smirc/client.rb', line 19

def queue
  @queue
end

Instance Method Details

#configObject



53
54
55
# File 'lib/smirc/client.rb', line 53

def config
  return @configuration
end

#connectObject

Attempt to connect to the server. Will fire :connect if successful



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/smirc/client.rb', line 36

def connect
  @registrations = []
  @registered = false

  server  = config.server || 'localhost'
  port    = config.port || 6667

  log("Connecting to #{server} #{port}")
  @socket = TCPSocket.new(server, port)

  send("PASS #{config.password}\r\n") if config.password
  send("NICK #{config.nickname}\r\n")
  send("USER #{config.nickname} 0 * :#{config.realname}\r\n")

  listen
end

#disconnectObject

Disconnect from the server



58
59
60
61
62
# File 'lib/smirc/client.rb', line 58

def disconnect
  send("QUIT\r\n")
  log "Disconnecting"
  @socket.close
end

#join(channel) ⇒ Object

Join the channel



71
72
73
# File 'lib/smirc/client.rb', line 71

def join (channel)
  send ("JOIN #{channel}\r\n")
end

#message(channel, text) ⇒ Object

Send a message to channel with text



66
67
68
# File 'lib/smirc/client.rb', line 66

def message(channel, text)
  @queue << "PRIVMSG #{channel} :#{text}\r\n"
end

#parse(line) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/smirc/client.rb', line 76

def parse(line)
  log "<- #{line}"
  case line.chomp
  when /(^:\S+ )?00([1-4])/
    @registrations << $2.to_i
  when /(^:(\S+)!\S+ )?PRIVMSG \S+ :?\001VERSION\001/
    send "NOTICE #{$2} :\001VERSION ruby.irk\001"
  when /^PING (\S+)/
    send "PONG #{$1}\r\n"
    @queue.unlock
  when /(^:\S+)?PONG/
    @queue.unlock
         
  when /(^:(\S+)!(\S+) )?PRIVMSG (\S+) :?(.*)/
    env = Mash.new({ :nick => $2, :userhost => $3, :channel => $4, :message => $5 })
    type = env[:channel].match(/^#/) ? :channel : :private
    fire(:message, env.channel, env.message, env.nick)
  when /(^:\S+ )?([4-5]\d\d) \S+ (\S+)/
    env = {:error => $2.to_i, :message => $2, :nick => $3, :channel => $3}
    fire(:error, env)
  end
end