tagging: Allow using titles in for related stories.
[matthijs/upstream/blosxom-plugins.git] / xtaran / xml_ping_generic
1 # -*- perl -*-
2 # Blosxom Plugin: xml_ping_generic
3 # Author(s): Axel Beckert <blosxom@deuxchevaux.org>, http://noone.org/blog
4 #            Bob Schumaker <cobblers@pobox.com>
5 #            Rael Dornfest <rael@oreilly.com> 
6 # Version: 0.01-2.0b4
7 # xml_ping_generic web page: http://noone.org/blog?-tags=Pinging
8 # xml_ping_generic download: http://noone.org/blosxom/xml_ping_generic
9 # Blosxom web page: http://www.blosxom.com/
10 # Documentation: See the bottom of this file or type: perldoc xml_ping_weblogs
11
12 package xml_ping_generic;
13
14 $VERSION = '0.01-2.0b4';
15
16 # --- Configurable variables -----
17
18 # What URL should this plugin ping?
19 require LWP::UserAgent;
20 use FileHandle;
21 use File::stat;
22 #use Data::Dumper;
23
24 # Keep track of the newest story's mtime
25 my $newest = 0;
26
27 my @urls_to_ping = 
28     qw(
29
30      http://rpc.technorati.com/rpc/ping
31      http://rpc.weblogs.com/RPC2
32
33        );
34
35 sub start {
36
37
38     return 1;
39 }
40
41 sub filter {
42     my($pkg, $files_ref) = @_;
43
44     my %mtimes = ();
45     foreach my $file (keys %$files_ref) {
46         $mtimes{$file} = stat($file)->mtime;
47     }
48
49     $newest_file = ( sort { $mtimes{$b} <=> $mtimes{$a} } keys %mtimes )[0];
50     $newest = $mtimes{$newest_file};
51
52     return 1;
53 }
54
55 sub end {
56     my $lockfile = "$blosxom::plugin_state_dir/.ping.lock";
57     my $outfile = "$blosxom::plugin_state_dir/.ping.output";
58
59     #print STDERR "lockfile=".(-e $lockfile)." preview=$preview::is_preview newest=".localtime($newest)." mtime=".localtime(stat($outfile)->mtime)." newest_file=$newest_file\n";
60
61     return 1 if (-e $lockfile or $preview::is_preview );
62
63     # If no timestamped touch-file or newest is newer than the touch-file...
64     if (!-e $outfile or $newest > stat($outfile)->mtime) {
65
66         open(LOCK, ">$lockfile");
67         close(LOCK);
68
69         # Okay, so we can't encode the values, we have to leave the
70         # slashes in and all.
71
72         my $blog_name = $blosxom::blog_title;
73         my $blog_url  = $blosxom::url;
74         
75         my $content = <<EOF;
76 <?xml version="1.0"?>
77 <methodCall>
78   <methodName>weblogUpdates.ping</methodName>
79   <params>
80      <param><value>$blog_name</value></param>
81      <param><value>$blog_url</value></param>
82   </params>
83 </methodCall>
84 EOF
85         # Touch the touchfile (creates if doesn't already exist)
86         open TOUCH, "> $blosxom::plugin_state_dir/.ping.output";
87
88         foreach my $url_to_ping (@urls_to_ping) {
89             $header = new HTTP::Headers;
90             (my $host = $urls_to_ping) =~ s!http://(.*?)/.*$!$1!;
91             $header->header( User_Agent => "blosxom/2.0-abe-dev xml_ping_weblogs/$VERSION",
92                              Host => $host,
93                              Content_Type => 'text/xml',
94                              Content_Length => length($content) );
95         
96             $request = HTTP::Request->new('POST', 
97                                           $url_to_ping,
98                                           $header,
99                                           $content);
100         
101             $ua = LWP::UserAgent->new;
102             $response = $ua->request($request);
103
104             #my $result = $response->{_content};
105             #$result =~ /Thanks for the ping/ or return 0;
106
107             my $result = $response->as_string;
108             my $reqas  = $request->as_string;
109             print TOUCH "$reqas\n$result\n";
110         }
111         close TOUCH;
112         unlink $lockfile;
113
114     }
115   
116     return 1;
117 }
118
119 sub encode {
120     my ($string) = @_;
121     
122     $string =~ s/([^-A-Za-z0-9_.!~*\'()])/sprintf('%%%02X', $1)/ge;
123
124     return $string;
125 }
126
127 1;
128
129 __END__
130
131 =pod
132
133 =head1 NAME
134
135 Blosxom Plug-in: xml_ping_generic
136
137 =head1 SYNOPSIS
138
139 Purpose: Notifies one or more configurable pages via weblogs.com's XML
140 RPC API ping that your weblog has been updated upon encountering any
141 new story. By default it informs weblogs.com and technorati.com.
142
143 Maintains a touch-file ($blosxom::plugin_state_dir/.ping.output) to
144 which to compare the newest story's creation date. (In comparsion to
145 the original xml_ping_weblogs this point works fine with Rael's
146 entries_caching index module, too.) Fills the touch-file with the sent
147 HTTP and XML requests and responses of the latest ping.
148
149 =head1 VERSION
150
151 0.01-2.0b4
152
153 Second part of version number coincides with the version of Bob
154 Schumaker's xml_ping_weblogs plugin, it's based on.
155
156 =head1 AUTHOR
157
158 Axel Beckert <abe@deuxchevaux.org>
159
160 Based on the xml_ping_weblogs plugin by Bob Schumaker
161 <cobblers@pobox.com>, http://www.cobblers.net/blog/
162
163 =head1 SEE ALSO
164
165 Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/
166
167 Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml
168
169 =head1 BUGS
170
171 Address bug reports and comments to the Blosxom mailing list 
172 [http://www.yahoogroups.com/group/blosxom].
173
174 =head1 LICENSE
175
176 Blosxom and this Blosxom Plug-in
177 Copyright 2003, Rael Dornfest 
178
179 Permission is hereby granted, free of charge, to any person obtaining a
180 copy of this software and associated documentation files (the "Software"),
181 to deal in the Software without restriction, including without limitation
182 the rights to use, copy, modify, merge, publish, distribute, sublicense,
183 and/or sell copies of the Software, and to permit persons to whom the
184 Software is furnished to do so, subject to the following conditions:
185
186 The above copyright notice and this permission notice shall be included
187 in all copies or substantial portions of the Software.
188
189 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
190 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
191 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
192 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
193 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
195 OTHER DEALINGS IN THE SOFTWARE.