Class: Serverspec::Type::Bitlbee

Inherits:
Base
  • Object
show all
Defined in:
lib/serverspec_extended_types/bitlbee.rb

Instance Method Summary collapse

Constructor Details

#initialize(port, nick, password, use_ssl = false) ⇒ Bitlbee

Bitlbee constructor

Connects to Bitlbee on the host specified by ENV

Examples:

Constructor

describe bitlbee(6697, 'nick', 'password') do
  # tests here
end

Parameters:

  • port (Integer)

    the port to connect to

  • nick (String)

    the nick to connect as

  • password (String)

    the password for nick

  • use_ssl (Boolean) (defaults to: false)

    whether to connect with SSL



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/serverspec_extended_types/bitlbee.rb', line 43

def initialize(port, nick, password, use_ssl=false)
  @port = port
  @host = ENV['TARGET_HOST']
  @nick = nick
  @use_ssl = use_ssl
  @password = password
  @connected_status = false
  @version_str = ""
  @timed_out_status = false
  @started = false
end

Instance Method Details

#communicatenil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Login to IRC and get version, over @socket

Returns:

  • (nil)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/serverspec_extended_types/bitlbee.rb', line 107

def communicate
  password = @password
  nick = @nick
  @socket.puts("PASS #{password}\n")
  @socket.puts("NICK #{nick}\n")
  @socket.puts("USER #{nick} #{nick} servername :TestUser\n")
  while buf = (@socket.readpartial(1024) rescue nil )
    @connected_status = true
    (data||="") << buf
    if data =~ /Welcome to the/
      @socket.puts("MODE #{nick} +i\n")
      data = ""
    elsif data =~ /If you've never/
      @socket.puts("PRIVMSG &bitlbee :identify #{password}\n")
      data = ""
    elsif data =~ /PING (\S+)/
      @socket.puts(":#{$1} PONG #{$1} :#{$1}\n")
      data = ""
    elsif data =~ /MODE #{nick} :\+i/
      break
    end
  end
  @socket.puts("PRIVMSG root :\001VERSION\001\n")
  while buf = (@socket.readpartial(1024) rescue nil )
    (data||="") << buf
    if data =~ /VERSION (.+)/
      @version_str = $1
      break
    end
  end
end

#connectnil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

connection to the IRC server (without SSL)

Returns:

  • (nil)


79
80
81
82
83
84
# File 'lib/serverspec_extended_types/bitlbee.rb', line 79

def connect
  @socket = TCPSocket.open(@host, @port)
  communicate
  @socket.puts("QUIT :\"outta here\"\n")
  @socket.close
end

#connect_sslnil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Open SSL connection to the IRC server

Returns:

  • (nil)


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/serverspec_extended_types/bitlbee.rb', line 90

def connect_ssl
  sock = TCPSocket.open(@host, @port)
  ctx = OpenSSL::SSL::SSLContext.new
  ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)
  @socket = OpenSSL::SSL::SSLSocket.new(sock, ctx).tap do |socket|
    socket.sync_close = true
    socket.connect
  end
  communicate
  @socket.puts("QUIT :\"outta here\"\n")
  @socket.close
end

#connectable?Boolean

Check whether we can successfully connect

Examples:

serverspec test

describe bitlbee(6697, 'myuser', 'mypass') do
  it { should be_connectable }
end

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/serverspec_extended_types/bitlbee.rb', line 162

def connectable?
  start if not @started
  @connected_status
end

#startnil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Begin timeout-wrapped connection

This just calls #connect within a 10-second Timeout::timeout wrapper, and handles both timeout and Errno::ECONNREFUSED.

Returns:

  • (nil)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/serverspec_extended_types/bitlbee.rb', line 62

def start
  @started = true
  begin
    Timeout::timeout(10) do
      @use_ssl ? ( connect_ssl ) : ( connect )
    end
  rescue Timeout::Error
    @timed_out_status = true
  rescue Errno::ECONNREFUSED
    @connected_status = false
  end
end

#timed_out?Boolean

Check whether the connection timed out

Examples:

serverspec test

describe bitlbee(6697, 'myuser', 'mypass') do
  it { should_not be_timed_out }
end

Returns:

  • (Boolean)


148
149
150
151
# File 'lib/serverspec_extended_types/bitlbee.rb', line 148

def timed_out?
  start if not @started
  @timed_out_status
end

#versionString

Return the version string from Bitlbee

Examples:

describe bitlbee(6697, 'myuser', 'mypass') do
  its(:version) { should match /foo/ }
end

Returns:

  • (String)


176
177
178
179
# File 'lib/serverspec_extended_types/bitlbee.rb', line 176

def version
  start if not @started
  @version_str
end