Class: CarriedPigeon

Inherits:
Object
  • Object
show all
Defined in:
lib/collins_notify/adapter/helper/carried-pigeon.rb

Overview

Code is largely ripped off from github.com/portertech/carrier-pigeon which carriers an MIT license. Carrier pigeon offers no ability to detect a 433 response (nickname already in use) and automatically fix it. There’s also not much in the way of debugging. That’s all that is added here.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CarriedPigeon

Returns a new instance of CarriedPigeon.



29
30
31
32
33
34
35
36
# File 'lib/collins_notify/adapter/helper/carried-pigeon.rb', line 29

def initialize(options={})
  [:host, :port, :nick, :channel, :logger].each do |option|
    raise "You must provide an IRC #{option}" unless options.has_key?(option)
  end
  @logger = options[:logger]
  @options = options
  connect! if (options.fetch(:connect, true)) # default to connect, same as old behavior
end

Instance Attribute Details

#connectedObject (readonly)

Returns the value of attribute connected.



28
29
30
# File 'lib/collins_notify/adapter/helper/carried-pigeon.rb', line 28

def connected
  @connected
end

#loggerObject (readonly)

Returns the value of attribute logger.



28
29
30
# File 'lib/collins_notify/adapter/helper/carried-pigeon.rb', line 28

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/collins_notify/adapter/helper/carried-pigeon.rb', line 28

def options
  @options
end

Class Method Details

.send(options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/collins_notify/adapter/helper/carried-pigeon.rb', line 11

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

Instance Method Details

#authObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/collins_notify/adapter/helper/carried-pigeon.rb', line 38

def auth
  # Only auth if not connected and password is specified
  if connected? or !password then
    return false
  end
  # Must be first according to RFC 2812
  sendln "PASS #{password}", :log
  sendln "USER #{nick} 0 * :#{real_name}", :log
  true
end

#connect!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/collins_notify/adapter/helper/carried-pigeon.rb', line 49

def connect!
  return false if connected?
  tcp_socket = TCPSocket.new(host, port)
  if ssl? then
    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
  if auth? and not auth then
    die
    raise "Could not authenticate"
  end
  set_nick!
  @connected = true
  issue_nickserv_command
  register
end

#dieObject



84
85
86
87
88
89
90
91
92
93
# File 'lib/collins_notify/adapter/helper/carried-pigeon.rb', line 84

def die
  begin
    sendln "QUIT :quit", :log
    socket.gets until socket.eof?
  rescue Exception => e
    logger.error "Error quitting IRC - #{e}"
  ensure
    socket.close
  end
end

#message(message, notice = false, opts = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/collins_notify/adapter/helper/carried-pigeon.rb', line 72

def message message, notice = false, opts = {}
  command = notice ? "NOTICE" : "PRIVMSG"
  # Reset join/channel/channel_password options if specified
  options[:join] = opts[:join] if opts.key?(:join)
  options[:channel] = opts[:channel] if opts.key?(:channel)
  if opts.key?(:channel) then
    options[:channel_password] = opts[:channel_password]
  end
  join!
  sendln "#{command} #{channel} :#{message}", :log
end