-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode-message.pl
More file actions
executable file
·72 lines (66 loc) · 1.19 KB
/
decode-message.pl
File metadata and controls
executable file
·72 lines (66 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl
#
# Hack to decode Postfix messages as stored in a queue.
#
# [created. -- rgr, 15-Dec-08.]
#
# $Id:$
use strict;
use warnings;
sub process_file {
my $stream = shift;
my $bytes;
my $in_message_p;
while (1) {
read($stream, $bytes, 2)
or die "eof";
my ($code, $n_bytes) = split(//, $bytes);
$n_bytes = ord($n_bytes);
if ($n_bytes) {
if ($n_bytes > 128) {
# This means we have a long line.
read($stream, $bytes, 1)
or die "eof";
$n_bytes = 128*ord($bytes) + $n_bytes%128;
}
read($stream, $bytes, $n_bytes)
or die "eof";
}
else {
$bytes = '';
}
# Maybe update the $in_message_p state.
if ($code eq 'M') {
$in_message_p = 1;
}
elsif ($code eq 'X') {
$in_message_p = 0;
next;
}
elsif ($code eq 'E') {
print "End of message.\n";
return;
}
# Generate output.
if ($in_message_p) {
$bytes .= "\n"
if $code eq 'N';
print $bytes;
}
else {
print("$code $n_bytes: $bytes\n");
}
}
}
# Process command-line arguments, else stdin.
if (@ARGV) {
for my $file (@ARGV) {
print "*** $file:\n";
open(my $in, $file)
or die;
process_file($in);
}
}
else {
process_file(*STDIN);
}