-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtext-event-regex-replace.pl
64 lines (50 loc) · 1.14 KB
/
text-event-regex-replace.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
use common::sense;
use Xchat;
my @rules = (
#highlight urls
{
'pattern' => qr/\b\Khttps?:\/\/[^ ]+|\b\K(?:(?<!https:\/\/)(?<!http:\/\/)[\da-z-]+\.)+[a-z]{2,}(?=\b)/io,
'replacement' => sub { format_text("%B$1%B") },
},
);
my @events = (
'Channel Message',
'Channel Msg Hilight',
'Private Message to Dialog',
'Channel Action',
'Channel Action Hilight',
'Private Action to Dialog',
'Your Action',
'Your Message',
#'Part with Reason',
#'Quit',
);
Xchat::register 'Text event regex replace', '1.00', 'Regex substitutions for text events.';
Xchat::hook_print $_, \&check, { 'data' => $_ } for @events;
sub check {
my ($word, $event) = @_;
my $matched = 0;
for my $w (@$word) {
for (@rules) {
if ($w =~ s/($_->{'pattern'})/$_->{'replacement'}->()/ge) {
$matched = 1;
}
}
}
if ($matched) {
Xchat::emit_print $event, @$word;
return Xchat::EAT_ALL;
}
return Xchat::EAT_NONE;
}
sub format_text {
my ($string) = @_;
$string =~ s/%B/\x02/g;
$string =~ s/%C/\x03/g;
$string =~ s/%H/\x08/g;
$string =~ s/%O/\x0f/g;
$string =~ s/%R/\x16/g;
$string =~ s/%I/\x1d/g;
$string =~ s/%U/\x1f/g;
return $string;
}