-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweechat_urlshort.pl
123 lines (108 loc) · 3.74 KB
/
weechat_urlshort.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# script for weechat that shortens long URLs
# https://github.com/Enlik/ShortIsGd
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ----
# by Enlik
use warnings;
use strict;
use ShortIsGd;
# installs command /cut
# /cut prints shortened version of last URL
# /cut NUMBER prints shortened version of NUMBER of last URLs
# /help cut prints help
weechat::register("urlshort", "Enlik", "0.1.1", "MIT",
"Shortens long URLs on demand", "", "");
weechat::hook_print ("", "", "://", 1, "print_cb", "");
weechat::hook_command ("cut", "/cut displays the last URL shortened. " .
"/cut <num> displays the last <number> URLs shortened (maximum 9).",
"", "", "", "cmd_cb", "");
# remember 9 last URLs per sender (buffer)
our %nickurl = ();
sub add_url {
# $id: buffer name
# $url is a long URL
my ($id, $url) = @_;
my @urls = ();
@urls = @{$nickurl{$id}} if defined $nickurl{$id};
push @urls, $url;
if (@urls > 9) {
splice @urls, 0, 1;
}
$nickurl{$id} = [ @urls ];
}
sub print_url_for_window {
my ($w, $max) = @_;
my $id = weechat::buffer_get_string($w, "name");
my %reply;
my $base_url = "";
my $bold = weechat::color ("bold");
my $green = weechat::color ("green");
my $deflt = weechat::color ("chat");
my $red = weechat::color ("red");
if ($nickurl{$id} and @{$nickurl{$id}}) {
my @urls = @{$nickurl{$id}};
# in order
my $begin = @urls - $max;
$begin = 0 if $begin < 0;
for my $url (@urls[$begin .. $#urls]) { # argh I tend to write a comma
$base_url = $1 if $url =~ m!^https?://([^/]+)!;
%reply = ShortIsGd::shorten($url);
if ($reply{url}) {
weechat::print ($w, "${bold}[$base_url] ${deflt}=> ${green}" .
$reply{url});
}
elsif ($reply{err_text}) {
weechat::print ($w, "${bold}[$base_url] ${deflt}-> ${red}error: " .
$reply{err_text});
}
else {
weechat::print ($w, "${red}-> full URL: [$url]: something wrong " .
"has occured. Please file a bug providing this message.");
}
}
}
else {
my $bold = weechat::color ("bold");
weechat::print ($w, "${bold}No URLs here.");
}
}
sub print_cb {
my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message)
= @_;
my %tags = map { $_ => 1 } split /,/, $tags;
return weechat::WEECHAT_RC_OK
unless ($tags{notify_message} or $tags{notify_private});
# just a note: $message contains nick
my $R = qr{(?:^| )((?:https?://)[\w\d:#@%/,;$()!~_\?\+-=\.&\|-]+)(?= |$)}m;
while ($message =~ /$R/g) {
add_url (weechat::buffer_get_string($buffer, "name"), $1);
}
weechat::WEECHAT_RC_OK;
}
sub cmd_cb {
my ($data, $buffer, $args) = @_;
$args = "" unless defined $args; # maybe always defined, but it won't hurt
if ($args =~ /^[0-9]?$/) {
$args = 1 if $args eq "";
print_url_for_window ($buffer, $args);
}
else {
weechat::print ($buffer, "try /help cut");
}
}