/[echolot]/trunk/Echolot/Commands.pm
ViewVC logotype

Contents of /trunk/Echolot/Commands.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 363 - (show annotations) (download)
Tue Jan 14 05:25:35 2003 UTC (10 years, 5 months ago) by weasel
File size: 3156 byte(s)
First go at sane logging
1 package Echolot::Commands;
2
3 # (c) 2002 Peter Palfrader <peter@palfrader.org>
4 # $Id: Commands.pm,v 1.12 2003/01/14 05:25:34 weasel Exp $
5 #
6
7 =pod
8
9 =head1 Name
10
11 Echolot::Commands - manage commands like add key, set ttl etc.
12
13 =head1 DESCRIPTION
14
15 This package provides functions for sending out and receiving pings.
16
17 =cut
18
19 use strict;
20 use Echolot::Log;
21 use Fcntl ':flock'; # import LOCK_* constants
22 #use Fcntl ':seek'; # import SEEK_* constants
23 use POSIX; # import SEEK_* constants (older perls don't have SEEK_ in Fcntl)
24 use English;
25
26 sub addCommand($) {
27 my ($command) = @_;
28
29 my $filename = Echolot::Config::get()->{'commands_file'};
30 open(FH, ">>$filename" ) or
31 Echolot::Log::warn("Cannot open $filename for appending $!."),
32 return 0;
33 flock(FH, LOCK_EX) or
34 Echolot::Log::warn("Cannot get exclusive lock on $filename: $!."),
35 return 0;
36
37 print FH $command,"\n";
38
39 flock(FH, LOCK_UN) or
40 Echolot::Log::warn("Cannot unlock $filename: $!.");
41 close(FH) or
42 Echolot::Log::warn("Cannot close $filename: $!.");
43 };
44
45 sub processCommands($) {
46 my $filename = Echolot::Config::get()->{'commands_file'};
47
48 (-e $filename) or
49 return 1;
50 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks)= stat $filename;
51 ($size > 0) or
52 return 1;
53
54 open(FH, "+<$filename" ) or
55 Echolot::Log::warn("Cannot open $filename for reading: $!."),
56 return 0;
57 flock(FH, LOCK_EX) or
58 Echolot::Log::warn("Cannot get exclusive lock on $filename: $!."),
59 return 0;
60
61
62 while (<FH>) {
63 chomp;
64 my ($command, @args) = split;
65
66 if ($command eq 'add') {
67 Echolot::Globals::get()->{'storage'}->add_address(@args);
68 } elsif ($command eq 'set') {
69 Echolot::Globals::get()->{'storage'}->set_stuff(@args);
70 } elsif ($command eq 'getkeyconf') {
71 Echolot::Globals::get()->{'scheduler'}->schedule('getkeyconf', 0, time(), \@args );
72 } elsif ($command eq 'sendpings') {
73 Echolot::Globals::get()->{'scheduler'}->schedule('ping', 0, time(), \@args );
74 } elsif ($command eq 'buildstats') {
75 Echolot::Globals::get()->{'scheduler'}->schedule('buildstats', 0, time() );
76 } elsif ($command eq 'buildkeys') {
77 Echolot::Globals::get()->{'scheduler'}->schedule('buildkeys', 0, time() );
78 } elsif ($command eq 'buildthesaurus') {
79 Echolot::Globals::get()->{'scheduler'}->schedule('buildthesaurus', 0, time() );
80 } elsif ($command eq 'delete') {
81 Echolot::Globals::get()->{'storage'}->delete_remailer(@args);
82 } elsif ($command eq 'setremailercaps') {
83 my $addr = shift @args;
84 my $conf = join(' ', @args);
85 Echolot::Conf::set_caps_manually($addr, $conf);
86 } elsif ($command eq 'deleteremailercaps') {
87 Echolot::Globals::get()->{'storage'}->delete_remailercaps(@args);
88 } else {
89 Echolot::Log::warn("Unkown command: '$_'.");
90 };
91 };
92
93 seek(FH, 0, SEEK_SET) or
94 Echolot::Log::warn("Cannot seek to start '$filename': $!."),
95 return 0;
96 truncate(FH, 0) or
97 Echolot::Log::warn("Cannot truncate '$filename' to zero length: $!."),
98 return 0;
99 flock(FH, LOCK_UN) or
100 Echolot::Log::warn("Cannot unlock '$filename': $!.");
101 close(FH) or
102 Echolot::Log::warn("Cannot close '$filename': $!.");
103 };
104
105 1;
106 # vim: set ts=4 shiftwidth=4:

  ViewVC Help
Powered by ViewVC 1.1.5