1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
#!/usr/bin/perl
# use perl -*- mode: Perl; -*-
use strict;
# The version of the config
my $NC_CONFIG_VERSION = '1.30';
my $VERSION = 0.40;
#-------------------------------------------------------------------------------
die <<EOF
$0 /path/NewsClipper.cfg
Run this program to modernize a NewsClipper.cfg file from a version of News
Clipper prior to $NC_CONFIG_VERSION
EOF
if $#ARGV == -1 || $ARGV[0] =~ /^-/;
my $configFile = $ARGV[0];
open CFG,$configFile or die "Can't open \"$configFile\": $!\n";
my $code = join '',<CFG>;
close CFG;
my $old_code = $code;
my $old_version = GetNewsClipperVersion($code);
if (defined $old_version && $old_version >= $NC_CONFIG_VERSION)
{
print <<" EOF";
Your existing NewsClipper.cfg file is for News Clipper version
$old_version, and this ConverConfig will update up to version
$NC_CONFIG_VERSION. No need to update.
EOF
exit;
}
if ($old_version < 1.18)
{
$code = ConvertHandlerConfig($code);
$code = AddAutoDownload($code);
$code = AddFTP($code);
}
if ($old_version < 1.21)
{
$code = AddUpdateNewsClipperVersion($code,$NC_CONFIG_VERSION);
}
if ($old_version < 1.29)
{
$code = AddTagText($code);
$code = AddChmod($code);
$code = AddLogging($code);
$code = UpdateOptionNames($code);
$code = AddUpdateNewsClipperVersion($code,$NC_CONFIG_VERSION);
}
if ($old_version < 1.30)
{
$code = AddEmail($code);
$code = AddUpdateNewsClipperVersion($code,$NC_CONFIG_VERSION);
}
if ($old_code eq $code)
{
print "No change to NewsClipper.cfg -- nothing written\n";
}
else
{
WriteConfig($configFile,$code);
}
#-------------------------------------------------------------------------------
sub ConvertHandlerConfig
{
my $code = shift;
my $imgCache;
($code,$imgCache) = ExtractImgCache($code);
$code =~ s/\n*1;\n*/\n\n/s;
$code .=<<" EOF";
# -----------------------------------------------------------------------------
\%NewsClipper::Handler::Filter::cacheimages::handlerconfig = (
$imgCache
);
# -----------------------------------------------------------------------------
1;
EOF
return $code;
}
#-------------------------------------------------------------------------------
sub AddAutoDownload
{
my $code = shift;
my $auto_download_bugfix_updates=<<' EOF';
# Automatically download any bugfix handlers. (Checks every few hours.)
'auto_download_bugfix_updates' => 'yes',
EOF
$code =~ s/\n+(\);\s*# --------------)/\n\n$auto_download_bugfix_updates$1/s;
return $code;
}
# -----------------------------------------------------------------------------
sub AddFTP
{
my $code = shift;
my ($outputFilesCode) = $code =~ /outputFiles.*?\[(.*?)\]/s;
$outputFilesCode =~ s/(["'])\s*$/$1,/s;
my $numFiles = ($outputFilesCode =~ tr/,//);
my $ftp=<<' EOF';
# ftpFiles allows you to ftp your output files to your web server. Make sure
# there is one set of data for each output file. The first set applies to the
# first output file, the second set to the second output file, etc. If you
# don't want to FTP a file, just use {} for the information.
'ftpFiles' => [
EOF
$ftp .= " {},\n" x $numFiles;
$ftp .=<<' EOF';
# {'server' => 'SERVER',
# 'username' => 'USER NAME',
# 'password' => 'PASSWORD',
# 'dir' => 'DEST DIR'},
],
EOF
$code =~ s/('outputFiles.*?\]\s*,)\s*/$1\n\n$ftp\n/s;
return $code;
}
# -----------------------------------------------------------------------------
sub AddEmail
{
my $code = shift;
my ($ftp_file_code) = $code =~ /(ftp_files.*?\[.*?\].*?\n)/s;
my $email=<<' EOF';
# email_files allows you to email your output files to one or more email
# addresses. Make sure there is one set of emails for each output file. The
# first set applies to the first output file, the second set to the second
# output file, etc. If you don't want to email a file, just use [] for the
# information. The example shows how to send one output file.
'email_files' => [
# {'From' => 'News Clipper <newsclipper@your.server>',
# 'To' => 'First Person <person1@their.server>',
# 'Cc' => '',
# 'Bcc' => '',
# 'Subject' => 'Newsletter'},
],
EOF
$code =~ s/(ftp_files.*?\[.*?\].*?\n)/$1\n$email/s;
return $code;
}
# -----------------------------------------------------------------------------
sub GetNewsClipperVersion
{
my $code = shift;
my ($for_news_clipper_version) =
$code =~ /'(?:for_news_clipper_version|forNewsClipperVersion)' *=> *(.*?) *,/s;
# Ug. Pre "forNewsClipperVersion" days...
if (!defined $for_news_clipper_version)
{
# A 1.18-style config file.
if ($code =~ /ftpFiles/)
{
return 1.18;
}
else
{
return 1.00;
}
}
else
{
return $for_news_clipper_version;
}
}
# -----------------------------------------------------------------------------
sub AddUpdateNewsClipperVersion
{
my $code = shift;
my $NC_CONFIG_VERSION = shift;
my ($nc_version_code,$for_news_clipper_version) =
$code =~ /('(?:for_news_clipper_version|forNewsClipperVersion)' *=> *(.*?) *,)/s;
if (defined $nc_version_code)
{
my $new_version_code = $nc_version_code;
$new_version_code =~ s/\Q$for_news_clipper_version\E/$NC_CONFIG_VERSION/;
$code =~ s/\Q$nc_version_code\E/$new_version_code/s;
}
else
{
my $new_version_code =<<" EOF";
# This value lets News Clipper know if the configuration file is incompatible.
'forNewsClipperVersion' => $NC_CONFIG_VERSION,
EOF
$code =~ s/(\%config *= *\( *\n)/$1\n$new_version_code/s;
}
return $code;
}
# -----------------------------------------------------------------------------
sub AddTagText
{
my $code = shift;
my $add_code =<<EOF;
# The keyword to indicate News Clipper commands. The default is "newsclipper",
# which results in <!-- newsclipper ... --> as the default command comment.
'tag_text' => 'newsclipper',
EOF
$code =~ s/(auto_download_bugfix.*?\n)(\);)/$1$add_code$2/s;
return $code;
}
# -----------------------------------------------------------------------------
sub AddChmod
{
my $code = shift;
my $add_code =<<EOF;
# Determines whether output files should be made executable as well as
# readable.
'make_output_files_executable' => 'yes',
EOF
$code =~ s/(tag_text.*?\n)(\);)/$1$add_code$2/s;
return $code;
}
# -----------------------------------------------------------------------------
sub AddLogging
{
my $code = shift;
my $add_code =<<EOF;
# The location of the News Clipper debug and error log files. Set to "STDOUT"
# or "STDERR" to send to standard output or standard error.
'debug_log_file' => "\$home/.NewsClipper/logs/debug.log",
'run_log_file' => "\$home/.NewsClipper/logs/run.log",
# These values allow you to configure the old logs that are saved. After the
# log file reaches the max_log_file_size (in bytes), it is renamed and a new
# one is started. If there the max_number_of_log_files has been reached, then
# the oldest one is deleted before the logs are rotated. On Unix systems the
# old log files are zipped.
'max_number_of_log_files' => 7,
'max_log_file_size' => 1000000,
EOF
$code =~ s/(tag_text.*?\n)(\);)/$1$add_code$2/s;
return $code;
}
# -----------------------------------------------------------------------------
sub UpdateOptionNames
{
my $code = shift;
$code =~ s/forNewsClipperVersion/for_news_clipper_version/g;
$code =~ s/regKey/registration_key/g;
$code =~ s/inputFiles/input_files/g;
$code =~ s/outputFiles/output_files/g;
$code =~ s/ftpFiles/ftp_files/g;
$code =~ s/handlerlocations/handler_locations/g;
$code =~ s/modulepath/module_path/g;
$code =~ s/cachelocation/cache_location/g;
$code =~ s/maxcachesize/max_cache_size/g;
$code =~ s/scriptTimeout/script_timeout/g;
$code =~ s/socketTries/socket_tries/g;
$code =~ s/socketTimeout/socket_timeout/g;
return $code;
}
# -----------------------------------------------------------------------------
sub ExtractImgCache()
{
my $code = shift;
my $cacheImg;
$code =~ s/\n*((# [^\n]+\n)*'imgcachedir'[^\n]*\n)/$cacheImg .= $1;"\n"/se;
$code =~ s/\n*((# [^\n]+\n)*'imgcacheurl'[^\n]*\n)/$cacheImg .= $1;"\n"/se;
$code =~ s/\n*((# [^\n]+\n)*'maximgcacheage'[^\n]*\n)/$cacheImg .= $1;"\n"/se;
return ($code,$cacheImg);
}
#-------------------------------------------------------------------------------
sub WriteConfig($$)
{
my $configFile = shift;
my $code = shift;
my $backup;
for (my $i = 1; 1; $i++)
{
$backup = $configFile;
$backup =~ s/$/.bak/i;
$backup .= $i unless $i == 1;
last unless -e $backup;
}
rename $configFile,$backup or
warn "Couldn't not rename $configFile\n to $backup. Skipping...\n"
and return;
open NEW, ">$configFile" or die "Can't open $configFile for writing: $!\n";
print NEW $code;
close NEW;
print<<" EOF";
- Config file $configFile
has been converted and saved. Check it to make sure everything look reasonable.
(The old configuration was renamed to $backup.)
EOF
}
|