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