diff options
| author | David Phillips <david@sighup.nz> | 2017-05-21 20:06:53 +1200 | 
|---|---|---|
| committer | David Phillips <david@sighup.nz> | 2017-05-21 20:06:53 +1200 | 
| commit | 4b34881adc9c508c59b4461f89708e300d04a34e (patch) | |
| tree | fe82ef83f36423978cfb94ec3c035eb4badc2eff /plugin | |
| parent | 105d7a3d91cc51db0096e81091979c4e760a46f1 (diff) | |
| download | idalius-4b34881adc9c508c59b4461f89708e300d04a34e.tar.xz | |
Separate functionalities into modules
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/tittilate.pm | 36 | ||||
| -rw-r--r-- | plugin/url_title.pm | 62 | 
2 files changed, 98 insertions, 0 deletions
diff --git a/plugin/tittilate.pm b/plugin/tittilate.pm new file mode 100644 index 0000000..a1ea7f3 --- /dev/null +++ b/plugin/tittilate.pm @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +package plugin::tittilate; + +use strict; +use warnings; + +my %config; + +sub configure { +	my $self = $_[0]; +	my $cref = $_[1]; +	%config = %$cref; +	return $self; +} + +sub message { +	my ($self, $me, $who, $where, $what) = @_; +	my $gathered = ""; +	my @expressions = (keys %{$config{triggers}}); +	my $expression = join '|', @expressions; +	while ($what =~ /($expression)/gi) { +		my $matched = $1; +		my $key; +		# figure out which key matched +		foreach (@expressions) { +			if ($matched =~ /$_/i) { +				$key = $_; +				last; +			} +		} +		$gathered .= $config{triggers}->{$key}; +	} +	return $gathered; +} +1; diff --git a/plugin/url_title.pm b/plugin/url_title.pm new file mode 100644 index 0000000..2a2258b --- /dev/null +++ b/plugin/url_title.pm @@ -0,0 +1,62 @@ +#!/usr/bin/env perl + +package plugin::url_title; + +use strict; +use warnings; +use HTTP::Tiny; +use HTML::HeadParser; + +my %config; + +sub configure { +	my $self = $_[0]; +	my $cref = $_[1]; +	%config = %$cref; +	return $self; +} + +sub message +{ +	my ($self, $me, $who, $where, $what) = @_; +	my $url; + +	if ($what =~ /(https?:\/\/[^ ]+)/i) { +		$url = $1; +	} +	return unless $url; + +	my $http = HTTP::Tiny->new((default_headers => {accept => 'text/html'}, timeout => 5)); + +	my $response = $http->get($url); + +	if (!$response->{success}) { +		print "Something broke: $response->{reason}\n"; +		return; +	} + +	if (!($response->{headers}->{"content-type"} =~ m,text/html ?,)) { +		print("Not html, giving up now\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}; + +	# remove http(s):// to avoid triggering other poorly configured bots +	$shorturl =~ s,^https?://,,g; +	$shorturl =~ s,/$,,g; + +	my $composed_title = "$title ($shorturl)"; +	return $composed_title; +} +1;  | 
