blib/lib/HTML/EP/Examples/POP3Client.pm untested

filter: Statementstmt 0.0% 0 / 157 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 61 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 355 CC 43 SCAR 47.0
CC
43
Cov
0%
_ep_examples_pop3client_show
182.0
_ep_examples_pop3client_reply
156.0
_ep_examples_pop3client_list
132.0
CRAP 110.3
1 # -*- perl -*-
2 #
3 # HTML::EP - A Perl based HTML extension.
4 #
5 #
6 # Copyright (C) 1998 Jochen Wiedmann
7 # Am Eisteich 9
8 # 72555 Metzingen
9 # Germany
10 #
11 # Phone: +49 7123 14887
12 # Email: joe@ispsoft.de
13 #
14 # All rights reserved.
15 #
16 # You may distribute this module under the terms of either
17 # the GNU General Public License or the Artistic License, as
18 # specified in the Perl README file.
19 #
20 ############################################################################
21  
22 require 5.004;
23 use strict;
24  
25 use HTML::EP ();
26 use HTML::EP::Locale ();
27 use Mail::POP3Client ();
28  
29  
30 package HTML::EP::Examples::POP3Client;
31  
32 $HTML::EP::Examples::POP3Client::VERSION = '0.01';
33 @HTML::EP::Examples::POP3Client::ISA = qw(HTML::EP::Locale HTML::EP);
34  
35  
36 sub _init {
37 my $self = shift; my $attr = shift;
38 my $debug = $self->{'debug'};
39 my $cgi = $self->{'cgi'};
40 my $server = $self->{'pop3server'} =
41 ($attr->{'pop3server'} or $cgi->param('pop3server'))
42 or die "Missing POP3 server";
43 my $user = $self->{'pop3user'} =
44 ($attr->{'pop3user'} or $cgi->param('pop3user'))
45 or die "Missing POP3 user";
46 my $password = $self->{'pop3password'} =
47 ($attr->{'pop3password'} or $cgi->param('pop3password'))
48 or die "Missing POP3 password";
49  
50 my $port;
51 if ($server =~ /(.*)\:(.*)/) {
52 $server = $1;
53 $port = $2;
54 }
55 my @args = ($user, $password, $server);
56 push(@args, $port) if defined($port) or $debug;
57 push(@args, 1) if $debug;
58  
59 $self->printf("Connecting to server %s, port %s as user %s\n",
60 $server, ($port || 110), $user) if $debug;
61  
62 Mail::POP3Client->new(@args) or die "Failed to connect: $!";
63 }
64  
65  
66 sub _ep_examples_pop3client_list {
67 my $self = shift; my $attr = shift;
68 my $pop = $self->_init($attr);
69 my $cgi = $self->{'cgi'};
70 my $start = $self->{'start'} = $cgi->param('start') || 0;
71 my $max = $self->{'max'} = $attr->{'max'} || $cgi->param('max') || 20;
72 my $debug = $self->{'debug'};
73  
74 my @sizes = map { $_ =~ s/^\d+\s+//; $_ } $pop->List();
75 my @list;
76 $self->{'count'} = $pop->Count();
77 $self->{'next'} = $start+$max if $start+$max < $self->{'count'};
78 $self->{'prev'} = $start-$max if $start>0;
79 for (my $i = $start; $i < $pop->Count() && $i < $start+$max; $i++) {
80 my($subject, $from, $id, $date);
81 foreach my $head ($pop->Head($i+1)) {
82 if ($head =~ /^\s*subject\:\s*(.*?)\s*$/i) {
83 $subject = $1;
84 } elsif ($head =~ /^\s*reply-to\:\s*(.*?)\s*$/i) {
85 $from = $1;
86 } elsif ($head =~ /^\s*from\:\s*(.*?)\s*$/i) {
87 $from = $1 unless $from;
88 } elsif ($head =~ /^\s*message-id\:\s*(.*?)\s*$/i) {
89 $id = $1;
90 } elsif ($head =~ /^\s*date\:\s*(.*?)\s*$/i) {
91 $date = $1;
92 }
93 }
94 my $mail = {'from' => ($from || ''),
95 'subject' => ($subject || ''),
96 'size' => $sizes[$i],
97 'nr' => $i+1,
98 'i' => $i,
99 'date' => $date,
100 'id' => $id};
101 $self->print("Found mail: ", %$mail, "\n") if $debug;
102 push(@list, $mail);
103 }
104 $self->{'list'} = \@list;
105 $self->{'list_num'} = @list;
106  
107 $self->print("$self->{'list_num'} items have been found.\n") if $debug;
108 '';
109 }
110  
111  
112 sub _ep_examples_pop3client_delete {
113 my $self = shift; my $attr = shift;
114 my $pop = $self->_init($attr);
115 my $cgi = $self->{'cgi'};
116 my $debug = $self->{'debug'};
117 my $count = $self->{'count'} = $pop->Count();
118 my $id = $cgi->param('id') or die "Missing Message ID";
119 my $i = $cgi->param('i');
120  
121 # Verify the message ID
122 foreach my $head ($pop->Head($i+1)) {
123 if ($head =~ /^\s*message-id\:\s*(.*?)\s*$/i) {
124 die "Mailfolder out of sync. Please reload folder."
125 unless ($1 eq $id);
126 $pop->Delete($i+1);
127 $pop->Close();
128 if ($i >= $self->{'count'} and $i > 0) {
129 --$i;
130 }
131 my $max = $self->{'max'} =
132 $attr->{'max'} || $cgi->param('max') || 20;
133 $self->{'start'} = ($i % $max);
134 return '';
135 }
136 }
137 die "Cannot delete message without Message-ID";
138 }
139  
140  
141 sub _ep_examples_pop3client_show {
142 my $self = shift; my $attr = shift;
143 my $pop = $self->_init($attr);
144 my $cgi = $self->{'cgi'};
145 my $debug = $self->{'debug'};
146  
147 $self->{'count'} = $pop->Count();
148 my $id = $cgi->param('id') or die "Missing Message ID";
149 my $i = $cgi->param('i');
150 if ($cgi->param('back')) {
151 die "No previous mails" if --$i < 0;
152 $id = 'any';
153 } elsif ($cgi->param('next')) {
154 die "No further mails" if ++$i >= $self->{'count'};
155 $id = 'any';
156 }
157 $self->{'num'} = $i+1;
158 $self->{'i'} = $i;
159 $self->{'id'} = $id;
160 die "Missing Message number" if !defined($i) || $i !~ /^\s*\d+\s*$/;
161  
162 my($subject, $from, $date);
163 my @headers;
164 foreach my $head ($pop->Head($i+1)) {
165 push(@headers, $head);
166 if ($head =~ /^\s*subject\:\s*(.*?)\s*$/i) {
167 $subject = $1;
168 } elsif ($head =~ /^\s*reply-to\:\s*(.*?)\s*$/i) {
169 $from = $1;
170 } elsif ($head =~ /^\s*from\:\s*(.*?)\s*$/i) {
171 $from = $1 unless $from;
172 } elsif ($head =~ /^\s*date\:\s*(.*?)\s*$/i) {
173 $date = $1;
174 } elsif ($head =~ /^\s*message-id\:\s*(.*?)\s*$/i) {
175 die "Mailfolder out of sync. Please reload folder."
176 unless ($id eq 'any' or $1 eq $id);
177 $self->{'id'} = $1;
178 }
179 }
180  
181 $self->{'body'} = join("\n", $pop->Body($i+1));
182 $self->{'headers'} = join("\n", @headers);
183 $self->{'from'} = $from;
184 $self->{'date'} = $date;
185 $self->{'subject'} = $subject;
186 '';
187 }
188  
189  
190 sub _ep_examples_pop3client_reply {
191 my $self = shift; my $attr = shift;
192 my $pop = $self->_init($attr);
193 my $cgi = $self->{'cgi'};
194 my $debug = $self->{'debug'};
195 my $count = $self->{'count'} = $pop->Count();
196 my $id = $cgi->param('id') or die "Missing Message ID";
197 my $i = $cgi->param('i');
198  
199 # Verify the message ID
200 my($subject, $date, $from, $to, $cc);
201 foreach my $head ($pop->Head($i+1)) {
202 if ($head =~ /^message-id\:\s*(.*?)\s*$/i) {
203 die "Mailfolder out of sync. Please reload folder."
204 unless ($1 eq $id);
205 } elsif ($head =~ /^reply-to\:\s*(.*?)\s*$/i) {
206 $from = $1;
207 } elsif ($head =~ /^from\:\s*(.*?)\s*$/i) {
208 $from = $1 unless $from;
209 } elsif ($head =~ /^date\:\s*(.*?)\s*$/i) {
210 $date = $1;
211 } elsif ($head =~ /^to\:\s*(.*?)\s*$/i) {
212 $to = $1;
213 } elsif ($head =~ /^cc\:\s*(.*?)\s*$/i) {
214 $cc = $1;
215 } elsif ($head =~ /^subject\:\s*(.*?)\s*$/i) {
216 $subject = $1;
217 }
218 }
219  
220 $self->{'date'} = $date;
221 $self->{'from'} = $from;
222 $self->{'to'} = $to;
223 $self->{'cc'} = $cc ? $to : "$to, $cc";
224 $subject =~ s/^(re|aw)\:\s+// if $subject;
225 $self->{'subject'} = "Re: $subject";
226 my $indent = $attr->{'indent'} || "> ";
227 $self->{'body'} = join("\n", map{ "$indent$_" } $pop->Body($i+1));
228  
229 '';
230 }
231  
232  
233 sub _format_HNBSP {
234 my $self = shift; my $str = shift;
235 return '&nbsp;' if !defined($str) || $str eq '';
236 $self->escapeHTML($str);
237 }
238  
239 sub _format_BR {
240 my $self = shift; my $str = shift;
241 return '' unless defined($str);
242 join("<BR>\n",
243 map { $self->escapeHTML($_) } split(/\r?\n/s, $str));
244 }
245  
246 1;