Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / clearcase / triggers / NotifyTrigger.pl
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         NotifyTrigger.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 # Assumptions:  Clearprompt is in the users PATH
9 # Author:       Andrew@DeFaria.com
10 # Created:      Tue Mar 12 15:42:55  2002
11 # Language:     Perl
12 # Modifications:
13 #
14 # (c) Copyright 2002, Andrew@DeFaria.com, all rights reserved
15 #
16 ################################################################################
17 use strict;
18
19 use Net::SMTP;
20
21 my $mailhost;
22
23 BEGIN {
24   # Add the appropriate path to our modules to @INC array. We use ipconfig to
25   # get the current host's IP address then determine whether we are in the US
26   # or China. Also set our mail server.
27   my @ipconfig = grep (/IP Address/, `ipconfig`);
28   my ($ipaddr) = ($ipconfig[0] =~ /(\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3})/);
29
30   # US is in the subnets of 192 and 172 while China is in the subnet of 10
31   if ($ipaddr =~ /^192|^172/) {
32     $mailhost="sons-exch02.salira.com";
33     unshift (@INC, "//sons-clearcase/Views/official/Tools/lib");
34   } elsif ($ipaddr =~ /^10/) {
35     $mailhost="sons-exch03.salira.com";
36     unshift (@INC, "//sons-cc/Views/official/Tools/lib");
37   } else {
38     die "Internal Error: Unable to find our modules!\n"
39   } # if
40 } # BEGIN
41
42 use TriggerUtils;
43
44 # This routine will replace references to environment variables. If an
45 # environment variable is not defined then the string <Unknown> is
46 # substituted.
47 sub ReplaceText {
48   my $line = shift (@_);
49
50   my ($var, $value);
51
52   while ($line =~ /\$(\w+)/) {
53     $line =~ /\$(\w+)/;
54     $var = $1;
55     if ($ENV{$var} eq "") {
56       $line =~ s/\$$var/\<Unknown\>/;
57     } else {
58       $value = $ENV{$var};
59       $value =~ s/\\/\//g;
60       $line =~ s/\$$var/$value/;
61     } # if
62   } # while
63
64   return $line;
65 } # ReplaceText
66
67 sub error {
68   my $message = shift;
69
70   clearlogmsg $message;
71
72   exit 1;
73 } # error 
74
75 # First open the message file. If we can't then there's a problem, die!
76 open (MSG, $ARGV[0]) || error "Unable to open message file:\n\n$ARGV[0]\n\n($!)";
77
78 my @lines = <MSG>;
79
80 # Connect to mail server
81 my $smtp = Net::SMTP->new ($mailhost);
82
83 error "Unable to open connection to mail host: $mailhost" if $smtp == undef;
84
85 # Compose message
86 my $data_sent = "F";
87 my $from_seen = "F";
88 my $to_seen   = "F";
89 my ($line, $from, $to, @addresses);
90
91 foreach $line (@lines) {
92   next if $line =~ /^\#/;
93   next if $line =~ /--/;
94
95   $line = ReplaceText $line;
96
97   if ($line =~ /^From:\s+/) {
98     $_ = $line;
99     $from = $line;
100     s/^From:\s+//;
101     $smtp->mail ($_);
102     $from_seen = "T";
103     next;
104   } # if
105
106   if ($line =~ /^To:\s+/) {
107     $_ = $line;
108     $to = $line;
109     s/^To:\s+//;
110     @addresses = split (/,|;| /);
111     $to_seen = "T";
112     foreach (@addresses) {
113       next if ($_ eq "");
114       $smtp->to ($_);
115     } # foreach
116     next;
117   } # if
118
119   if ($data_sent eq "F") {
120     $smtp->data ();
121     $smtp->datasend ($from);
122     $smtp->datasend ($to);
123     $data_sent = "T";
124   } # if
125
126   if ($from_seen eq "T" && $to_seen eq "T" && $data_sent eq "T") {
127     $smtp->datasend ($line);
128   } else {
129     clearlogmsg "Message file ($ARGV[0]) missing From and/or To!";
130     exit 1;
131   } # if
132 } # foreach
133
134 $smtp->dataend ();
135 $smtp->quit;
136
137 exit 0;