File Coverage

blib/lib/OpenInteract2/Observer/UsePerlPost.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package OpenInteract2::Observer::UsePerlPost;
2              
3             # $Id: UsePerlPost.pm,v 1.9 2005/01/17 00:06:59 cwinters Exp $
4              
5 1     1   832 use strict;
  1         2  
  1         38  
6 1     1   423 use Log::Log4perl qw( get_logger );
  0            
  0            
7             use Net::Blogger;
8             use OpenInteract2::Constants qw( :log );
9             use OpenInteract2::Context qw( CTX DEPLOY_URL );
10              
11             $OpenInteract2::Observer::UsePerlPost::VERSION = '0.05';
12              
13             my $DEFAULT_PROXY = 'http://use.perl.org/journal.pl';
14             my $DEFAULT_URI = 'http://use.perl.org/Slash/Journal/SOAP';
15              
16             my @REQUIRED_FIELDS = qw(
17             use_perl_subject use_perl_content
18             use_perl_user_id use_perl_password
19             );
20              
21             my ( $log );
22              
23             sub update {
24             my ( $class, $action, $type, $object ) = @_;
25             return unless ( $type eq 'post add' );
26              
27             my $request = CTX->request;
28              
29             my $do_skip = $action->param( 'use_perl_skip' );
30             unless ( $do_skip ) {
31             if ( $request ) {
32             $do_skip = $request->param( 'use_perl_skip' );
33             }
34             }
35             return if ( $do_skip eq 'yes' );
36              
37             $log ||= get_logger( LOG_APP );
38              
39             my $subject_field = $action->param( 'use_perl_subject' );
40             my $content_field = $action->param( 'use_perl_content' );
41             my $user_id = $action->param( 'use_perl_user_id' );
42             my $password = $action->param( 'use_perl_password' );
43              
44             my $action_name = $action->name;
45             my $error_preamble = "Cannot post use.perl journal from action '$action_name'!";
46             unless ( $subject_field and $content_field and $user_id and $password ) {
47             $log->error(
48             "$error_preamble You must define the following parameters in ",
49             "your action: ", join( ', ', @REQUIRED_FIELDS ), ". You can ",
50             "do so in the configuration file or in the action code itself."
51             );
52             return;
53             }
54              
55             my $subject = $object->$subject_field();
56             my $content = $object->$content_field();
57             unless ( $subject and $content ) {
58             $log->error(
59             "$error_preamble No subject found from method '$subject_field' ",
60             "or no content found from method '$content_field'; not creating ",
61             "journal entry."
62             );
63             return;
64             }
65              
66             if ( my $footer = $action->param( 'use_perl_footer' ) ) {
67             $content .= "\n\n" . $class->_generate_footer( $object, $footer );
68             }
69              
70             my $blogger = Net::Blogger->new(
71             engine => 'slash',
72             debug => $log->is_debug,
73             );
74              
75             my $use_perl_proxy = $action->param( 'use_perl_proxy' )
76             || $DEFAULT_PROXY;
77             my $use_perl_uri = $action->param( 'use_perl_uri' )
78             || $DEFAULT_URI;
79              
80             # Before we send the content we want to get rid of any HTML that
81             # use.perl might not like. (This could be better done...)
82              
83             # First create 'ecode' sections...
84              
85             $content =~ s|]+>||g;
86             $content =~ s|||g;
87              
88             # ...then remove all img tags and replace them with links to the
89             # image and a note about what you're seeing
90              
91             my @image_tags = $content =~ /(]+>)/gsm;
92             foreach my $img_tag ( @image_tags ) {
93             my ( $src ) = $img_tag =~ /src="([^"]+)"/sm;
94             my ( $alt ) = $img_tag =~ /alt="([^"]+)"/sm;
95             unless ( $alt ) {
96             my $base_src = '';
97             if ( $alt =~ m|/| ) {
98             ( $base_src ) = $src =~ m|.*/(.*)$|;
99             }
100             else {
101             $base_src = $src;
102             }
103             $alt = $base_src;
104             }
105             $content =~ s|$img_tag|(view image: $alt)|sm;
106             }
107              
108             my $debug_only = $action->param( 'use_perl_debug' );
109             if ( $debug_only =~ /^(yes|true)/i ) {
110             $log->warn( "Not sending data to use.perl server since ",
111             "'use_perl_debug' is set." );
112             $log->warn( "Proxy: $use_perl_proxy" );
113             $log->warn( "Uri: $use_perl_uri" );
114             $log->warn( "Username: $user_id" );
115             my $masked = join( '', map { 'X' } ( 1 .. length $password ) );
116             $log->warn( "Password: $masked (masked)" );
117             $log->warn( "Subject:\n$subject" );
118             $log->warn( "Body:\n$content" );
119             }
120             else {
121             $blogger->Proxy( $use_perl_proxy );
122             $blogger->Uri( $use_perl_uri );
123             $blogger->Username( $user_id );
124             $blogger->Password( $password );
125             my $post_id = $blogger->slash()->add_entry(
126             subject => $subject,
127             body => $content,
128             );
129             $log->is_info &&
130             $log->info( "Result from adding entry '$subject': $post_id" );
131             }
132             }
133              
134             sub _generate_footer {
135             my ( $class, $object, $footer ) = @_;
136             if ( $footer =~ /\$LINK/ || $footer =~ /\$ID/ ) {
137             my ( $object_info, $object_url, $object_id );
138             eval {
139             $object_info = $object->object_description;
140             $object_url = $object_info->{url};
141             $object_id = $object_info->{object_id};
142             };
143              
144             # last-ditch to define the ID
145             eval {
146             $object_id ||= $object->id
147             };
148              
149             if ( $object_url ) {
150             my $request = CTX->request;
151             my $host = ( $request )
152             ? $request->server_name
153             : CTX->server_config->{server_host};
154             if ( $host ) {
155             my $server_url = "http://$host" . DEPLOY_URL;
156             $footer =~ s/\$LINK/$server_url$object_url/g;
157             }
158             else {
159             $log->warn( "Cannot generate footer: no server host found. ",
160             "Please define server configuration key ",
161             "'Global.server_host' so I know what hostname to use." );
162             return '';
163             }
164             }
165             if ( $object_id ) {
166             $footer =~ s/\$ID/$object_id/g;
167             }
168             }
169             $log->is_info && $log->info( "Adding footer: $footer" );
170             return $footer;
171             }
172              
173             1;
174              
175             __END__