| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 25748 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 55 |  | 
| 2 |  |  |  |  |  |  | package Template::Plugin::Mariachi; | 
| 3 | 1 |  |  | 1 |  | 413 | use URI::Find::Schemeless::Stricter; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | use Email::Find; | 
| 5 |  |  |  |  |  |  | use Carp qw(croak); | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | use base qw(Template::Plugin::Filter); | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $FILTER_NAME = "mariachi"; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | Template::Plugin::Mariachi - gussy up email for the Mariachi mailing list archiver | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | [% USE Mariachi %] | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | From: [% message.from | mariachi(uris => 0) %] 
 | 
| 20 |  |  |  |  |  |  | Subject: [% message.subject | html | mariachi %] 
 | 
| 21 |  |  |  |  |  |  | Date: [% date.format(message.epoch_date) %] 
 | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | [% message.body | html | mariachi %] | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | Used by the mariachi mailing list archiver to make emails more | 
| 28 |  |  |  |  |  |  | suitable for display as html by hiding email addresses and turning | 
| 29 |  |  |  |  |  |  | bare urls into links. | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | Theoretically this could be done with some other C | 
| 32 |  |  |  |  |  |  | plugins but this is easier for us. | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | =head1 METHODS | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =head2 [% USE Mariachi %] | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | Initialise the Mariahci filter in your template. Can take options like so: | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | [% USE Mariachi( uris => 0, email => 1) %] | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | which, in this case, turns off uri munging and turns on email munging. | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | Both options are on by default. | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | =cut | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub init { | 
| 49 |  |  |  |  |  |  | my ($self,@args)  = @_; | 
| 50 |  |  |  |  |  |  | my $config = (ref $args[-1] eq 'HASH')? pop @args : {}; | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | $self->{_DYNAMIC}   = 1; | 
| 53 |  |  |  |  |  |  | $self->{_MYCONFIG}  = $config; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | $self->install_filter($FILTER_NAME); | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | return $self; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =head2 [% FILTER mariachi %] | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | =head2 [% somevar | mariachi %] | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | Filter some text. Can take options in a similar manner to initialisation. | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | [% FILTER mariachi(email => 0) %] | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | [% somevar | mariachi(uris => 0) %] | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =cut | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | # possibly extraneous cargo culting but it works so ... | 
| 74 |  |  |  |  |  |  | sub filter { | 
| 75 |  |  |  |  |  |  | my ($self, $text, @args) = @_; | 
| 76 |  |  |  |  |  |  | my $config = (ref $args[-1] eq 'HASH')? pop @args : {}; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | if ($self->_should_do('email', $config)) { | 
| 79 |  |  |  |  |  |  | find_emails($text, \&munge_email); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | if ($self->_should_do('uris', $config)) { | 
| 83 |  |  |  |  |  |  | URI::Find::Schemeless::Stricter->new(\&munge_uri)->find(\$text); | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | if ($self->_should_do('quoting', $config)) { | 
| 87 |  |  |  |  |  |  | munge_quoting(\$text); | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | return $text; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub _should_do { | 
| 95 |  |  |  |  |  |  | my $self   = shift; | 
| 96 |  |  |  |  |  |  | my $key    = shift || croak("Must pass a key"); | 
| 97 |  |  |  |  |  |  | my $config = shift || {}; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | # if it's defined in the local config then use that value | 
| 100 |  |  |  |  |  |  | return $config->{$key}             if defined $config->{$key}; | 
| 101 |  |  |  |  |  |  | # otherwise check in the initialised config | 
| 102 |  |  |  |  |  |  | return $self->{_MY_CONFIG}->{$key} if defined $self->{_MY_CONFIG}->{$key}; | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | # otherwise we're on by default | 
| 105 |  |  |  |  |  |  | return 1; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =head2 munge_quoting | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | Takes a reference to some text and returns it munged for quoting | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =cut | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub munge_quoting { | 
| 117 |  |  |  |  |  |  | my $textref = shift; | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | $$textref =~ s!^(\s*>.+)$!$1!gm; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =head2 munge_email | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | Takes exactly the same options as callbacks to | 
| 125 |  |  |  |  |  |  | C. Currently turns all non period characters in the | 
| 126 |  |  |  |  |  |  | domain part of an email address and turns them into 'x's such that : | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | simon@thegestalt.org | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | becomes | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | simon@xxxxxxxxxx.xxx | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | Should be overridden if you want different behaviour. | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =cut | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | sub munge_email { | 
| 139 |  |  |  |  |  |  | my ($email, $orig_email) = @_; | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | $orig_email =~ s{ | 
| 142 |  |  |  |  |  |  | \@(.+)$                    # everything after the '@' | 
| 143 |  |  |  |  |  |  | }{ | 
| 144 |  |  |  |  |  |  | "@". | 
| 145 |  |  |  |  |  |  | join '.',                  # join together with dots | 
| 146 |  |  |  |  |  |  | map { "x" x length($_) } # make each part into 'x's | 
| 147 |  |  |  |  |  |  | split /\./, $1         # split stuff apart on dots | 
| 148 |  |  |  |  |  |  | }ex; | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | return $orig_email; | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head2 munge_uri | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | Takes exactly the same options as callbacks to C although | 
| 156 |  |  |  |  |  |  | it actually uses C. | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | As such you should be wary if overriding that the uri may not have a | 
| 159 |  |  |  |  |  |  | scheme. This | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | $uri->scheme('http') unless defined $uri->scheme; | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | solves that particular problem (for various values of solve) | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | Currently just turns uris into simple clickable links | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | www.foo.com | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | becomes | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | www.foo.com | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | Should be overridden if you want different behaviour. | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | =cut | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | sub munge_uri { | 
| 180 |  |  |  |  |  |  | my ($uri,$orig_uri) = @_; | 
| 181 |  |  |  |  |  |  | $uri->scheme('http') unless defined $uri->scheme(); | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | return "$orig_uri"; | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | 1; | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | __END__ |