html/bitlbee_timestamp.pl
1 use strict;
2 use Data::Dumper;
3 use vars qw($VERSION %IRSSI);
4 use DateTime;
5
6 $VERSION = '0.5';
7 %IRSSI = (
8 authors => 'Tijmen "timing" Ruizendaal',
9 contact => 'tijmen.ruizendaal@gmail.com',
10 name => 'bitlbee_timestamp',
11 description => 'Replace Irssi\'s timestamps with those sent by BitlBee',
12 license => 'GPLv2',
13 url => 'http://the-timing.nl/stuff/irssi-bitlbee',
14 changed => '2010-05-01',
15 );
16
17 my $tf = Irssi::settings_get_str('timestamp_format');
18
19 my $bitlbee_server; # server object
20 my @control_channels; # mostly: &bitlbee, &facebook etc.
21 init();
22
23 sub init { # if script is loaded after connect
24 my @servers = Irssi::servers();
25 foreach my $server(@servers) {
26 if( $server->isupport('NETWORK') eq 'BitlBee' ){
27 $bitlbee_server = $server;
28 my @channels = $server->channels();
29 foreach my $channel(@channels) {
30 if( $channel->{mode} =~ /C/ ){
31 push @control_channels, $channel->{name} unless (grep $_ eq $channel->{name}, @control_channels);
32 }
33 }
34 }
35 }
36 }
37 # if connect after script is loaded
38 Irssi::signal_add_last('event 005' => sub {
39 my( $server ) = @_;
40 if( $server->isupport('NETWORK') eq 'BitlBee' ){
41 $bitlbee_server = $server;
42 }
43 });
44 # if new control channel is synced after script is loaded
45 Irssi::signal_add_last('channel sync' => sub {
46 my( $channel ) = @_;
47 if( $channel->{mode} =~ /C/ && $channel->{server}->{tag} eq $bitlbee_server->{tag} ){
48 push @control_channels, $channel->{name} unless (grep $_ eq $channel->{name}, @control_channels);
49 }
50 });
51
52 my $prev_date = '';
53
54 sub privmsg {
55 my ($server, $data, $nick, $address) = @_;
56
57 # What we need to match: ^B[^B^B^B2010-03-21 16:33:41^B]^B
58
59 if( $server->{tag} eq $bitlbee_server->{tag} ){
60
61 my ($target, $text) = split(/ :/, $data, 2);
62
63 #if( $text =~ /^B[^B^B^B[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}^B]^B/ ){
64
65 if( $text =~ /\x02\[\x02\x02\x02.*\x02\]\x02/ ){
66 my $window;
67 my $timestamp = $text;
68 my $time;
69 my $date;
70 $timestamp =~ s/.*\x02\[\x02\x02\x02(.*?)\x02\]\x02.*/$1/g;
71 $text =~ s/\x02\[\x02\x02\x02(.*?)\x02\]\x02 //g;
72
73 ($date, $time) = split(/ /, $timestamp);
74 if( !$time ){ # the timestamp doesn't have a date
75 $time = $date;
76 # use today as date
77 $date = DateTime->now->ymd;
78 }
79
80 if( $date ne $prev_date ){
81 if( $target =~ /#|&/ ){ # find channel window based on target
82 $window = Irssi::window_find_item($target);
83 } else { # find query window based on nick
84 $window = Irssi::window_find_item($nick);
85 }
86 if( $window != undef ){
87 my($year, $month, $day) = split(/-/, $date);
88 my $dt = DateTime->new(year => $year, month => $month, day => $day);
89 my $formatted_date = $day.' '.$dt->month_abbr.' '.$year;
90
91 $window->print('Day changed to '.$formatted_date, MSGLEVEL_NEVER);
92 }
93 }
94 $prev_date = $date;
95
96 Irssi::settings_set_str('timestamp_format', $time);
97 Irssi::signal_continue($server, $target . ' :' . $text, $nick, $address);
98 my $escaped = $tf;
99 $escaped =~ s/%/%%/g;
100 Irssi::settings_set_str('timestamp_format', $tf);
101 }
102 }
103 }
104
105 Irssi::signal_add('event privmsg', 'privmsg');