-
Notifications
You must be signed in to change notification settings - Fork 1
/
ekg2_urlshort.pl
124 lines (110 loc) · 3.22 KB
/
ekg2_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
124
# script for ekg2 that shortens long URLs
# https://github.com/Enlik/ShortIsGd
# by Enlik
use Ekg2;
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
# /cut help for help
our $VERSION = "0.4";
our %EKG2 = (
authors => "Enlik",
contact => "poczta-sn*gazeta.pl",
description => "URL shortener",
license => "MIT",
changed => "2012-02-18"
);
# remember 9 last URLs per sender
our %nickurl = ();
sub add_url {
# $nick is sender's "proto:id" string (can be irc:channel) or a nick
# $url is a long URL
my ($nick, $url) = @_;
my @urls = ();
@urls = @{$nickurl{$nick}} if defined $nickurl{$nick};
push @urls, $url;
if (@urls > 9) {
splice @urls, 0, 1;
}
$nickurl{$nick} = [ @urls ];
}
sub print_url_for_window {
my ($w, $max) = @_;
# my $ul = Ekg2::Window::userlist ($w); --> no luck
my $ul = Ekg2::Session::userlist ($w->{session});
my $user = Ekg2::Userlist::find ($ul, $w->{target});
my $USER = defined $user ? $user->{nickname} : $w->{target};
my %reply;
my $base_url = "";
if ($nickurl{$USER} and @{$nickurl{$USER}}) {
my @urls = @{$nickurl{$USER}};
# 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}) {
Ekg2::Window::print_format ($w, "urlshort_print_url",
$base_url, $reply{url});
}
elsif ($reply{err_text}) {
Ekg2::Window::print_format ($w, "generic2",
"[$base_url] -> error: " . $reply{err_text});
}
else {
Ekg2::Window::print_format ($w, "generic_error",
$base_url . " -> full URL: [$url]: something wrong has " .
"occured. Please file a bug providing this message.");
}
}
}
else {
Ekg2::Window::print_format ($w, "generic2", "No URLs here.");
}
}
sub msg_handler {
my ($session, $sender, $rcpt, $text) = @_;
return 1 if ($$session eq $$sender);
# \x{1b} is Esc control char. Hello IRC plugin!
my $R = qr{(?:^| )((?:https?://)[\w\d:#@%/,;$()!~_\?\+-=\.&\|-]+)(?= |\x{1b}|$)}m;
my $url;
my $USER;
while ($$text =~ /$R/g) {
$url = $1;
# find $USER when needed, and only once
if (!defined $USER) {
my $ses = Ekg2::session_find($$session);
my $ul = Ekg2::Session::userlist($ses);
my $user = Ekg2::Userlist::find($ul, $$sender);
$USER = defined $user ? $user->{nickname} : $$sender;
}
add_url ($USER, $url);
}
1;
}
sub cmd_handler {
my ($cmd, $args) = @_;
$args = "" unless defined $args; # maybe always defined, but it won't hurt
return unless $cmd and $cmd eq "cut";
my $w = Ekg2::window_current();
if ($args eq "help") {
Ekg2::Window::print ($w, "/cut displays the last URL shortened.");
Ekg2::Window::print ($w, "/cut <number> displays the last <number> " .
"URLs shortened (maximum 9)");
}
elsif ($args =~ /^[0-9]?$/) {
$args = 1 if $args eq "";
print_url_for_window ($w, $args);
}
else {
Ekg2::Window::print ($w, "try /cut help");
}
}
Ekg2::format_add("urlshort_print_url", "%> %T[%1]%n => %G%2%n");
Ekg2::handler_bind('protocol-message', 'msg_handler');
Ekg2::command_bind('cut', 'cmd_handler');
return 1;