forked from arkeet/irssi-server-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_time.pl
95 lines (79 loc) · 2.76 KB
/
server_time.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
use strict;
use Irssi;
use DateTime;
use DateTime::TimeZone;
our $VERSION = '1.0';
our %IRSSI = (
authors => 'Adrian Keet & John Sullivan',
contact => '[email protected]',
name => 'server_time',
description => 'Implements the IRCv3 "server-time" capability',
license => 'MIT',
url => 'https://github.com/itsjohncs/irssi-server-time',
);
sub parse_servertime {
my ($servertime, ) = @_;
# Matches exactly YYYY-MM-DDThh:mm:ss.sssZ as specified in the server time spec
my ($year, $month, $day, $hour, $minute, $second, $milliseconds) =
($servertime =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})Z$/);
if ($year) {
return DateTime->new(
year => $year,
month => $month,
day => $day,
hour => $hour,
minute => $minute,
second => $second,
nanosecond => $milliseconds * 1000000,
time_zone => "UTC",
);
} else {
return undef;
}
}
# Parse the @time tag on a server message
sub server_incoming {
my ($server, $line) = @_;
if ($line =~ /^\@time=([\S]*)\s+(.*)$/) {
my $servertime = $1;
$line = $2;
my $tz = DateTime::TimeZone->new(name => 'local');
my $ts = parse_servertime($servertime);
unless ($ts) {
Irssi::print("Badly formatted servertime: $servertime");
return;
}
$ts->set_time_zone($tz);
my $orig_format = Irssi::settings_get_str('timestamp_format');
my $format = $orig_format;
# Prepend the date if it differs from the current date.
my $now = DateTime->now();
$now->set_time_zone($tz);
if ($ts->ymd() ne $now->ymd()) {
$format = '[%F] ' . $format;
}
my $timestamp = $ts->strftime($format);
Irssi::settings_set_str('timestamp_format', $timestamp);
Irssi::signal_emit('setup changed');
Irssi::signal_continue($server, $line);
Irssi::settings_set_str('timestamp_format', $orig_format);
Irssi::signal_emit('setup changed');
}
}
# Request the server-time capability during capability negotiation
sub event_cap {
my ($server, $args, $nick, $address) = @_;
if ($args =~ /^\S+ (\S+) :(.*)$/) {
my $subcmd = uc $1;
if ($subcmd eq 'LS') {
my @servercaps = split(/\s+/, $2);
my @caps = grep {$_ eq 'server-time' or $_ eq 'znc.in/server-time-iso'} @servercaps;
my $capstr = join(' ', @caps);
if (!$server->{connected}) {
$server->send_raw_now("CAP REQ :$capstr");
}
}
}
}
Irssi::signal_add_first('server incoming', \&server_incoming);
Irssi::signal_add('event cap', \&event_cap);