ab6a2ff685906e6a04a6ebae31ca9148318e3811
[clearscm.git] / maps / bin / MAPSDeliver
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         $RCSfile: MAPSDeliver,v $
5 # Revision:     $Revision: 1.1 $
6 # Description:  This script simply delivers the mail. It is separated out so
7 #                     it can be the only portion that is setgid to the group mail
8 #               for the purposes of being able to deliver the mail to the users
9 #               maildrop
10 # Author:       Andrew@DeFaria.com
11 # Created:      Fri Nov 29 14:17:21  2002
12 # Modified:     $Date: 2013/06/12 14:05:47 $
13 # Language:     perl
14 #
15 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
16 #
17 ################################################################################
18 use strict;
19 use warnings;
20
21 use FindBin;
22
23 use lib "$FindBin::Bin/../lib";
24
25 use MAPSFile;
26 use MAPS;
27 use MAPSLog;
28
29 sub DeliverMail($$) {
30   my ($userid, $msgfileName) = @_;
31
32   # Open maildrop file
33   open my $maildrop, '>>', "/var/mail/$userid"
34     or return "Unable to open maildrop file (/var/mail/$userid): $!";
35
36   # Open msgfile
37   open my $msgfile, '<', $msgfileName
38     or return "Unable to open msgfile ($msgfileName): $!";
39
40   # Lock file
41   Lock $maildrop;
42
43   # Write msgfile -> $maildrop
44   print $maildrop "\n\n";
45   print $maildrop $_ while (<$msgfile>);
46
47   # Unlock the file
48   Unlock $maildrop;
49
50   # Close files
51   close $maildrop;
52   close $msgfile;
53
54   return;
55 } # DeliverMail
56
57 # Main
58 my ($userid, $msgfile) = @ARGV;
59
60 die 'User id not specified' unless $userid;
61 die 'Msgfile not specified' unless $msgfile;
62
63 my $err = DeliverMail($userid, $msgfile);
64
65 if ($err) {
66   OpenDB('maps', 'spam');
67
68   SetContext($userid);
69
70   Error($err);
71
72   exit 1;
73 } # if
74
75 exit 0;