File Coverage

blib/lib/App/Addex/Output/Mutt.pm
Criterion Covered Total %
statement 58 58 100.0
branch 19 22 86.3
condition 2 5 40.0
subroutine 8 8 100.0
pod 2 2 100.0
total 89 95 93.6


line stmt bran cond sub pod time code
1 1     1   7 use strict;
  1         2  
  1         31  
2 1     1   5 use warnings;
  1         1  
  1         40  
3              
4             package App::Addex::Output::Mutt 0.027;
5             # ABSTRACT: generate mutt configuration from an address book
6              
7 1     1   6 use parent qw(App::Addex::Output::ToFile);
  1         2  
  1         4  
8              
9 1     1   582 use Text::Unidecode ();
  1         2949  
  1         696  
10              
11             #pod =head1 DESCRIPTION
12             #pod
13             #pod This plugin produces a file that contains a list of alias lines. The first
14             #pod email address for each entry will be aliased to the entry's aliasified nickname
15             #pod and name. Every other address will be aliased to one of those with an
16             #pod appended, incrementing counter. The entry's name is added as the alias's "real
17             #pod name."
18             #pod
19             #pod If the entry has a "folder" value (given as a line in the card's "notes" that
20             #pod looks like "folder: value") a save-hook is created to save mail from the entry
21             #pod to that folder and a mailboxes line is created for the folder. If the entry
22             #pod has a "sig" value, a send-hook is created to use that signature when composing
23             #pod a message to the entry.
24             #pod
25             #pod =head1 CONFIGURATION
26             #pod
27             #pod The valid configuration parameters for this plugin are:
28             #pod
29             #pod filename - the filename to which to write the Mutt configuration
30             #pod
31             #pod unidecode - if set (to 1) this will transliterate all aliases to ascii before
32             #pod adding them to the file
33             #pod
34             #pod =cut
35              
36             sub new {
37 1     1 1 5 my ($class, $arg) = @_;
38 1   50     5 $arg ||= {};
39              
40 1         8 my $self = $class->SUPER::new($arg);
41              
42 1         3 $self->{unidecode} = $arg->{unidecode};
43              
44 1         6 return $self;
45             }
46              
47             #pod =method process_entry
48             #pod
49             #pod $mutt_outputter->process_entry($addex, $entry);
50             #pod
51             #pod This method does the actual writing of configuration to the file.
52             #pod
53             #pod =cut
54              
55             sub _aliasify {
56 9     9   16 my ($self, $text) = @_;
57              
58 9 50       16 return unless defined $text;
59 9         18 $text =~ tr/ .'//d;
60 9 50       19 Text::Unidecode::unidecode($text) if $self->{unidecode};
61 9         23 return lc $text;
62             }
63              
64             sub _is_group {
65 20 50 33 20   43 return($_[0] =~ /;$/ && $_[0] =~ /:/ ? 1 : 0);
66             }
67              
68             sub process_entry {
69 6     6 1 12 my ($self, $addex, $entry) = @_;
70              
71 6         12 my $name = $entry->name;
72 6         17 my @emails = $entry->emails;
73              
74 6         16 my $folder = $entry->field('folder');
75 6         240 my $sig = $entry->field('sig');
76              
77 6 100       175 if ($folder) {
78 3         7 $folder =~ tr{/}{.};
79 3         5 $self->output("save-hook ~f$_ =$folder") for grep { $_->sends } @emails;
  3         8  
80             $self->output("mailboxes =$folder")
81 3 100       26 unless $self->{_saw_folder}{$folder}++;
82             }
83              
84 6 100       15 if ($sig) {
85             $self->output(qq{send-hook ~t$_ set signature="~/.sig/$sig"})
86 1         3 for grep { $_->receives } @emails;
  1         3  
87             }
88              
89             my @aliases =
90 6         32 map { $self->_aliasify($_) } grep { defined } $entry->nick, $name;
  9         19  
  12         33  
91              
92 6         17 my @name_strs = (qq{"$name" }, q{});
93              
94 6         10 my ($rcpt_email) = grep { $_->receives } @emails;
  12         24  
95             $self->output("alias $_ $name_strs[_is_group($rcpt_email)]<$rcpt_email>")
96 6         18 for @aliases;
97              
98             # It's not that you're expected to -use- these aliases, but they allow
99             # mutt's reverse_alias to do its thing.
100 6 100       23 if (@emails > 1) {
101 2         3 my %label_count;
102              
103 2 100       6 if (defined(my $label = $rcpt_email->label)) {
104             $self->output("alias $_-$label $name_strs[_is_group($rcpt_email)]<$rcpt_email>")
105 1         5 for @aliases;
106              
107 1         2 $label_count{$label} = 1;
108             }
109              
110 2         4 my @rcpt_emails = grep { $_->receives } @emails;
  8         14  
111 2         8 for my $i (1 .. $#rcpt_emails) {
112 6         16 my $label = $rcpt_emails[$i]->label;
113 6 100       14 $label = '' unless defined $label;
114 6         11 $label_count{$label}++;
115              
116 6         12 for my $id (@aliases) {
117 9 100       20 my $alias = length $label ? "$id-$label" : $id;
118 9 100       23 $alias .= "-" . ($label_count{$label} - 1) if $label_count{$label} > 1;
119              
120 9         18 $self->output("alias $alias $name_strs[_is_group($rcpt_emails[$i])]<$rcpt_emails[$i]>");
121             }
122             }
123             }
124             }
125              
126             1;
127              
128             __END__
129              
130             =pod
131              
132             =encoding UTF-8
133              
134             =head1 NAME
135              
136             App::Addex::Output::Mutt - generate mutt configuration from an address book
137              
138             =head1 VERSION
139              
140             version 0.027
141              
142             =head1 DESCRIPTION
143              
144             This plugin produces a file that contains a list of alias lines. The first
145             email address for each entry will be aliased to the entry's aliasified nickname
146             and name. Every other address will be aliased to one of those with an
147             appended, incrementing counter. The entry's name is added as the alias's "real
148             name."
149              
150             If the entry has a "folder" value (given as a line in the card's "notes" that
151             looks like "folder: value") a save-hook is created to save mail from the entry
152             to that folder and a mailboxes line is created for the folder. If the entry
153             has a "sig" value, a send-hook is created to use that signature when composing
154             a message to the entry.
155              
156             =head1 PERL VERSION SUPPORT
157              
158             This module has the same support period as perl itself: it supports the two
159             most recent versions of perl. (That is, if the most recently released version
160             is v5.40, then this module should work on both v5.40 and v5.38.)
161              
162             Although it may work on older versions of perl, no guarantee is made that the
163             minimum required version will not be increased. The version may be increased
164             for any reason, and there is no promise that patches will be accepted to lower
165             the minimum required perl.
166              
167             =head1 METHODS
168              
169             =head2 process_entry
170              
171             $mutt_outputter->process_entry($addex, $entry);
172              
173             This method does the actual writing of configuration to the file.
174              
175             =head1 CONFIGURATION
176              
177             The valid configuration parameters for this plugin are:
178              
179             filename - the filename to which to write the Mutt configuration
180              
181             unidecode - if set (to 1) this will transliterate all aliases to ascii before
182             adding them to the file
183              
184             =head1 AUTHOR
185              
186             Ricardo SIGNES <rjbs@semiotic.systems>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is copyright (c) 2006 by Ricardo SIGNES.
191              
192             This is free software; you can redistribute it and/or modify it under
193             the same terms as the Perl 5 programming language system itself.
194              
195             =cut