Class: SSCBot::ChatLog::MessageParser
- Inherits:
-
Object
- Object
- SSCBot::ChatLog::MessageParser
- Extended by:
- AttrBool::Ext
- Defined in:
- lib/ssc.bot/chat_log/message_parser.rb
Overview
Constant Summary collapse
- MAX_NAMELEN =
24
Instance Attribute Summary collapse
- #check_history_count ⇒ Object
- #commands ⇒ Object readonly
- #messages ⇒ Object readonly
- #namelen ⇒ Object
- #regex_cache ⇒ Object readonly
Instance Method Summary collapse
- #clear_history ⇒ Object
- #command?(type, name, delete: true) ⇒ Boolean
-
#initialize(autoset_namelen: true, check_history_count: 5, namelen: nil, store_history: true, strict: true) ⇒ MessageParser
constructor
A new instance of MessageParser.
- #match_kill?(line) ⇒ Boolean
-
#match_player(line, type_name:, name_prefix: '', name_suffix: '> ', type_prefix: /../, use_namelen: true) ⇒ Object
The Ruby interpreter should cache the args’ default values, so no reason to manually cache them unless a variable is involved inside.
- #match_pub?(line) ⇒ Boolean
- #match_q_find?(line) ⇒ Boolean
- #match_q_log?(line) ⇒ Boolean
- #match_q_namelen?(line) ⇒ Boolean
- #match_remote?(line) ⇒ Boolean
- #parse(line) ⇒ Object
- #parse_chat(line) ⇒ Object
- #parse_freq(line) ⇒ Object
- #parse_kill(line, match:) ⇒ Object
- #parse_player(line, type_name:, match: :default) ⇒ Object
- #parse_private(line) ⇒ Object
- #parse_pub(line, match:) ⇒ Object
- #parse_q_find(line, match:) ⇒ Object
- #parse_q_log(line, match:) ⇒ Object
- #parse_q_namelen(line, match:) ⇒ Object
- #parse_remote(line, match:) ⇒ Object
- #parse_team(line) ⇒ Object
- #reset_namelen ⇒ Object
- #store_command(type, name) ⇒ Object
Constructor Details
#initialize(autoset_namelen: true, check_history_count: 5, namelen: nil, store_history: true, strict: true) ⇒ MessageParser
Returns a new instance of MessageParser.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 41 def initialize(autoset_namelen: true,check_history_count: 5,namelen: nil,store_history: true,strict: true) super() @autoset_namelen = autoset_namelen @check_history_count = check_history_count @commands = {} @messages = [] @namelen = namelen @regex_cache = {} @store_history = store_history @strict = strict end |
Instance Attribute Details
#check_history_count ⇒ Object
33 34 35 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 33 def check_history_count @check_history_count end |
#commands ⇒ Object (readonly)
34 35 36 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 34 def commands @commands end |
#messages ⇒ Object (readonly)
35 36 37 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 35 def @messages end |
#namelen ⇒ Object
36 37 38 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 36 def namelen @namelen end |
#regex_cache ⇒ Object (readonly)
37 38 39 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 37 def regex_cache @regex_cache end |
Instance Method Details
#clear_history ⇒ Object
374 375 376 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 374 def clear_history @messages.clear end |
#command?(type, name, delete: true) ⇒ Boolean
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 393 def command?(type,name,delete: true) return true if @check_history_count < 1 type_hash = @commands[type] if !type_hash.nil? index = type_hash[name] if !index.nil? && (@messages.length - index) <= @check_history_count type_hash.delete(name) if delete return true end end return false end |
#match_kill?(line) ⇒ Boolean
413 414 415 416 417 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 413 def match_kill?(line) return false if line.length < 19 # ' N(0) killed by: N' return /\A (?<killed>.*?\S)\((?<bounty>\d+)\) killed by: (?<killer>.*?\S)\z/.match(line) end |
#match_player(line, type_name:, name_prefix: '', name_suffix: '> ', type_prefix: /../, use_namelen: true) ⇒ Object
The Ruby interpreter should cache the args’ default values, so no reason to manually cache them unless a variable is involved inside.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 59 def match_player(line,type_name:,name_prefix: '',name_suffix: '> ',type_prefix: /../,use_namelen: true) cached_regex = @regex_cache[type_name] if cached_regex.nil? cached_regex = {} @regex_cache[type_name] = cached_regex end use_namelen &&= !@namelen.nil? key = use_namelen ? @namelen : :no_namelen regex = cached_regex[key] if regex.nil? name_prefix = Util.quote_str_or_regex(name_prefix) name_suffix = Util.quote_str_or_regex(name_suffix) type_prefix = Util.quote_str_or_regex(type_prefix) if use_namelen name = /.{#{@namelen}}/ else name = /.*?\S/ end name = Util.quote_str_or_regex(name) # Be careful to not use spaces ' ', but to use '\\ ' (or '\s') instead # because of the '/x' option. regex = / \A#{type_prefix} #{name_prefix}(?<name>#{name})#{name_suffix} (?<message>.*)\z /x cached_regex[key] = regex end return regex.match(line) end |
#match_pub?(line) ⇒ Boolean
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 421 def match_pub?(line) return false if line.length < 5 # ' N> ' match = match_player(line,type_name: :pub,type_prefix: ' ') if !match.nil? name = Util.u_lstrip(match[:name]) if name.empty? || name.length > MAX_NAMELEN return false end end return match end |
#match_q_find?(line) ⇒ Boolean
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 446 def match_q_find?(line) return false if line.length < 7 # ' N - A' return false unless command?(:pub,%s(?find)) if line.start_with?(' Not online, last seen ') match = line.match(/(?<more>more) than (?<days>\d+) days ago\z/) match = line.match(/(?<days>\d+) days? ago\z/) if match.nil? match = line.match(/(?<hours>\d+) hours? ago\z/) if match.nil? return match else match = line.match(/\A (?<player>.+) is in (?<zone>.+)\z/) match = line.match(/\A (?<player>.+) - (?<arena>.+)\z/) if match.nil? if match caps = match.named_captures player = caps['player'] return false if player.length > MAX_NAMELEN if caps.key?('arena') area = caps['arena'] elsif caps.key?('zone') area = caps['zone'] else return false end # If do /\A (?<player>[^[[:space:]]].+[^[[:space:]])/, then it won't # capture names/zones/arenas that are only 1 char long, so do this. [player[0],player[-1],area[0],area[-1]].each do |c| if c =~ /[[:space:]]/ return false end end return match end end return false end |
#match_q_log?(line) ⇒ Boolean
493 494 495 496 497 498 499 500 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 493 def match_q_log?(line) return false if line.length < 17 match = /\A Log file open: (?<filename>.+)\z/.match(line) match = /\A Log file closed\z/.match(line) if match.nil? return match end |
#match_q_namelen?(line) ⇒ Boolean
504 505 506 507 508 509 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 504 def match_q_namelen?(line) return false if line.length < 24 # '...: 0' return false if line[21] != ':' return /\A Message Name Length: (?<namelen>\d+)\z/.match(line) end |
#match_remote?(line) ⇒ Boolean
515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 515 def match_remote?(line) return false if line.length < 5 # 'P :N:' case line[2] when ':' return match_player(line,type_name: %s(remote.out), name_prefix: ':',name_suffix: ':',use_namelen: false) when '(' return match_player(line,type_name: %s(remote.in), name_prefix: '(',name_suffix: ')>',use_namelen: false) end return false end |
#parse(line) ⇒ Object
98 99 100 101 102 103 104 105 106 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 98 def parse(line) if line.nil? if @strict raise ArgumentError,"invalid line{#{line.inspect}}" else line = '' end end = nil if !line.empty? case line[0] when 'C' = parse_chat(line) when 'E' = parse_freq(line) when 'P' if (match = match_remote?(line)) = parse_remote(line,match: match) else = parse_private(line) end when 'T' = parse_team(line) else # Check this one first to prevent abuse from pubbers. if (match = match_pub?(line)) = parse_pub(line,match: match) else if (match = match_kill?(line)) = parse_kill(line,match: match) elsif (match = match_q_log?(line)) = parse_q_log(line,match: match) elsif (match = match_q_namelen?(line)) = parse_q_namelen(line,match: match) else # These are last because too flexible. if (match = match_q_find?(line)) = parse_q_find(line,match: match) end end end end end if .nil? = Message.new(line,type: :unknown) end if @store_history @messages << end return end |
#parse_chat(line) ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 158 def parse_chat(line) match = match_player(line,type_name: :chat,name_prefix: /(?<channel>\d+)\:/,use_namelen: false) player = parse_player(line,type_name: :chat,match: match) return nil if player.nil? channel = match[:channel].to_i return ChatMessage.new(line,channel: channel,name: player.name,message: player.) end |
#parse_freq(line) ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 171 def parse_freq(line) player = parse_player(line,type_name: :freq) return nil if player.nil? return FreqMessage.new(line,name: player.name,message: player.) end |
#parse_kill(line, match:) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 181 def parse_kill(line,match:) if match.nil? if @strict raise ParseError,"invalid kill message{#{line}}" else return nil end end killed = match[:killed] bounty = match[:bounty].to_i killer = match[:killer] return KillMessage.new(line,killed: killed,bounty: bounty,killer: killer) end |
#parse_player(line, type_name:, match: :default) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 199 def parse_player(line,type_name:,match: :default) if match.nil? if @strict raise ParseError,"invalid #{type_name} message{#{line}}" else return nil end elsif match == :default # Use type_name of :player (not passed in param) for regex_cache. match = match_player(line,type_name: :player) end name = Util.u_lstrip(match[:name]) = match[:message] if name.empty? || name.length > MAX_NAMELEN if @strict raise ParseError,"invalid player name for #{type_name} message{#{line}}" else return nil end end return PlayerMessage.new(line,type: :unknown,name: name,message: ) end |
#parse_private(line) ⇒ Object
227 228 229 230 231 232 233 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 227 def parse_private(line) player = parse_player(line,type_name: :private) return nil if player.nil? return PrivateMessage.new(line,name: player.name,message: player.) end |
#parse_pub(line, match:) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 237 def parse_pub(line,match:) player = parse_player(line,type_name: :pub,match: match) return nil if player.nil? cmd = Util.u_strip(player.).downcase if cmd.start_with?('?find') store_command(:pub,%s(?find)) # See: match_q_find?() end return PubMessage.new(line,name: player.name,message: player.) end |
#parse_q_find(line, match:) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 260 def parse_q_find(line,match:) if match.nil? if @strict raise ParseError,"invalid ?find message{#{line}}" else return nil end end caps = match.named_captures q_find = nil if (days = caps['days']) more = caps.key?('more') days = days.to_i q_find = QFindMessage.new(line,find_type: :days,more: more,days: days) elsif (hours = caps['hours']) hours = hours.to_i q_find = QFindMessage.new(line,find_type: :hours,hours: hours) elsif (player = caps['player']) if (arena = caps['arena']) private = (arena == '(Private arena)') q_find = QFindMessage.new(line,find_type: :arena,player: player,arena: arena,private: private) elsif (zone = caps['zone']) q_find = QFindMessage.new(line,find_type: :zone,player: player,zone: zone) end end if q_find.nil? && @strict raise ParseError,"invalid ?find message{#{line}}" end return q_find end |
#parse_q_log(line, match:) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 301 def parse_q_log(line,match:) if match.nil? if @strict raise ParseError,"invalid ?log message{#{line}}" else return nil end end filename = match.named_captures['filename'] log_type = filename.nil? ? :close : :open return QLogMessage.new(line,log_type: log_type,filename: filename) end |
#parse_q_namelen(line, match:) ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 318 def parse_q_namelen(line,match:) if match.nil? if @strict raise ParseError,"invalid ?namelen message{#{line}}" else return nil end end namelen = match[:namelen].to_i if namelen < 1 if @strict raise ParseError,"invalid namelen for ?namelen message{#{line}}" else return nil end elsif namelen > MAX_NAMELEN warn("namelen{#{namelen}} > max{#{MAX_NAMELEN}} for ?namelen message{#{line}}",uplevel: 0) end if @autoset_namelen @namelen = namelen end return QNamelenMessage.new(line,namelen: namelen) end |
#parse_remote(line, match:) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 350 def parse_remote(line,match:) player = parse_player(line,type_name: %s(remote.private),match: match) return nil if player.nil? own = (line[2] == ':') squad = (player.name[0] == '#') return RemoteMessage.new(line, own: own,squad: squad, name: player.name,message: player., ) end |
#parse_team(line) ⇒ Object
366 367 368 369 370 371 372 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 366 def parse_team(line) player = parse_player(line,type_name: :team) return nil if player.nil? return TeamMessage.new(line,name: player.name,message: player.) end |
#reset_namelen ⇒ Object
378 379 380 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 378 def reset_namelen @namelen = nil end |
#store_command(type, name) ⇒ Object
382 383 384 385 386 387 388 389 390 391 |
# File 'lib/ssc.bot/chat_log/message_parser.rb', line 382 def store_command(type,name) type_hash = @commands[type] if type_hash.nil? type_hash = {} @commands[type] = type_hash end type_hash[name] = @messages.length # Index of when command was found/stored end |