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   6 use strict;
  1         1  
  1         29  
2 1     1   6 use warnings;
  1         1  
  1         42  
3              
4             package App::Addex::Output::Mutt;
5             # ABSTRACT: generate mutt configuration from an address book
6             $App::Addex::Output::Mutt::VERSION = '0.026';
7 1     1   5 use parent qw(App::Addex::Output::ToFile);
  1         1  
  1         6  
8              
9 1     1   973 use Text::Unidecode ();
  1         3351  
  1         834  
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 2 my ($class, $arg) = @_;
38 1   50     4 $arg ||= {};
39              
40 1         11 my $self = $class->SUPER::new($arg);
41              
42 1         4 $self->{unidecode} = $arg->{unidecode};
43              
44 1         8 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   12 my ($self, $text) = @_;
57              
58 9 50       20 return unless defined $text;
59 9         13 $text =~ tr/ .'//d;
60 9 50       28 Text::Unidecode::unidecode($text) if $self->{unidecode};
61 9         31 return lc $text;
62             }
63              
64             sub _is_group {
65 20 50 33 20   53 return($_[0] =~ /;$/ && $_[0] =~ /:/ ? 1 : 0);
66             }
67              
68             sub process_entry {
69 6     6 1 9 my ($self, $addex, $entry) = @_;
70              
71 6         17 my $name = $entry->name;
72 6         16 my @emails = $entry->emails;
73              
74 6         22 my $folder = $entry->field('folder');
75 6         210 my $sig = $entry->field('sig');
76              
77 6 100       257 if ($folder) {
78 3         6 $folder =~ tr{/}{.};
79 3         5 $self->output("save-hook ~f$_ =$folder") for grep { $_->sends } @emails;
  3         9  
80 3 100       18 $self->output("mailboxes =$folder")
81             unless $self->{_saw_folder}{$folder}++;
82             }
83              
84 6 100       15 if ($sig) {
85 1         14 $self->output(qq{send-hook ~t$_ set signature="~/.sig/$sig"})
86 1         3 for grep { $_->receives } @emails;
87             }
88              
89 9         20 my @aliases =
90 6         20 map { $self->_aliasify($_) } grep { defined } $entry->nick, $name;
  12         25  
91              
92 6         17 my @name_strs = (qq{"$name" }, q{});
93              
94 6         9 my ($rcpt_email) = grep { $_->receives } @emails;
  12         35  
95             $self->output("alias $_ $name_strs[_is_group($rcpt_email)]<$rcpt_email>")
96 6         22 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       36 if (@emails > 1) {
101 2         3 my %label_count;
102              
103 2 100       7 if (defined(my $label = $rcpt_email->label)) {
104             $self->output("alias $_-$label $name_strs[_is_group($rcpt_email)]<$rcpt_email>")
105 1         6 for @aliases;
106              
107 1         4 $label_count{$label} = 1;
108             }
109              
110 2         4 my @rcpt_emails = grep { $_->receives } @emails;
  8         21  
111 2         7 for my $i (1 .. $#rcpt_emails) {
112 6         20 my $label = $rcpt_emails[$i]->label;
113 6 100       16 $label = '' unless defined $label;
114 6         11 $label_count{$label}++;
115              
116 6         9 for my $id (@aliases) {
117 9 100       24 my $alias = length $label ? "$id-$label" : $id;
118 9 100       24 $alias .= "-" . ($label_count{$label} - 1) if $label_count{$label} > 1;
119              
120 9         26 $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.026
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 METHODS
157              
158             =head2 process_entry
159              
160             $mutt_outputter->process_entry($addex, $entry);
161              
162             This method does the actual writing of configuration to the file.
163              
164             =head1 CONFIGURATION
165              
166             The valid configuration parameters for this plugin are:
167              
168             filename - the filename to which to write the Mutt configuration
169              
170             unidecode - if set (to 1) this will transliterate all aliases to ascii before
171             adding them to the file
172              
173             =head1 AUTHOR
174              
175             Ricardo SIGNES <rjbs@cpan.org>
176              
177             =head1 COPYRIGHT AND LICENSE
178              
179             This software is copyright (c) 2006 by Ricardo SIGNES.
180              
181             This is free software; you can redistribute it and/or modify it under
182             the same terms as the Perl 5 programming language system itself.
183              
184             =cut