SimpleBlog.pm

filter: Statementstmt 62.6% 42 / 67 Branchbran 7.1% 1 / 14 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 81.2% 13 / 16 Podpod 100% 4 / 4 totaltotal 55.0% 60 / 109 CC 10 SCAR 28.5
CC
10
Cov
50%
said
56.0
help
6.0
set_blogurl
6.0
CRAP 17.3
↑ Summary SimpleBlog/Store/SQLite.pm »
1 package Bot::BasicBot::Pluggable::Module::SimpleBlog;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 $VERSION = '0.02';
6  
7 1 ▶use base qw(Bot::BasicBot::Pluggable::Module::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Regexp::Common 'RE_URI';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Bot::BasicBot::Pluggable::Module::SimpleBlog - A simple URL collector for Bot::BasicBot::Pluggable.
16  
17 =head1 SYNOPSIS
18  
19 use Bot::BasicBot::Pluggable;
20 use Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite;
21  
22 my $bot = Bot::BasicBot::Pluggable->new( ... );
23 $bot->load( "SimpleBlog" );
24  
25 my $blog_handler = $bot->handler( "SimpleBlog" );
26  
27 $blog_handler->set_store(
28 Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite
29 ->new( "/home/bot/brane.db" )
30 );
31  
32 $blog_handler->set_blogurl( "http://example.com/simpleblog.cgi" );
33  
34 $bot->run;
35  
36 =head1 DESCRIPTION
37  
38 A plugin module for L<Bot::BasicBot::Pluggable> to grab, store and
39 output URLs from IRC channels. It is intentionally simplistic - see
40 L<Bot::BasicBot::Pluggable::Module::Blog> for a more complicated
41 chump-like thing.
42  
43 =head1 IMPORTANT NOTE WHEN UPGRADING FROM PRE-0.02 VERSIONS
44  
45 I'd made a thinko in version 0.01 in one of the column names in the
46 table used to store the URLs in the database, so you'll have to delete
47 your store file and start again. It didn't seem worth automatically
48 detecting and fixing this since I only released 0.01 yesterday and I
49 don't expect anyone to have installed it yet.
50  
51 =head1 METHODS
52  
53 =over 4
54  
55 =cut
56  
57 sub help {
58 0 ▶ my $self = shift;
Subroutine help: not called CC=2 SCAR=17.9
Pod documented
59 0 my $helptext = "Simple URL collector for Bot::BasicBot::Pluggable. Requires direct addressing. Usage: 'http://foo.com/ # the foo website'.";
60 0 my $blogurl = $self->{blogurl};
61 0 ▶ $helptext .= " The URLs can be viewed at $blogurl" if $blogurl;
Branch: 1 branch
BranchTrueFalseTotal
if $blogurl 0 0 0
62 0 return $helptext;
63 }
64  
65 sub said {
66 0 ▶ my ($self, $mess, $pri) = @_;
Subroutine said: not called CC=7 SCAR=40.3
Pod documented
67 0 ▶ return unless $mess->{address} and $pri == 2; # require direct addressing
Branch: 1 branch
BranchTrueFalseTotal
unless $mess->{"address"} and $pri == 2 0 0 0
Condition0%0/3
$mess->{"address"} and $pri == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$mess->{"address"} and $pri == 2
$mess->{"address"} $pri == 2
Truth table0%0/3
A and B
A= $mess->{"address"}B= $pri == 2
ABresult
0X0
100
111
68 my $store = $self->{store}
69 0 ▶ or return "Error: no store configured.";
Branch: 1 branch
BranchTrueFalseTotal
my $store = $self->{"store"} or return "Error: no store configured." 0 0 0
70  
71 0 my $body = $mess->{body};
72 0 my $who = $mess->{who};
73 0 my $channel = $mess->{channel};
74  
75 0 my ($url, $comment) = split(/\s+\#*\s*/, $body, 2);
76 0 ▶ $comment ||= "";
Condition0%0/2
$comment ||= ""
l!l
00
MC/DC0%0/1
$comment ||= ""
$comment
Truth table0%0/2
A
A= $comment
Aresult
11
00
77 0 my $url_re = RE_URI;
78 0 ▶ unless ( $url =~ /^$url_re$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ /^$url_re$/) 0 0 0
79 0 ($url, $comment) = ("", $body);
80 }
81  
82 0 my $now = localtime;
83 0 my $timestamp = $now->strftime("%Y-%m-%d %H:%M:%S");
84  
85 0 $store->store( timestamp => $timestamp,
86 name => $who,
87 channel => $channel,
88 url => $url,
89 comment => $comment );
90  
91 0 ▶ $self->{Bot}->say( who => $who,
Branch: 1 branch
BranchTrueFalseTotal
$comment ? : 0 0 0
92 channel => "msg",
93 body => "Stored URL '$url'"
94 . ($comment ? " and comment '$comment'" : "" )
95 );
96 0 return; # nice quiet bot
97 }
98  
99 =item B<set_store>
100  
101 my $blog_store =
102 Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite->new(
103 "/home/bot/brane.db" );
104 $blog_handler->set_store( $blog_store );
105  
106 Supply a C<Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::*> object.
107  
108 =cut
109  
110 sub set_store {
111 1 ▶ my ($self, $store) = @_;
Subroutine set_store: called CC=2 SCAR=7.0
Pod documented
112 1 ▶ croak "ERROR: No store specified" unless $store;
Branch: 1 branch
BranchTrueFalseTotal
unless $store 0 1 1
113  
114 1 $self->{store} = $store;
115 1 return $self;
116 }
117  
118 =item B<set_blogurl>
119  
120 $blog_handler->set_blogurl( "http://example.com/simpleblog.cgi" );
121  
122 Supply the URL for your CGI script to view the stored URLs.
123  
124 =cut
125  
126 sub set_blogurl {
127 0 ▶ my ($self, $blogurl) = @_;
Subroutine set_blogurl: not called CC=2 SCAR=17.9
Pod documented
128 0 ▶ croak "ERROR: No blogurl specified" unless $blogurl;
Branch: 1 branch
BranchTrueFalseTotal
unless $blogurl 0 0 0
129 0 $self->{blogurl} = $blogurl;
130 0 return $self;
131 }
132  
133 =head1 EXAMPLES
134  
135 use strict;
136 use warnings;
137 use Bot::BasicBot::Pluggable;
138  
139 my $bot = Bot::BasicBot::Pluggable->new(channels => [ "#test" ],
140 server => "irc.example.com",
141 port => "6667",
142 nick => "bot",
143 username => "bot",
144 name => "bot",
145 );
146 $bot->load( "SimpleBlog" );
147  
148 my $blog_handler = $bot->handler( "SimpleBlog" );
149  
150 $blog_handler->set_store(
151 Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite
152 ->new( "/home/bot/brane.db" )
153 );
154  
155 $blog_handler->set_blogurl( "http://example.com/simpleblog.cgi" );
156  
157 $bot->run;
158  
159 Yes, this is your entire program.
160  
161 The file supplied as an argument to the constructor of
162 L<Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite> need
163 not already exist; it will be created and the correct database schema
164 set up as necessary.
165  
166 Talk to the bot on IRC for help:
167  
168 17:37 <nou> kakebot: help SimpleBlog
169 <kakebot> nou: Simple URL collector for Bot::BasicBot::Pluggable.
170 Requires direct addressing. Usage:
171 'http://foo.com/ # the foo website'. The URLs can be viewed at
172 http://example.com/simpleblog.cgi
173  
174 Get stuff out of the database in your favoured fashion, for example:
175  
176 use strict;
177 use warnings;
178 use CGI;
179 use DBI;
180  
181 my $sqlite_db = "/home/bot/brane.db";
182 my $q = CGI->new;
183 my $dbh = DBI->connect("dbi:SQLite:dbname=$sqlite_db", "", "")
184 or die DBI->errstr;
185  
186 print $q->header;
187 print <<EOF;
188  
189 <html>
190 <head><title>simpleblogbot</title></head>
191 <body><h1 align="center">simpleblogbot</h1>
192  
193 EOF
194  
195 my $sql = "SELECT timestamp, name, channel, url, comment FROM blogged
196 ORDER BY timestamp DESC";
197 my $sth = $dbh->prepare($sql) or die $dbh->errstr;
198 $sth->execute;
199 my ($timestamp, $name, $channel, $url, $comment);
200  
201 while ( ($timestamp, $name, $channel, $url, $comment)
202 = $sth->fetchrow_array ) {
203 print "<br><i>$timestamp</i>: <b>$name/$channel</b>: ";
204 print "<a href=\"$url\">$url</a> " if $url;
205 print $q->escapeHTML($comment) if $comment;
206 }
207  
208 print "</body></html>\n";
209  
210 (This will just print everything ever; being more discriminating and
211 adding prettiness is left as an exercise for people who don't hate
212 writing CGI scripts.)
213  
214 At some point there will be
215 C<Bot::BasicBot::Pluggable::Module::Store::*> methods for retrieving
216 as well as storing the data. Probably.
217  
218 =head1 WARNING
219  
220 Unstable API - L<Bot::BasicBot::Pluggable> is liable to change and
221 hence so is this.
222  
223 =head1 BUGS
224  
225 More tests would be nice.
226  
227 =head1 SEE ALSO
228  
229 =over 4
230  
231 =item * L<Bot::BasicBot::Pluggable>
232  
233 =item * L<Bot::BasicBot::Pluggable::Module::Blog>
234  
235 =item * L<Bot::BasicBot::Pluggable::Module::SimpleBlog::Store::SQLite>
236  
237 =back
238  
239 =head1 AUTHOR
240  
241 Kake Pugh (kake@earth.li).
242  
243 =head1 COPYRIGHT
244  
245 Copyright (C) 2003 Kake Pugh. All Rights Reserved.
246  
247 This module is free software; you can redistribute it and/or modify it
248 under the same terms as Perl itself.
249  
250 =head1 CREDITS
251  
252 Tom Insam, author of L<Bot::BasicBot::Pluggable>, answered my dumb
253 questions on how to get it working. Mark Fowler fixed my bad SQL, and
254 told me off until I agreed to abstract out the storage and retrieval bits.
255  
256 =cut
257  
258 1 1;
↑ Summary SimpleBlog/Store/SQLite.pm »