aboutsummaryrefslogtreecommitdiffstats
path: root/.irssi/scripts/bitlbee_timestamp.pl
blob: 7957d579766aa4e16863bcecd42f500a8c7819fd (plain)
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
use strict;
use Data::Dumper;
use vars qw($VERSION %IRSSI);
use DateTime;

$VERSION = '0.5';
%IRSSI = (
    authors	=> 'Tijmen "timing" Ruizendaal',
    contact	=> 'tijmen.ruizendaal@gmail.com',
    name	=> 'bitlbee_timestamp',
    description	=> 'Replace Irssi\'s timestamps with those sent by BitlBee',
    license	=> 'GPLv2',
    url		=> 'http://the-timing.nl/stuff/irssi-bitlbee',
    changed	=> '2010-05-01',
);

my $tf = Irssi::settings_get_str('timestamp_format');

my $bitlbee_server; # server object
my @control_channels; # mostly: &bitlbee, &facebook etc.
init();

sub init { # if script is loaded after connect
	my @servers = Irssi::servers();
	foreach my $server(@servers) {
		if( $server->isupport('NETWORK') eq 'BitlBee' ){
			$bitlbee_server = $server;
			my @channels = $server->channels();
			foreach my $channel(@channels) {
				if( $channel->{mode} =~ /C/ ){
					push @control_channels, $channel->{name} unless (grep $_ eq $channel->{name}, @control_channels);
				}
			}
		}
	}
}
# if connect after script is loaded
Irssi::signal_add_last('event 005' => sub {
	my( $server ) = @_;
	if( $server->isupport('NETWORK') eq 'BitlBee' ){
		$bitlbee_server = $server;
	}
});
# if new control channel is synced after script is loaded
Irssi::signal_add_last('channel sync' => sub {
	my( $channel ) = @_;
	if( $channel->{mode} =~ /C/ && $channel->{server}->{tag} eq $bitlbee_server->{tag} ){
		push @control_channels, $channel->{name} unless (grep $_ eq $channel->{name}, @control_channels);
	}
});

my $prev_date = '';

sub privmsg {
	my ($server, $data, $nick, $address) = @_;

	# What we need to match: ^B[^B^B^B2010-03-21 16:33:41^B]^B

	if( $server->{tag} eq $bitlbee_server->{tag} ){
	
		my ($target, $text) = split(/ :/, $data, 2);

		#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/ ){

		if( $text =~ /\x02\[\x02\x02\x02.*\x02\]\x02/ ){
			my $window;
			my $timestamp = $text;
			my $time;
			my $date;
			$timestamp =~ s/.*\x02\[\x02\x02\x02(.*?)\x02\]\x02.*/$1/g;
			$text =~ s/\x02\[\x02\x02\x02(.*?)\x02\]\x02 //g;

			($date, $time) = split(/ /, $timestamp);
			if( !$time ){ # the timestamp doesn't have a date
				$time = $date;
				# use today as date
				$date = DateTime->now->ymd;
			}

			if( $date ne $prev_date ){
				if( $target =~ /#|&/ ){ # find channel window based on target
					$window = Irssi::window_find_item($target);
				} else { # find query window based on nick
					$window = Irssi::window_find_item($nick);
				}
				if( $window != undef ){
					my($year, $month, $day) = split(/-/, $date);
					my $dt = DateTime->new(year => $year, month => $month, day => $day);
					my $formatted_date = $day.' '.$dt->month_abbr.' '.$year;
					
					$window->print('Day changed to '.$formatted_date, MSGLEVEL_NEVER);
				}
			}
			$prev_date = $date;
			
			Irssi::settings_set_str('timestamp_format', $time);
			Irssi::signal_continue($server, $target . ' :' . $text, $nick, $address);
			my $escaped = $tf;
			$escaped =~ s/%/%%/g;
			Irssi::settings_set_str('timestamp_format', $tf);
		}
	}
}

Irssi::signal_add('event privmsg', 'privmsg');