Mail.pm untested

filter: Statementstmt 0.0% 0 / 154 Branchbran 0.0% 0 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 200 CC 15 SCAR 41.7
CC
15
Cov
0%
ParseRecord
132.0
PackRecord
30.0
import
2.0
CRAP 64.8
1 # Palm::Mail.pm
2 #
3 # Perl class for dealing with Palm Mail databases.
4 #
5 # Copyright (C) 1999, 2000, Andrew Arensburger.
6 # You may distribute this file under the terms of the Artistic
7 # License, as specified in the README file.
8 #
9 # $Id: Mail.pm,v 1.16 2002/11/07 14:11:59 arensb Exp $
10  
11 use strict;
12 package Palm::Mail;
13 use Palm::Raw();
14 use Palm::StdAppInfo();
15 use vars qw( $VERSION @ISA );
16  
17 # One liner, to allow MakeMaker to work.
18 $VERSION = do { my @r = (q$Revision: 1.16 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
19  
20 @ISA = qw( Palm::StdAppInfo Palm::Raw );
21  
22 =head1 NAME
23  
24 Palm::Mail - Handler for Palm Mail databases.
25  
26 =head1 SYNOPSIS
27  
28 use Palm::Mail;
29  
30 =head1 DESCRIPTION
31  
32 The Mail PDB handler is a helper class for the Palm::PDB package. It
33 parses Mail databases.
34  
35 =head2 AppInfo block
36  
37 The AppInfo block begins with standard category support. See
38 L<Palm::StdAppInfo> for details.
39  
40 Other fields include:
41  
42 $pdb->{appinfo}{sortOrder}
43 $pdb->{appinfo}{unsent}
44 $pdb->{appinfo}{sigOffset}
45  
46 I don't know what these are.
47  
48 =head2 Sort block
49  
50 $pdb->{sort}
51  
52 This is a scalar, the raw data of the sort block.
53  
54 =head2 Records
55  
56 $record = $pdb->{records}[N]
57  
58 $record->{year}
59 $record->{month}
60 $record->{day}
61 $record->{hour}
62 $record->{minute}
63  
64 The message's timestamp.
65  
66 $record->{is_read}
67  
68 This is defined and true iff the message has been read.
69  
70 $record->{has_signature}
71  
72 For outgoing messages, this is defined and true iff the message should
73 have a signature attached. The signature itself is stored in the
74 "Saved Preferences.prc" database, and is of type "mail" with ID 2.
75  
76 $record->{confirm_read}
77  
78 If this is defined and true, then the sender requests notification
79 when the message has been read.
80  
81 $record->{confirm_delivery}
82  
83 If this is defined and true, then the sender requests notification
84 when the message has been delivered.
85  
86 $record->{priority}
87  
88 An integer in the range 0-2, for high, normal, or low priority,
89 respectively.
90  
91 $record->{addressing}
92  
93 An integer in the range 0-2, indicating the addressing type: To, Cc,
94 or Bcc respectively. I don't know what this means.
95  
96 $record->{subject}
97 $record->{from}
98 $record->{to}
99 $record->{cc}
100 $record->{bcc}
101 $record->{replyTo}
102 $record->{sentTo}
103  
104 Strings, the various header fields.
105  
106 $record->{body}
107  
108 A string, the body of the message.
109  
110 =head1 METHODS
111  
112 =cut
113 #'
114  
115 sub import
116 {
117 &Palm::PDB::RegisterPDBHandlers(__PACKAGE__,
118 [ "mail", "DATA" ],
119 );
120 }
121  
122 =head2 new
123  
124 $pdb = new Palm::Mail;
125  
126 Create a new PDB, initialized with the various Palm::Mail fields
127 and an empty record list.
128  
129 Use this method if you're creating a Mail PDB from scratch.
130  
131 =cut
132 #'
133  
134 sub new
135 {
136 my $classname = shift;
137 my $self = $classname->SUPER::new(@_);
138 # Create a generic PDB. No need to rebless it,
139 # though.
140  
141 $self->{name} = "MailDB"; # Default
142 $self->{creator} = "mail";
143 $self->{type} = "DATA";
144 $self->{attributes}{resource} = 0;
145 # The PDB is not a resource database by
146 # default, but it's worth emphasizing,
147 # since MailDB is explicitly not a PRC.
148  
149 # Initialize the AppInfo block
150 $self->{appinfo} = {
151 sortOrder => undef, # XXX - ?
152 unsent => undef, # XXX - ?
153 sigOffset => 0, # XXX - ?
154 };
155  
156 # Add the standard AppInfo block stuff
157 &Palm::StdAppInfo::seed_StdAppInfo($self->{appinfo});
158  
159 $self->{sort} = undef; # Empty sort block
160  
161 $self->{records} = []; # Empty list of records
162  
163 return $self;
164 }
165  
166 =head2 new_Record
167  
168 $record = $pdb->new_Record;
169  
170 Creates a new Mail record, with blank values for all of the fields.
171  
172 C<new_Record> does B<not> add the new record to C<$pdb>. For that,
173 you want C<$pdb-E<gt>append_Record>.
174  
175 Note: the time given by the C<year>, C<month>, C<day>, C<hour>, and
176 C<minute> fields in the new record are initialized to the time when
177 the record was created. They should be reset to the time when the
178 message was sent.
179  
180 =cut
181  
182 sub new_Record
183 {
184 my $classname = shift;
185 my $retval = $classname->SUPER::new_Record(@_);
186  
187 # Set the date and time on this message to today and now. This
188 # is arguably bogus, since the Date: header on a message ought
189 # to represent the time when the message was sent, rather than
190 # the time when the user started composing it, but this is
191 # better than nothing.
192  
193 ($retval->{year},
194 $retval->{month},
195 $retval->{day},
196 $retval->{hour},
197 $retval->{minute}) = (localtime(time))[5,4,3,2,1];
198  
199 $retval->{is_read} = 0; # Message hasn't been read yet.
200  
201 # No delivery service notification (DSN) by default.
202 $retval->{confirm_read} = 0;
203 $retval->{confirm_delivery} = 0;
204  
205 $retval->{priority} = 1; # Normal priority
206  
207 $retval->{addressing} = 0; # XXX - ?
208  
209 # All header fields empty by default.
210 $retval->{from} = undef;
211 $retval->{to} = undef;
212 $retval->{cc} = undef;
213 $retval->{bcc} = undef;
214 $retval->{replyTo} = undef;
215 $retval->{sentTo} = undef;
216  
217 $retval->{body} = "";
218  
219 return $retval;
220 }
221  
222 # ParseAppInfoBlock
223 # Parse the AppInfo block for Mail databases.
224 sub ParseAppInfoBlock
225 {
226 my $self = shift;
227 my $data = shift;
228 my $dirtyAppInfo;
229 my $sortOrder;
230 my $unsent;
231 my $sigOffset; # XXX - Offset of signature?
232 my $appinfo = {};
233 my $std_len;
234  
235 # Get the standard parts of the AppInfo block
236 $std_len = &Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data);
237  
238 $data = $appinfo->{other}; # Look at the non-category part
239  
240 # Get the rest of the AppInfo block
241 my $unpackstr = # Argument to unpack()
242 "x2" . # Padding
243 "n" . # Dirty AppInfo (what is this?)
244 "Cx" . # Sort order
245 "N" . # Unique ID of unsent message (what is this?)
246 "n"; # Signature offset
247  
248 ($dirtyAppInfo, $sortOrder, $unsent, $sigOffset) =
249 unpack $unpackstr, $data;
250  
251 $appinfo->{dirty_AppInfo} = $dirtyAppInfo;
252 $appinfo->{sort_order} = $sortOrder;
253 $appinfo->{unsent} = $unsent;
254 $appinfo->{sig_offset} = $sigOffset;
255  
256 return $appinfo;
257 }
258  
259 sub PackAppInfoBlock
260 {
261 my $self = shift;
262 my $retval;
263  
264 # Pack the non-category part of the AppInfo block
265 $self->{appinfo}{other} = pack "x2 n Cx N n",
266 $self->{appinfo}{dirty_AppInfo},
267 $self->{appinfo}{sort_order},
268 $self->{appinfo}{unsent},
269 $self->{appinfo}{sig_offset};
270  
271 # Pack the AppInfo block
272 $retval = &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo});
273  
274 return $retval;
275 }
276  
277 sub ParseRecord
278 {
279 my $self = shift;
280 my %record = @_;
281 my $data = $record{data};
282  
283 delete $record{offset}; # This is useless
284 delete $record{data};
285  
286 my $date;
287 my $hour;
288 my $minute;
289 my $flags;
290 my $subject;
291 my $from;
292 my $to;
293 my $cc;
294 my $bcc;
295 my $replyTo;
296 my $sentTo;
297 my $body;
298 my $extra; # Extra field after body. I don't know what
299 # it is.
300 my $unpackstr =
301 "n" . # Date
302 "C" . # Hour
303 "C" . # Minute
304 "n"; # Flags
305  
306 ($date, $hour, $minute, $flags) = unpack $unpackstr, $data;
307  
308 my $year;
309 my $month;
310 my $day;
311  
312 if ($date != 0)
313 {
314 $day = $date & 0x001f; # 5 bits
315 $month = ($date >> 5) & 0x000f; # 4 bits
316 $year = ($date >> 9) & 0x007f; # 7 bits (years since 1904)
317 $year += 1904;
318  
319 $record{year} = $year;
320 $record{month} = $month;
321 $record{day} = $day;
322 $record{hour} = $hour;
323 $record{minute} = $minute;
324 }
325  
326 my $is_read = ($flags & 0x8000);
327 my $has_signature = ($flags & 0x4000);
328 my $confirm_read = ($flags & 0x2000);
329 my $confirm_delivery = ($flags & 0x1000);
330 my $priority = ($flags >> 10) & 0x03;
331 my $addressing = ($flags >> 8) & 0x03;
332  
333 # The signature is problematic: it's not stored in
334 # "MailDB.pdb": it's actually in "Saved Preferences.pdb". Work
335 # around this somehow; either read it from "Saved
336 # Preferences.pdb" or, more simply, just read ~/.signature if
337 # it exists.
338  
339 $record{is_read} = 1 if $is_read;
340 $record{has_signature} = 1 if $has_signature;
341 $record{confirm_read} = 1 if $confirm_read;
342 $record{confirm_delivery} = 1 if $confirm_delivery;
343 $record{priority} = $priority;
344 $record{addressing} = $addressing;
345  
346 my $fields = substr $data, 6;
347 my @fields = split /\0/, $fields;
348  
349 ($subject, $from, $to, $cc, $bcc, $replyTo, $sentTo, $body,
350 $extra) = @fields;
351  
352 # Clean things up a bit
353  
354 # Multi-line values are bad in these headers. Replace newlines
355 # with commas. Ideally, we'd use arrays for multiple
356 # recipients, but that would involve parsing addresses, which
357 # is non-trivial. Besides, most likely we'll just wind up
358 # sending these strings as they are to 'sendmail', which is
359 # better equipped to parse them.
360  
361 $to =~ s/\s*\n\s*(?!$)/, /gs if defined($to);
362 $cc =~ s/\s*\n\s*(?!$)/, /gs if defined($cc);
363 $bcc =~ s/\s*\n\s*(?!$)/, /gs if defined($bcc);
364 $replyTo =~ s/\s*\n\s*(?!$)/, /gs if defined($replyTo);
365 $sentTo =~ s/\s*\n\s*(?!$)/, /gs if defined($sentTo);
366  
367 $record{subject} = $subject;
368 $record{from} = $from;
369 $record{to} = $to;
370 $record{cc} = $cc;
371 $record{bcc} = $bcc;
372 $record{replyTo} = $replyTo;
373 $record{sentTo} = $sentTo;
374 $record{body} = $body;
375 $record{extra} = $extra;
376  
377 return \%record;
378 }
379  
380 sub PackRecord
381 {
382 my $self = shift;
383 my $record = shift;
384 my $retval;
385 my $rawDate;
386 my $flags;
387  
388 $rawDate = ($record->{day} & 0x001f) |
389 (($record->{month} & 0x000f) << 5) |
390 ((($record->{year} - 1904) & 0x07f) << 9);
391 $flags = 0;
392 $flags |= 0x8000 if $record->{is_read};
393 $flags |= 0x4000 if $record->{has_signature};
394 $flags |= 0x2000 if $record->{confirm_read};
395 $flags |= 0x1000 if $record->{confirm_delivery};
396 $flags |= (($record->{priority} & 0x03) << 10);
397 $flags |= (($record->{addressing} & 0x03) << 8);
398  
399 $retval = pack "n C C n",
400 $rawDate,
401 $record->{hour},
402 $record->{minute},
403 $flags;
404  
405 $retval .= join "\0",
406 $record->{subject},
407 $record->{from},
408 $record->{to},
409 $record->{cc},
410 $record->{bcc},
411 $record->{replyTo},
412 $record->{sentTo},
413 $record->{body};
414 $retval .= "\0";
415  
416 return $retval;
417 }
418  
419 1;
420 __END__