forked from cppvik/teleperl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumphistcbor.pl
384 lines (315 loc) · 10.3 KB
/
dumphistcbor.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
97
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
345
346
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
375
376
377
378
379
380
381
382
383
384
#!/usr/bib/env perl5
my $VERSION = 0.02;
use Modern::Perl;
use utf8;
use Teleperl::Util qw(:DEFAULT unlock_hashref_recurse);
use Config::Tiny;
use Storable qw(store retrieve freeze thaw dclone);
use Getopt::Long::Descriptive;
use CBOR::XS;
use AnyEvent;
use AnyEvent::Log;
eval "use Time::HiRes qw(time);";
use Telegram;
use Telegram::ObjTable;
use Telegram::Help::GetNearestDc;
use Telegram::InputPeer;
use Telegram::Messages::GetHistory;
use Telegram::Users::GetFullUser;
use Telegram::Messages::GetFullChat;
use Telegram::Channels::GetFullChannel;
use Telegram::Channels::GetParticipants;
use Telegram::ChannelParticipantsFilter;
use Data::Dumper;
sub option_spec {
[ 'verbose|v!' => 'be verbose, by default also influences logger' ],
[ 'noupdate!' => 'pass noupdate to Telegram->new', { default => 1 } ],
[ 'debug|d:+' => 'pass debug (2=trace) to Telegram->new & AE::log', {default=>0}],
[ 'session=s' => 'name of session data save file', { default => 'session.dat'} ],
[ 'config|c=s' => 'name of configuration file', { default => "teleperl.conf" } ],
[ 'logfile|l=s' => 'path to log file', { default => "$0.log" } ],
[ 'file|f=s' => 'CBOR file name for dump', { required => 1 } ],
[ "offset_id=i" => "same named API param, default 0" ],
[ "offset_date=i" => "same named API param, default 0" ],
[ "add_offset=i" => "same named API param, default 0" ],
[ "limit=i" => "same named API param, default 10" ],
[ "max_id=i" => "same named API param, default 0" ],
[ "min_id=i" => "same named API param, default 0" ],
}
### initialization
my ($opts, $usage);
eval { ($opts, $usage) = describe_options( '%c %o <peer>', option_spec() ) };
die "Invalid opts: $@\nUsage: $usage\n" if $@;
die "user/chat must be specified" unless defined $ARGV[0];
my $session = retrieve( $opts->session ) if -e $opts->session;
my $conf = Config::Tiny->read($opts->config);
$Data::Dumper::Indent = 1;
$AnyEvent::Log::FILTER->level(
$opts->debug > 1 ? "trace" :
$opts->debug ? "debug" :
$opts->verbose ? "info" : "note"
);
$AnyEvent::Log::LOG->log_to_path($opts->logfile) if $opts->{logfile}; # XXX path vs file
install_AE_log_crutch();
my $pid = &check_exit();
die "flag exists on start with $pid contents\n" if $pid;
install_AE_log_SIG_WARN();
install_AE_log_SIG_DIE();
my $tg = Telegram->new(
dc => $conf->{dc},
app => $conf->{app},
proxy => $conf->{proxy},
session => $session,
reconnect => 1,
keepalive => 1,
noupdate => $opts->{noupdate},
debug => $opts->{debug}
);
#$tg->{on_raw_msg} = \&one_message;
$tg->{after_invoke} = \&after_invoke;
my $cbor = CBOR::XS->new->pack_strings(1);
my $cbor_data;
my @clones;
sub one_message {
my $mesg = shift;
return if ref($mesg) =~ /MTProto::P.ng/ and not $opts->verbose;
AE::log error => ">1 arg " . Dumper(@_) if @_;
# XXX workaround of 'use fields' :(
my $clone = dclone $mesg;
AE::log trace => "$mesg $clone".Dumper($mesg, $clone);
unlock_hashref_recurse($clone);
push @clones, +{ time => time, in => $clone };
_pack() if @clones > 512; # XXX what's balance b. often vs compress ratio?
};
# NOTE this doesn't make sense here in right this daemon - serves mostly
# an example for real app wishing to log
sub after_invoke {
my ($req_id, $query, $res_cb) = @_;
my $cbname = eval {
require Sub::Util;
Sub::Util::subname($res_cb);
};
# XXX workaround of 'use fields' :(
my $clone = dclone $query;
AE::log trace => "$req_id $clone".Dumper($req_id, $clone);
unlock_hashref_recurse($clone);
push @clones, +{
time => time,
out => $clone,
req_id => $req_id,
($cbname ? (cb => $cbname) : ())
};
# don't pack here, request may be still on queue and not sent yet
}
sub _pack {
return unless @clones;
$cbor_data .= $cbor->encode(@clones > 1 ? \@clones : $clones[0]);
@clones = ();
}
sub get_fname {
return $opts->file;
}
sub save_cbor {
_pack();
return unless length $cbor_data > 3;
my $fname = get_fname();
$cbor_data = $CBOR::XS::MAGIC
. $cbor->encode({ # what version decoder should use
time => time,
schema => $Telegram::ObjTable::GENERATED_FROM,
})
. $cbor_data
unless -e $fname;
sysopen my $fh, $fname, AnyEvent::IO::O_CREAT | AnyEvent::IO::O_WRONLY | AnyEvent::IO::O_APPEND, 0666
or AE::log fatal => "can't open $fname: $!";
binmode($fh);
AE::log info => "length cbor=" . length $cbor_data;
my ($n, $wrlen) = (0, 0);
while ($wrlen < length $cbor_data) {
$n = syswrite $fh, $cbor_data; #, $wrlen; # XXX bug with 3 arg on mswin O_o
$n or AE::log fatal => "can't write $fname: $!";
$n == length $cbor_data or AE::log fatal => "can't write $fname: short write $n";
AE::log debug => "write returned $n";
$wrlen += $n;
}
close $fh
or AE::log fatal => "can't close $fname: $!";
$cbor_data = '';
}
sub save_session {
AE::log note => "saving session file";
store( $tg->{session}, $opts->session );
}
sub check_exit {
my $flag = $opts->{session} . ".exitflg";
return 0 unless -e $flag;
my $body = do {
local $/ = undef;
open FLG, "<$flag";
<FLG>
};
unlink $flag;
return ($body || 'empty');
}
$tg->start;
# XXX socks5 crutch!
AnyEvent->_poll until defined $tg->{_mt};
my $cond = AnyEvent->condvar;
# XXX FIXME make first request dummy 'coz error 32 possible :(
$tg->invoke(
Telegram::Help::GetNearestDc->new,
sub { "noop" }
);
my $peer = $ARGV[0];
if ($peer eq 'self') {
$peer = Telegram::InputPeerSelf->new;
}
else {
$peer = $tg->name_to_id($peer);
$peer = $tg->peer_from_id($peer);
}
die "unknown user/chat" unless defined $peer;
sub handle_history
{
my ($peer, $messages, $ptop, $opts) = @_;
AE::log debug => "ptop=%d %s", $ptop, ($ptop < 2 ? Dumper($messages) : ref $messages);
my $top = 0;
$tg->_cache_users(@{$messages->{users}}) ;
for my $upd (@{$messages->{messages}}) {
$top = $upd->{id};
AE::log debug => "top=%d", $top;
$opts->{limit}-- if $opts->{limit};
# if ($upd->isa('Telegram::Message')) {
one_message($upd);
# }
}
$cond->send, return unless $top; # if bottom reached, slice will be empty
if ($ptop == 0 or $top < $ptop && ($opts->{limit} // 1)) {
$tg->invoke( Telegram::Messages::GetHistory->new(
peer => $peer,
offset_id => $top,
offset_date => $opts->{offset_date} // 0,
add_offset => $opts->{add_offset} // 0,
limit => $opts->{limit} || 20,
max_id => $opts->{max_id} || 0,
min_id => $opts->{min_id} || 0,
hash => 0
), sub {
handle_history($peer, $_[0], $top, $opts) if $_[0]->isa('Telegram::Messages::MessagesABC');
}
);
}
}
sub handle_chanparticipants {
my ($reply, $offset) = @_;
one_message($_[0]);
unless ($reply->isa('Telegram::Channels::ChannelParticipants')) {
AE::log warn => "not a Telegram::Channels::ChannelParticipants" . Dumper($reply);
return;
}
AE::log debug => "offset=%d count=%d", $offset, $reply->{count};
$offset += 20; # XXX hardcode
return if $offset > $reply->{count};
$tg->invoke(
Telegram::Channels::GetParticipants->new(
channel => $peer,
filter => Telegram::ChannelParticipantsRecent->new(),
offset => $offset,
limit => 0,
hash => 0,
),
sub {
handle_chanparticipants($_[0], $offset);
}
);
}
# save info about peer in addition to history
sub mainreqs {
if ($peer->isa('Telegram::InputPeerUser')) {
# users.getFullUser
$tg->invoke(
Telegram::Users::GetFullUser->new(
id => $peer,
),
\&one_message,
);
}
elsif ($peer->isa('Telegram::InputPeerChat')) {
# messages.getFullChat
$tg->invoke(
Telegram::Messages::GetFullChat->new(
chat_id => $peer->{chat_id},
),
sub {
# TODO do request Telegram::UserFull for each?
one_message($_[0]);
}
);
}
elsif ($peer->isa('Telegram::InputPeerChannel')) {
# channels.getFullChannel
$tg->invoke(
Telegram::Channels::GetFullChannel->new(
channel => $peer,
),
\&one_message,
);
# channels.getParticipants
$tg->invoke(
Telegram::Channels::GetParticipants->new(
channel => $peer,
filter => Telegram::ChannelParticipantsRecent->new(),
offset => 0, # XXX ?
limit => 0,
hash => 0,
),
sub {
handle_chanparticipants($_[0], 0);
}
);
}
# then, history itself
$tg->invoke(
Telegram::Messages::GetHistory->new(
peer => $peer,
offset_id => $opts->{offset_id} // 0,
offset_date => $opts->{offset_date} // 0,
add_offset => $opts->{add_offset} // 0,
limit => $opts->{limit} // 20,
max_id => $opts->{max_id} // 0,
min_id => $opts->{min_id} // 0,
hash => 0
), sub {
handle_history($peer, $_[0], 0, $opts) if $_[0]->isa('Telegram::Messages::MessagesABC');
}
);
}
# give a time for possible MTProto sync errors...
my $req = AnyEvent->timer(
after => 3,
cb => \&mainreqs,
);
my $save_i = 1;
my $watch = AnyEvent->timer(
after => 2,
interval => 1,
cb => sub {
save_cbor if $save_i % 60 == 0;
$cond->send if &check_exit;
save_session() if $save_i++ % 3600 == 0;
},
);
my $signal;
$SIG{TERM} = sub {
AE::log note => "INT recvd";
$cond->send;
};
# XXX should work in console, not under wperl.exe
if ($^O ne 'MSWin32') {
$signal = AnyEvent->signal( signal => 'INT', cb => $SIG{TERM} );
}# else {
$SIG{INT} = $SIG{TERM};
$cond->recv;
save_cbor() if @clones;
AE::log note => "quittin..";
save_session();