Class: ShoutBot

Inherits:
Object
  • Object
show all
Defined in:
lib/shout-bot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, port, nick, password, ssl) {|_self| ... } ⇒ ShoutBot

Returns a new instance of ShoutBot.

Yields:

  • (_self)

Yield Parameters:

  • _self (ShoutBot)

    the object that the method was called on

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/shout-bot.rb', line 57

def initialize(server, port, nick, password, ssl)
  raise ArgumentError unless block_given?

  tcp_socket = TCPSocket.new(server, port || 6667)
  if ssl
    ssl_context = OpenSSL::SSL::SSLContext.new
    ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
    @socket.sync = true
    @socket.connect
  else
    @socket = tcp_socket
  end
  sendln "PASS #{password}" if password
  sendln "NICK #{nick}"
  sendln "USER #{nick} 0 * :#{nick}"
  sleep 1
  yield self
  sendln "QUIT :quit"
  @socket.gets until @socket.eof?
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



49
50
51
# File 'lib/shout-bot.rb', line 49

def channel
  @channel
end

Class Method Details

.shout(uri, password = nil, ssl = false, &block) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shout-bot.rb', line 35

def self.shout(uri, password = nil, ssl = false, &block)
  raise ArgumentError unless block_given?

  uri = Addressable::URI.parse(uri)
  irc = new(uri.host, uri.port, uri.user, uri.password, ssl) do |irc|
    if channel = uri.fragment
      irc.join(channel, password, &block)
    else
      irc.channel = uri.path[1..-1]
      yield irc
    end
  end
end

Instance Method Details

#join(channel, password) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (ShoutBot)

    the object that the method was called on

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
# File 'lib/shout-bot.rb', line 79

def join(channel, password)
  raise ArgumentError unless block_given?

  @channel = "##{channel}"
  password = password && " #{password}" || ""
  sendln "JOIN #{@channel}#{password}"
  yield self
  sendln "PART #{@channel}"
end

#say(message) ⇒ Object



89
90
91
92
# File 'lib/shout-bot.rb', line 89

def say(message)
  return unless @channel
  sendln "PRIVMSG #{@channel} :#{message}"
end

#sendln(cmd) ⇒ Object



51
52
53
54
55
# File 'lib/shout-bot.rb', line 51

def sendln(cmd)
  puts "Send: #{cmd}"
  @socket.write("#{cmd}\r\n")
  STDOUT.flush
end