blib/lib/Data/ID/Exim.pm

filter: Statementstmt 100% 126 / 126 Branchbran 90.0% 27 / 30 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 24 / 24 Podpod 100% 10 / 10 totaltotal 94.6% 194 / 205 CC 17 SCAR 7.7
CC
17
Cov
96%
read_base36
4.2
exim_mid
3.0
exim_mid36
3.0
CRAP 2.2
↑ Summary
1 =head1 NAME
2  
3 Data::ID::Exim - generate Exim message IDs
4  
5 =head1 SYNOPSIS
6  
7 use Data::ID::Exim qw(exim_mid exim_mid36);
8  
9 $mid = exim_mid;
10 $mid = exim_mid36;
11  
12 use Data::ID::Exim qw(
13 exim_mid_time exim_mid36_time read_exim_mid read_exim_mid36);
14  
15 $mid_time = exim_mid_time(Time::Unix::time());
16 $mid_time = exim_mid36_time(Time::Unix::time());
17 ($sec, $usec, $pid) = read_exim_mid($mid);
18 ($sec, $usec, $pid) = read_exim_mid36($mid);
19  
20 use Data::ID::Exim qw(base62 base36 read_base62 read_base36);
21  
22 $digits = base62(3, $value);
23 $digits = base36(3, $value);
24 $value = read_base62($digits);
25 $value = read_base36($digits);
26  
27 =head1 DESCRIPTION
28  
29 This module supplies functions which generate IDs using the algorithms
30 that the Exim MTA uses to generate message IDs, and functions to
31 manipulate such IDs. Exim has two schemes for message IDs, one using
32 base 62 to compactly represent numeric components and one using base
33 36. Base 62 is the preferred system, and is used where filenames are
34 case-sensitive. Base 36, which yields monocase (specifically uppercase)
35 message IDs, is used where filenames are case-insensitive. Apart from
36 the radix the two schemes are very similar. This module supplies separate
37 functions for the two schemes.
38  
39 =cut
40  
41 package Data::ID::Exim;
42  
43 5 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use Time::HiRes 1.00 qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 5 our $VERSION = "0.009";
51  
52 5 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
53 5 our @EXPORT_OK = qw(
54 exim_mid exim_mid36
55 exim_mid_time exim_mid36_time read_exim_mid read_exim_mid36
56 base62 base36 read_base62 read_base36
57 );
58  
59 {
60 5 my(%base62, %read_base62);
61 5 ▶ for(my $v = 10; $v--; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v--) 50 5 55
62 50 my $d = chr(ord("0") + $v);
63 50 $base62{$v} = $d;
64 50 $read_base62{$d} = $v;
65 }
66 5 ▶ for(my $i = 26; $i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 130 5 135
67 {
68 130 my $v = 10 + $i;
69 130 my $d = chr(ord("A") + $i);
70 130 $base62{$v} = $d;
71 130 $read_base62{$d} = $v;
72 }
73 {
74 130 my $v = 36 + $i;
75 130 my $d = chr(ord("a") + $i);
76 130 $base62{$v} = $d;
77 130 $read_base62{$d} = $v;
78 }
79 }
80  
81 22 ▶ sub base62($$) {
Subroutine base62: called CC=2 SCAR=6.9
Pod documented
82 22 my($ndigits, $value) = @_;
83 22 my $digits = "";
84 22 ▶ while($ndigits--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigits--) 104 22 126
85 5 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 104 $digits .= $base62{$value % 62};
87 104 $value /= 62;
88 }
89 22 return scalar(reverse($digits));
90 }
91  
92 22 ▶ sub base36($$) {
Subroutine base36: called CC=2 SCAR=6.9
Pod documented
93 22 my($ndigits, $value) = @_;
94 22 my $digits = "";
95 22 ▶ while($ndigits--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigits--) 104 22 126
96 5 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 104 $digits .= $base62{$value % 36};
98 104 $value /= 36;
99 }
100 22 return scalar(reverse($digits));
101 }
102  
103 10 ▶ sub read_base62($) {
Subroutine read_base62: called CC=2 SCAR=6.9
Pod documented
104 10 my($digits) = @_;
105 10 my $value = 0;
106 10 ▶ while($digits =~ /(.)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digits =~ /(.)/gs) 44 10 54
107 44 $value = 62 * $value + $read_base62{$1};
108 }
109 10 return $value;
110 }
111  
112 10 ▶ sub read_base36($) {
Subroutine read_base36: called CC=4 SCAR=14.2
Pod documented
113 10 my($digits) = @_;
114 10 my $value = 0;
115 10 ▶ while($digits =~ /(.)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($digits =~ /(.)/gs) 44 10 54
116 44 my $v = $read_base62{$1};
117 44 ▶ $v = undef if defined($v) && $v >= 36;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $v >= 36 0 44 44
Condition33%1/3
defined $v and $v >= 36
!ll&&!rl&&r
0440
MC/DC0%0/2
defined $v and $v >= 36
defined $v $v >= 36
Truth table33%1/3
A and B
A= defined $vB= $v >= 36
ABresult
0X0
100
111
118 44 $value = 36 * $value + $v;
119 }
120 10 return $value;
121 }
122 }
123  
124 =head1 FUNCTIONS
125  
126 All of these functions come in matched pairs, for the base-62 and the
127 base-36 message ID schemes. Each pair is described together, because
128 the functions are used identically.
129  
130 =over
131  
132 =item exim_mid
133  
134 =item exim_mid36
135  
136 Generates an Exim message ID. (This ID may, of course, be used to label
137 things other than mail messages, but Exim refers to them as message IDs.)
138 The ID is based on the time and process ID, such that it is guaranteed
139 to be unique among IDs generated by this algorithm on this host.
140 This function is completely interoperable with Exim, in the sense that
141 it uses exactly the same algorithm so that the uniqueness guarantee
142 applies between IDs generated by this function and by Exim itself.
143  
144 The format of the message ID is three groups of base 62 or base 36 digits
145 respectively, separated by hyphens. The first group, of six digits,
146 gives the integral number of seconds since the epoch. The second group,
147 also of six digits, gives the process ID. The third group, of two
148 digits, gives the fractional part of the number of seconds since the
149 epoch, in units of 1/2000 of a second (500 us) or 1/1000 of a second
150 (1000 us) respectively. The function does not return until the clock
151 has advanced far enough that another call would generate a different ID.
152  
153 The strange structure of the ID comes from compatibility with earlier
154 versions of Exim, in which the last two digits were a sequence number.
155  
156 =item exim_mid(HOST_NUMBER)
157  
158 =item exim_mid36(HOST_NUMBER)
159  
160 Exim has limited support for making message IDs unique among a group
161 of hosts. Each host is assigned a number in the range 0 to 16 or 11
162 respectively inclusive. The last two digits of the message IDs give the
163 host number multiplied by 200 or 100 respectively plus the fractional part
164 of the number of seconds since the epoch in units of 1/200 of a second
165 (5 ms) or 1/100 of a second (10 ms) respectively. This makes message
166 IDs unique across the group of hosts, at the expense of generation rate.
167  
168 To generate this style of ID, pass the host number to C<exim_mid> or
169 C<exim_mid36>. The host number must be configured by some out-of-band
170 mechanism.
171  
172 =cut
173  
174 5 ▶sub _make_fraction_62($$) {
Subroutine _make_fraction_62: called CC=2 SCAR=6.9
175 5 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 2641 my($host_number, $usec) = @_;
177 2641 ▶ defined($host_number) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $host_number ? : 2521 120 2641
178 200*$host_number + $usec/5000 :
179 $usec/500;
180 }
181  
182 4 ▶sub exim_mid(;$) {
Subroutine exim_mid: called CC=3 SCAR=11.0
Pod documented
183 4 my($host_number) = @_;
184 4 my($sec, $usec) = gettimeofday;
185 4 my $frac = _make_fraction_62($host_number, $usec);
186 4 my($new_sec, $new_usec, $new_frac);
187 4 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$new_sec == $sec and $new_frac == $frac and do { do { () = &gettimeofday(); $new_frac = &_make_fraction_62($host_number, $new_usec) } } 2633 4 2637
Condition66%2/3
$new_sec == $sec and $new_frac == $frac
!ll&&!rl&&r
042633
MC/DC50%1/2
$new_sec == $sec and $new_frac == $frac
$new_sec == $sec $new_frac == $frac
Truth table66%2/3
A and B
A= $new_sec == $secB= $new_frac == $frac
ABresult
0X0
100
111
188 2637 ($new_sec, $new_usec) = gettimeofday;
189 2637 $new_frac = _make_fraction_62($host_number, $new_usec);
190 } while($new_sec == $sec && $new_frac == $frac);
191 4 return base62(6, $sec)."-".base62(6, $$)."-".base62(2, $frac);
192 }
193  
194 4035 ▶sub _make_fraction_36($$) {
Subroutine _make_fraction_36: called CC=2 SCAR=6.9
195 5 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
196 4035 my($host_number, $usec) = @_;
197 4035 ▶ defined($host_number) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $host_number ? : 3588 447 4035
198 100*$host_number + $usec/10000 :
199 $usec/1000;
200 }
201  
202 4 ▶sub exim_mid36(;$) {
Subroutine exim_mid36: called CC=3 SCAR=11.0
Pod documented
203 4 my($host_number) = @_;
204 4 my($sec, $usec) = gettimeofday;
205 4 my $frac = _make_fraction_36($host_number, $usec);
206 4 my($new_sec, $new_usec, $new_frac);
207 4 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$new_sec == $sec and $new_frac == $frac and do { do { () = &gettimeofday(); $new_frac = &_make_fraction_36($host_number, $new_usec) } } 4027 4 4031
Condition66%2/3
$new_sec == $sec and $new_frac == $frac
!ll&&!rl&&r
044027
MC/DC50%1/2
$new_sec == $sec and $new_frac == $frac
$new_sec == $sec $new_frac == $frac
Truth table66%2/3
A and B
A= $new_sec == $secB= $new_frac == $frac
ABresult
0X0
100
111
208 4031 ($new_sec, $new_usec) = gettimeofday;
209 4031 $new_frac = _make_fraction_36($host_number, $new_usec);
210 } while($new_sec == $sec && $new_frac == $frac);
211 4 return base36(6, $sec)."-".base36(6, $$)."-".base36(2, $frac);
212 }
213  
214 =item exim_mid_time(TIME)
215  
216 =item exim_mid36_time(TIME)
217  
218 Because the first section of an Exim message ID encodes the time to a
219 resolution of a second, these IDs sort in a useful way. For the purposes
220 of lexical comparison using this feature, it is sometimes useful to
221 construct a string encoding a specified time in Exim message ID format.
222 (This can also be used as a very concise clock display.)
223  
224 This function constructs the initial time portion of an Exim message
225 ID. TIME must be an integral Unix time number. The corresponding
226 six-digit string is returned.
227  
228 =cut
229  
230 2 ▶sub exim_mid_time($) {
Subroutine exim_mid_time: called CC=1 SCAR=0.0
Pod documented
231 2 my($t) = @_;
232 2 return base62(6, $t);
233 }
234  
235 2 ▶sub exim_mid36_time($) {
Subroutine exim_mid36_time: called CC=1 SCAR=0.0
Pod documented
236 2 my($t) = @_;
237 2 return base36(6, $t);
238 }
239  
240 =item read_exim_mid(MID)
241  
242 =item read_exim_mid36(MID)
243  
244 This function extracts the information encoded in an Exim message ID.
245 This is a slightly naughty thing to do: the ID should really only be
246 used as a unique identifier. Nevertheless, the time encoded in an ID
247 is sometimes useful.
248  
249 The function returns a three-element list. The first two elements encode
250 the time at which the ID was generated, as a (seconds, microseconds)
251 pair giving the time since the epoch. This is the same time format as
252 is returned by C<gettimeofday>. The message ID does not encode the time
253 with a resolution as fine as a microsecond; the returned microseconds
254 value is rounded down appropriately. The third item in the result list
255 is the encoded PID.
256  
257 =item read_exim_mid(MID, HOST_NUMBER_P)
258  
259 =item read_exim_mid36(MID, HOST_NUMBER_P)
260  
261 The optional HOST_NUMBER_P argument is a truth value indicating whether the
262 message ID was encoded using the variant algorithm that includes a host
263 number in the ID. It is essential to decode the ID using the correct
264 algorithm. The host number, if present, is returned as a fourth item
265 in the result list.
266  
267 =cut
268  
269 2 ▶sub read_exim_mid($;$) {
Subroutine read_exim_mid: called CC=3 SCAR=11.0
Pod documented
270 2 my($mid, $host_number_p) = @_;
271 2 ▶ croak "malformed message ID"
Branch: 1 branch
BranchTrueFalseTotal
unless $mid =~ /\A([0-9A-Za-z]{6})-([0-9A-Za-z]{6})- ([0-9A-Za-z]{2})\z/x 0 2 2
272 unless $mid =~ /\A([0-9A-Za-z]{6})-([0-9A-Za-z]{6})-
273 ([0-9A-Za-z]{2})\z/x;
274 2 my @b62 = ($1, $2, $3);
275 2 my($sec, $pid, $frac) = map { read_base62($_) } @b62;
276 2 ▶ if($host_number_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host_number_p) { } 1 1 2
277 5 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
278 1 my $host_number = $frac / 200;
279 1 my $usec = ($frac % 200) * 5000;
280 1 return ($sec, $usec, $pid, $host_number);
281 } else {
282 1 my $usec = $frac * 500;
283 1 return ($sec, $usec, $pid);
284 }
285 }
286  
287 2 ▶sub read_exim_mid36($;$) {
Subroutine read_exim_mid36: called CC=3 SCAR=11.0
Pod documented
288 2 my($mid, $host_number_p) = @_;
289 2 ▶ croak "malformed message ID"
Branch: 1 branch
BranchTrueFalseTotal
unless $mid =~ /\A([0-9A-Z]{6})-([0-9A-Z]{6})-([0-9A-Z]{2})\z/x 0 2 2
290 unless $mid =~ /\A([0-9A-Z]{6})-([0-9A-Z]{6})-([0-9A-Z]{2})\z/x;
291 2 my @b36 = ($1, $2, $3);
292 2 my($sec, $pid, $frac) = map { read_base36($_) } @b36;
293 2 ▶ if($host_number_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host_number_p) { } 1 1 2
294 5 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
295 1 my $host_number = $frac / 100;
296 1 my $usec = ($frac % 100) * 10000;
297 1 return ($sec, $usec, $pid, $host_number);
298 } else {
299 1 my $usec = $frac * 1000;
300 1 return ($sec, $usec, $pid);
301 }
302 }
303  
304 =item base62(NDIGITS, VALUE)
305  
306 =item base36(NDIGITS, VALUE)
307  
308 These perform base 62 and base 36 encoding respectively. VALUE and
309 NDIGITS must both be non-negative native integers. VALUE is expressed
310 in base 62 or base 36 respectively, and the least significant NDIGITS
311 digits are returned as a string.
312  
313 =item read_base62(DIGITS)
314  
315 =item read_base36(DIGITS)
316  
317 These perform base 62 and base 36 decoding. DIGITS must be a string
318 of base 62 or base 36 digits respectively. It is interpreted and the
319 value returned as a native integer.
320  
321 =back
322  
323 =head1 BUGS
324  
325 Can theoretically generate duplicate message IDs during a leap second.
326 Exim suffers the same problem.
327  
328 =head1 SEE ALSO
329  
330 L<Data::ID::Maildir>,
331 L<UUID>,
332 L<Win32::Guidgen>,
333 L<http://www.exim.org>
334  
335 =head1 AUTHOR
336  
337 Andrew Main (Zefram) <zefram@fysh.org>
338  
339 =head1 COPYRIGHT
340  
341 Copyright (C) 2004, 2006, 2007, 2009, 2010, 2011, 2017
342 Andrew Main (Zefram) <zefram@fysh.org>
343  
344 =head1 LICENSE
345  
346 This module is free software; you can redistribute it and/or modify it
347 under the same terms as Perl itself.
348  
349 =cut
350  
351 5 1;
↑ Summary