lib/NewsClipper/HandlerFactory.pm untested

filter: Statementstmt 0.0% 0 / 358 Branchbran 0.0% 0 / 152 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 588 CC 77 SCAR 41.1
CC
77
Cov
0%
_Do_Handler_Functional_Update
132.0
_Do_Handler_Bugfix_Update
132.0
_Download_New_Handler
90.0
CRAP 60.9
1 # -*- mode: Perl; -*-
2 package NewsClipper::HandlerFactory;
3  
4 # This package implements a "Handler Factory", which is used to find and
5 # return handler objects.
6  
7 use strict;
8 use Carp;
9 # For UserAgent
10 use LWP::UserAgent;
11 # For mkpath
12 use File::Path;
13  
14 use vars qw( $VERSION );
15  
16 $VERSION = 0.91;
17  
18 use NewsClipper::Globals;
19 use NewsClipper::Server;
20  
21 my $userAgent = new LWP::UserAgent;
22  
23 my $TIME_BETWEEN_FUNCTIONAL_UPDATES = 24 * 60 * 60;
24 my $TIME_BETWEEN_BUGFIX_UPDATES = 8 * 60 * 60;
25  
26 # A reference to the server object
27 my $SERVER;
28  
29 # Caches used to avoid unnecessary processing of handlers
30 my @UPDATED_HANDLERS;
31 my @ALLOWED_HANDLERS;
32 my %COMPATIBLE_HANDLERS;
33  
34 # Avoid multiple warnings about the server being down
35 my $ALREADY_WARNED_SERVER_DOWN = 0;
36  
37 # This version of News Clipper uses handlers of this type
38 my $COMPATIBLE_NEWS_CLIPPER_VERSION = 1.18;
39  
40 # ------------------------------------------------------------------------------
41  
42 sub new
43 {
44 my $proto = shift;
45  
46 # We take the ref if "new" was called on an object, and the class ref
47 # otherwise.
48 my $class = ref($proto) || $proto;
49  
50 # Create an "object"
51 my $self = {};
52  
53 # Make the object a member of the class
54 bless ($self, $class);
55  
56 # Initialize the server connection
57 $SERVER = new NewsClipper::Server;
58  
59 return $self;
60 }
61  
62 # ------------------------------------------------------------------------------
63  
64 # Finds and creates a handler object for the given name. Downloads a new
65 # handler from the server if the handler is not installed on the system, or if
66 # an update is needed. Returns undef if the handler can not be loaded and
67 # created.
68  
69 sub Create
70 {
71 my $self = shift;
72 my $handler_name = shift;
73  
74 croak "You must supply a handler name to HandlerFactory\n"
75 unless defined $handler_name;
76  
77 $handler_name = lc($handler_name);
78  
79 # First see if the handler is okay to use given the trial/personal
80 # restrictions.
81 _Check_Registration_Restriction($handler_name);
82  
83 # Download the handler if we need to, either because ours is
84 # out of date, or because we don't have it installed.
85 _Update_Handler($handler_name);
86  
87 # Check that the handler version is compatible
88 return undef unless _Handler_Version_Is_Compatible($handler_name);
89  
90 my $loadResult;
91  
92 # Try to load the handler
93 $loadResult = _Load_Handler($handler_name);
94  
95 if ($loadResult =~ /^found/)
96 {
97 dprint "Creating handler \"$handler_name\"";
98 my ($fullHandler) = $loadResult =~ /found as (.*)/;
99 return "$fullHandler"->new;
100 }
101 elsif ($loadResult eq 'not found')
102 {
103 return undef;
104 }
105 }
106  
107 # ------------------------------------------------------------------------------
108  
109 # Downloads or updates the handler from the remote server if necessary.
110 # Updates are necessary if:
111 # * the handler isn't anywhere on the system
112 # * auto_download_bugfix_updates is 'yes' and there is a bugfix version
113 # * -n was specified and a functional or bugfix version is available.
114  
115 sub _Update_Handler
116 {
117 my $handler_name = shift;
118  
119 dprint "Checking if handler \"$handler_name\" needs to be updated.";
120  
121 # Skip this handler if we've already processed it.
122 if (grep { /^$handler_name$/i } @UPDATED_HANDLERS)
123 {
124 dprint "Skipping already updated handler \"$handler_name\".";
125 return;
126 }
127  
128 push @UPDATED_HANDLERS,$handler_name;
129  
130 # First check if the handler isn't on the system.
131 if (_Load_Handler($handler_name) eq 'not found')
132 {
133 dprint "Handler isn't installed, so we need to download it.";
134 _Download_New_Handler($handler_name);
135 return;
136 }
137  
138 # The handler is on the system, so do an update if we need to.
139 my $update_status;
140  
141 $update_status = _Do_Handler_Functional_Update($handler_name);
142 return if $update_status =~ /^(updated|failed)$/;
143  
144 $update_status = _Do_Handler_Bugfix_Update($handler_name);
145 }
146  
147 # ------------------------------------------------------------------------------
148  
149 # Downloads and installs the latest version of a handler
150  
151 sub _Download_New_Handler($)
152 {
153 my $handler_name = shift;
154  
155 my ($versionStatus,$newVersion,$updateType) =
156 $SERVER->Get_New_Handler_Version($handler_name, 'functional',
157 undef, $COMPATIBLE_NEWS_CLIPPER_VERSION);
158  
159 if ($versionStatus eq 'not found')
160 {
161 warn reformat dequote <<" EOF";
162 Can't download handler $handler_name.
163 The handler server reports that the handler $handler_name is not in
164 the database.
165 EOF
166 return;
167 }
168  
169 if ($versionStatus eq 'no update')
170 {
171 die "News Clipper encountered a \"no update\" when there is no local " .
172 "version of handler $handler_name";
173 }
174  
175 if ($versionStatus eq 'failed')
176 {
177 return if $ALREADY_WARNED_SERVER_DOWN;
178 $ALREADY_WARNED_SERVER_DOWN = 1;
179  
180 warn reformat dequote <<" EOF";
181 Couldn't determine which version of the handler $handler_name to
182 download because the server is down. Try again in a while, and send
183 email to bugreport\@newsclipper.com if the problem persists.
184 Additional warnings regarding this problem will not be displayed.
185 EOF
186 return;
187 }
188  
189 if ($versionStatus ne 'okay')
190 {
191 die "News Clipper encountered an unknown \$versionStatus";
192 }
193  
194 dprint "There is a remote handler available.";
195  
196 my $download_result = _Download_Handler_By_Version($handler_name,$newVersion);
197  
198 if ($download_result eq 'okay')
199 {
200 _Unload_Handler($handler_name);
201 return;
202 }
203 elsif ($download_result eq 'not found')
204 {
205 warn reformat dequote <<" EOF";
206 Couldn't install the handler $handler_name. The handler server reports
207 that the handler is not in the database.
208 EOF
209 return;
210 }
211 elsif ($download_result =~ /failed: (.*)/s)
212 {
213 warn reformat dequote $1;
214 return;
215 }
216 else
217 {
218 die "News Clipper encountered an unknown \$download_result";
219 }
220 }
221  
222 # ------------------------------------------------------------------------------
223  
224 # Checks for and does a functional update for a handler. Returns 'updated',
225 # 'not updated', or 'failed'. Handles any error messages to the user.
226  
227 sub _Do_Handler_Functional_Update
228 {
229 my $handler_name = shift;
230  
231 # Functional updates are only prompted by -n
232 unless ($opts{n})
233 {
234 dprint "Skipping functional update check -- -n not specified";
235 return 'not updated';
236 }
237  
238 # Check if we've already done a functional check in the last time period
239 {
240 my $lastCheck =
241 $NewsClipper::Globals::state->get("last_functional_check_$handler_name");
242  
243 if (defined $lastCheck &&
244 (time - $lastCheck < $TIME_BETWEEN_FUNCTIONAL_UPDATES))
245 {
246 dprint "Don't need to check for a functional update yet.";
247 return 'not updated';
248 }
249 }
250  
251 # Now do the check
252 my $localVersion = _Get_Local_Handler_Version($handler_name);
253 my ($versionStatus,$newVersion,$updateType) =
254 $SERVER->Get_New_Handler_Version($handler_name,'functional',$localVersion,
255 $COMPATIBLE_NEWS_CLIPPER_VERSION);
256  
257 if ($versionStatus eq 'not found')
258 {
259 dprint reformat (65,dequote <<" EOF");
260 Can't do functional update of handler $handler_name.
261 Handler server reports that handler $handler_name is not in the database.
262 EOF
263 $NewsClipper::Globals::state->set("last_functional_check_$handler_name",time);
264 $NewsClipper::Globals::state->set("last_bugfix_check_$handler_name",time);
265 return 'not updated';
266 }
267  
268 if ($versionStatus eq 'no update')
269 {
270 dprint "There is a no new functional or bugfix update version.";
271 $NewsClipper::Globals::state->set("last_functional_check_$handler_name",time);
272 $NewsClipper::Globals::state->set("last_bugfix_check_$handler_name",time);
273 return 'not updated';
274 }
275  
276 # Failed, so we check next time too instead of waiting
277 if ($versionStatus eq 'failed')
278 {
279 return 'failed' if $ALREADY_WARNED_SERVER_DOWN;
280 $ALREADY_WARNED_SERVER_DOWN = 1;
281  
282 $errors{"handler#$handler_name"} = reformat dequote <<" EOF";
283 Couldn't determine if there is a newer functional update version of
284 $handler_name available because the server is down. Try again in a while,
285 and send email to bugreport\@newsclipper.com if the problem persists.
286 Additional warnings regarding this problem will not be displayed.
287 EOF
288 return 'failed';
289 }
290  
291 dprint "There is a new " . $updateType . " version.";
292 $NewsClipper::Globals::state->set("last_functional_check_$handler_name",time);
293 $NewsClipper::Globals::state->set("last_bugfix_check_$handler_name",time);
294  
295 return 'not updated' unless _Okay_To_Download($handler_name,$updateType);
296  
297 my $download_result = _Download_Handler_By_Version($handler_name,$newVersion);
298  
299 if ($download_result eq 'okay')
300 {
301 _Unload_Handler($handler_name);
302 return 'updated';
303 }
304  
305 if ($download_result eq 'not found')
306 {
307 $errors{"handler#$handler_name"} = reformat dequote <<" EOF";
308 Couldn't do a functional update of the handler $handler_name. The handler
309 server reports that the handler is not in the database.
310 EOF
311 return 'failed';
312 }
313  
314 if ($download_result =~ /failed: (.*)/s)
315 {
316 $errors{"handler#$handler_name"} = reformat dequote $1;
317 return 'failed';
318 }
319 }
320  
321 # ------------------------------------------------------------------------------
322  
323 # Checks for and does a bugfix update for a handler. Returns 'updated',
324 # 'not updated', or 'failed'. Handles any error messages to the user.
325  
326 sub _Do_Handler_Bugfix_Update
327 {
328 my $handler_name = shift;
329  
330 # Bugfix updates are only prompted by -n or auto_download_bugfix_updates
331 unless ($opts{n} || $config{auto_download_bugfix_updates} =~ /^y/i)
332 {
333 dprint "Skipping bugfix update check -- neither -n nor " .
334 "auto_download_bugfix_updates was specified";
335 return 'not updated';
336 }
337  
338 # Check if we've already done a bugfix check in the last time period
339 {
340 my $lastCheck =
341 $NewsClipper::Globals::state->get("last_bugfix_check_$handler_name");
342  
343 if (defined $lastCheck &&
344 (time - $lastCheck < $TIME_BETWEEN_BUGFIX_UPDATES))
345 {
346 dprint "Don't need to check for a bugfix update yet.";
347 return 'not updated';
348 }
349 }
350  
351 # Now do the check
352 my $localVersion = _Get_Local_Handler_Version($handler_name);
353 my ($versionStatus,$newVersion,$updateType) =
354 $SERVER->Get_New_Handler_Version($handler_name,'bugfix',$localVersion,
355 $COMPATIBLE_NEWS_CLIPPER_VERSION);
356  
357 if ($versionStatus eq 'not found')
358 {
359 dprint reformat (65,dequote <<" EOF");
360 Can't do bugfix update of handler $handler_name.
361 Handler server reports that handler $handler_name is not in the database.
362 EOF
363 $NewsClipper::Globals::state->set("last_bugfix_check_$handler_name",time);
364 return 'not updated';
365 }
366  
367 if ($versionStatus eq 'no update')
368 {
369 dprint "There is a no new bugfix update version.";
370 $NewsClipper::Globals::state->set("last_bugfix_check_$handler_name",time);
371 return 'not updated';
372 }
373  
374 if ($versionStatus eq 'failed')
375 {
376 return 'failed' if $ALREADY_WARNED_SERVER_DOWN;
377 $ALREADY_WARNED_SERVER_DOWN = 1;
378  
379 $errors{"handler#$handler_name"} = reformat dequote <<" EOF";
380 Couldn't determine if there is a newer bugfix update version of
381 $handler_name available because the server is down. Try again in a while,
382 and send email to bugreport\@newsclipper.com if the problem persists.
383 Additional warnings regarding this problem will not be displayed.
384 EOF
385 return 'failed';
386 }
387  
388 dprint "There is a new bugfix version.";
389 $NewsClipper::Globals::state->set("last_bugfix_check_$handler_name",time);
390  
391 return 'not updated' unless _Okay_To_Download($handler_name,$updateType);
392  
393 my $download_result = _Download_Handler_By_Version($handler_name,$newVersion);
394  
395 if ($download_result eq 'okay')
396 {
397 _Unload_Handler($handler_name);
398 return 'updated';
399 }
400  
401 if ($download_result eq 'not found')
402 {
403 $errors{"handler#$handler_name"} = reformat dequote <<" EOF";
404 Couldn't do a bugfix update of the handler $handler_name. The handler
405 server reports that the handler is not in the database.
406 EOF
407 return 'failed';
408 }
409  
410 if ($download_result =~ /failed: (.*)/s)
411 {
412 $errors{"handler#$handler_name"} = reformat dequote $1;
413 return 'failed';
414 }
415 }
416  
417 # ------------------------------------------------------------------------------
418  
419 # Checks to see if the -a flag or auto_download_bugfix_updates config option
420 # allow the handler update to be downloaded. Also tries to ask the user
421 # interactively.
422  
423 sub _Okay_To_Download
424 {
425 my $handler_name = shift;
426 my $updateType = shift;
427  
428 if ($updateType eq 'bugfix')
429 {
430 if ($opts{a} || $config{auto_download_bugfix_updates} =~ /^y/i)
431 {
432 dprint "Doing automatic download for handler \"$handler_name\"";
433 return 1;
434 }
435  
436 # Prompt the user if run interactively, and the user didn't specify one of
437 # the auto download options
438 if (-t STDIN)
439 {
440 warn "There is a newer version of handler \"$handler_name\".\n";
441 warn "Would you like News Clipper to attempt to download it? [y/n]\n";
442 my $response = <STDIN>;
443  
444 if ($response =~ /^y/i)
445 {
446 return 1;
447 }
448 else
449 {
450 return 0;
451 }
452 }
453 # Otherwise we just warn the user we can't do a download
454 else
455 {
456 $errors{"handler#$handler_name"} = reformat dequote <<" EOF";
457 A bugfix update to handler "$handler_name" is available, but it can't be
458 downloaded because auto_download_bugfix_updates is not "yes" in your
459 configuration file, and since News Clipper can't ask you interactively.
460 EOF
461 return 0;
462 }
463 }
464 # It's a functional update
465 else
466 {
467 if ($opts{a})
468 {
469 dprint "Doing automatic download for handler \"$handler_name\"";
470 return 1;
471 }
472 else
473 {
474 $errors{"handler#$handler_name"} = reformat dequote <<" EOF";
475 A functional update to handler "$handler_name" is available, but it can't
476 be downloaded because the -a flag was not specified, and since News
477 Clipper can't ask you interactively.
478 EOF
479 return 0;
480 }
481 }
482 }
483  
484 # ------------------------------------------------------------------------------
485  
486 # This routine checks that a handler on the system is for the current version
487 # of News Clipper. Returns 1 if the handler is compatible, 0 if it is not or
488 # if it wasn't found.
489  
490 sub _Handler_Version_Is_Compatible($)
491 {
492 my $handler_name = shift;
493  
494 dprint "Checking if handler \"$handler_name\" is of a compatible version.";
495  
496 # Skip this handler if we've already processed it.
497 if (exists $COMPATIBLE_HANDLERS{$handler_name})
498 {
499 dprint reformat (65,
500 "Skipping handler \"$handler_name\" (already checked compatibility).");
501 return $COMPATIBLE_HANDLERS{$handler_name};
502 }
503  
504 my $local_handler_nc_version = _Get_Local_Handler_NC_Version($handler_name);
505 unless (defined $local_handler_nc_version)
506 {
507 dprint "Could not get local handler version for $handler_name";
508 $COMPATIBLE_HANDLERS{$handler_name} = 0;
509 return 0;
510 }
511  
512 dprint reformat (65,dequote <<" EOF");
513 Handler "$handler_name" was written for News Clipper version
514 $local_handler_nc_version, and this version of News Clipper is
515 compatible with version $COMPATIBLE_NEWS_CLIPPER_VERSION.
516 EOF
517  
518 if ($local_handler_nc_version != $COMPATIBLE_NEWS_CLIPPER_VERSION)
519 {
520 dprint "Handler $handler_name is incompatible";
521 $errors{"handler#$handler_name"} = reformat dequote <<" EOF";
522 Handler $handler_name is incompatible with this version of News Clipper.
523 (The handler is compatible with News Clipper versions that take handlers
524 from version $local_handler_nc_version, but this version of News Clipper
525 uses handlers from version $COMPATIBLE_NEWS_CLIPPER_VERSION).
526 EOF
527 $COMPATIBLE_HANDLERS{$handler_name} = 0;
528 return 0;
529 }
530 else
531 {
532 dprint "Handler $handler_name is compatible";
533 $COMPATIBLE_HANDLERS{$handler_name} = 1;
534 return 1;
535 }
536 }
537  
538 # ------------------------------------------------------------------------------
539  
540 # This function finds the News Clipper compatible version of the locally
541 # installed handler. Returns the version number or undef if the handler could
542 # not be found.
543  
544 sub _Get_Local_Handler_NC_Version($)
545 {
546 my $handler_name = shift;
547  
548 # Find the handler
549 my $foundDirectory = _Get_Handler_Path($handler_name);
550 return undef unless defined $foundDirectory;
551  
552 open LOCALHANDLER, "$foundDirectory/$handler_name.pm";
553 my $handler_code = join '',<LOCALHANDLER>;
554 close LOCALHANDLER;
555  
556 # Really there should be underscores between the words, but there are a few
557 # handlers out there with the wrong thing.
558 my ($for_news_clipper_version) =
559 $handler_code =~ /'For.News.Clipper.Version'} *= *'(.*?)' *;/s;
560  
561 my $nc_version;
562  
563 # Ug. Pre "For_News_Clipper_Version" days...
564 if (!defined $for_news_clipper_version)
565 {
566 return '1.00';
567 }
568 else
569 {
570 return $for_news_clipper_version;
571 }
572 }
573  
574 # ------------------------------------------------------------------------------
575  
576 # This function finds the version of the locally installed handler. Returns
577 # the version number or undef if the handler could not be found.
578  
579 sub _Get_Local_Handler_Version($)
580 {
581 my $handler_name = shift;
582  
583 my $foundDirectory = _Get_Handler_Path($handler_name);
584 return undef unless defined $foundDirectory;
585  
586 dprint "Found local copy of handler $handler_name in:\n $foundDirectory";
587  
588 open LOCALHANDLER, "$foundDirectory/$handler_name.pm";
589 my $localHandler = join '',<LOCALHANDLER>;
590 close LOCALHANDLER;
591  
592 my ($versionCode) = $localHandler =~ /\$VERSION\s*=\s*do\s*({.*?});/;
593 my $localVersion = eval "$versionCode";
594  
595 dprint "Local version for handler \"$handler_name\" is: $localVersion";
596  
597 return $localVersion;
598 }
599  
600 # ------------------------------------------------------------------------------
601  
602 # This routine restricts the trial version to use only the yahootopstories
603 # handler, and restricts the personal versions to use only the number of
604 # handlers they have registered to use. It dies if the user is trying to use
605 # more than their registration allows.
606  
607 sub _Check_Registration_Restriction($)
608 {
609 my $handler_name = shift;
610  
611 dprint "Checking if handler \"$handler_name\" is okay to use.";
612  
613 return if ($config{product} ne 'Personal') &&
614 ($config{product} ne 'Trial');
615  
616 # Skip this handler if we've already processed it.
617 if (grep { /^$handler_name$/i } @ALLOWED_HANDLERS)
618 {
619 dprint "Skipping already checked handler \"$handler_name\".";
620 return;
621 }
622  
623 if (!_Is_Acquisition_Handler($handler_name))
624 {
625 dprint "$handler_name isn't an acquisition handler -- okay to use.";
626 push @ALLOWED_HANDLERS,$handler_name;
627  
628 return;
629 }
630  
631 # Trial version can only use yahootopstories
632 if ($config{product} eq 'Trial')
633 {
634 if ($handler_name eq 'yahootopstories')
635 {
636 push @ALLOWED_HANDLERS,$handler_name;
637 return;
638 }
639 else
640 {
641 die reformat dequote <<" EOF";
642 You can not use the "$handler_name" handler. The trial version of News
643 Clipper only allows you to use the yahootopstories handler.
644 EOF
645 }
646 }
647  
648 # Now process personal licenses
649  
650 my @installedHandlers;
651  
652 foreach my $dir (@INC)
653 {
654 push @installedHandlers, glob("$dir/NewsClipper/Handler/Acquisition/*.pm");
655 }
656  
657 dprint $#installedHandlers+1," total acquisition handlers found.";
658  
659 # Yell if they have more than the registered number of handlers on their
660 # system.
661 if ($#installedHandlers+1 > $config{number_handlers})
662 {
663 local $" = "\n";
664 warn reformat dequote <<" EOF";
665 You currently have more than the allowed number of handlers on your
666 system. This personal version of News Clipper is only registered to
667 use $config{number_handlers} handlers.
668  
669 Please delete one or more of the following files:
670 EOF
671 die "@installedHandlers\n";
672 }
673  
674 # Yell if they have the registered number of handlers on their system, and
675 # the current handler isn't one of them.
676 if (($#installedHandlers+1 == $config{number_handlers}) &&
677 (!grep {/$handler_name.pm$/} @installedHandlers))
678 {
679 local $" = "\n";
680 warn reformat dequote <<" EOF";
681 You currently have $config{number_handlers} handlers on your
682 system, and are trying to use a handler that is not one of these
683 $config{number_handlers}
684 ($handler_name). This personal version of News Clipper is only registered
685 to use $config{number_handlers} handlers.
686  
687 Please delete one or more of the following files if you want to be able
688 to use this handler:
689 EOF
690 die "@installedHandlers\n";
691 }
692 }
693  
694 # ------------------------------------------------------------------------------
695  
696 # Checks to see if a handler is an acquisition handler. First it looks
697 # locally, then checks the list of remote acquisition handlers. Dies (in
698 # Server::Get_Handler_Type) if the handler is not installed locally and the
699 # handler type can not be determined from the server.
700  
701 sub _Is_Acquisition_Handler
702 {
703 my $handler_name = shift;
704  
705 # First look locally
706 my $loadResult = _Load_Handler($handler_name);
707  
708 if ($loadResult =~ /^found/)
709 {
710 if ($loadResult =~ /Acquisition/)
711 {
712 return 1;
713 }
714 else
715 {
716 return 0;
717 }
718 }
719 else
720 {
721 my ($remoteStatus,$remoteResult) = $SERVER->Get_Handler_Type($handler_name);
722  
723 if ($remoteStatus == 0)
724 {
725 die reformat dequote <<" EOF"
726 Couldn't download the handler type for handler $handler_name. Maybe
727 the server is down. This version of News Clipper can only use a limited
728 number of acquisition handlers, and must contact the server to determine
729 if the handler is an acquisition handler. Try again in a while, and send
730 email to bugreport\@newsclipper.com if the problem persists.
731 EOF
732 }
733  
734 if ($remoteResult eq 'Acquisition')
735 {
736 return 1;
737 }
738 else
739 {
740 return 0;
741 }
742 }
743 }
744  
745 # ------------------------------------------------------------------------------
746  
747 # Figure out where the handler is in the file system. Returns undef if not
748 # found.
749  
750 sub _Get_Handler_Path($)
751 {
752 my $handler_name = shift;
753  
754 # Try to load the handler so we can figure out where to put the replacement
755 my $loadResult = _Load_Handler($handler_name);
756  
757 if ($loadResult eq 'not found')
758 {
759 dprint "Handler \"$handler_name\" not found locally. Can't get path.";
760 return undef;
761 }
762  
763 my @dirs = qw(Acquisition Filter Output);
764  
765 foreach my $dir (@INC)
766 {
767 return "$dir/NewsClipper/Handler/Acquisition"
768 if -e "$dir/NewsClipper/Handler/Acquisition/$handler_name.pm";
769 return "$dir/NewsClipper/Handler/Filter"
770 if -e "$dir/NewsClipper/Handler/Filter/$handler_name.pm";
771 return "$dir/NewsClipper/Handler/Output"
772 if -e "$dir/NewsClipper/Handler/Output/$handler_name.pm";
773 }
774  
775 return undef;
776 }
777  
778 # ------------------------------------------------------------------------------
779  
780 # "Unloads" a handler by deleting the entry in %INC and undefining any
781 # subroutines.
782  
783 sub _Unload_Handler($)
784 {
785 my $handler_name = shift;
786  
787 dprint "Unloading handler \"$handler_name\"";
788  
789 my $handler_type = undef;
790  
791 # Find out what kind of handler it is
792 $handler_type = 'Acquisition'
793 if exists $INC{"NewsClipper/Handler/Acquisition/$handler_name.pm"};
794 $handler_type = 'Filter'
795 if exists $INC{"NewsClipper/Handler/Filter/$handler_name.pm"};
796 $handler_type = 'Output'
797 if exists $INC{"NewsClipper/Handler/Output/$handler_name.pm"};
798  
799 return unless defined $handler_type;
800  
801 # Delete it from %INC
802 delete $INC{"NewsClipper/Handler/$handler_type/$handler_name.pm"};
803  
804 # Now undef the package
805 no strict 'refs';
806 my %oldconfig =
807 %{"NewsClipper::Handler::${handler_type}::${handler_name}::handlerconfig"};
808 Symbol::delete_package("NewsClipper::Handler::${handler_type}::${handler_name}::");
809 %{"NewsClipper::Handler::${handler_type}::${handler_name}::handlerconfig"} =
810 %oldconfig;
811 }
812  
813 # ------------------------------------------------------------------------------
814  
815 # Loads a handler. Returns "found as
816 # NewsClipper::Handler::${dir}::$handler_name" if the handler is found on the
817 # system, and "not found" if it can't be found. Dies if the handler is found
818 # but has errors.
819  
820 sub _Load_Handler($)
821 {
822 my $handler_name = shift;
823  
824 my @dirs = qw(Acquisition Filter Output);
825  
826 dprint "Trying to load handler \"$handler_name\"";
827  
828 # Return if it has already been loaded before. This helps speed things up.
829 foreach my $dir (@dirs)
830 {
831 if (defined $INC{"NewsClipper/Handler/$dir/$handler_name.pm"})
832 {
833 dprint "Handler \"$handler_name\" already loaded";
834 return "found as NewsClipper::Handler::${dir}::$handler_name"
835 }
836 }
837  
838 foreach my $dir (@dirs)
839 {
840 # Try to load it in $dir
841 dprint "Looking for handler as:";
842 dprint " NewsClipper::Handler::${dir}::$handler_name";
843  
844 # Here we need to store errors.
845 my $errors;
846 {
847 local $SIG{__WARN__} = sub { $errors .= $_[0] };
848  
849 eval "require \"NewsClipper/Handler/${dir}/$handler_name.pm\"";
850 }
851  
852 # At this point, the possibilities are:
853 # $errors empty, $@ non-empty, $INC{} non-empty: impossible
854 # $errors non-empty, $@ non-empty, $INC{} non-empty: compile error on eval
855 # $errors empty, $@ empty, $INC{} non-empty: winner!
856 # $errors non-empty, $@ empty, $INC{} non-empty: $errors holds warnings
857 # $errors empty, $@ non-empty, $INC{} empty: handler not found, etc.
858 # $errors non-empty, $@ non-empty, $INC{} empty: $errors holds errors?
859 # $errors empty, $@ empty, $INC{} empty: impossible
860 # $errors non-empty, $@ empty, $INC{} empty: eval had syntax error
861  
862 # Something went wrong
863 if ($@)
864 {
865 # We'll skip can't locate messages, but stop on everything else
866 if ($@ !~ /Can't locate NewsClipper.Handler.$dir.$handler_name/)
867 {
868 $@ =~ s/Compilation failed in require at \(eval.*?\n//s;
869  
870 warn "Handler $handler_name was found in:\n";
871 warn " ",$INC{"NewsClipper/Handler/$dir/$handler_name.pm"},"\n";
872 warn " but could not be loaded because of the following error:\n\n";
873 warn "$errors\n" if defined $errors;
874 die "$@\n";
875 }
876 }
877  
878 if (defined $INC{"NewsClipper/Handler/$dir/$handler_name.pm"})
879 {
880 dprint "Found handler as:\n ",
881 $INC{"NewsClipper/Handler/$dir/$handler_name.pm"};
882  
883 # If there's anything in $errors, it must be warnings. Store them
884 # for later printing.
885 $errors{"handler#$handler_name"} = $errors if defined $errors;
886  
887 return "found as NewsClipper::Handler::${dir}::$handler_name"
888 }
889  
890 # We can get here if the eval has a syntax error. (e.g. if someone tries
891 # to use handler.pm as the handler name)
892 if ($errors)
893 {
894 warn "Handler $handler_name could not be loaded. The error was:\n";
895 die "$errors\n";
896 }
897 }
898  
899 # Darn. Couldn't find it anywhere!
900 dprint "Couldn't find handler";
901 return 'not found';
902 }
903  
904 # ------------------------------------------------------------------------------
905  
906 # This function downloads and saves a remote handler, if one exists. Returns
907 # 'okay', 'not found', or 'failed: error message'
908  
909 sub _Download_Handler_By_Version($$)
910 {
911 my $handler_name = shift;
912 my $version = shift;
913  
914 dprint "Downloading handler $handler_name, version $version";
915  
916 my ($getResult,$code) = $SERVER->Get_Handler($handler_name,$version,
917 $COMPATIBLE_NEWS_CLIPPER_VERSION);
918 return $getResult if $getResult ne 'okay';
919  
920 my $foundDirectory = _Get_Handler_Path($handler_name);
921  
922 # Use the old directory, or create a new one based on what the handler calls
923 # itself.
924 my $destDirectory;
925 if (defined $foundDirectory)
926 {
927 dprint "Replacing handler located in\n $foundDirectory";
928  
929 # Remove the outdated one.
930 unlink "$foundDirectory/$handler_name.pm";
931  
932 $destDirectory = $foundDirectory;
933 }
934 else
935 {
936 my ($subDir) = $code =~ /package NewsClipper::Handler::([^:]*)::/;
937 $destDirectory =
938 "$config{handler_locations}[0]/NewsClipper/Handler/$subDir";
939  
940 dprint "Saving new handler to $destDirectory";
941 }
942  
943 mkpath $destDirectory unless -e $destDirectory;
944  
945 # Write the handler.
946 open HANDLER,">$destDirectory/$handler_name.pm"
947 or return "failed: Handler $handler_name was downloaded, but could " .
948 " not be saved. The message from the operating system is:\n\n$!";
949 print HANDLER $code;
950 close HANDLER;
951  
952 lprint "The $handler_name handler has been downloaded and saved as\n";
953 lprint " $destDirectory/$handler_name.pm\n";
954  
955 # Figure out if the handler needs any other modules.
956 my @uses = $code =~ /\nuse (.*?);/g;
957  
958 @uses = grep {!/(vars|constant|NewsClipper|strict)/} @uses;
959  
960 if ($#uses != -1)
961 {
962 lprint "The handler uses the following modules:\n";
963 $" = "\n ";
964 lprint " @uses\n";
965 lprint "Make sure you have them installed.\n";
966 }
967  
968 return 'okay';
969 }
970  
971 # ------------------------------------------------------------------------------
972  
973 1;