-
Notifications
You must be signed in to change notification settings - Fork 0
/
svn-watch.pl
executable file
·1527 lines (1191 loc) · 40.6 KB
/
svn-watch.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env perl
# svn-watch.pl
use strict;
use warnings;
use feature qw(say);
use File::Basename;
use Getopt::Long;
use Data::Dumper;
use Pod::Usage;
sub debug;
use constant {
DIRECTORY_DEFAULT => 'watchers',
SUFFIX_DEFAULT => 'cfg',
DOMAIN_DEFAULT => 'travelclick.com',
SMTP_HOST_DEFAULT => 'webmail.travelclick.com',
SENDER_DEFAULT => 'svnwatch@travelclick.com',
SVNLOOK_DEFAULT => '/usr/local/bin/svnlook',
DEBUG_DEFAULT => '0',
URL_DEFAULT => 'http://svn.travelclick.net/svn/TravelClick',
SUBJECT_DEFAULT => '[SVN-WATCH] Revision %REVISION%: Change in '
. 'Subversion repository',
};
use constant {
WATCH_EMAIL_LINE => qr/^\s*e?mail\s*=\s*(.*)/,
WATCH_GLOB_LINE => qr/^\s*(?:file|glob)\s*=\s*(.*)/,
WATCH_REGEX_LINE => qr/^\s*(?:match|regex)\s*=\s*(.*)/,
};
#
# Default Notification Message
#
use constant MESSAGE_DEFAULT => <<MESSAGE;
%USER%:
A file that you are watching in the Subversion repository has been
changed. You are being notified of this change because you have
configured your Subversion repository watch file to detect this change.
Subversion Repository: %URL%
Subversion Revision: %REVISION%
Author of Change: %AUTHOR%
Commit Message: %COMMIT%
Files that were changed:
------------------------------------------------------------------------
%CHANGED%
To modify your watch files, you need to checkout your watchfile and modify
it. Your watch file is at %URL%/%WATCHFILE%.
MESSAGE
########################################################################
# GET COMMAND LINE OPTIONS
#
my $watch_file_dir = DIRECTORY_DEFAULT;
my $watch_file_suffix = SUFFIX_DEFAULT;
my $default_domain = DOMAIN_DEFAULT;
my $smtp_host = SMTP_HOST_DEFAULT;
my $message_sender = SENDER_DEFAULT;
my $subject = SUBJECT_DEFAULT;
my $svnlook_cmd = SVNLOOK_DEFAULT;
our $debug_level = DEBUG_DEFAULT;
my $repository_url = URL_DEFAULT;
my ( $revision, $message_file, $smtp_user, $smtp_password, $help, $options,
$no_email );
GetOptions(
"debug=i" => \$debug_level,
"directory=s" => \$watch_file_dir,
"domain=s" => \$default_domain,
"help" => \$help,
"message=s" => \$message_file,
"noemail" => \$no_email,
"options" => \$options,
"revision=s" => \$revision,
"sender=s" => \$message_sender,
"smtphost=s" => \$smtp_host,
"smtppassword=s" => \$smtp_password,
"smtpuser=s" => \$smtp_user,
"subject=s" => \$subject,
"svnlook=s" => \$svnlook_cmd,
"suffix=s" => \$watch_file_suffix,
"url=s" => \$repository_url,
) or pod2usage( { -message => "ERROR: Invalid Parameter" } );
my $repository = $ARGV[0];
if ($help) {
pod2usage(
{
-message =>
qq(Use "svn-watch.pl -options" to see a detailed description of the parameters),
-exitstatus => 0,
-verbose => 0,
}
);
}
if ($options) {
pod2usage( { -exitstatus => 0 } );
}
if ( not $repository ) {
pod2usage( { -message => "ERROR: Missing Repository directory" } );
}
if ( not $revision ) {
pod2usage( { -message => "ERROR: Missing revision number" } );
}
my $message;
if ($message_file) {
open( my $message_fh, "$message_file" )
or die( qq(ERROR: Message File "$message_file" )
. qq(can't be opened for reading: $!\n) );
$message = join "\n" => <$message_fh>;
close $message_fh
}
else {
$message = MESSAGE_DEFAULT;
}
chomp( my $author = qx($svnlook_cmd author -r $revision "$repository") );
chomp( my $comment = qx($svnlook_cmd log -r $revision "$repository") );
my $watch = Watch->new(
{
author => $author,
comment => $comment,
domain => $default_domain,
message => $message,
revision => $revision,
repository => $repository,
sender => $message_sender,
subject => $subject,
smtphost => $smtp_host,
url => $repository_url,
watch_dir => $watch_file_dir,
}
);
$watch->Smtp_User($smtp_user) if ( defined $smtp_user );
$watch->Smtp_Password($smtp_password) if ( defined $smtp_password );
#
########################################################################
########################################################################
# GATHER WATCHFILE NAMES AND USERS
#
for my $watch_file (qx($svnlook_cmd tree -N "$repository" "$watch_file_dir"))
{
chomp($watch_file);
$watch_file =~ s/^\s+//;
my $user = $watch_file;
next if $user !~ s/\.$watch_file_suffix$//;
next if $author eq $user; #Don't notify watcher if they did commit
my $watcher = Watcher->new( $user, $watch_file );
$watch->Watcher($watcher);
}
#
########################################################################
########################################################################
# GO THROUGH ALL WATCHERS AND GATHER THE INFORMATION
#
if ( not defined $watch->Watcher ) {
exit 0; #No watchers Defined. No need to do any further processing
}
for my $watcher ( $watch->Watcher ) {
my $watch_file = $watcher->Watch_File;
my $repository = $watch->Repository;
for my $line (qx[$svnlook_cmd cat $repository "$watch_file_dir/$watch_file"])
{
chomp $line;
my ( $type, $value ) = process_line($line);
next if not $type; #Line not valid if process_line returns null;
if ( $type eq "EMAIL" and not $no_email ) {
$watcher->Email_List($value);
}
elsif ( $type eq "REGEX" ) {
$watcher->Watch_List($value);
}
elsif ( $type eq "GLOB" ) {
my $regex = glob2regex($value);
$watcher->Watch_List($regex);
}
}
}
#
########################################################################
########################################################################
# NOW FOR EACH CHANGE, SEE IF A USER IS WATCHING FOR IT
#
for my $line (qx/$svnlook_cmd changed -r $revision "$repository"/) {
chomp($line);
for my $watcher ( $watch->Watcher ) {
my ( $change_type, $file_changed ) = split( /\s+/, $line, 2 );
if ( $watcher->Find($file_changed) ) {
$watcher->Notify($line);
}
}
}
#
########################################################################
########################################################################
# SEND OUT THE NOTIFICATIONS
#
for my $watcher ( $watch->Watcher ) {
if ( $watcher->Notify ) {
$watch->Send_Email($watcher);
}
}
exit $debug_level;
#
########################################################################
########################################################################
# Subroutine debug
#
# This function prints out the debugging information based upon
sub debug {
my $message = shift;
my $message_level = shift;
our $debug_level;
$message_level = 1 if not defined $message_level;
return if $message_level > $debug_level;
my $print_message = " " x $message_level . "DEBUG: $message\n";
print STDERR $print_message;
return $print_message;
}
########################################################################
# Subroutine process_line
#
# This function takes a line from the Watch file, and determines if it
# is an email address, a regular expression, or a glob, and returns
# a two member list. The 0th member of the list will be the string
# "EMAIL", "REGEX", or "GLOB" and the second member will be the
# email address, regular expression, or glob returned.
#
sub process_line {
my $line = shift;
chomp $line;
if ( $line =~ /@{[WATCH_EMAIL_LINE]}/i ) {
return ( EMAIL => $1 ), 4;
}
elsif ( $line =~ /@{[WATCH_GLOB_LINE]}/i ) {
return ( GLOB => $1 );
}
elsif ( $line =~ /@{[WATCH_REGEX_LINE]}/i ) {
return ( REGEX => $1 );
}
else {
return;
}
}
#
########################################################################
########################################################################
# SUBROUTINE glob2regex
#
# Globs are easier for developers, but Perl uses regular expressions.
# This means we have to convert from one to the other. In Ant Globs,
# the following characters have special meaning:
#
# ? - Match any single character. Equivelent to: .
# * - Match any string of characters with in a directory.
# ** - Match any string of characters in a directory tree:
#
# The questionmark is easy: It's equal to the regular expression period (.)
#
# The single astrisk is harder. It can be a string of characters of any
# length, but it can't include a directory separator. That's [^/]*.
#
# Double astrisk is like the .* match in regular expressions
#
# Here's the trick: When we see a single astrisk, we have to wait until
# we check the character after it. If it's also an astrisk, we know to
# substitute .* for both of them. If the next letter isn't an asstrisk,
# we need to substiture [^/]* for the astrisk and then figure out what
# to do with the next character.
#
# We also have to quote out special regular expression characters to
# remove their magic, and while we're at it, we will also translate
# back slashes (which are directory separartors in Windows to forward
# slashes.
#
sub glob2regex {
my $glob = shift;
my $regex = undef;
my $previous_astrisk = undef;
for my $letter ( split( //, $glob ) ) {
#
# ####Check if previous letter was astrisk
#
if ($previous_astrisk) {
if ( $letter eq "*" ) { #Double astrisk
$regex .= ".*";
$previous_astrisk = undef;
next;
}
else { #Single astrisk: Write prev match and handle current letter
$regex .= "[^/]*";
$previous_astrisk = undef;
}
}
#
# ####Quote all Regular expression characters w/ no meaning in glob
#
if ( $letter =~ /[\{\}\.\+\(\)\[\]]/ ) {
$regex .= "\\$letter";
#
# ####Translate "?" to Regular expression equivelent
#
}
elsif ( $letter eq "?" ) {
$regex .= ".";
#
# ####You don't know how to handle astrisks until you see the next one
#
}
elsif ( $letter eq "*" ) {
$previous_astrisk = 1;
#
# ####Convert backslashes to forward slashes
#
}
elsif ( $letter eq '\\' ) {
$regex .= "/";
#
# ####Just a letter
#
}
else {
$regex .= $letter;
}
}
#
# ####Handle if last letter was astrisk
#
if ($previous_astrisk) {
$regex .= "[^/]*";
}
#
# ####Globs are anchored to both beginning and ending
#
$regex = "^$regex\$";
return $regex;
}
#
########################################################################
########################################################################
# PACKAGE WATCH
#
# This object type tracks the basic information the program needs to
# run. For example, Author, Repository, Etc. It also contans the list
# of file changes associated with the list. These changes are of the
# type "Change". And, it also keeps track of all the watchers. These
# are object type "Watcher"
#
package Watch;
use Carp;
use Net::SMTP;
use Data::Dumper;
########################################################################
# CONSTRUCTOR Watch->new
#
# Parameters: Hash
# Return: Watch Object
#
sub new {
my $class = shift;
my %params = %{ (shift) };
my $self = {};
bless $self, $class;
$self->Author( $params{author} );
$self->Comment( $params{comment} );
$self->Default_Domain( $params{domain} );
$self->Message( $params{message} );
$self->Revision( $params{revision} );
$self->Repository( $params{repository} );
$self->Sender( $params{sender} );
$self->Smtp_Host( $params{smtphost} );
$self->Smtp_Password( $params{smtppass} );
$self->Subject( $params{subject} );
$self->Url( $params{url} );
$self->Watch_Dir( $params{watch_dir} );
return $self;
}
#
########################################################################
########################################################################
# Method Watch->Author
#
# Getter/Setter of user who committed the change
#
sub Author {
my $self = shift;
my $author = shift;
if ( defined $author ) {
$self->{AUTHOR} = $author;
}
return $self->{AUTHOR};
}
#
########################################################################
########################################################################
# Method Watch->Comment
#
# Getter/Setter of commit message
#
sub Comment {
my $self = shift;
my $command = shift;
if ( defined $comment ) {
$self->{COMMENT} = $comment;
}
return $self->{COMMENT};
}
#
########################################################################
########################################################################
# Method Watch->Default_Domain
#
# Getter/Setter of email default domain
#
sub Default_Domain {
my $self = shift;
my $domain = shift;
if ( defined $domain ) {
$self->{DOMAIN} = $domain;
}
return $self->{DOMAIN};
}
#
########################################################################
########################################################################
# Method Watch->Message
#
# Getter/Setter of email template message
#
sub Message {
my $self = shift;
my $message = shift;
if ( defined $message ) {
$self->{MESSAGE} = $message;
}
return $self->{MESSAGE};
}
#
########################################################################
########################################################################
# Method Watch->Repository
#
# Getter/Setter of Subversion repository directory location
#
sub Repository {
my $self = shift;
my $repository = shift;
if ( defined $repository ) {
$self->{REPOSITORY} = $repository;
}
return $self->{REPOSITORY};
}
#
########################################################################
########################################################################
# Method Watch->Sender
#
# Getter/Setter of email address for sender
#
sub Sender {
my $self = shift;
my $sender = shift;
if ( defined $sender ) {
$self->{SENDER} = $sender;
}
return $self->{SENDER};
}
#
########################################################################
########################################################################
# Method Watch->Smtp_Host
#
# Getter/Setter of SMTP Host
#
sub Smtp_Host {
my $self = shift;
my $host = shift;
if ( defined $host ) {
$self->{SMTP_HOST} = $host;
}
return $self->{SMTP_HOST};
}
#
########################################################################
########################################################################
# Method Watch->Smtp_User
#
# Getter/Setter of Account needed for connecting to SMTP host
#
sub Smtp_User {
my $self = shift;
my $user = shift;
if ( defined $user ) {
$self->{SMTP_USER} = $user;
}
return $self->{SMTP_USER};
}
#
########################################################################
########################################################################
# Method Watch->Smtp_Password
#
# Getter/Setter of password for account needed for connecting to SMTP Host
#
sub Smtp_Password {
my $self = shift;
my $pass = shift;
if ( defined $pass ) {
$self->{SMTP_PASSWORD} = $pass;
}
return $self->{SMTP_PASSWORD};
}
#
########################################################################
########################################################################
# Method Watch->Subject
#
# Getter/Setter for Email Watch Subject Line
#
sub Subject {
my $self = shift;
my $subject = shift;
if ( defined $subject ) {
$self->{SUBJECT} = $subject;
}
return $self->{SUBJECT};
}
#
########################################################################
########################################################################
# Method Watch->Url
#
# Getter/Setter of URL for subversion repository
#
sub Url {
my $self = shift;
my $url = shift;
if ( defined $url ) {
$self->{URL} = $url;
}
return $self->{URL};
}
#
########################################################################
########################################################################
# Method Watch-Watch_Dir
#
# Getter/Setter of Watch Directory in Repository
#
sub Watch_Dir {
my $self = shift;
my $directory = shift;
if ( defined $directory ) {
$self->{WATCH_DIR} = $directory;
}
return $self->{WATCH_DIR};
}
#
########################################################################
########################################################################
# Method Watch->Revision
#
# Getter/Setter of Subversion revision that triggered post-commit hook
#
sub Revision {
my $self = shift;
my $rev = shift;
if ( defined $rev ) {
$self->{REVISION} = $rev;
}
return $self->{REVISION};
}
#
########################################################################
########################################################################
# Method Watch->Change
#
# List of Change->new objects. If a Change object is passed as a parameter,
# the Change object is pushed onto the list.
# Returns a list of Change Objects
#
sub Change {
my $self = shift;
my $change = shift;
$self->{CHANGE} = [] if not exists $self->{CHANGE};
if ( defined $change ) {
if ( ref($change) ne "Change" ) {
croak qq(Change must be a "Change" object);
}
push @{ $self->{CHANGE} }, $change;
}
return @{ $self->{CHANGE} };
}
#
########################################################################
########################################################################
# METHOD Watch->Watcher
#
# List of Watchers->new objects. If a Watcher object is passed as a
# parameter, the Watcher object is pushed into the list.
# Returns a list of Watcher objects.
#
sub Watcher {
my $self = shift;
my $watcher = shift;
$self->{WATCHER} = [] if not exists $self->{WATCHER};
if ( defined $watcher ) {
if ( ref($watcher) ne "Watcher" ) {
croak qq(Watcher must be a "Watcher" object);
}
push @{ $self->{WATCHER} }, $watcher;
}
return @{ $self->{WATCHER} };
}
#
########################################################################
########################################################################
# Method Watch->Send_Email
#
# Requires Watcher->new object parameter. This method sends an email out
# for all email address returned by Watcher->Email Method.
# list of changes the Watcher object is storing.
# Returns nothing
#
sub Send_Email {
my $watch = shift;
my $watcher = shift;
#
# Create a Default Email if Watcher doesn't have one
#
my @addresses = $watcher->Email_List;
if ( not @addresses and $watch->Default_Domain ) {
$watcher->Email_List( $watcher->User . '@' . $watch->Default_Domain );
}
#
# If Mail::Sendmail isn't installed, fallback to Net::SMTP
#
eval { require Mail::Sendmail; };
if ($@) {
$watch->_Send_Email_Net_SMTP($watcher);
return;
}
#
# Mail::Sendmail is installed: Use that
#
for my $email ( $watcher->Email_List ) {
my $message = $watch->Munge_Message( $watcher, $email );
my $subject =
$watch->Munge_Message( $watcher, $email, $watch->Subject );
unshift @{ $Mail::Sendmail::mailcfg{smtp} } => $watch->Smtp_Host;
$Mail::Sendmail::mailcfg{debug} = $debug_level;
my %mail = (
From => $watch->Sender,
Subject => $subject,
To => $email,
Message => $message,
);
Mail::Sendmail::sendmail(%mail);
}
return;
}
sub _Send_Email_Net_SMTP {
my $watch = shift;
my $watcher = shift;
for my $email ( $watcher->Email_List ) {
my $smtp = Net::SMTP->new(
Host => $watch->Smtp_Host,
Debug => $debug_level,
);
if ( not defined $smtp ) {
croak qq(Unable to connect to mailhost "@{[$watch->Smtp_Host]}");
}
if ($smtp_user) {
$smtp->auth( $watch->Smtp_User, $watch->Smtp_Password )
or croak
qq(Unable to connect to mailhost "@{[$watch->Smtp_Host]}")
. qq( as user "@{[$watch->Smtp_User]}");
}
if ( not $smtp->mail( $watch->Sender ) ) {
carp qq(Cannot send as user "@{[$watch->Sender]}")
. qq( on mailhost "@{[$watch->Smtp_Host]}");
next;
}
if ( not $smtp->to($email) ) {
$smtp->reset;
next; #Can't send email to this address. Skip it
}
#
# Prepare Message
#
# In Net::SMTP, the Subject and the To fields are actually part
# of the message with a separate blank line separating the
# actual message from the header.
#
my $message = $watch->Munge_Message( $watcher, $email );
my $subject =
$watch->Munge_Message( $watcher, $email, $watch->Subject );
$message = "To: $email\n" . "Subject: $subject\n\n" . $message;
$smtp->data;
$smtp->datasend("$message");
$smtp->dataend;
$smtp->quit;
}
return;
}
#
########################################################################
########################################################################
# Method Watch->Munge_Message
#
# Paramters:
# Watch object (Required)
# Email Address (Required)
# Text to Munge (Optional)
#
# This module taks the information needed from the Watch object, the
# email address passed, and the optional text field, and replaces the
# following parameters in the text with these fields.
#
# If Text to Munge is not given, then the Watch->Message is used.
#
# %EMAIL% -> Passed in email address
#
# %REVISION% -> Watch->Revision
# %URL% -> Watch->Url
# %AUTHOR% -> Watch->Author
# %COMMIT% -> Watch->Comment
#
# %USER% -> Watcher->User
# %WATCHFILE% -> Watcher->Watch_File
# %CHANGED% -> Watcher->Notify
#
sub Munge_Message {
my $watch = shift;
my $watcher = shift;
my $email = shift;
my $message = shift;
$message = $watch->Message if not defined $message;
$message =~ s/%EMAIL%/$email/g;
$message =~ s/%AUTHOR%/@{[$watch->Author]}/g;
$message =~ s/%COMMIT%/@{[$watch->Comment]}/g;
$message =~ s/%REVISION%/@{[$watch->Revision]}/g;
$message =~ s/%URL%/@{[$watch->Url]}/g;
$message =~ s/%USER%/@{[$watcher->User]}/g;
my $changes = $watcher->Notify;
$message =~ s/%CHANGED%/$changes/g;
my $watch_file = $watch->Watch_Dir . "/" . $watcher->Watch_File;
$message =~ s/%WATCHFILE%/$watch_file/g;
return $message;
}
#
########################################################################
########################################################################
# Package Watcher
#
# This object type is for the individual Watcher and tracks the
# name of their watchfile, the email addresses where they want their
# notifications sent, the files (in regex format) they want to watch,
# and all matching changes in the particular Subversion revision.
#
package Watcher;
use Carp;
########################################################################
# Constructor Watcher->new
#
# Creates a new Watcher object.
#
sub new {
my $class = shift;
my $user = shift;
my $watch_file = shift;
my $self = {};
bless $self, $class;
$self->User($user) if defined $user;
$self->Watch_File($watch_file) if defined $watch_file;
return $self;
}
#
########################################################################
########################################################################
# Method Watcher->User
#
# Getter/Setter of Watcher's Subversion user ID
#
sub User {
my $self = shift;
my $user = shift;
if ( defined $user ) {
$self->{USER} = $user;
}
return $self->{USER};
}
#
########################################################################
########################################################################
# Method Watcher->Watch_File
#
# Getter/Setter of the Watcher's watchfile in the Subversion repository
#
sub Watch_File {
my $self = shift;
my $watch_file = shift;
if ( defined $watch_file ) {
$self->{WATCHFILE} = $watch_file;
}
return $self->{WATCHFILE};
}
#
########################################################################
########################################################################
# Method Watcher->Email_List
#
# Getter/Setter of email list of where the Watcher wants their
# notifcations sent. If an Email address is passed as a parameter, it
# will be pushed onto the list of email addresses. Returns a list of
# email addresses.
#
# Email addresses are stored in a hash in order to prevent duplicates
#
sub Email_List {
my $self = shift;
my $email = shift;
if ( defined $email ) {
$self->{EMAIL} = {} if not exists $self->{EMAIL};
$self->{EMAIL}->{$email} = $email;
}
return sort keys %{ $self->{EMAIL} };
}
#
########################################################################
########################################################################
# Method Watcher->Watch_List
#
# Getter/Setter of list of files being watched in regex format. If a
# regular expression is passed to this routine, it will be added to the
# list of files being watched. Returns a list of regular expressions
# of files being watched.
#
sub Watch_List {
my $self = shift;
my $watch = shift;
$self->{WATCH} = [] if not exists $self->{WATCH};
if ( defined $watch ) {
push @{ $self->{WATCH} }, $watch;
}
return sort @{ $self->{WATCH} };
}
#
########################################################################
########################################################################
# Method Watcher->Find
#
# This method takes a file name, and checks it against the
# Watcher-Watch_List to see if there are any matches. If a match is
# found, it will be returned. Otherwise, nothing is returned.
#
sub Find {
my $self = shift;
my $file = shift;
$file = "/" . $file if $file =~ m(^/);