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
|
# -*-Perl-*-
################################################################
###
### MsgStore.pm
###
### Copyright (C) 1997 Internet Message Group
###
### This Perl5 library conforms
### GNU GENERAL PUBLIC LICENSE Version 2.
###
###
### Author: Internet Message Group <img@mew.org>
### Created: Apr 23, 1997
### Revised: @im_revised@
###
my $PM_VERSION = "IM::MsgStore.pm @im_version@";
package IM::MsgStore;
require 5.003;
require Exporter;
use Fcntl;
use IM::Config qw(getsbrfile msg_mode msgdbfile expand_path no_sync);
use IM::Util;
use IM::Folder qw(message_number message_name create_folder touch_folder);
use IM::Header qw(gen_date);
use integer;
use strict;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(store_message exec_getsbrfile open_fcc excl_create fsync);
=head1 NAME
MsgStore - store message in MH-style folder
=head1 SYNOPSIS
=head1 DESCRIPTION
=cut
use vars qw($MsgNum $PrevFolder $First $Last $PrevDst $sys_fsync);
BEGIN {
$MsgNum = 0;
$PrevFolder = '';
$First = 0;
$Last = 0;
$PrevDst = '';
}
##### OPEN A FILE TO SAME NEW MESSAGE IN MAIL FOLDER #####
#
# new_message(handle, folder_name)
# folder_name: a folder name to be saved in
# return value:
# success: file name to be saved
# fail: NULL
#
sub new_message (\*$) {
(local *MESSAGE, my $folder) = @_;
if ($folder ne $PrevFolder) {
$MsgNum = 0;
$PrevFolder = $folder;
}
if ($MsgNum == 0) {
$MsgNum = message_number($folder, 'new');
if ($MsgNum == 0) {
im_warn("can't get new message number in $folder\n");
return ('', '');
}
$First = $Last = $MsgNum;
} else {
$MsgNum++;
}
my $try = 3;
while ($try--) {
my $file = message_name($folder, $MsgNum);
im_notice("creating file: $file\n");
unless ($file) {
# message path allocation failed
return ('', '');
}
if (excl_create(\*MESSAGE, $file) == 0) {
# created successfully
$Last = $MsgNum;
return ("$folder/$MsgNum", $file);
}
$MsgNum++;
}
im_warn("excl_create failed.\n");
# message creation failed
return ('', '');
}
sub store_message ($$) {
my ($Msg, $dst) = @_;
local *ART;
require IM::Scan && import IM::Scan qw(store_header parse_header
parse_body disp_msg);
im_notice("saving the message into $dst\n");
if ($PrevDst ne $dst) {
if (create_folder($dst) < 0) {
return -1;
}
touch_folder($dst);
$PrevDst = $dst;
}
my ($msgfile, $filepath) = &new_message(\*ART, $dst);
my $size = 0;
if ($filepath ne '') {
my $line;
my $hcount = 0;
my $inheader = 1;
if (&unixp() && !&no_sync()) {
select (ART); $| = 1; select (STDOUT);
}
im_notice("creating $filepath\n");
foreach $line (@$Msg) {
$size += length($line);
if ($line eq "\n") {
$inheader = 0;
}
$hcount++ if ($inheader);
unless (print ART $line) {
im_err("writing to $filepath failed ($!).\n");
close(ART);
unlink($filepath) if (-z $filepath);
return -1;
}
}
if (&unixp() && !&no_sync()) {
if (fsync(fileno(ART)) < 0) {
im_err("writing to $filepath failed ($!).\n");
close(ART);
unlink($filepath) if (-z $filepath);
return -1;
}
}
unless (close(ART)) {
im_err("writing to $filepath failed ($!).\n");
unlink($filepath) if (-z $filepath);
return -1;
}
my @Hdr = @$Msg[0..$hcount];
my %Head;
store_header(\%Head, join('', @Hdr));
unless ($main::opt_noscan) {
splice(@$Msg, 0, $hcount);
$Head{'body:'} = &parse_body($Msg, 1);
# $Head{'bytes:'} = $size;
$Head{'kbytes:'} = int(($size + 1023) / 1024);
($Head{'number:'} = $msgfile) =~ s/^.*\///;
$Head{'folder:'} = $dst;
&parse_header(\%Head);
# if ($main::opt_thread) {
# &make_thread(%Head);
# } else {
&disp_msg(\%Head);
$main::scan_count++;
# }
}
my $mid = $Head{'message-id'};
# my $dt = $Head{'date'};
(my $ver = $Head{'mime-version'}) =~ s/\s//g;
my $master = '';
if ($ver eq '1.0') {
my $ct = $Head{'content-type'} . ';';
$ct =~ s/\s//g;
if ($ct =~ m|^Message/partial;(.*;)?id=([^;]+);|i) {
$master = $2;
$master =~ s/^"(.*)"$/$1/;
}
}
if (&msgdbfile() ne '' && $mid ne '') {
require IM::History && import IM::History;
unless (history_open(1) < 0) {
history_store($mid, $msgfile);
history_store("partial:$master", $mid) if ($master ne '');
history_close();
}
}
return 0;
} else {
im_err("message can not be saved to $dst.\n");
return -1;
}
}
sub exec_getsbrfile ($) {
my $dst = shift;
my $get_hook = getsbrfile();
if ($get_hook) {
if ($main::INSECURE) {
im_warn("Sorry, GetSbr is ignored for SUID root script\n");
return;
}
if ($get_hook =~ /^(\S+)$/) {
$get_hook = $1; # to pass through taint check
if (-f $get_hook) {
require $get_hook;
} else {
im_err("get subroutine file $get_hook not found.\n");
}
}
eval { &get_sub($dst, $First, $Last); };
if ($@) {
im_warn("Form seems to be wrong.\nPerl error message is: $@");
}
}
return;
}
##### OPEN FILE FOR FCC #####
#
# open_fcc(folder_name, save_style)
# folder_name: a folder name to be saved in
# save_style:
# 0 = messages in a file
# 1 = separated messages in a directory
# return values: (handle, fcc_dir, path, rm_file_on_error)
# handle:
# NULL : failed
# Handle: success
# fcc_dir: directory name
# path: file name to be saved
# rm_file_on_error: a path to be deleted on error
#
sub open_fcc ($$) {
my ($folder, $dir_style) = @_;
my ($fcc_dir, $rm_file_on_error, $fcc_folder, $FILE, $msgfile);
$fcc_folder = &expand_path($folder);
if (-d $fcc_folder) {
$fcc_dir = 1;
} elsif (-f $fcc_folder) {
$fcc_dir = 0;
} else {
# set default style unless exists
$fcc_dir = $dir_style;
}
im_debug("FCC style: ".($fcc_dir?"Dir":"File")."\n") if (&debug('fcc'));
unless ($fcc_dir) {
msg_mode(1);
im_debug("FCC file: $fcc_folder\n") if (&debug('fcc'));
unless (open(FCC, ">>$fcc_folder")) {
im_warn("can't open FCC file: $fcc_folder\n");
return undef;
}
my $date = &gen_date(2);
unless (print(FCC "From $main::Sender $date\n")) {
close(FCC);
im_warn("can't write FCC file: $fcc_folder\n");
return undef;
}
$msgfile = $folder;
$rm_file_on_error = '';
} else {
unless (-d $fcc_folder) {
if (create_folder($fcc_folder) < 0) {
im_warn("can't create folder: $fcc_folder\n");
return undef;
}
}
($msgfile, $rm_file_on_error) = &new_message(\*FCC, $folder);
return undef if ($msgfile eq '');
touch_folder($msgfile);
im_debug("FCC storing in $rm_file_on_error\n")
if (&debug('fcc'));
}
if (&unixp() && !&no_sync()) {
select (FCC); $| = 1; select (STDOUT);
}
return (\*FCC, $fcc_dir, $msgfile, $rm_file_on_error);
}
# excl_create(handle, file)
# file: path of file to be created exclusively
# handle: file handle
# return value:
# 0: success
# -1: fail
#
sub excl_create (*$) {
(local *MESSAGE, my $file) = @_;
msg_mode(1);
return -1 unless (sysopen(MESSAGE, $file, @im_file_attr@));
return 0;
}
sub fsync ($) {
my $fno = shift;
unless (defined($sys_fsync)) {
my $inc = 'syscall.ph'; # only for BSDs?
my $prefix;
if (-f '/usr/include/sys.s') { # for IRIX...
# create sys.ph from sys.s
require 'sys.ph';
$sys_fsync = &SYS_fsync if (defined(&SYS_fsync));
}
unless ($sys_fsync) {
foreach $prefix (@INC) {
if (-f "$prefix/$inc") {
require "$prefix/$inc";
$sys_fsync = &SYS_fsync if (defined(&SYS_fsync));
last;
}
}
}
unless ($sys_fsync) {
# im_warn ("syscall.ph not found. using syscall.h instead.\n");
if (open(SYSCALL_H, '</usr/include/sys/syscall.h')) {
while (<SYSCALL_H>) {
if (/^\s*#\s*define\s+SYS_fsync\s+(\d+)/) {
$sys_fsync = $1;
last;
}
}
close(SYSCALL_H);
}
}
unless ($sys_fsync) {
im_die("Can't find a way to fsync(). Set NoSync=yes in your Config file and be careful on file system overflow if your mail folders are on NFS.\n");
}
}
return syscall($sys_fsync, $fno);
}
1;
|