Pkt.pm

filter: Statementstmt 36.2% 66 / 182 Branchbran 3.0% 2 / 66 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 56.2% 18 / 32 Podpod 100% 4 / 4 totaltotal 28.8% 90 / 312 CC 45 SCAR 42.7
CC
45
Cov
26%
_packed
240.0
as_string
90.0
write_pkt
90.0
CRAP 71.5
↑ Summary Utils/OS_features.pm »
1 package FTN::Pkt;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 require 5.6.0;
6 1 our $VERSION = "1.02";
7  
8 package FTN::Pkt::utils;
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 require Exporter;
13 1 ▶use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 BEGIN
16 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 @ISA = qw(Exporter);
18 1 %EXPORT_TAGS = (utils => [qw(parse_addr datetime trunk trunkzero hextime my_sleep)]);
19 1 Exporter::export_ok_tags('utils');
20 1 1;
21 }
22  
23 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Time::HiRes qw(usleep gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 my $PRECISION = 0.1;
27  
28 #========================================================
29  
30 # Here is some auxiliary functions. Not for client use.
31  
32 #========================================================
33  
34 sub parse_addr($)
35 0 ▶{
Subroutine parse_addr: not called CC=3 SCAR=24.8
36 0 my $addr = shift;
37 0 ▶ return (undef, undef, undef, undef) unless $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr 0 0 0
38 0 ▶ $addr .= ".0" unless $addr =~ /\.\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr =~ /\.\d+$/ 0 0 0
39 0 my @result = $addr =~ /(\d)\:(\d+)\/(\d+)\.(\d+)/;
40 0 return @result;
41 }
42  
43 #========================================================
44  
45 sub datetime(;$)
46 0 ▶{
Subroutine datetime: not called CC=2 SCAR=17.9
47 0 my $tm = shift;
48 0 ▶ $tm ||= time();
Condition0%0/2
$tm ||= time
l!l
00
MC/DC0%0/1
$tm ||= time
$tm
Truth table0%0/2
A
A= $tm
Aresult
11
00
49 0 my @MON = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
50 0 my @curtime = localtime($tm);
51 0 return strftime("%d ", @curtime).$MON[$curtime[4]].strftime(" %y %H:%M:%S", @curtime);
52 }
53  
54 #========================================================
55  
56 sub trunk($$)
57 0 ▶{
Subroutine trunk: not called CC=3 SCAR=24.8
58 0 my ($str, $len) = @_;
59 0 ▶ if (length($str) > $len && $len > 0){
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > $len and $len > 0) 0 0 0
Condition0%0/3
length $str > $len and $len > 0
!ll&&!rl&&r
000
MC/DC0%0/2
length $str > $len and $len > 0
length $str > $len $len > 0
Truth table0%0/3
A and B
A= length $str > $lenB= $len > 0
ABresult
0X0
100
111
60 0 $str = substr($str, 0, $len);
61 }
62 0 return $str;
63 }
64  
65 #========================================================
66  
67 sub trunkzero($$)
68 0 ▶{
Subroutine trunkzero: not called CC=1 SCAR=6.9
69 0 return trunk($_[0], $_[1]) . "\0";
70 }
71  
72 #========================================================
73  
74 sub hextime()
75 0 ▶{
Subroutine hextime: not called CC=1 SCAR=6.9
76 0 my $msec = int(gettimeofday() / $PRECISION) % 0xffffffff;
77 0 return sprintf("%08x", $msec);
78 }
79  
80 #========================================================
81  
82 sub my_sleep()
83 0 ▶{
Subroutine my_sleep: not called CC=1 SCAR=6.9
84 0 usleep($PRECISION*1000000*1.1);
85 }
86  
87 #========================================================
88  
89 package FTN::Msg;
90  
91 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 1 import FTN::Pkt::utils qw(:utils);
95  
96  
97 #========================================================
98  
99 1 ▶use fields qw(fromaddr toaddr fromname toname tearline origin subj text area msgid reply
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 topkt frompkt pid tid date);
101  
102 #========================================================
103  
104 sub new
105 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
106 0 my FTN::Msg $self = shift;
107 0 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
108 0 $self->update(@_);
109 0 return $self;
110 }
111  
112 #========================================================
113  
114 sub update
115 0 ▶{
Subroutine update: not called CC=2 SCAR=17.9
116 0 my FTN::Msg $self = shift;
117 0 my %params = @_;
118 0 foreach(keys %params){
119 0 $self->{$_} = $params{$_};
120 }
121 }
122  
123 #========================================================
124  
125 sub make_msgid(;$)
126 0 ▶{
Subroutine make_msgid: not called CC=3 SCAR=24.8
127 0 my FTN::Msg $self = shift;
128 0 my $msgid = shift;
129 0 ▶ unless ($msgid){
Branch: 1 branch
BranchTrueFalseTotal
unless ($msgid) 0 0 0
130 0 $msgid = hextime();
131 0 my_sleep();
132 }
133 0 ▶ die "make_msgid: unknown fromaddr" unless $self->{fromaddr};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fromaddr"} 0 0 0
134 0 return ($self->{msgid} = "$self->{fromaddr} $msgid");
135 }
136  
137 #========================================================
138  
139 sub as_string()
140 0 ▶{
Subroutine as_string: not called CC=9 SCAR=45.0
141 0 my FTN::Msg $self = shift;
142 0 my $res = "\n";
143 0 foreach(qw (fromname fromaddr toname toaddr frompkt topkt frompkt area msgid reply pid subj)) {
144 0 ▶ $res .= "$_ : $self->{$_}\n" if exists $self->{$_} and defined $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$_} and defined $self->{$_} 0 0 0
Condition0%0/3
exists $self->{$_} and defined $self->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{$_} and defined $self->{$_}
exists $self->{$_} defined $self->{$_}
Truth table0%0/3
A and B
A= exists $self->{$_}B= defined $self->{$_}
ABresult
0X0
100
111
145 }
146 $res .= '-' x 72 . "\n$self->{text}\n".'-' x 72 ."\n"
147 0 ▶ if exists $self->{text} and defined $self->{text};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"text"} and defined $self->{"text"} 0 0 0
Condition0%0/3
exists $self->{"text"} and defined $self->{"text"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"text"} and defined $self->{"text"}
exists $self->{"text"} defined $self->{"text"}
Truth table0%0/3
A and B
A= exists $self->{"text"}B= defined $self->{"text"}
ABresult
0X0
100
111
148 0 foreach(qw (tearline origin)) {
149 0 ▶ $res .= "$_ : $self->{$_}\n" if exists $self->{$_} and defined $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$_} and defined $self->{$_} 0 0 0
Condition0%0/3
exists $self->{$_} and defined $self->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{$_} and defined $self->{$_}
exists $self->{$_} defined $self->{$_}
Truth table0%0/3
A and B
A= exists $self->{$_}B= defined $self->{$_}
ABresult
0X0
100
111
150 }
151 0 return $res;
152 }
153  
154 #========================================================
155 #
156 # Internal method. Returns binary representation of the message inside the packet.
157  
158 sub _packed()
159 0 ▶{
Subroutine _packed: not called CC=15 SCAR=54.8
160 0 my FTN::Msg $self = shift;
161 0 my ($fromzone, $fromnet, $fromnode, $frompoint) = parse_addr($self->{fromaddr});
162 0 my ($tozone, $tonet, $tonode, $topoint) = parse_addr($self->{toaddr});
163 0 my ($pfromzone, $pfromnet, $pfromnode, $pfrompoint) = parse_addr($self->{frompkt});
164 0 my ($ptozone, $ptonet, $ptonode, $ptopoint) = parse_addr($self->{topkt});
165 0 my $template = "v7a20";
166 0 ▶ $self->make_msgid() unless ($self->{msgid});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"msgid"} 0 0 0
167 my $result = pack $template, 2, $pfromnode, $ptonode, $pfromnet, $ptonet,
168 0 0, 0, datetime($self->{date});
169 0 ▶ $result .= trunkzero(($self->{toname} ? $self->{toname} : "All"), 35);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'toname'} ? : 0 0 0
170 0 $result .= trunkzero($self->{fromname}, 35);
171 0 ▶ $result .= trunkzero(($self->{subj} ? $self->{subj} : ""), 71);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'subj'} ? : 0 0 0
172 0 my $msgtail = "\x0";
173 0 ▶ if ($self->{area}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'area'}) { } 0 0 0
174 0 $result .= "AREA:".$self->{area}."\xd";
175 0 $msgtail = "SEEN-BY: $pfromnet/$pfromnode\x0d\x01PATH: $pfromnet/$pfromnode\x0d\x00";
176 # -------------->
177 }else{
178 0 $result .= "\x01INTL $tozone:$tonet/$tonode $fromzone:$fromnet/$fromnode\xd";
179 0 ▶ $result .= "\x01FMPT $frompoint\xd" if $frompoint != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $frompoint != 0 0 0 0
180 0 ▶ $result .= "\x01TOPT $topoint\xd" if $topoint != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $topoint != 0 0 0 0
181 }
182 0 ▶ $result .= "\x01REPLY: $self->{reply}\x0d" if $self->{reply};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reply"} 0 0 0
183 0 $result .= "\x01MSGID: $self->{msgid}\x0d";
184 0 $result .= "\x01CHRS: CP866 2\x0d";
185 0 ▶ $result .= "\x01PID: $self->{pid}\x0d" if $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} 0 0 0
186 0 ▶ $result .= sprintf("\x01TID: FTN::Pkt %s\x0d", $FTN::Pkt::VERSION) if $self->{tid};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tid"} 0 0 0
187 0 ▶ $result .= "\x01Posted: ".datetime()."\x0d" if $self->{date};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"date"} 0 0 0
188 0 my $text = $self->{text};
189 0 $text =~ s/\n/\xd/sg;
190 0 $result .= $text;
191 0 ▶ $result .= "\x0d--- ".($self->{tearline} ? $self->{tearline} : "")."\xd";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tearline'} ? : 0 0 0
192 0 my $origin = " * Origin: ";
193 0 ▶ my $origtext = ($self->{origin} ? $self->{origin} : "");
Branch: 1 branch
BranchTrueFalseTotal
$self->{'origin'} ? : 0 0 0
194 0 my $origtail = " (".$self->{fromaddr}.")\xd";
195 0 my $origtxln = 79 - length ($origin.$origtail);
196 0 $origtext = trunk($origtext, $origtxln);
197 0 $origin .= $origtext .= $origtail;
198 0 ▶ $result .= $origin if ($self->{origin} || $self->{area});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"origin"} or $self->{"area"} 0 0 0
Condition0%0/3
$self->{"origin"} or $self->{"area"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"origin"} or $self->{"area"}
$self->{"origin"} $self->{"area"}
Truth table0%0/3
A or B
A= $self->{"origin"}B= $self->{"area"}
ABresult
1X1
011
000
199 0 $result .= $msgtail;
200 0 return $result;
201 }
202  
203 #========================================================
204  
205 package FTN::Pkt;
206 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
208  
209 1 import FTN::Pkt::utils qw(:utils);
210  
211 1 ▶use FTN::Utils::OS_features;
Subroutine BEGIN: called CC=1 SCAR=0.0
212 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
213  
214  
215 #========================================================
216  
217 1 ▶use fields qw(fromaddr toaddr password inbound _msgs);
Subroutine BEGIN: called CC=1 SCAR=0.0
218  
219 #========================================================
220  
221 # fromaddr, toaddr, password, inbound
222 # msgs
223  
224 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
225  
226 1 my FTN::Pkt $self = shift;
227 1 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 0 1
228 1 $self->update(@_);
229 1 $self->{_msgs} = [];
230 1 return $self;
231 }
232  
233 #========================================================
234  
235 sub update
236 1 ▶{
Subroutine update: called CC=3 SCAR=11.4
Pod documented
237 1 my FTN::Pkt $self = shift;
238 1 my %params = @_;
239 1 ▶ if(exists $params{_msgs}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"_msgs"}) 0 1 1
240 0 croak "FATAL: can't update '_msgs' directly!";
241 }
242 1 foreach(keys %params){
243 4 $self->{$_} = $params{$_};
244 }
245 }
246  
247 #========================================================
248  
249 sub add_msg($)
250 0 ▶{
Subroutine add_msg: not called CC=1 SCAR=6.9
Pod documented
251 0 my FTN::Pkt $self = shift;
252 0 my $msg = shift;
253 0 push @{$self->{_msgs}}, $msg;
254 }
255  
256 #========================================================
257 #
258 # Internal method. Returns binary representation of the packet.
259  
260 sub _packed()
261 0 ▶{
Subroutine _packed: not called CC=3 SCAR=24.8
262 0 my FTN::Pkt $self = shift;
263 0 my ($fromzone, $fromnet, $fromnode, $frompoint) = parse_addr($self->{fromaddr});
264 0 my ($tozone, $tonet, $tonode, $topoint) = parse_addr($self->{toaddr});
265 0 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
266  
267 0 my $template = "v13a8v10V";
268 my $result = pack $template, $fromnode, $tonode,
269 $year+1900, $mon, $mday, $hour, $min, $sec, 0, 2,
270 $fromnet, $tonet, 0x7766,
271 0 ▶ $self->{password} ? $self->{password} : "",
Branch: 1 branch
BranchTrueFalseTotal
$self->{'password'} ? : 0 0 0
272 $fromzone, $tozone, 0, 0x100, 0x7766, 1,
273 $fromzone, $tozone, $frompoint, $topoint, 0;
274 0 foreach my $msg(@{$self->{_msgs}}){
275 0 $msg->update(frompkt => $self->{fromaddr}, topkt => $self->{toaddr});
276 0 $result .= $msg->_packed();
277 }
278 0 $result .= "\x00\x00";
279 0 return $result;
280 }
281  
282  
283 #========================================================
284  
285 sub write_pkt()
286 0 ▶{
Subroutine write_pkt: not called CC=9 SCAR=45.0
Pod documented
287 0 my FTN::Pkt $self = shift;
288 0 my $regexp = "${dir_separator}\$";
289 0 ▶ $self->{inbound} .= $dir_separator unless $self->{inbound} =~ /$regexp/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"inbound"} =~ /$regexp/ 0 0 0
290 0 my $filename = $self->{inbound}.hextime() .".tmp";
291 0 my $newname = $filename;
292 0 $newname =~ s/tmp$/pkt/;
293 0 my @repl = split //, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
294 0 ▶ for(my $i = 0; -e $filename; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) 0 0 0
295 0 ▶ if($i >= scalar @repl){die "can't make unique tmp name";}
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= scalar @repl) 0 0 0
296 0 substr($filename, -12, 1) = $repl[$i];
297 }
298 0 ▶ open(PKT, ">", $filename) or die "can't open $filename : $!";
Branch: 1 branch
BranchTrueFalseTotal
open PKT, ">", $filename or die "can't open $filename : $!" 0 0 0
299 0 ▶ binmode PKT if $needs_binmode;
Branch: 1 branch
BranchTrueFalseTotal
if $FTN::Pkt::needs_binmode 0 0 0
300 0 print PKT $self->_packed();
301 0 close PKT;
302 0 ▶ for(my $i = 0; -e $newname; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $newname) 0 0 0
303 0 ▶ if($i >= scalar @repl){die "can't make unique pkt name";}
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= scalar @repl) 0 0 0
304 0 substr($newname, -12, 1) = $repl[$i];
305 }
306 0 ▶ rename $filename, $newname or die "can't rename $filename -> $newname : $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $filename, $newname or die "can't rename $filename -> $newname : $!" 0 0 0
307 0 return $newname;
308 }
309  
310 #========================================================
311  
312 1 1;
313  
314 =head1 NAME
315  
316 FTN::Pkt - a module to make FTN-style mail packets
317  
318 =head1 SYNOPSIS
319
320 my $pkt = new FTN::Pkt (
321 fromaddr => '2:9999/999.128',
322 toaddr => '2:9999/999',
323 password => 'password',
324 inbound => '/var/spool/fido/inbound'
325 );
326 my $msg = new FTN::Msg(
327 fromname => 'Vassily Poupkine',
328 toname => 'Poupa Vassilyev',
329 subj => 'Hello',
330 text => "Hi, Poupa!\n\nHow do you do?\n\n--\nVassily",
331 fromaddr => '2:9999/999.128',
332 origin => 'My cool origin',
333 tearline => '/usr/bin/perl',
334 area => 'poupa.local',
335 reply => '2:9999/999.1 fedcba987',
336 date => 1210918822, # unixtime format
337 pid => 'Super-Duper Editor v0.01',
338 tid => 1
339 );
340 $pkt->add_msg($msg);
341 $pkt->write_pkt();
342  
343 =head1 DESCRIPTION
344  
345 This module can be used to make FTN-style mail packets. Either echomail or netmail are supported.
346 You can specify @REPLY cludge. @MSGID may be auto-generated or specified manually.
347  
348 If C<area> present then message treated as echomail. Othervise it becomes netmail (C<toaddr> required).
349  
350 =head1 FTN::Msg methods
351  
352 =over 8
353  
354 =item C<new(%hash)>
355  
356 A constructor. Some initialization parameters can be passed via C<%hash>.
357 Possible ones are:
358 C<fromaddr toaddr fromname toname tearline origin subj text area msgid reply pid tid date>.
359  
360 All parameters are text but C<tid> is boolean. If I<true> then @TID cludge will be added to message.
361  
362 =item C<update(%hash)>
363  
364 Changes the message. See C<FTN::Msg::new> for parameters allowed.
365  
366 =item C<make_msgid([$msgid])>
367  
368 Generates @MSGID, sets it inside the message and and returns it. Possible parameter is only second part of @MSGID, without source address.
369 If parameter omitted then all @MSGID parts will be auto-generated. Auto-generation method use I<unixtime> as basis,
370 so don't allow more than one process to generate @MSGIDs in the same time.
371  
372 =item C<as_string()>
373  
374 Returns string representation of message. For debugging.
375  
376 =back
377  
378 =head1 FTN::Pkt methods
379  
380 =over 8
381  
382 =item C<new(%hash)>
383  
384 A constructor. Some initialization parameters can be passed via C<%hash>.
385 Possible ones are: C<fromaddr toaddr password inbound>
386  
387 =item C<update(%hash)>
388  
389 Changes the packet. See C<FTN::Pkt::new> for parameters allowed.
390  
391 =item C<add_msg($msg)>
392  
393 Adds a message to the packet. C<$msg> must be a reference to C<FTN::Msg> object.
394  
395 =item C<write_pkt()>
396  
397 Writes the packet to a disk into C<inbound> directory. Filename is auto-generated.
398 Don't allow more than one process to write at the same time. Returns resulting filename.
399  
400 =back
401  
402 =head1 LIMITATIONS
403  
404 CP866 codepage is hardcoded.
405  
406 =head1 REQUIREMENTS
407  
408 FTN::OS_features module required (included in this package).
409  
410 Supported platforms: UNIX and Win32 have been tested. All others may work or not.
411  
412 =head1 COPYRIGHT
413  
414 Copyright 2008 Dmitry V. Kolvakh
415  
416 This library is free software.
417 You may copy or redistribute it under the same terms as Perl itself.
418  
419 =head1 AUTHOR
420  
421 Dmitry V. Kolvakh aka Keu
422  
423 2:5054/89@FIDOnet
424  
425 =cut
↑ Summary Utils/OS_features.pm »