From 11dae6b927b5faee00ecd51fe6115f73c6773e1b Mon Sep 17 00:00:00 2001 From: Elijah Perrault Date: Fri, 18 Feb 2011 14:54:49 -0700 Subject: [PATCH] Added a LinkTitle module for getting the title of a web page posted in a channel. This is of little use, but meh. --- doc/CHANGELOG | 3 +- modules/LinkTitle.pm | 114 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 modules/LinkTitle.pm diff --git a/doc/CHANGELOG b/doc/CHANGELOG index 5595a5a..fe6953b 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -3,7 +3,8 @@ Auto IRC Bot 3.0: Change Log 3.0 Indev =============================================================================== - + * Added a LinkTitle module for getting the title of a web page posted in a + channel. 3.0 Alpha 4 =============================================================================== diff --git a/modules/LinkTitle.pm b/modules/LinkTitle.pm new file mode 100644 index 0000000..41a34d0 --- /dev/null +++ b/modules/LinkTitle.pm @@ -0,0 +1,114 @@ +# Module: LinkTitle. See below for documentation. +# Copyright (C) 2010-2011 Xelhua Development Group, et al. +# This program is free software; rights to this code are stated in doc/LICENSE. +package M::LinkTitle; +use strict; +use warnings; +use LWP::UserAgent; +use API::Std qw(hook_add hook_del); +use API::IRC qw(privmsg); + +# Initialization subroutine. +sub _init +{ + # Create the on_cprivmsg hook. + hook_add('on_cprivmsg', 'privmsg.html.returntitle', \&M::LinkTitle::gettitle) or return; + + # Success. + return 1; +} + +# Void subroutine. +sub _void +{ + # Delete the hook we created. + hook_del('on_cprivmsg', 'privmsg.html.returntitle') or return; + + # Success. + return 1; +} + +# Hook callback. +sub gettitle +{ + my ($src, $chan, @msg) = @_; + + # Check if the message contains a URL. + foreach my $smw (@msg) { + if ($smw =~ m{(http|https)://}xsm) { + # We've got a match, connect to the server. + my $srv = $1; + # Create an instance of LWP::UserAgent. + my $ua = LWP::UserAgent->new(); + $ua->agent('Auto IRC Bot'); + $ua->timeout(3); + # Get data. + my $res = $ua->get($smw); + + # Check if we're successful. + if ($res->is_success) { + # We were, decode the data. + my $data = $res->decoded_content; + + # Check for + if ($data =~ m{<title>(.*)}ixsm) { + # Found. Return it to channel. + privmsg($src->{svr}, $chan, "\2Title:\2 $1"); + } + } + } + } + + return 1; +} + +# Start initialization. +API::Std::mod_init('LinkTitle', 'Xelhua', '1.00', '3.0.0a5', __PACKAGE__); +# vim: set ai sw=4 ts=4: +# build: cpan=LWP::UserAgent perl=5.010000 + +__END__ + +=head1 NAME + +LinkTitle - A module for returning the page title of links. + +=head1 VERSION + +Version 1.00. + +=head1 SYNOPSIS + + http://xelhua.org/auto.php + Title: Xelhua / Projects / Auto + +=head1 DESCRIPTION + +This module will make Auto parse all links sent to a channel. When a link is +detected, Auto will connect to it and get the page title by scanning for the + tag and returning its contents to the channel. + +=head1 DEPENDENCIES + +This module is dependent on one module from the CPAN. + +=over + +=item L<LWP::UserAgent|LWP::UserAgent> + +This module is used for connecting to the target web server via HTTP(S). + +=back + +=head1 AUTHOR + +This module was written by Elijah Perrault. + +This module is maintained by Xelhua Development Group. + +=head1 LICENSE AND COPYRIGHT + +This module is Copyright 2010-2011 Xelhua Development Group. All rights +reserved. + +This module is released under the same licensing terms as Auto itself.