-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathviewlog.pl
86 lines (66 loc) · 1.71 KB
/
viewlog.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
use common::sense;
use Xchat;
use POSIX 'strftime';
use File::Spec;
my $default_viewer = 'notepad';
Xchat::register 'Viewlog', '1.00', 'Opens the log file of the currect context.';
my $logmask = Xchat::get_prefs 'irc_logmask';
Xchat::hook_command 'viewlog', \&viewlog, { 'help_text' => 'Usage: viewlog [viewer] - Opens the log file of the currect context.' };
sub viewlog {
my $viewer = $_[0][1] // $default_viewer;
my $network = Xchat::get_info 'network';
my $server = Xchat::get_info 'server';
my $channel = Xchat::get_info 'channel';
#according to text.c:log_create_pathname
if (!defined $network) {
$network = 'NETWORK'
}
else {
$network = hexchat_lc($network)
}
if (!Xchat::nickcmp($channel, $server)) {
$channel = 'server';
}
else {
$channel = hexchat_lc($channel);
}
my $logmask = $logmask;
for ($logmask) {
s/%s/$server/g;
s/%c/$channel/g;
s/%n/$network/g;
}
my $filename = File::Spec->rel2abs(
strftime($logmask, localtime),
File::Spec->catdir(Xchat::get_info('xchatdirfs'), 'logs')
);
if(-f $filename) {
display_file($filename, $viewer);
}
else {
Xchat::command("GUI MSGBOX \"Log file for the current channel/dialog does not seem to exist. ($filename)\"");
}
return Xchat::EAT_ALL;
}
sub hexchat_lc {
my ($channel) = @_;
if ($^O eq 'MSWin32') {
$channel =~ s/[\\|\/><:"*?]/_/g;
}
else {
#almost according to rfc2812, except for ^ -> ~
#blame (he)xchat
$channel =~ tr/A-Z[]\\^/a-z{}|~/;
}
return $channel;
}
sub display_file {
my ($filename, $viewer) = @_;
if ($viewer =~ m/ / && $viewer !~ m/^".*"$/) {
$viewer = "\"$viewer\""
}
if ($^O eq 'MSWin32') {
return system "start \"\" $viewer \"$filename\"";
}
return system "$viewer \"$filename\" &"
}