1use Test;
2BEGIN { $| = 1; plan(tests => 4); chdir 't' if -d 't'; }
3use blib;
4
5##
6## editing a new recipe file programmatically
7##
8
9use Mail::Procmailrc;
10
11my $pmrc = new Mail::Procmailrc;
12
13$rcfile =<<'_RCFILE_';
14## this is my procmailrc file
15LOGABSTRACT=yes
16
17PMDIR=$HOME/.procmail
18
19:0H:
20## deliver stuff from home
21* 1^0 From: dad
22* 1^0 From: mom
23* 1^0 From: babycakes
24$DEFAULT
25
26:0B:
27## block indecent emails
28* 1^0 people talking dirty
29* 1^0 dirty persian poetry
30* 1^0 dirty pictures
31* 1^0 xxx
32/dev/null
33_RCFILE_
34
35ok( $pmrc = new Mail::Procmailrc( { 'data' => $rcfile } ) );
36ok( $pmrc->dump, $rcfile );
37
38## alter a literal
39for my $literal ( @{$pmrc->literals} ) {
40    next unless $literal->literal =~ /my procmailrc file/i;
41    $literal->literal('## this is a borrowed procmailrc file');
42    last;
43}
44
45## alter a variable
46for my $variable ( @{$pmrc->variables} ) {
47    next unless $variable->lval() eq 'LOGABSTRACT';
48    $variable->rval('no');
49    last;
50}
51
52## add a new condition or two to a recipe
53my $conditions;
54for my $recipe (@{$pmrc->recipes}) {
55    next unless $recipe->info()->[0] =~ /^\s*\#\# block indecent emails/i;
56    $conditions = $recipe->conditions();
57    last;
58}
59
60ok( scalar(@$conditions) );
61
62push @$conditions, '* 1^0 my name is not important';
63push @$conditions, '* 1^0 I have a very low IQ';
64
65## check out the results
66ok( $pmrc->dump, <<'_RCFILE_' );
67## this is a borrowed procmailrc file
68LOGABSTRACT=no
69
70PMDIR=$HOME/.procmail
71
72:0H:
73## deliver stuff from home
74* 1^0 From: dad
75* 1^0 From: mom
76* 1^0 From: babycakes
77$DEFAULT
78
79:0B:
80## block indecent emails
81* 1^0 people talking dirty
82* 1^0 dirty persian poetry
83* 1^0 dirty pictures
84* 1^0 xxx
85* 1^0 my name is not important
86* 1^0 I have a very low IQ
87/dev/null
88_RCFILE_
89
90exit;
91