Removed /usr/local from CDPATH
[clearscm.git] / cc / triggers / NotifyTrigger.pl
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         NotifyTrigger.pl,v
5 # Revision:     1.1.1.1
6 # Description:  This script is a generalized notify trigger. It takes one
7 #               parameter, a message file. The format of this file is similar
8 #               to an email message. Environment variables will be substituted.
9 #
10 #               This trigger is typically added, perhaps multiple times with
11 #               different message files, then attached to elements in the vob
12 #               as needed. Make the trigger with:
13 #
14 #                       cleartool mktrtype -element -postop checkin \
15 #                         -c "<comment>" \
16 #                         -exec "<perl> <path_to_trigger>/NotifyTrigger.pl \
17 #                               <msg file>" <TRIGGER_NAME>
18 #
19 # Assumptions:  Clearprompt is in the users PATH
20 # Author:       Andrew@DeFaria.com
21 # Created:      Tue Mar 12 15:42:55  2002
22 # Modified:     2007/05/17 07:45:48
23 # Language:     Perl
24 #
25 # (c) Copyright 2004, Andrew@DeFaria.com, all rights reserved
26 #
27 ################################################################################
28 use strict;
29 use warnings;
30
31 use File::Spec;
32 use Net::SMTP;
33
34 # Define mailhost here (or set in the environment)
35 my $mailhost = defined $ENV {MAILHOST} ? $ENV {MAILHOST} : undef;
36
37 # This will be set in the BEGIN block but by putting them here the become
38 # available for the whole script.
39 my (
40   $abs_path,
41   $lib_path,
42   $log_path,
43   $me,
44   $triggers_path
45 );
46
47 BEGIN {
48   # Extract relative path and basename from script name.
49   $0 =~ /(.*)[\/\\](.*)/;
50
51   $abs_path     = (!defined $1) ? "." : File::Spec->rel2abs ($1);
52   $me           = (!defined $2) ? $0  : $2;
53
54   # Setup paths
55   $lib_path             = "$abs_path/../lib";
56   $log_path             = "$abs_path/../log";
57   $triggers_path        = "$abs_path/../triggers";
58
59   # Add the appropriate path to our modules to @INC array.
60   unshift (@INC, "$lib_path");
61 } # BEGIN
62
63 use TriggerUtils;
64
65 # This routine will replace references to environment variables. If an
66 # environment variable is not defined then the string <Unknown> is
67 # substituted.
68 sub ReplaceText {
69   my $line = shift;
70
71   while ($line =~ /\$(\w+)/) {
72     my $var = $1;
73     if ($ENV{$var} eq "") {
74       $line =~ s/\$$var/\<Unknown\>/;
75     } else {
76       my $value = $ENV{$var};
77       $value =~ s/\\/\//g;
78       $line =~ s/\$$var/$value/;
79     } # if
80   } # while
81
82   return $line;
83 } # ReplaceText
84
85 sub error {
86   my $message = shift;
87
88   clearlogmsg $message;
89
90   exit 1;
91 } # error
92
93 # First open the message file. If we can't then there's a problem, die!
94 my $msgfile = "$triggers_path/$ARGV[0]";
95 open MSG, $msgfile
96   or error "Unable to open message file:\n\n$msgfile\n\n($!)";
97
98 # Suck in file
99 my @lines = <MSG>;
100
101 # Connect to mail server
102 error "Mailhost is not defined!" if !defined $mailhost;
103 my $smtp = Net::SMTP->new ($mailhost);
104
105 error "Unable to open connection to mail host: $mailhost" if $smtp == undef;
106
107 # Compose message
108 my $data_sent = "F";
109 my $from_seen = "F";
110 my $to_seen   = "F";
111 my ($line, $from, $to, @addresses);
112
113 foreach $line (@lines) {
114   next if $line =~ /^\#/;
115   next if $line =~ /--/;
116
117   $line = ReplaceText $line;
118
119   if ($line =~ /^From:\s+/) {
120     $_ = $line;
121     $from = $line;
122     s/^From:\s+//;
123     $smtp->mail ($_);
124     $from_seen = "T";
125     next;
126   } # if
127
128   if ($line =~ /^To:\s+/) {
129     $_ = $line;
130     $to = $line;
131     s/^To:\s+//;
132     @addresses = split (/,|;| /);
133     $to_seen = "T";
134     foreach (@addresses) {
135       next if ($_ eq "");
136       $smtp->to ($_);
137     } # foreach
138     next;
139   } # if
140
141   if ($data_sent eq "F") {
142     $smtp->data ();
143     $smtp->datasend ($from);
144     $smtp->datasend ($to);
145     $data_sent = "T";
146   } # if
147
148   if ($from_seen eq "T" && $to_seen eq "T" && $data_sent eq "T") {
149     $smtp->datasend ($line);
150   } else {
151     clearlogmsg "Message file ($ARGV[0]) missing From and/or To!";
152     exit 1;
153   } # if
154 } # foreach
155
156 $smtp->dataend ();
157 $smtp->quit;
158
159 exit 0;