Skip to content

Commit

Permalink
Guard more thoroughly against empty message ids [#72]
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdstaaij committed Feb 25, 2017
1 parent cd260ea commit 650580f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dumpers/lib/dumper_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def start_dialog(dialog, progress)
# This default makes sense in simple cases, override for advanced custom logic
def msg_fresh?(msg, progress)
# msg: Hash, progress: DumpProgress
return false unless msg['id']
return false if msg['id'].to_s.empty?
!progress.newest_id || MsgId.new(msg['id']) > progress.newest_id
end

Expand Down
4 changes: 2 additions & 2 deletions lib/dump_progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DumpProgress
attr_writer :dumper_state

def initialize(newest_id = nil, newest_date = nil, dumper_state = {})
@newest_id = newest_id ? MsgId.new(newest_id) : nil
@newest_id = (newest_id.to_s.empty?) ? nil : MsgId.new(newest_id)
@newest_date = newest_date
@dumper_state = dumper_state
end
Expand All @@ -35,7 +35,7 @@ def to_json(*a)
end

def update(msg)
msg_id = msg['id'] ? MsgId.new(msg['id']) : nil
msg_id = (msg['id'].to_s.empty?) ? nil : MsgId.new(msg['id'])
if msg_id && (!@newest_id || msg_id > @newest_id)
@newest_id = msg_id
@newest_date = msg['date'] || @newest_date
Expand Down
10 changes: 6 additions & 4 deletions telegram-history-dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ def dump_dialog(dialog)

msg_chunk.reverse_each do |msg|
dump_msg = true
unless msg['id']
if msg['id'].to_s.empty?
$log.warn('Dropping message without id: %s' % msg)
dump_msg = false
msg_id = nil
else
msg_id = MsgId.new(msg['id'])
end
msg_id = msg['id'] ? MsgId.new(msg['id']) : nil
unless !msg_id || !prev_msg_id || msg_id < prev_msg_id
if msg_id && prev_msg_id && msg_id >= prev_msg_id
$log.warn('Message ids are not sequential (%s[%s] -> %s[%s])' % [
prev_msg_id.raw_hex, prev_msg_id.sequence_hex,
msg_id.raw_hex, msg_id.sequence_hex,
Expand All @@ -118,7 +120,7 @@ def dump_dialog(dialog)
dump_msg = false
end

prev_msg_id = MsgId.new(msg['id']) if msg['id']
prev_msg_id = msg_id
cur_progress.update(msg)

if msg['text'] && filter_regex && filter_regex =~ msg['text']
Expand Down

0 comments on commit 650580f

Please sign in to comment.