Sha256: d59ffe95d931d80ee46eb0346b20e56e8e4feb319d152389285c215070e3228e

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require "addressable/uri"
require "socket"
require "openssl"

class CarrierPigeon

  def initialize(options={})
    [:host, :port, :nick, :channel].each do |option|
      raise "You must provide an IRC #{option}" unless options.has_key?(option)
    end
    tcp_socket = TCPSocket.new(options[:host], options[:port])
    if options[: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.sync_close = true
      @socket.connect
    else
      @socket = tcp_socket
    end
    sendln "PASS #{options[:password]}" if options[:password]
    sendln "NICK #{options[:nick]}"
    sendln "USER #{options[:nick]} 0 * :#{options[:nick]}"
    sendln options[:nickserv_command] if options[:nickserv_command]
    sendln "JOIN #{options[:channel]} #{options[:channel_password]}" if options[:join]
  end

  def message(channel, message)
    sendln "PRIVMSG #{channel} :#{message}"
  end

  def die
    sendln "QUIT :quit"
    @socket.gets until @socket.eof?
    @socket.close
  end

  def self.send(options={})
    raise "You must supply a valid IRC URI" unless options[:uri]
    raise "You must supply a message" unless options[:message]
    uri = Addressable::URI.parse(options[:uri])
    options[:host] = uri.host
    options[:port] = uri.port || 6667
    options[:nick] = uri.user
    options[:password] = uri.password
    options[:channel] = "#" + uri.fragment
    if options[:nickserv_password]
      options[:nickserv_command] ||=
        "PRIVMSG NICKSERV :IDENTIFY #{options[:nickserv_password]}"
    end
    pigeon = new(options)
    pigeon.message(options[:channel], options[:message])
    pigeon.die
  end

  private

  def sendln(cmd)
    @socket.puts(cmd)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carrier-pigeon-0.4.0 lib/carrier-pigeon.rb