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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
#!/usr/bin/perl -w
use strict;
# htag.pl - a tagline generator, sig manager and over engineered program.
# Copyright (C) 1999-2003 Project Purple, Simon Huggins
# Simon Huggins <huggie@earth.li>
# http://www.earth.li/projectpurple/progs/htag.html
# For ChangeLog and Known Bugs see HISTORY and BUGS.
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; version 2 of the License only
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place, Suite 330, Boston, MA 02111-1307 USA
# FIXME Check for UNIX first?
if ($< == 0 or $> == 0 or $( == 0 or $) == 0) {
die "UID/GID or effective UID/GID is 0\n".
"Htag is no doubt not safe for use when run as root\n";
}
# If the "use HtagPlugin 0.5;" line causes problems due to older versions of
# HtagPlugin lying around the system then uncomment the following with the
# path to your local copy of HtagPlugin
# use lib '/home/huggie/perl/huggietag/htag-0.0.24/HtagPlugin';
use HtagPlugin 0.6;
use Getopt::Long;
use POSIX qw/tcgetpgrp/;
use vars qw(%cfg);
my %override;
my $package_num=0;
# Controls "Doing config filename" messages.
my $cfgdebug=0;
# $infinity is how many times we are allowed to loop in run_plugins
# That is how many times a plugin is allowed to call back to earlier ones.
# The counter is only increased in this case so keep it fairly small but
# increase this if you need to and mail huggie@earth.li to say you did so I
# know what is a sane magic number for future releases.
# Did that make sense?
my $infinity = 80;
### Defines
$override{'VERSION'} = $cfg{'VERSION'} = "0.0.24";
$override{'HOME'} = $cfg{'HOME'} = $ENV{"HOME"} || $ENV{"LOGDIR"}
|| (getpwuid($<))[7];
$cfg{'nicedie'} = 1;
# srand(time() ^ ($$ + ($$ << 15) )); # Since 5.004 not required
sub print_header {
print STDERR "Htag.pl $cfg{'VERSION'} - Simon Huggins <huggie\@earth.li> Released under GPL\n";
print STDERR "Copyright (C) 1999-2002 Project Purple. http://www.earth.li/projectpurple/\n\n";
}
sub process_options {
$cfg{'basecfgfile'}=$cfg{'HOME'} . "/.htrc";
$cfg{'debug'} = 0; # default. Can be overriden in cfgfile
# For process_configfile/undef %cfg logic.
$override{'basecfgfile'} = $cfg{'basecfgfile'};
my %getopt=( "tagfile=s" => \$override{'tagfile'},
"t=s" => \$override{'tagfile'},
"cfgfile=s" => \$override{'basecfgfile'},
"c=s" => \$override{'basecfgfile'},
"fillsig=s" => \$cfg{'fillsig'},
"f=s" => \$cfg{'fillsig'},
"help" => \$cfg{'help'},
"h" => \$cfg{'help'},
"msgfile=s" => \$cfg{'msgfile'},
"m=s" => \$cfg{'msgfile'});
if (not &GetOptions(%getopt)) {
print STDERR <<'EOF';
htag.pl - tagline and general sig adder.
Usage: htag.pl -t tagfile -c cfgfile -m msgfile
htag.pl -h gives perldoc
htag.pl -f sigfile
Fills a sig with spaces to check your @nn@ bits line up (or don't
depending what you are trying to achieve).
Believe me this is useful.
EOF
nicedie "\n";
}
if (defined $cfg{'fillsig'}) {
fillsig($cfg{'fillsig'});
exit;
}
if (defined $cfg{'help'}) {
exec "perldoc $0";
die "Could not run perldoc.\nPlease less $0 and read the (lack of) documentation at the end\n";
}
if (not defined $cfg{'msgfile'}) {
print STDERR "No message file?\n";
nicedie "Sorry you need to give me a message file to add to (or a new filename or -)\n";
}
# For process_configfile/undef %cfg logic.
$override{'msgfile'} = $cfg{'msgfile'};
$cfg{'basecfgfile'} = $override{'basecfgfile'};
}
sub expand_home_scalar_or_ref($); # suppress warning about unknown prototype
# for the calls to itself inside itself.
sub expand_home_scalar_or_ref($) {
my $foo = shift;
return if not defined $foo;
if (ref($foo) eq 'ARRAY') {
foreach (@{$foo}) {
$_ = expand_home_scalar_or_ref($_);
}
} elsif (ref($foo) eq 'HASH') {
foreach my $key (keys %{$foo}) {
$foo->{$key} = expand_home_scalar_or_ref($foo->{$key});
}
} else {
$foo =~ s#^~/#$cfg{'HOME'}/#o;
}
return $foo;
}
sub process_configfile {
my @list = ($cfg{'basecfgfile'},$cfg{'extracfgfile'});
undef %cfg;
# nicedie controls whether to ask for keypress when dying (useful when
# normally called by mutt or tin etc.)
# Default to on until cfgfile read. After all if there is a problem before
# then we want the user to know about it.
$cfg{'nicedie'} = 1;
foreach my $cfgfile (@list) {
print STDERR "Doing $cfgfile\n" if $cfgdebug and defined
$cfgfile;
next if not defined $cfgfile;
unless (my $retval = do "$cfgfile") {
warn "couldn't parse $cfgfile: $@"
if $@;
warn "couldn't do $cfgfile: $!"
unless defined $retval;
warn "couldn't run $cfgfile"
unless $retval;
nicedie "Problem with $cfgfile! Aborting";
}
}
foreach (keys %override) {
$cfg{$_} = $override{$_} if defined $override{$_};
}
foreach my $key (keys %cfg) {
if (defined $cfg{$key}) {
$cfg{$key} = expand_home_scalar_or_ref($cfg{$key});
}
}
}
sub run_plugin($) {
my $program = shift;
if (-f $program) {
print STDERR "Running \"$program\"\n" if $cfg{'debug'};
my ($lines,$rc);
$rc=0;
# Plugins are allowed to scribble over %cfg but %cfg holds values that must
# be reset (generally) before a second run of the same plugin will work
# (the print "\n" while $cfg{'newline'}--; hit this)
# Plugins can change $cfg{'basecfgfile'} themselves. This is considered a
# feature. (Stop laughing at the back there).
# To ensure that plugins written in other languages see the changes to %cfg
# this is done for both forks of the if.
process_configfile();
open(H,"<$program");
$lines = <H>;
if ($lines =~ m&^#!/[a-zA-Z/.-]+perl .*$&) {
{ # Otherwise $/ is undef in eval. Mucho ick.
local $/;
undef $/;
$lines .= <H>;
close(H);
}
# I tried to use Safe to do this but it fouls up when using modules.
$package_num++;
$program =~ s/.*?([^\/]+)$/$1/;
my $eval_code = "package HtagPlugin::$package_num;".
'local $SIG{\'__WARN__\'} = sub { (my $mess = $_[0])'.
" =~ s/\\(eval[^)]*\\)/$program/g; ".
' $mess =~ s/(HtagPlugin::)\d+::([^ ]*)/$1$2/; '.
' warn $mess; }; '.
"my \$rc = eval {$lines}; ".
'die $@ if $@; $rc;';
$_ = "HtagPlugin::$package_num";
{
no strict 'refs';
*{$_.'::cfg'} = \%cfg;
*{$_.'::htagdie'} = \&nicedie;
*{$_.'::subst_macros'} = \&subst_macros;
*{$_.'::scansigfile'} = \&scansigfile;
*{$_.'::process_msgbody'}
= \&process_msgbody;
*{$_.'::process_configfile'}
= \&process_configfile;
*{$_.'::chunksizealign'}
= \&chunksizealign;
*{$_.'::reg_deletion'}
= \®_deletion;
}
$rc = eval $eval_code;
$override{'notag'} = $cfg{'notag'} if defined $cfg{'notag'};
if ($@) {
$@ =~ s/\(eval[^)]*\)/$program/g;
nicedie "$program: $@";
}
if (not defined $rc) {
$rc = 253;
}
} else {
# if not perl construct arg list
my @args = ($cfg{'msgfile'},$cfg{'basecfgfile'},
$cfg{'VERSION'});
close(H);
$rc = 0xffff & system($program,@args);
$rc >>= 8;
}
if ($rc == 254) {
my $msg="";
$msg = "Plugin control, plugin $program requesting clearance to die...\n" if $cfg{'debug'};
nicedie $msg; # Ensure we wait on a keypress if asekd to
} elsif ($rc == 255) {
my $msg="";
$msg = "User requested death... Complying.\n" if $cfg{'debug'};
die $msg;
}
return $rc unless $rc == 253;
} else {
# XXX Don't die?
nicedie "$program does not exist!\n";
}
return;
}
sub pick_rand(\@) {
my $ref = shift;
return @{$ref}[rand scalar @{$ref}];
}
sub run_plugins($) {
my $dir = shift;
my (@plugins,%plugins,$program);
opendir(DIR, $dir) or nicedie "Cannot open $dir: $!\n";
@plugins = grep { -f $_ }
map { $dir . $_ }
grep { ! /^\./ }
readdir(DIR);
closedir(DIR);
foreach my $plugin (@plugins) {
if ($plugin !~ m#/(\d\d).+$#) {
nicedie "Found unexpected $plugin\n";
} else {
push @{$plugins{$1}}, $plugin;
}
}
my @order = sort keys %plugins;
my (@trueorder,$infinite_loop);
$infinite_loop=0;
@trueorder = @order;
while (my $num = shift @order) {
$program = pick_rand(@{$plugins{$num}});
if (my $back = run_plugin($program)) {
my @redo=@trueorder;
while ($redo[0] < $back) {
shift @redo;
}
@order = @redo;
$infinite_loop++;
if ($infinite_loop > $infinity) {
nicedie "Purple Alert! This is not a daffodil! Too much recursion\n".
"This probably happened because your taglines are too short compared to the\n".
"space left in the sig chosen.\n";
}
}
}
}
sub fillsig($) {
my $sigfile = shift;
my ($sig,$len,$type);
open(HANDLE, $sigfile) or nicedie "Could not open $sigfile!: $!";
while (<HANDLE>) {
$sig .= $_;
}
close(HANDLE);
while ($sig =~ /@[A-Za-z]?[1-9][0-9]*[RC]?@/) {
$sig =~ s/@[A-Za-z]?([1-9][0-9]*)[RC]?@/" "x$1/e;
}
$sig =~ s/\@V/$cfg{'VERSION'}/g;
print $sig;
}
sub choose_configfile() {
process_configfile(); # Pick up the changeconf stuff.
return $cfg{'basecfgfile'} if not defined $cfg{'changeheaders'};
my $file;
if (defined $cfg{'changeheaders'}) {
my (@headers,$match,@l);
if (open(HANDLE, $cfg{'msgfile'})) {
while (my $line = <HANDLE>) {
last if ($line =~ /^$/); # end of headers
push @headers, $line;
}
close(HANDLE);
}
foreach (@{$cfg{'changeheaders'}}) {
$file = pop;
foreach (@$_) {
eval { "" =~ /$_/; };
nicedie "Pattern \"$_\" would have killed me if I'd tried to run it.\nPerl said: $@" if $@;
}
}
# Ugh.
# There must be a nicer way to implement this?
CH: foreach (@{$cfg{'changeheaders'}}) {
@l = @$_;
$match=0;
$file = pop @l;
foreach my $line (@headers) {
PAT: foreach my $pattern (@l) {
if ($line =~ /$pattern/) {
my $temp = $1;
$file =~ s/\$1/$temp/e
if defined $temp;
$match++;
last PAT;
}
}
if ($match == @l or $l[0] eq "") {
$override{'extracfgfile'} =
$cfg{'extracfgfile'} = $file;
last CH;
}
}
if (not @headers and $l[0] eq "") {
$override{'extracfgfile'} =
$cfg{'extracfgfile'} = $file;
last CH;
}
}
}
}
### START HERE
{
print_header;
process_options;
choose_configfile;
process_configfile;
if (not defined $cfg{'plugindir'}) {
nicedie "Sorry, \$cfg{'plugindir'} was not defined in your config file.\n";
}
if ($cfg{'plugindir'} !~ m#/$#) { $cfg{'plugindir'} .= "/"; }
run_plugins($cfg{'plugindir'});
}
END {
&delete_tmpfiles;
}
__END__
=head1 NAME
htag.pl - Add taglines and sigs to email, news and fidonet messages.
=head1 SYNOPSIS
htag.pl [I<-t> tagfile I<-c> cfgfile] I<-m> msgfile
htag.pl I<-f> sigfile
htag.pl I<-h>
=head1 DESCRIPTION
B<htag.pl> is a sigmonster. It is designed to be extendable in many
different ways through its use of plugins. It might be getting a little bit
too sentient in its old age though.
It can be used like this:
htag.pl -m $1
$EDITOR $1
For information on configuration see the B<sample.htrc> file
To create signature files, it is tedious to have to work out what will and
won't line up. This is why the I<-f> option exists. Feed it a sigfile
and it will replace the @[0-9]+[RC]?@ bits with required number of spaces so
you can see if you got it right or not. (You could even run it from your
favourite editor e.g. C<:! htag.pl -f %> for vim on the current file.)
=head1 BUGS
Inserting a tagline containing C<@[0-9]+[RC]?@> has interesting
results.
This documentation is useless. Use The Source Luke.
=head1 FILES
=over 4
=item ~/.htrc
Config file
=back
=head1 SEE ALSO
http://www.earth.li/progs/htag.html
=head1 AUTHOR
Simon Huggins <huggie@earth.li>
=cut
|