eee5f2644a03224d164f7503fccb3f391ad943c6
[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 use lib "$FindBin::Bin/../../lib";
25
26 use MAPSFile;
27 use MAPS;
28 use MAPSLog;
29
30 sub DeliverMail($$) {
31   my ($userid, $msgfileName) = @_;
32
33   # Open maildrop file
34   open my $maildrop, '>>', "/var/mail/$userid"
35     or return "Unable to open maildrop file (/var/mail/$userid): $!";
36
37   # Open msgfile
38   open my $msgfile, '<', $msgfileName
39     or return "Unable to open msgfile ($msgfileName): $!";
40
41   # Lock file
42   Lock $maildrop;
43
44   # Write msgfile -> $maildrop
45   print $maildrop "\n\n";
46   print $maildrop $_ while (<$msgfile>);
47
48   # Unlock the file
49   Unlock $maildrop;
50
51   # Close files
52   close $maildrop;
53   close $msgfile;
54
55   return;
56 } # DeliverMail
57
58 # Main
59 my ($userid, $msgfile) = @ARGV;
60
61 die 'User id not specified' unless $userid;
62 die 'Msgfile not specified' unless $msgfile;
63
64 my $err = DeliverMail($userid, $msgfile);
65
66 if ($err) {
67   OpenDB('maps', 'spam');
68
69   SetContext($userid);
70
71   Error($err);
72
73   exit 1;
74 } # if
75
76 exit 0;