Module: Lich::GameBase::XMLCleaner

Defined in:
documented/games.rb

Overview

XML string cleaner module

Class Method Summary collapse

Class Method Details

.clean_nested_quotes(server_string) ⇒ String

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.

Escapes nested single and double quotes inside XML attribute values.

Processes both patterns: single quotes inside single-quoted attributes and double quotes inside double-quoted attributes. Logs detected malformations and repairs.

Parameters:

  • server_string (String) —

    the XML string to repair, modified in place

Returns:

  • (String) —

    the repaired server string



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'documented/games.rb', line 304

def clean_nested_quotes(server_string)
  # Fix nested single quotes
  unless (matches = server_string.scan(/'([^=>]*'[^=>]*)'/)).empty?
    Lich.log "Invalid nested single quotes XML tags detected: #{server_string.inspect}"
    matches.flatten.each do |match|
      server_string.gsub!(match, match.gsub(/'/, '''))
    end
    Lich.log "Invalid nested single quotes XML tags fixed to: #{server_string.inspect}"
  end

  # Fix nested double quotes
  unless (matches = server_string.scan(/"([^=>]*"[^=>]*)"/)).empty?
    Lich.log "Invalid nested double quotes XML tags detected: #{server_string.inspect}"
    matches.flatten.each do |match|
      server_string.gsub!(match, match.gsub(/"/, '"'))
    end
    Lich.log "Invalid nested double quotes XML tags fixed to: #{server_string.inspect}"
  end

  server_string
end

.fix_invalid_characters(server_string) ⇒ String

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.

Removes invalid control characters (e.g., bell character \a) from the stream.

The Ox parser tolerates bare ampersands and does not need character entity escaping, but still rejects the bell character. Logs any removals.

Parameters:

  • server_string (String) —

    the server string to clean, modified in place

Returns:

  • (String) —

    the cleaned server string



334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'documented/games.rb', line 334

def fix_invalid_characters(server_string)
  # Note: a bare '&' is intentionally not escaped here. REXML raised on it
  # (hence the old escaping); Ox tolerates it (convert_special: false emits
  # it verbatim with no error), so escaping is no longer needed for parsing.

  # Fix bell character
  if server_string.include?("\a")
    Lich.log "Invalid \\a detected: #{server_string.inspect}"
    server_string.gsub!("\a", '')
    Lich.log "Invalid \\a stripped out: #{server_string.inspect}"
  end

  server_string
end

.fix_xml_tags(server_string) ⇒ String

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.

Repairs malformed XML tags: open-ended component/dynaStream tags, dangling closing tags, and unclosed wound (nerves) tags from empath appraisals.

Logs detected and repaired malformations.

Parameters:

  • server_string (String) —

    the XML string to repair, modified in place

Returns:

  • (String) —

    the repaired server string



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'documented/games.rb', line 357

def fix_xml_tags(server_string)
  # Fix open-ended XML tags
  if /^<(?<xmltag>dynaStream|component) id='.*'>[^<]*(?!<\/\k<xmltag>>)\r\n$/ =~ server_string
    Lich.log "Open-ended #{xmltag} tag: #{server_string.inspect}"
    server_string.gsub!("\r\n", "</#{xmltag}>")
    Lich.log "Open-ended #{xmltag} tag fixed to: #{server_string.inspect}"
  end

  # Remove dangling closing tags
  if server_string =~ /^(?:(\"|<compass><\/compass>))?<\/(dynaStream|component)>\r\n/
    Lich.log "Extraneous closing tag detected and deleted: #{server_string.inspect}"
    server_string = ""
  end

  # Remove unclosed tag in long strings from empath appraisals
  if server_string =~ / and <d cmd=\"transfer .+? nerves\">a/
    Lich.log "Unclosed wound (nerves) tag detected and deleted: #{server_string.inspect}"
    server_string.sub!(/ and <d cmd=\"transfer .+? nerves\">a.+?$/, " and more.")
  end

  server_string
end