forked from zigdon/twirssi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twirssi.pl
4388 lines (3869 loc) · 154 KB
/
twirssi.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use strict;
use v5.10;
use Irssi;
use Irssi::Irc;
use Net::Twitter qw/3.11009/;
use Twitter::API;
use HTTP::Date;
use HTML::Entities; # decode_entities
use File::Temp;
use LWP::Simple;
use Data::Dumper;
use FileHandle;
use POSIX qw/:sys_wait_h strftime/;
use Encode;
use JSON::MaybeXS;
use DateTime;
use DateTime::Format::Strptime;
$Data::Dumper::Indent = 1;
use vars qw($VERSION %IRSSI);
$VERSION = sprintf '%s', q$Version: v2.9.0$ =~ /^\w+:\s+v(\S+)/;
%IRSSI = (
authors => '@zigdon, @gedge',
contact => '[email protected]',
name => 'twirssi',
description => 'Send twitter updates using /tweet. '
. 'Can optionally set your bitlbee /away message to same',
license => 'GNU GPL v2',
url => 'http://twirssi.com',
changed => '$Date: 2023-06-10 18:00:00 +0000$',
);
my $twit; # $twit is current logged-in Net::Twitter or Twitter::API object (usually one of %twits)
my %twits; # $twits{$username} = logged-in object
my %oauth;
my $user; # current $account
my $defservice; # current $service
my $poll_event; # timeout_add event object (regular update)
my %last_poll; # $last_poll{$username}{tweets|friends|blocks|lists} = time of last update
# {__interval|__poll} = time
my %nicks; # $nicks{$screen_name} = last seen/mentioned time (for sorting completions)
my %friends; # $friends{$username}{$nick} = $epoch_when_refreshed (rhs value used??)
my %blocks; # $blocks {$username}{$nick} = $epoch_when_refreshed (rhs value used??)
my %tweet_cache; # $tweet_cache{$tweet_id} = time of tweet (helps keep last hour of IDs, to avoid dups)
my %state;
# $state{__ids} {$lc_nick}[$cache_idx] = $tweet_id
# $state{__u} {$lc_nick} = { id=>$user_id }
# $state{__i} {$user_id} = $lc_nick
# $state{__i_is_bad} {$user_id} = $epoch_time_when_failed_to_find_user_id
# $state{__tweets} {$lc_nick}[$cache_idx] = $tweet_text
# $state{__nls} {$lc_nick}[$cache_idx] = $nl (infix in above)
# $state{__usernames} {$lc_nick}[$cache_idx] = $username_that_polled_tweet
# $state{__reply_to_ids} {$lc_nick}[$cache_idx] = $polled_tweet_replies_to_this_id
# $state{__reply_to_users} {$lc_nick}[$cache_idx] = $polled_tweet_replies_to_this_user
# $state{__created_ats} {$lc_nick}[$cache_idx] = $time_of_tweet
# $state{__indexes} {$lc_nick} = $last_cache_idx_used
# $state{__last_id} {$username}{timeline|reply|dm} = $id_of_last_tweet
# {__sent} = $id_of_last_tweet_from_act
# {__extras}{$lc_nick} = $id_of_last_tweet (fix_replies)
# {__search}{$topic} = $id_of_last_tweet
# $state{__lists} {$username}{$list_name} = { id => $list_id, members=>[$nick,...] }
# $state{__channels} {$type}{$tag}{$net_tag} = [ channel,... ]
# $state{__windows} {$type}{$tag} = $window_name
my $failstatus = 0; # last update status: 0=ok, 1=warned, 2=failwhaled
my $first_call = 1;
my $child_pid;
my %fix_replies_index; # $fix_replies_index($username} = 0..100 idx in sort keys $state{__last_id}{$username}{__extras}
my %search_once;
my $update_is_running = 0;
my %logfile;
my %settings;
my %last_ymd; # $last_ymd{$chan_or_win} = $last_shown_ymd
my @datetime_parser;
my %completion_types = ();
my %expanded_url = ();
my $ua;
my %valid_types = (
'window' => [ qw/ tweet search dm reply sender error default /], # twirssi_set_window
'channel' => [ qw/ tweet search dm reply sender error * / ], # twirssi_set_channel
);
my $local_tz = DateTime::TimeZone->new( name => 'local' );
my @settings_defn = (
[ 'broadcast_users', 'twirssi_broadcast_users', 's', undef, 'list{,}' ],
[ 'charset', 'twirssi_charset', 's', 'utf8', ],
[ 'default_service', 'twirssi_default_service', 's', 'Twitter', ],
[ 'ignored_accounts', 'twirssi_ignored_accounts', 's', '', 'list{,},norm_user' ],
[ 'ignored_twits', 'twirssi_ignored_twits', 's', '', 'lc,list{,}' ],
[ 'ignored_tags', 'twirssi_ignored_tags', 's', '', 'lc,list{,}' ],
[ 'location', 'twirssi_location', 's', Irssi::get_irssi_dir . "/scripts/$IRSSI{name}.pl" ],
[ 'nick_color', 'twirssi_nick_color', 's', '%B', ],
[ 'ymd_color', 'twirssi_ymd_color', 's', '%r', ],
[ 'oauth_store', 'twirssi_oauth_store', 's', Irssi::get_irssi_dir . "/scripts/$IRSSI{name}.oauth" ],
[ 'replies_store', 'twirssi_replies_store', 's', Irssi::get_irssi_dir . "/scripts/$IRSSI{name}.json" ],
[ 'dump_store', 'twirssi_dump_store', 's', Irssi::get_irssi_dir . "/scripts/$IRSSI{name}.dump" ],
[ 'poll_store', 'twirssi_poll_store', 's', Irssi::get_irssi_dir . "/scripts/$IRSSI{name}.polls" ],
[ 'id_store', 'twirssi_id_store', 's', Irssi::get_irssi_dir . "/scripts/$IRSSI{name}.ids" ],
[ 'retweet_format', 'twirssi_retweet_format', 's', 'RT $n: "$t" ${-- $c$}' ],
[ 'retweeted_format', 'twirssi_retweeted_format', 's', 'RT $n: $t' ],
[ 'stripped_tags', 'twirssi_stripped_tags', 's', '', 'list{,}' ],
[ 'topic_color', 'twirssi_topic_color', 's', '%r', ],
[ 'timestamp_format', 'twirssi_timestamp_format', 's', '%H:%M:%S', ],
[ 'window_priority', 'twirssi_window_priority', 's', 'account', ],
[ 'upgrade_branch', 'twirssi_upgrade_branch', 's', 'master', ],
[ 'upgrade_dev', 'twirssi_upgrade_dev', 's', 'gedge', ],
[ 'bitlbee_server', 'bitlbee_server', 's', 'bitlbee' ],
[ 'hilight_color', 'twirssi_hilight_color', 's', '%M' ],
[ 'unshorten_color', 'twirssi_unshorten_color', 's', '%b' ],
[ 'shorter_color', 'twirssi_shorter_color', 's', '%X7O' ],
[ 'passwords', 'twitter_passwords', 's', undef, 'list{,}' ],
[ 'usernames', 'twitter_usernames', 's', undef, 'list{,}' ],
[ 'update_usernames', 'twitter_update_usernames', 's', undef, 'list{,}' ],
[ 'url_provider', 'short_url_provider', 's', 'TinyURL' ],
[ 'url_unshorten', 'short_url_domains', 's', '', 'lc,list{ }' ],
[ 'url_args', 'short_url_args', 's', undef ],
[ 'window', 'twitter_window', 's', 'twitter' ],
[ 'debug_win_name', 'twirssi_debug_win_name', 's', '' ],
[ 'limit_user_tweets', 'twitter_user_results', 's', '20' ],
[ 'always_shorten', 'twirssi_always_shorten', 'b', 0 ],
[ 'rt_to_expand', 'twirssi_retweet_to_expand', 'b', 1 ],
[ 'avoid_ssl', 'twirssi_avoid_ssl', 'b', 0 ],
[ 'debug', 'twirssi_debug', 'b', 0 ],
[ 'notify_timeouts', 'twirssi_notify_timeouts', 'b', 1 ],
[ 'logging', 'twirssi_logging', 'b', 0 ],
[ 'mini_whale', 'twirssi_mini_whale', 'b', 0 ],
[ 'own_tweets', 'show_own_tweets', 'b', 1 ],
[ 'to_away', 'tweet_to_away', 'b', 0 ],
[ 'upgrade_beta', 'twirssi_upgrade_beta', 'b', 1 ],
[ 'use_oauth', 'twirssi_use_oauth', 'b', 1 ],
[ 'use_reply_aliases', 'twirssi_use_reply_aliases', 'b', 0 ],
[ 'window_input', 'tweet_window_input', 'b', 0 ],
[ 'retweet_classic', 'retweet_classic', 'b', 0 ],
[ 'retweet_show', 'retweet_show', 'b', 0 ],
[ 'force_first', 'twirssi_force_first', 'b', 0 ],
[ 'multiline_show_empty', 'twirssi_multiline_show_empty', 'b', 1 ],
[ 'multiline_show_blank', 'twirssi_multiline_show_blank', 'b', 1 ],
[ 'multiline_show_contentless', 'twirssi_multiline_show_contentless', 'b', 1 ],
[ 'friends_poll', 'twitter_friends_poll', 'i', 600 ],
[ 'blocks_poll', 'twitter_blocks_poll', 'i', 900 ],
[ 'lists_poll', 'twitter_lists_poll', 'i', 900 ],
[ 'poll_interval', 'twitter_poll_interval', 'i', 300 ],
[ 'poll_schedule', 'twitter_poll_schedule', 's', '', 'list{,}' ],
[ 'search_results', 'twitter_search_results', 'i', 5 ],
[ 'autosearch_results','twitter_autosearch_results','i', 0 ],
[ 'timeout', 'twitter_timeout', 'i', 30 ],
[ 'track_replies', 'twirssi_track_replies', 'i', 100 ],
[ 'tweet_max_chars', 'twirssi_tweet_max_chars', 'i', 280 ],
[ 'dm_max_chars', 'twirssi_dm_max_chars', 'i', 10000 ],
);
my %meta_to_twit = ( # map file keys to twitter keys
'id' => 'id',
'created_at' => 'created_at',
'reply_to_user' => 'in_reply_to_screen_name',
'reply_to_id' => 'in_reply_to_status_id',
);
my %irssi_to_mirc_colors = (
'%k' => '01', # black
'%r' => '05', # brown
'%g' => '03', # green
'%y' => '07', # orange
'%b' => '02', # deep blue
'%m' => '06', # purple / magenta
'%c' => '10', # aqua / dull cyan
'%w' => '15', # light grey
'%K' => '14', # grey / bright black (appears black XXX)
'%R' => '04', # red
'%G' => '09', # bright green
'%Y' => '08', # yellow
'%B' => '12', # bright blue
'%M' => '13', # pink / bright magenta
'%C' => '11', # bright aqua / cyan
'%W' => '00', # white
);
sub cmd_direct {
my ( $data, $server, $win ) = @_;
my ( $target, $text ) = split ' ', $data, 2;
unless ( $target and $text ) {
¬ice( ["dm"], "Usage: /dm <nick> <message>" );
return;
}
&cmd_direct_as( "$user $data", $server, $win );
}
sub user_to_id {
my $obj = shift;
my $user = shift;
my $ctx = shift // "u2id";
my $fh = shift;
if (not defined $state{__u}{lc $user} or not defined $state{__u}{lc $user}{id}) {
my $r;
eval {
$r = $obj->lookup_users({screen_name=>$user, include_entities=>0});
if (not defined $r) {
&error([$ctx, $fh], "Cannot get id for user: $user" );
return;
}
};
if ($@) {
&error([$ctx, $fh], "Failed to get id for user: $user" );
return;
}
if (not defined $r->[0] or not exists $r->[0]->{id_str}) {
&error([$ctx, $fh], "Bad response for id for user: $user" );
return;
}
if (defined $fh) { printf $fh "t:uid id:%s nick:%s\n", $r->[0]->{id_str}, lc $user; }
$state{__u}{lc $user}{id} = $r->[0]->{id_str};
$state{__i}{$r->[0]->{id_str}} = lc $user;
}
return $state{__u}{lc $user}{id};
}
sub id_to_user {
my $obj = shift;
my $u_id = shift;
my $ctx = shift // "id2u";
my $fh = shift;
if (not defined $state{__i}{$u_id}) {
my $r;
eval {
$r = $obj->lookup_users({user_id=>$u_id, include_entities=>0});
if (not defined $r) {
&error([$ctx, $fh], "Cannot get user for id $u_id" );
return;
}
};
if ($@) {
if ($@ =~ /^No user matches /) {
if (defined $state{__i_is_bad}{$u_id} and $state{__i_is_bad}{$u_id} + 86400 > time) {
return;
}
$state{__i_is_bad}{$u_id} = time;
if (defined $fh) { printf $fh "t:unuid id:%s\n", $u_id; }
&error([$ctx, $fh], "FAILED TO GET User $u_id" . (defined($r) ? " " . Dumper(\$r) : ''));
return;
}
&error([$ctx, $fh], "Failed to get user for id $u_id: $@" );
return;
}
if (not defined $r->[0] or not exists $r->[0]->{screen_name}) {
&error([$ctx, $fh], "Bad response for id for user: $u_id" );
return;
}
if (defined $fh) { printf $fh "t:uid id:%s nick:%s\n", $u_id, lc $r->[0]->{screen_name}; }
$state{__i}{$u_id} = lc $r->[0]->{screen_name};
$state{__u}{lc $r->[0]->{screen_name}}{id} = $u_id;
}
return $state{__i}{$u_id};
}
sub cmd_direct_as {
my ( $data, $server, $win ) = @_;
my ( $username, $target, $text ) = split ' ', $data, 3;
unless ( $username and $target and $text ) {
¬ice( ["dm"], "Usage: /dm_as <username> <nick> <message>" );
return;
}
return unless $username = &valid_username($username);
return unless &logged_in($twits{$username});
my $target_norm = &normalize_username($target, 1);
my $target_id = &user_to_id($twits{$username}, $target, "dm");
return unless defined $target_id;
$text = &shorten($text);
return if &too_long($text, ['dm', $target_norm]);
eval {
my $r = $twits{$username}->request(post => 'direct_messages/events/new', {
-to_json => {
event => {
type => 'message_create',
message_create => {
target => { recipient_id => $target_id, },
message_data => { text => $text, },
},
},
},
});
if (not defined $r) {
my $error;
eval {
$error = decode_json( $twits{$username}->get_error() );
$error = $error->{error};
};
die "$error\n" if $error;
¬ice( [ "dm", $target_norm ], "DM to $target failed" );
return;
}
¬ice( [ "dm", $target_norm ], "DM sent to $target: $text" );
$nicks{$target} = time;
};
if ($@) {
&error( "DM caused an error: $@" );
return;
}
}
sub cmd_retweet {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//;
unless ($data) {
¬ice( [ "tweet", $user ], "Usage: /retweet <nick[:num]> [comment]" );
return;
}
(my $id, $data ) = split ' ', $data, 2;
&cmd_retweet_as( "$user $id $data", $server, $win );
}
sub cmd_retweet_as {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//;
( my $username, my $id, $data ) = split ' ', $data, 3;
unless ($username) {
¬ice( ["tweet"],
"Usage: /retweet_as <username> <nick[:num]> [comment]" );
return;
}
return unless $username = &valid_username($username);
return unless &logged_in($twits{$username});
my $nick;
$id =~ s/[^\w\d\-:]+//g;
( $nick, $id ) = split /:/, $id;
unless ( exists $state{__ids}{ lc $nick } ) {
¬ice( [ "tweet", $username ],
"Can't find a tweet from $nick to retweet!" );
return;
}
$id = $state{__indexes}{lc $nick} unless defined $id;
unless ( $state{__ids}{ lc $nick }[$id] ) {
¬ice( [ "tweet", $username ],
"Can't find a tweet numbered $id from $nick to retweet!" );
return;
}
unless ( $state{__tweets}{ lc $nick }[$id] ) {
¬ice( [ "tweet", $username ],
"The text of this tweet isn't saved, sorry!" );
return;
}
my $text = &format_expand(fmt => $settings{retweet_format}, nick => $nick, data => $data,
tweet => $state{__tweets}{ lc $nick }[$id]);
my $modified = $data;
$data = &shorten($text);
return if ($modified or $settings{retweet_classic})
and &too_long($data, ['tweet', $username]);
my $success = 1;
my $extra_info = '';
eval {
if ($modified or $settings{retweet_classic}) {
$success = $twits{$username}->update(
{
status => $data,
# in_reply_to_status_id => $state{__ids}{ lc $nick }[$id]
}
);
$extra_info = ' (classic/edited)';
} else {
$success =
$twits{$username}->retweet( { id => $state{__ids}{ lc $nick }[$id] } );
# $retweeted_id{$username}{ $state{__ids}{ lc $nick }[$id] } = 1;
$extra_info = ' (native)';
}
};
unless ($success) {
¬ice( [ "tweet", $username ], "Update failed" );
return;
}
if ($@) {
&error( [ $username ], "Update caused an error: $@. Aborted" );
return;
}
$extra_info .= ' id=' . $success->{id} if $settings{debug};
foreach ( $data =~ /@([-\w]+)/g ) {
$nicks{$_} = time;
}
¬ice( [ "tweet", $username ], "Retweet of $nick:$id sent" . $extra_info );
}
sub format_expand {
my %args = @_;
$args{fmt} =~ s/\$n/\@$args{nick}/g;
if (defined $args{data} and $args{data} ne '') {
$args{fmt} =~ s/\$\{|\$}//g;
$args{fmt} =~ s/\$c/$args{data}/g;
} else {
$args{fmt} =~ s/\$\{.*?\$}//g;
}
$args{fmt} =~ s/\$t/$args{tweet}/g;
return $args{fmt};
}
sub cmd_retweet_to_window {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//;
( my $id, $data ) = split ' ', $data, 2;
$id =~ s/[^\w\d\-:]+//g;
( my $nick, $id ) = split ':', $id;
unless ( exists $state{__ids}{ lc $nick } ) {
¬ice( [ "tweet" ],
"Can't find a tweet from $nick to retweet!" );
return;
}
$id = $state{__indexes}{lc $nick} unless defined $id;
unless ( $state{__ids}{ lc $nick }[$id] ) {
¬ice( [ "tweet" ],
"Can't find a tweet numbered $id from $nick to retweet!" );
return;
}
unless ( $state{__tweets}{ lc $nick }[$id] ) {
¬ice( [ "tweet" ],
"The text of this tweet isn't saved, sorry!" );
return;
}
my $target = '';
my $got_net = 0;
my $got_target = 0;
while (not $got_target and $data =~ s/^(\S+)\s*//) {
my $arg = $1;
if (not $got_net and lc($arg) ne '-channel' and lc($arg) ne '-nick' and $arg =~ /^-/) {
$got_net = 1;
} else {
if (lc($arg) eq '-channel' or lc($arg) eq '-nick') {
last if not $data =~ s/^(\S+)\s*//;
$arg .= " $1";
}
$got_target = 1;
}
$target .= ($target ne '' ? ' ' : '') . $arg;
}
if (not $got_target) {
¬ice( [ "tweet" ], "Missing target." );
return;
}
my $text = &format_expand(fmt => $settings{retweet_format}, nick => $nick, data => $data,
tweet => &post_process_tweet($state{__tweets}{ lc $nick }[$id], not $settings{rt_to_expand}));
Irssi::command("msg $target $text");
foreach ( $text =~ /@([-\w]+)/g ) {
$nicks{$_} = time;
}
&debug("Retweet of $nick:$id sent to $target");
}
sub cmd_reload {
if ($settings{force_first} and $settings{poll_store}) {
&save_state();
&save_polls();
}
Irssi::command("script load $IRSSI{name}");
}
sub cmd_tweet {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//;
unless ($data) {
¬ice( ["tweet"], "Usage: /tweet <update>" );
return;
}
&cmd_tweet_as( "$user\@$defservice $data", $server, $win );
}
sub cmd_tweet_as {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//;
$data =~ s/\s\s+/ /g;
( my $username, $data ) = split ' ', $data, 2;
unless ( $username and $data ) {
¬ice( ["tweet"], "Usage: /tweet_as <username> <update>" );
return;
}
return unless $username = &valid_username($username);
return unless &logged_in($twits{$username});
$data = &shorten($data);
return if &too_long($data, ['tweet', $username]);
my $success = 1;
my $res;
eval {
unless ( $res = $twits{$username}->update($data) ) {
¬ice( [ "tweet", $username ], "Update failed" );
$success = 0;
}
};
return unless $success;
if ($@) {
&error( [ $username ], "Update caused an error: $@. Aborted." );
return;
}
foreach ( $data =~ /@([-\w]+)/g ) {
$nicks{$_} = time;
}
# TODO: What's the official definition of a Hashtag? Let's use #[-\w]+ like above for now.
if ( $settings{autosearch_results} > 0 and $data =~ /#[-\w]+/ ) {
my @topics;
while ( $data =~ /(#[-\w]+)/g ) {
push @topics, $1;
$search_once{$username}->{$1} = $settings{autosearch_results};
}
&get_updates([ 0, [
[ $username, { up_searches => [ @topics ] } ],
],
]);
}
$state{__last_id}{$username}{__sent} = $res->{id};
my $id_info = ' id=' . $res->{id} if $settings{debug};
my $away_info = '';
if ( $username eq "$user\@$defservice"
and $settings{to_away}
and &update_away($data) ) {
$away_info = " (and away msg set)";
}
¬ice( [ "tweet", $username ], "Update sent" . $away_info . $id_info );
}
sub cmd_broadcast {
my ( $data, $server, $win ) = @_;
my @bcast_users = @{ $settings{broadcast_users} };
@bcast_users = keys %twits if not @bcast_users;
foreach my $buser (@bcast_users) {
&cmd_tweet_as( "$buser $data", $server, $win );
}
}
sub cmd_info {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//g;
unless ( $data ) {
¬ice( ["info"], "Usage: /twitter_info <nick[:num]>" );
return;
}
$data =~ s/[^\w\-:]+//g;
my ( $nick_orig, $id ) = split /:/, $data;
my $nick = lc $nick_orig;
unless ( exists $state{__ids}{ $nick } ) {
¬ice( [ "info" ],
"Can't find any tweet from $nick_orig!" );
return;
}
$id = $state{__indexes}{$nick} unless defined $id;
my $statusid = $state{__ids}{$nick}[$id];
unless ( $statusid ) {
¬ice( [ "info" ],
"Can't find a tweet numbered $id from $nick_orig!" );
return;
}
my $username = $state{__usernames}{$nick}[$id];
my $timestamp = $state{__created_ats}{$nick}[$id];
my $tweet = $state{__tweets}{$nick}[$id];
my $nl = $state{__nls}{$nick}[$id];
my $reply_to_id = $state{__reply_to_ids}{$nick}[$id];
my $reply_to_user = $state{__reply_to_users}{$nick}[$id];
my $exp_tweet = $tweet;
if ($tweet) {
$tweet = &post_process_tweet($tweet, 1);
$exp_tweet = &post_process_tweet($exp_tweet);
}
my $url = '';
if ( defined $username ) {
if ( $username =~ /\@Twitter/ ) {
$url = "http://twitter.com/$nick/statuses/$statusid";
} elsif ( $username =~ /\@Identica/ ) {
$url = "http://identi.ca/notice/$statusid";
}
}
¬ice( [ "info" ], ",--------- $nick:$id" );
¬ice( [ "info" ], "| nick: $nick_orig <http://twitter.com/$nick_orig>" );
¬ice( [ "info" ], "| id: $statusid" . ($url ? " <$url>" : ''));
¬ice( [ "info" ], "| time: " . ($timestamp
? DateTime->from_epoch( epoch => $timestamp, time_zone => $local_tz)
: '<unknown>') );
¬ice( [ "info" ], "| account: " . ($username ? $username : '<unknown>' ) );
for my $line_n (&get_multilines({ text => $tweet, nl => $nl })) {
¬ice( [ "info" ], "| text: " . ($line_n ? $line_n : '<unknown>' ) );
}
¬ice( [ "info" ], "| +url: " . $exp_tweet ) if $exp_tweet ne $tweet;
if ($reply_to_id and $reply_to_user) {
my $reply_to_marker = id_to_marker(lc($reply_to_user), $reply_to_id);
¬ice( [ "info" ], "| ReplyTo: $reply_to_user:$reply_to_marker (id=$reply_to_id)" );
¬ice( [ "info" ], "| thread: http://twitter.theinfo.org/$statusid");
}
¬ice( [ "info" ], "`---------" );
}
sub cmd_reply {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//;
unless ($data) {
¬ice( ["reply"], "Usage: /reply <nick[:num]> <update>" );
return;
}
( my $id, $data ) = split ' ', $data, 2;
unless ( $id and $data ) {
¬ice( ["reply"], "Usage: /reply <nick[:num]> <update>" );
return;
}
&cmd_reply_as( "$user $id $data", $server, $win );
}
sub cmd_reply_as {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//;
( my $username, my $id, $data ) = split ' ', $data, 3;
unless ( $username and $data ) {
¬ice( ["reply"],
"Usage: /reply_as <username> <nick[:num]> <update>" );
return;
}
return unless $username = &valid_username($username);
return unless &logged_in($twits{$username});
my $nick;
$id =~ s/[^\w\d\-:]+//g;
( $nick, $id ) = split /:/, $id;
unless ( exists $state{__ids}{ lc $nick } ) {
¬ice( [ "reply", $username ],
"Can't find a tweet from $nick to reply to!" );
return;
}
$id = $state{__indexes}{lc $nick} unless defined $id;
unless ( $state{__ids}{ lc $nick }[$id] ) {
¬ice( [ "reply", $username ],
"Can't find a tweet numbered $id from $nick to reply to!" );
return;
}
$data = "\@$nick $data";
$data = &shorten($data);
return if &too_long($data, ['reply', $username]);
my $success = 1;
eval {
unless (
$twits{$username}->update(
{
status => $data,
in_reply_to_status_id => $state{__ids}{ lc $nick }[$id]
}
)
) {
¬ice( [ "reply", $username ], "Update failed" );
$success = 0;
}
};
return unless $success;
if ($@) {
¬ice( [ "reply", $username ],
"Update caused an error: $@. Aborted" );
return;
}
foreach ( $data =~ /@([-\w]+)/g ) {
$nicks{$_} = time;
}
my $away = $settings{to_away} ? &update_away($data) : 0;
¬ice( [ "reply", $username ],
"Update sent" . ( $away ? " (and away msg set)" : "" ) );
}
sub gen_cmd {
my ( $usage_str, $api_name, $post_ref, $data_ref ) = @_;
return sub {
my ( $data, $server, $win ) = @_;
return unless &logged_in($twit);
if ($data_ref) {
$data = $data_ref->($data);
}
$data =~ s/^\s+|\s+$//;
unless ($data) {
¬ice("Usage: $usage_str");
return;
}
my $success = 1;
eval {
unless ( $twit->$api_name($data) ) {
¬ice("$api_name failed");
$success = 0;
}
};
return unless $success;
if ($@) {
&error("$api_name caused an error. Aborted: $@");
return;
}
&$post_ref($data, $server, $win) if $post_ref;
}
}
sub cmd_listinfo {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//g;
if ( length $data > 0 ) {
my ($list_user, $list_name) = split(' ', lc $data, 2);
my $list_account = &normalize_username($list_user, 1);
my $list_ac = ($list_account eq "$user\@$defservice" ? '' : "$list_account/");
if (defined $list_name) {
¬ice("Getting list: '$list_ac$list_name'");
} else {
¬ice("Getting all lists for '$list_account'");
}
&get_updates([ 0, [
[ "$user\@$defservice", { up_lists => [ $list_user, $list_name ] } ],
],
]);
} else {
&error( 'Usage: /twitter_listinfo [ <user> [<list name>] ]' );
}
}
sub cmd_search {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//g;
if ( length $data > 0 ) {
my $username = &normalize_username($user);
if ( exists $search_once{$username}->{$data} ) {
¬ice( [ "search", $data ], "Search is already queued" );
return;
}
$search_once{$username}->{$data} = $settings{search_results};
¬ice( [ "search", $data ], "Searching for '$data'" );
&get_updates([ 0, [
[ $username, { up_searches => [ $data ] } ],
],
]);
} else {
¬ice( ["search"], "Usage: /twitter_search <search term>" );
}
}
sub cmd_dms_as {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//g;
( my $username, $data ) = split ' ', $data, 2;
unless ( $username ) {
¬ice( ['dm'], 'Usage: /twitter_dms_as <username>' );
return;
}
return unless $username = &valid_username($username);
return unless &logged_in($twits{$username});
if ( length $data > 0 ) {
&error( 'Usage: /' . ($username eq "$user\@$defservice"
? 'twitter_dms' : 'twitter_dms_as <username>') );
return;
}
¬ice( [ 'dm' ], 'Fetching direct messages' );
&get_updates([ 0, [
[ $username, { up_dms => 1 } ],
],
]);
}
sub cmd_dms {
my ( $data, $server, $win ) = @_;
&cmd_dms_as("$user $data", $server, $win);
}
sub cmd_switch {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//g;
$data = &normalize_username($data);
if ( exists $twits{$data} ) {
¬ice( [ "tweet", $data ], "Switching to $data" );
$twit = $twits{$data};
if ( $data =~ /(.*)\@(.*)/ ) {
$user = $1;
$defservice = $2;
} else {
¬ice( [ "tweet", $data ],
"Couldn't figure out what service '$data' is on" );
}
} else {
¬ice( ["tweet"], "Unknown user $data" );
}
}
sub cmd_logout {
my ( $data, $server, $win ) = @_;
$data =~ s/^\s+|\s+$//g;
$data = $user unless $data;
return unless $data = &valid_username($data);
¬ice( [ "tweet", $data ], "Logging out $data..." );
eval { $twits{$data}->end_session(); };
delete $twits{$data};
delete $last_poll{$data};
undef $twit;
if ( keys %twits ) {
&cmd_switch( ( keys %twits )[0], $server, $win );
} else {
Irssi::timeout_remove($poll_event) if $poll_event;
undef $poll_event;
}
}
sub cmd_login {
my ( $data, $server, $win ) = @_;
my $username;
my $pass;
&debug("logging in: $data");
if ($data) {
( $username, $pass ) = split ' ', $data, 2;
unless ( $settings{use_oauth} or $pass ) {
¬ice( ["tweet"],
"usage: /twitter_login <username>[\@<service>] [<password>*]",
" *required if not using OAUTH" );
return;
}
&debug("%G$username%n manual data login");
} elsif ( $settings{use_oauth} and @{ $settings{usernames} } ) {
&debug("oauth autouser login @{ $settings{usernames} }" );
%nicks = ();
my $some_success = 0;
foreach my $user ( @{ $settings{usernames} } ) {
$some_success = &cmd_login($user);
}
return $some_success;
} elsif ( @{ $settings{usernames} } and @{ $settings{passwords} } ) {
&debug("autouser login");
if ( @{ $settings{usernames} } != @{ $settings{passwords} } ) {
&error( "Number of usernames doesn't match "
. "the number of passwords - auto-login failed" );
return;
} else {
%nicks = ();
my $some_success = 0;
for (my $i = 0; $i < @{ $settings{usernames} }; $i++) {
$some_success ||= &cmd_login("$settings{usernames}->[$i] $settings{passwords}->[$i]");
}
return $some_success;
}
} else {
&error( "/twitter_login requires either a username/password "
. "or twitter_usernames and twitter_passwords to be set. "
. "Note that if twirssi_use_oauth is true, passwords are "
. "not required" );
return;
}
$username = &normalize_username($username, 1);
( $user, $defservice ) = split('@', $username, 2);
$state{__lists}{$username} = {};
$blocks{$username} = {};
$friends{$username} = {};
if ( $settings{use_oauth} ) {
&debug("%G$username%n Attempting OAuth to $defservice");
eval {
if ( $defservice eq 'Identica' ) {
$twit = Net::Twitter->new(
($defservice eq 'Identica' ? ( identica => 1 ) : ()),
traits => [ 'API::REST', 'API::Search' ],
source => "twirssi", # XXX
ssl => !$settings{avoid_ssl},
);
} else {
$twit = Twitter::API->new_with_traits(
traits => [ qw/ Migration ApiMethods RetryOnError / ],
(
grep tr/a-zA-Z/n-za-mN-ZA-M/, map $_,
pbafhzre_xrl => 'OMINiOzn4TkqvEjKVioaj',
pbafhzre_frperg =>
'0G5xnujYlo34ipvTMftxN9yfwgTPD05ikIR2NCKZ',
),
source => "twirssi", # XXX
ssl => !$settings{avoid_ssl},
);
}
};
if ($@) {
&error( "Error when creating object: $@" );
}
if ($twit) {
if ( open( my $oa_fh, '<', $settings{oauth_store} ) ) {
while (<$oa_fh>) {
chomp;
next unless /^$username (\S+) (\S+)/i;
&debug("%G$username%n Trying cached oauth creds");
$twit->access_token($1);
$twit->access_token_secret($2);
last;
}
close $oa_fh;
}
# leave undefined if authorized
my $authorize_url;
my %req_tokes = ();
if ( ref($twit) =~ /Twitter::API/
and not ($twit->has_access_token and $twit->has_access_token_secret ) ) {
my $oauth_ref;
eval { $oauth_ref = $twit->oauth_request_token(); };
if ($@) {
&error( "Failed to get oauth_request_token: $@" );
return;
}
if (not $oauth_ref->{oauth_token} or not $oauth_ref->{oauth_token_secret}) {
&error( "Failed to return oauth_token*" );
return;