| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | |
|---|
| 5 | BEGIN { chdir 't' if -d 't' } |
|---|
| 6 | use lib '../lib'; |
|---|
| 7 | |
|---|
| 8 | use Test::More tests => 35; |
|---|
| 9 | use File::Path; |
|---|
| 10 | |
|---|
| 11 | use Lancelot::DB; |
|---|
| 12 | use Lancelot::Message; |
|---|
| 13 | |
|---|
| 14 | sub copy_msg { |
|---|
| 15 | return Lancelot::Message->new($_[0]->as_string); |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | BEGIN { use_ok('Lancelot::Module::list_headers'); } |
|---|
| 19 | |
|---|
| 20 | my $listname = 'pl-testlist@example.com'; |
|---|
| 21 | |
|---|
| 22 | rmtree("$ENV{HOME}/.pl/$listname", 0); |
|---|
| 23 | |
|---|
| 24 | my $db = new Lancelot::DB $listname, { create => 1 }; |
|---|
| 25 | $db->set_config('list.name', 'Pl-testlist'); |
|---|
| 26 | $db->set_config('mail.delimiter', '+'); |
|---|
| 27 | |
|---|
| 28 | # Check RFC2919 (List-ID) |
|---|
| 29 | |
|---|
| 30 | my $msg = Lancelot::Message->new('From: hugo@example.com |
|---|
| 31 | To: susi@example.net |
|---|
| 32 | Subject: Test |
|---|
| 33 | Foo: bar |
|---|
| 34 | Baz: quux |
|---|
| 35 | |
|---|
| 36 | This is a test. |
|---|
| 37 | '); |
|---|
| 38 | |
|---|
| 39 | $db->set_config('mail.listid', '<pl-testlist.example.com>'); |
|---|
| 40 | my $tmsg = copy_msg($msg); |
|---|
| 41 | my $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 42 | is($tmsg->header('List-ID'), '<pl-testlist.example.com>', 'explicit list ID'); |
|---|
| 43 | |
|---|
| 44 | $db->delete_configs('mail.listid'); |
|---|
| 45 | $tmsg = copy_msg($msg); |
|---|
| 46 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 47 | is($tmsg->header('List-ID'), '<pl-testlist.list-id.example.com>', |
|---|
| 48 | 'default list ID'); |
|---|
| 49 | |
|---|
| 50 | # Check RFC2369 (List headers) |
|---|
| 51 | |
|---|
| 52 | $db->delete_configs('mail.listheaders'); |
|---|
| 53 | $tmsg = copy_msg($msg); |
|---|
| 54 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 55 | my @hdrs = qw/help owner subscribe unsubscribe/; |
|---|
| 56 | foreach my $h (@hdrs) { |
|---|
| 57 | ok(! defined $tmsg->header('List-'.ucfirst($h)), |
|---|
| 58 | "List-\u$h should be undefined"); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | $db->set_config('mail.listheaders', 1); |
|---|
| 62 | $tmsg = copy_msg($msg); |
|---|
| 63 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 64 | @hdrs = qw/help owner subscribe unsubscribe/; |
|---|
| 65 | foreach my $h (@hdrs) { |
|---|
| 66 | is($tmsg->header('List-'.ucfirst($h)), |
|---|
| 67 | "<mailto:pl-testlist+$h\@example.com>", |
|---|
| 68 | "List-\u$h value"); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | $db->delete_configs('mail.delimiter'); |
|---|
| 72 | $tmsg = copy_msg($msg); |
|---|
| 73 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 74 | @hdrs = qw/help owner subscribe unsubscribe/; |
|---|
| 75 | foreach my $h (@hdrs) { |
|---|
| 76 | is($tmsg->header('List-'.ucfirst($h)), |
|---|
| 77 | "<mailto:pl-testlist-$h\@example.com>", |
|---|
| 78 | "List-\u$h value (default mail.delimiter)"); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | # mail.removeheaders |
|---|
| 82 | |
|---|
| 83 | $db->set_config('mail.removeheaders', 'foo'); |
|---|
| 84 | $db->delete_configs('mail.listheaders'); |
|---|
| 85 | $tmsg = copy_msg($msg); |
|---|
| 86 | my $hcount = $tmsg->header_names(); |
|---|
| 87 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 88 | ok(! defined $tmsg->header('Foo'), |
|---|
| 89 | "removeheaders 1: foo should be removed"); |
|---|
| 90 | is(scalar($tmsg->header_names()), $hcount, |
|---|
| 91 | "removeheaders 1: all other headers still there"); |
|---|
| 92 | |
|---|
| 93 | $db->set_config('mail.removeheaders', 'foo baz'); |
|---|
| 94 | $tmsg = copy_msg($msg); |
|---|
| 95 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 96 | ok(! defined $tmsg->header('Foo'), |
|---|
| 97 | "removeheaders 2: foo should be removed"); |
|---|
| 98 | ok(! defined $tmsg->header('Baz'), |
|---|
| 99 | "removeheaders 2: baz should be removed"); |
|---|
| 100 | is(scalar($tmsg->header_names()), $hcount-1, |
|---|
| 101 | "removeheaders 1: all other headers still there"); |
|---|
| 102 | |
|---|
| 103 | $db->delete_configs('mail.listheaders'); |
|---|
| 104 | $db->set_config('mail.removeheaders', ''); |
|---|
| 105 | $tmsg = copy_msg($msg); |
|---|
| 106 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 107 | # $hcount+1 because List-ID will always be generated |
|---|
| 108 | is(scalar($tmsg->header_names()), $hcount+1, |
|---|
| 109 | "removeheaders 3: all headers still there"); |
|---|
| 110 | |
|---|
| 111 | $db->delete_configs('mail.removeheaders'); |
|---|
| 112 | |
|---|
| 113 | # mail.addheaders |
|---|
| 114 | |
|---|
| 115 | $db->set_config('mail.addheaders', 'abc'); |
|---|
| 116 | $db->set_config('mail.header.abc', 'xyzzy'); |
|---|
| 117 | $tmsg = copy_msg($msg); |
|---|
| 118 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 119 | is(scalar($tmsg->header_names()), $hcount+2, |
|---|
| 120 | "addheaders 1: one more header"); |
|---|
| 121 | is($tmsg->header('Abc'), 'xyzzy', 'new abc header value'); |
|---|
| 122 | |
|---|
| 123 | # Add one header, second header to be added does not exist |
|---|
| 124 | $db->set_config('mail.addheaders', 'abc xyz'); |
|---|
| 125 | $db->set_config('mail.header.abc', 'xyzzy'); |
|---|
| 126 | $tmsg = copy_msg($msg); |
|---|
| 127 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 128 | is(scalar($tmsg->header_names()), $hcount+2, |
|---|
| 129 | "addheaders 1: one more header"); |
|---|
| 130 | is($tmsg->header('Abc'), 'xyzzy', 'new abc header value'); |
|---|
| 131 | ok(!defined $tmsg->header('Xyz'), 'new (nonexistent) xyz header value'); |
|---|
| 132 | |
|---|
| 133 | # Add two new headers |
|---|
| 134 | $db->set_config('mail.addheaders', 'abc def'); |
|---|
| 135 | $db->set_config('mail.header.def', 'zyxxy'); |
|---|
| 136 | $tmsg = copy_msg($msg); |
|---|
| 137 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 138 | is(scalar($tmsg->header_names()), $hcount+3, |
|---|
| 139 | "addheaders 1: two more headers"); |
|---|
| 140 | is($tmsg->header('Abc'), 'xyzzy', 'new abc header value'); |
|---|
| 141 | is($tmsg->header('Def'), 'zyxxy', 'new def header value'); |
|---|
| 142 | |
|---|
| 143 | $db->delete_configs(qw/mail.addheaders mail.header.abc mail.header.def/); |
|---|
| 144 | |
|---|
| 145 | # subject header munging |
|---|
| 146 | |
|---|
| 147 | $db->set_config('mail.subjecttag', '[List] '); |
|---|
| 148 | $tmsg = copy_msg($msg); |
|---|
| 149 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 150 | is($tmsg->header('Subject'), '[List] Test', 'with subject tag'); |
|---|
| 151 | |
|---|
| 152 | $tmsg = copy_msg($msg); |
|---|
| 153 | $tmsg->header_set('Subject', 'Re: Re: [List] Test'); |
|---|
| 154 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 155 | is($tmsg->header('Subject'), 'Re: Re: [List] Test', 'with prefixes'); |
|---|
| 156 | |
|---|
| 157 | $tmsg = copy_msg($msg); |
|---|
| 158 | $tmsg->header_set('Subject', 'RE: [List] AW: [List] Test'); |
|---|
| 159 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 160 | is($tmsg->header('Subject'), 'RE: [List] AW: [List] Test', 'with prefixes 2'); |
|---|
| 161 | |
|---|
| 162 | $db->delete_configs('mail.subjecttag'); |
|---|
| 163 | |
|---|
| 164 | # reply-to munging |
|---|
| 165 | |
|---|
| 166 | $db->set_config('mail.replyto', 'pl-testlist@example.com'); |
|---|
| 167 | $db->set_config('mail.forcereplyto', 0); |
|---|
| 168 | $tmsg = copy_msg($msg); |
|---|
| 169 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 170 | is($tmsg->header('Reply-To'), 'pl-testlist@example.com', |
|---|
| 171 | 'optional list reply-to'); |
|---|
| 172 | |
|---|
| 173 | $tmsg = copy_msg($msg); |
|---|
| 174 | $tmsg->header_set('Reply-To', 'hugo@example.com'); |
|---|
| 175 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 176 | is($tmsg->header('Reply-To'), 'hugo@example.com', 'message reply-to'); |
|---|
| 177 | |
|---|
| 178 | $db->set_config('mail.forcereplyto', 1); |
|---|
| 179 | $result = Lancelot::Module::list_headers::execute($db, $tmsg); |
|---|
| 180 | is($tmsg->header('Reply-To'), 'pl-testlist@example.com', |
|---|
| 181 | 'forced list reply-to'); |
|---|
| 182 | |
|---|
| 183 | $db->delete_configs('mail.replyto'); |
|---|