aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2017-04-18 21:09:08 +1200
committerDavid Phillips <david@sighup.nz>2017-04-18 21:21:46 +1200
commitd95fa59541aaa4ea34f3188ef895e5fc5ed1f08a (patch)
treeb285dd112e784d92af3d842aa619a870d63b903b
parentfc701ff151b8826aac23aab77493718d1836c492 (diff)
downloadidalius-d95fa59541aaa4ea34f3188ef895e5fc5ed1f08a.tar.xz
Make URL titles optional, enable in config
-rw-r--r--bot.conf.example5
-rw-r--r--config_file.pm4
-rwxr-xr-xidalius.pl77
3 files changed, 50 insertions, 36 deletions
diff --git a/bot.conf.example b/bot.conf.example
index cd5ae2d..71230bd 100644
--- a/bot.conf.example
+++ b/bot.conf.example
@@ -13,3 +13,8 @@ quit_msg = "Goodbye!"
user = nobody
group = nobody
triggers = 'sa+x' => '🎷 ', 'trumpet' => '🎺 ', 'snake' => '🐍 '
+
+# URL stuff
+url_on = 1
+url_len = 0
+
diff --git a/config_file.pm b/config_file.pm
index fe23b89..feb2c9f 100644
--- a/config_file.pm
+++ b/config_file.pm
@@ -19,7 +19,9 @@ sub parse_config
'must_id',
'quit_msg',
'user',
- 'group');
+ 'group',
+ 'url_on',
+ 'url_len');
my @list_configs = (
'channels',
'ignore',
diff --git a/idalius.pl b/idalius.pl
index 6bd9bfd..1a81917 100755
--- a/idalius.pl
+++ b/idalius.pl
@@ -18,6 +18,8 @@ $| = 1;
my $current_nick = $config{nick};
+# Hack: coerce into numeric type
++$config{url_on};
+$config{url_len};
# New PoCo-IRC object
@@ -62,6 +64,42 @@ sub drop_priv {
setuid($config{uid}) or die "Failed to setuid: $!\n";
}
+sub url_get_title
+{
+ my $url = $_[0];
+ my $response = HTTP::Tiny->new((timeout => 5))->head($url);
+ if (!$response->{success}) {
+ print "Something broke: $response->{content}\n";
+ return;
+ }
+
+ if (!$response->{headers}->{"content-type"} =~ m,text/html ?,) {
+ print("Not html, giving up now");
+ return;
+ }
+
+ $response = HTTP::Tiny->new((timeout => 5))->get($url);
+ if (!$response->{success}) {
+ print "Something broke: $response->{content}\n";
+ return;
+ }
+
+ my $html = $response->{content};
+
+ my $parser = HTML::HeadParser->new;
+ $parser->parse($html);
+
+ # get title and unpack from utf8 (assumption)
+ my $title = $parser->header("title");
+ return unless $title;
+
+ my $shorturl = $url;
+ $shorturl = (substr $url, 0, $config{url_len}) . "…" if length ($url) > $config{url_len};
+
+ my $composed_title = "$title ($shorturl)";
+ return $composed_title;
+}
+
sub _start {
my $heap = $_[HEAP];
my $irc = $heap->{irc};
@@ -114,44 +152,13 @@ sub irc_public {
my $me = $irc->nick_name;
- # Parse urls in all other regular messages
- if ($what =~ /(https?:\/\/[^ ]+)/i)
+ if ($config{url_on} and $what =~ /(https?:\/\/[^ ]+)/i)
{
- my $url = $1;
- my $response = HTTP::Tiny->new((timeout => 5))->head($url);
- if (!$response->{success}) {
- print "Something broke: $response->{content}\n";
- return;
- }
-
- if (!$response->{headers}->{"content-type"} =~ m,text/html ?,) {
- print("Not html, giving up now");
- return;
- }
-
- $response = HTTP::Tiny->new((timeout => 5))->get($url);
- if (!$response->{success}) {
- print "Something broke: $response->{content}\n";
- return;
- }
-
- my $html = $response->{content};
-
- my $parser = HTML::HeadParser->new;
- $parser->parse($html);
-
- # get title and unpack from utf8 (assumption)
- my $title = $parser->header("title");
- return unless $title;
-
- my $shorturl = $url;
- $shorturl = (substr $url, 0, $config{url_len}) . "…" if length ($url) > $config{url_len};
-
- print "Title: $title ($url)\n";
- $irc->yield(privmsg => $channel => "$title ($shorturl)");
+ my $title = url_get_title($1);
+ print "Title: $title\n";
+ $irc->yield(privmsg => $channel => $title);
}
-
my $gathered = "";
my @expressions = (keys %{$config{triggers}});
my $expression = join '|', @expressions;