Class: DICT

Inherits:
Object show all
Defined in:
lib/dict.rb

Constant Summary collapse

DATABASES_PRESENT =
'110'
STRATEGIES_AVAILABLE =
'111'
DATABASE_INFORMATION =
'112'
HELP_TEXT =
'113'
SERVER_INFORMATION =
'114'
CHALLENGE_FOLLOWS =
'130'
DEFINITIONS_RETRIEVED =
'150'
WORD_DEFINITION =
'151'
MATCHES_PRESENT =
'152'
STATUS_RESPONSE =
'210'
CONNECTION_ESTABLISHED =
'220'
CLOSING_CONNECTION =
'221'
AUTHENTICATION_SUCCESSFUL =
'230'
OK =
'250'
SEND_RESPONSE =
'330'
TEMPORARILY_UNAVAILABLE =
'420'
SHUTTING_DOWN =
'421'
UNRECOGNISED_COMMAND =
'500'
ILLEGAL_PARAMETERS =
'501'
COMMAND_NOT_IMPLEMENTED =
'502'
PARAMETER_NOT_IMPLEMENTED =
'503'
ACCESS_DENIED =
'530'
AUTH_DENIED =
'531'
UNKNOWN_MECHANISM =
'532'
INVALID_DATABASE =
'550'
INVALID_STRATEGY =
'551'
NO_MATCH =
'552'
NO_DATABASES_PRESENT =
'554'
NO_STRATEGIES_AVAILABLE =
'555'
ALL_DATABASES =
'*'
DEFAULT_MATCH_STRATEGY =
'.'
DEFAULT_PORT =
2628
ERROR =
/^[45]/
FIRST_DATABASE =
'!'
MAX_LINE_LENGTH =
1024
PAIR =
/^(\S+)\s"(.+)"\r$/
REPLY_CODE =
/^\d\d\d/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hosts, port = DEFAULT_PORT, debug = false, verbose = false) ⇒ DICT

Returns a new instance of DICT.

Raises:



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/dict.rb', line 242

def initialize(hosts, port = DEFAULT_PORT, debug = false, verbose = false)
  hosts.each do |host|
    @debug = debug
    @verbose = verbose
    printf("Attempting to connect to %s:%d...\n", host, port) if @verbose

    begin
	@sock = TCPSocket.open(host, port)
    rescue
	next	# cycle through list of servers, if more than one
    end

    break	# continue if connection to this host succeeded
  end

  # catch failure
  raise ConnectError, 'Unable to connect to host' unless defined? @sock

  # check status line on connect
  line = get_line
  raise ConnectError, line if line =~ ERROR

  caps, @msgid = /<(.+?)>\s(<.+>)/.match(line)[1..2]
  @capabilities = caps.split(/\./)
  if @verbose
    printf("Capabilities: %s\n", @capabilities.join(', '))
    printf("Msgid: %s\n", @msgid)
  end
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



201
202
203
# File 'lib/dict.rb', line 201

def capabilities
  @capabilities
end

#codeObject (readonly)

Returns the value of attribute code.



201
202
203
# File 'lib/dict.rb', line 201

def code
  @code
end

#messageObject (readonly)

Returns the value of attribute message.



201
202
203
# File 'lib/dict.rb', line 201

def message
  @message
end

#msgidObject (readonly)

Returns the value of attribute msgid.



201
202
203
# File 'lib/dict.rb', line 201

def msgid
  @msgid
end

Instance Method Details

#auth(user, secret) ⇒ Object

AUTHorise user



433
434
435
436
# File 'lib/dict.rb', line 433

def auth(user, secret)
  auth = MD5::new(@msgid + secret).hexdigest
  exec_cmd('AUTH %s %s' % [ user, auth ])
end

#client(info) ⇒ Object

send CLIENT information



427
428
429
# File 'lib/dict.rb', line 427

def client(info)
  exec_cmd('CLIENT %s' % info)
end

#define(db, word) ⇒ Object

DEFINE a word



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/dict.rb', line 347

def define(db, word)
  definitions = Array.new
  d = Definition.new
  d.word = word
  d.definition = Array.new

  exec_cmd('DEFINE %s "%s"' % [ db, word ])

  return nil if @code =~ ERROR

  while line = get_line
    return definitions if line =~ /^#{OK}/

    if line =~ /^#{WORD_DEFINITION}/
	word, d.database, d.description =
 /^\d\d\d\s"(.+?)"\s(\S+)\s"(.+)"\r$/.match(line)[1..3]
    elsif end_of_text? line	# finish definition and start a new one
	definitions << d
	d = Definition.new
	d.word = word
	d.definition = Array.new
    else
	line.undot!
	d.definition << line
    end

  end
end

#disconnectObject

QUIT from the server



340
341
342
343
# File 'lib/dict.rb', line 340

def disconnect
  exec_cmd('QUIT')
  @sock.close
end

#helpObject

request server-side HELP



420
421
422
423
# File 'lib/dict.rb', line 420

def help
  exec_cmd('HELP')
  parse_response
end

#match(db, strategy, word) ⇒ Object

MATCH a word



378
379
380
381
# File 'lib/dict.rb', line 378

def match(db, strategy, word)
  exec_cmd('MATCH %s %s "%s"' % [ db, strategy, word ])
  parse_response
end

#show_dbObject

get database list



385
386
387
388
# File 'lib/dict.rb', line 385

def show_db
  exec_cmd("SHOW DB")
  parse_response
end

#show_info(db) ⇒ Object

get information on database



399
400
401
402
# File 'lib/dict.rb', line 399

def show_info(db)
  exec_cmd('SHOW INFO %s' % db)
  parse_response
end

#show_serverObject

get server information



406
407
408
409
# File 'lib/dict.rb', line 406

def show_server
  exec_cmd("SHOW SERVER")
  parse_response
end

#show_stratObject

get strategy list



392
393
394
395
# File 'lib/dict.rb', line 392

def show_strat
  exec_cmd("SHOW STRAT")
  parse_response
end

#statusObject

request server STATUS information



413
414
415
416
# File 'lib/dict.rb', line 413

def status
  exec_cmd('STATUS')
  @message
end