File: packlog.mpi

package info (click to toggle)
madoka 4.2.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 960 kB
  • ctags: 45
  • sloc: perl: 1,006; makefile: 45; sh: 4
file content (160 lines) | stat: -rw-r--r-- 4,059 bytes parent folder | download
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
# madoka 4.2 rc_packlog
#
# rc/packlog.mpi
#    Copyriht(c)2000- cookie / The madoka project
#

my($var, $arg) = split(/=\s*/, $rc_line, 2);
unless ($packlog_init) {
  $packlog_init = 1;
  &list_add($per_min, 'packlog');
}

if ($var eq 'time') {
  if ($arg =~ /^(\d+):(\d+)$/ && $2 < 60) {
    $packlog_time = sprintf("%02d:%02d", $1 % 24, $2);
  } elsif ($arg =~ /^(\d+):(\d+)i$/ && $2 < 60) {
    $packlog_time = $packlog_timec = $1*60 + $2;
  } else {
    &down("[ERROR] time=$arg\n");
  }
} elsif ($var eq 'logfile') {
  my $l;
  $arg =~ s/^~/$homedir/;
  push(@packlog_logfile, $arg);
  while ($arg =~ /%(.)/) {
    $l = $1;
    &down("[ERROR] wrong logfile format($packlog_logfile[-1])" .
	  "in [channel]\n") if $l !~ /^[YyMDh]$/;
    $arg =~ s/%$l//;
  }
} elsif ($var eq 'command') {
  $packlog_command = $arg;
} elsif ($var eq 'address') {
  eval 'use Mail::Sendmail';
  &down("[ERROR] Package Mail::Sendmail not installed!\n") if $@;
  foreach (split(/,/, $arg)) {
    &down("[ERROR] packlog: address=$arg\n") unless /.+\@.+/;
  }
  $packlog_address = $arg;
} elsif ($var eq 'from') {
  $packlog_from = $arg;
} elsif ($var eq 'subject') {
  $packlog_subject = $arg;
  if ($arg =~ /%c/) {
    eval 'use MIME::Base64';
    &down("[ERROR] Package MIME::Base64 not installed!\n") if $@;
  }
  $arg =~ s/%[%cdf]//g;
  &down("[ERROR] packlog: subject=$arg\n") if $arg =~ /%./;
} elsif ($var eq 'move') {
  if ($arg) {
    $arg .= '/' if $arg =~ /\/$/;
    $packlog_move = $arg;
  }
} elsif ($var eq 'remove') {
  $packlog_remove = 1;
}

sub packlog {
  my @y;
  if ($packlog_time =~ /:/) {
    return if $packlog_time ne sprintf("%02d:%02d", $hour, $min);
    @y = localtime(time - 86400);
  } else {
    $packlog_timec--;
    return if $packlog_timec != 0;
    $packlog_timec = $packlog_time;
    @y = localtime(time - $packlog_time);
  }
  &mes("[packlog] start...", 'D');
  $y[5] += 1900; $y[4]++;
  my($i, $j, $k, $l, $pr, $subject) = ();
  local($/);

  foreach $i (@packlog_logfile) {
    &mes("[packlog] logfile: $i", 'D');
    $pr = $log_dir . $i;
    $pr .= '%M%D' if $pr =~ /\/$/;
    while ($pr =~ /(\%[YyMDh])/) {
      $j = $1;
      if ($j eq '%Y') {
	$l = sprintf("%04d", $y[5]);
      } elsif ($j eq '%y') {
	$l = sprintf("%02d", $y[5] % 100);
      } elsif ($j eq '%M') {
	$l = sprintf("%02d", $y[4]);
      } elsif ($j eq '%D') {
	$l = sprintf("%02d", $y[3]);
      } elsif ($j eq '%h') {
	$l = sprintf("%02d", $y[2]);
      }
      $pr =~ s/$j/$l/;
    }
    $l = $pr;
    unless (-e $l) {
      &mes("[packlog] no such file: $l", 'D');
      next;
    }
    if ($packlog_address && $packlog_from) {
      &mes("[packlog] mail ok: $packlog_address / $packlog_from", 'D');
      if ($packlog_subject) {
	$pr = $packlog_subject;
	while ($pr =~ /(%[fcd])/) {
	  $j = $1;
	  if ($j eq '%f') {
	    $k = $l;
	  } elsif ($j eq '%c') {
	    $k = $chan;
	  } elsif ($j eq '%d') {
	    $k = sprintf("%04d/%02d/%02d", $y[5], $y[4], $y[3]);
	  }
	  $pr =~ s/$j/$k/;
	}
	$pr =~ s/%%/%/g;
	if ($packlog_subject =~ /%c/) {
	  $subject = '=?ISO-2022-JP?B?' . chomp(MIME::Base64::encode($pr)) .
	      '?=';
	} else {
	  $subject = $pr;
	}
      } else {
	$subject = "IRC log: $us_nick";
      }
      &mes("[packlog] mail pre.", 'D');
      if (open(MAIL, $l)) {
	undef $/;
	$j = <MAIL>;
	close(MAIL);
	$/ = "\n";
	%mail = (
		 To      => $packlog_address,
		 From    => $packlog_from,
		 Subject => $subject,
		 'Content-type' => 'text/plain; charset=ISO-2022-JP',
		 'Content-transfer-encoding' => '7bit',
		 Message => $j,
		 );
	sendmail(%mail);
	&mes("[packlog] mail log sent.", 'D');
      } else {
	&mes("[packlog] cannot mail log: $l", 'D');
      }
    }
    if ($packlog_command) {
      &mes("[packlog] command: $packlog_command", 'D');
      $pr = $packlog_command;
      while ($pr =~ /(%f)/) {
	$pr =~ s/%f/$l/;
      }
      system($pr);
    }
    if ($packlog_move) {
      ($pr) = ($l =~ /\/([^\/]+)$/);
      $pr = $log_dir . $packlog_move . $pr;
      rename($l, $pr);
    } elsif ($packlog_remove) {
      unlink($l);
    }
  }
}