File Coverage

blib/lib/Bot/BasicBot/Pluggable/Module/HTTPIRCGW.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Bot::BasicBot::Pluggable::Module::HTTPIRCGW;
2 1     1   64334 use strict;
  1         4  
  1         41  
3 1     1   7 use warnings;
  1         2  
  1         30  
4 1     1   6 use Carp;
  1         7  
  1         97  
5 1     1   7 use vars qw($VERSION);
  1         3  
  1         79  
6             $VERSION = '0.012';
7              
8 1     1   665 use Bot::BasicBot::Pluggable::Module;
  0            
  0            
9             use base qw(Bot::BasicBot::Pluggable::Module);
10             use LWP::UserAgent;
11             use URI::Escape;
12              
13             =head1 NAME
14              
15             Bot::BasicBot::Pluggable::Module::HTTPIRCGW - A Simple HTTP Action for Bot::BasicBot::Pluggable
16              
17             =head1 SYNOPSIS
18              
19             use Bot::BasicBot::Pluggable::Module::Delicious
20              
21             my $bot = Bot::BasicBot::Pluggable->new(...);
22              
23             $bot->load("HTTPIRCGW");
24             my $HttpIrcGw_handler = $bot->handler("HTTPIRCGW");
25             $HttpIrcGw_handler->set($action_file);
26              
27             here is an exmple of the action file:
28             ^!(fnord)$ # GET=>http://xxx.xxx/fnordtune.php # sub{$web_out=~s/\r\n//g;}
29             ^!todo # POST=>http://xxx.xx/wiki/?add_todoTNOnick=$who&text=$body # sub{$web_out = "task added";}
30            
31             # are delimiters
32             first there is a regex for a command
33             the action, GET or POST, with the url, in the case of a POST, TNO is the separator
34             then a sub with what to do (parsing, result), in the var "$web_out"
35              
36             =head1 DESCRIPTION
37              
38             A plugin module for L to perform HTTP actions
39              
40             =head1 USAGE
41              
42              
43              
44             =head1 BUGS
45              
46              
47              
48             =head1 SUPPORT
49              
50              
51              
52             =head1 AUTHOR
53              
54             Franck Cuny
55             CPAN ID: FRANCKC
56             tirnanog
57             franck@breizhdev.net
58            
59              
60             =head1 COPYRIGHT
61              
62             This program is free software; you can redistribute
63             it and/or modify it under the same terms as Perl itself.
64              
65             The full text of the license can be found in the
66             LICENSE file included with this module.
67              
68              
69             =head1 SEE ALSO
70              
71             =over 4
72              
73             =item * L
74              
75             =item * L
76              
77             =back
78              
79             =cut
80              
81             sub init {
82             my $self = shift;
83             }
84              
85             sub said {
86             my ($self, $mess, $pri) = @_;
87              
88             return unless $pri == 2;
89              
90             my $body = $mess->{body};
91             my $who = $mess->{who};
92             my $channel = $mess->{channel};
93             my $web_out;
94             my $res;
95            
96             foreach my $regex (keys %{$self->{hash}}){
97             next unless $body =~ /$regex/;
98             $body =~ s/$regex//;
99             my ($action, $url) = split'=>',$self->{hash}->{$regex}->{cmd};
100             my $callback = $self->{hash}->{$regex}->{callback};
101             $action =~ s/\s+//g;
102             $url =~ s/\s+//g;
103             if ($action eq "GET"){
104             my $req = HTTP::Request->new($action, $url);
105             $res = $self->{ua}->request($req);
106             } elsif ($action eq "POST") {
107             my ($url, $query) = split 'TNO', $url;
108             my @res = split '&', $query;
109             my %hash;
110             foreach (@res) {
111             my ($field, $value) = split '=', $_;
112             $hash{$field} = eval $value;
113             }
114             $res = $self->{ua}->post($url, \%hash);
115             }
116             if ($res->is_success) {
117             $web_out = $res->content;
118             my $refsub = eval $callback;
119             $refsub->();
120             return $web_out;
121             } else {
122             return $res->status_line;
123             }
124             }
125             }
126              
127             sub set {
128             my ($self, $file) = @_;
129             open HANDLE, $file;
130             while(){
131             chomp;
132             my ($regex, $cmd, $callback) = split '#',$_;
133             $regex =~ s/\s+$//g;
134             $self->{hash}->{$regex}->{cmd} = $cmd;
135             $self->{hash}->{$regex}->{callback} = $callback;
136             }
137             close HANDLE;
138             $self->{ua} = LWP::UserAgent->new();
139             $self->{ua}->agent("Mozilla/5.0 (Macintosh; U; PPC Mac OS X; fr) AppleWebKit/412.7 (KHTML, like Gecko) Safari/412.5");
140             return $self;
141             }
142              
143             1;