| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright (C) 2002-2020 National Marrow Donor Program. All rights reserved. | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # For a description of this module, please refer to the POD documentation | ||||||||||||||||||||||||||||
| 6 | # embedded at the bottom of the file (e.g. perldoc EMDIS::ECS::Message). | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package EMDIS::ECS::Message; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use EMDIS::ECS qw($ECS_CFG $VERSION ecs_is_configured | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | pgp2_decrypt openpgp_decrypt); | |||||||||||||||||||||||||||
| 12 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use MIME::QuotedPrint qw( decode_qp ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use vars qw($EOL_PATTERN); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | $EOL_PATTERN = "\r?\n"; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 22 | # Constructor. | ||||||||||||||||||||||||||||
| 23 | # If error encountered, returns error message instead of object reference. | ||||||||||||||||||||||||||||
| 24 | 20 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=16 SCAR=27.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 20 | my $arg1 = shift; | |||||||||||||||||||||||||||
| 26 | 20 | my $this; | |||||||||||||||||||||||||||
| 27 | 20 | ▶ | if(ref $arg1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | # invoked as instance method | ||||||||||||||||||||||||||||
| 29 | 0 | $this = $arg1; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||
| 32 | # invoked as class method | ||||||||||||||||||||||||||||
| 33 | 20 | $this = {}; | |||||||||||||||||||||||||||
| 34 | 20 | bless $this, $arg1; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # remember raw text | ||||||||||||||||||||||||||||
| 38 | 20 | my $raw_text = shift; | |||||||||||||||||||||||||||
| 39 | 20 | $this->{raw_text} = $raw_text; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # parse raw email message text | ||||||||||||||||||||||||||||
| 42 | 20 | $raw_text =~ s/$EOL_PATTERN/\n/g; # convert to more easily parseable format | |||||||||||||||||||||||||||
| 43 | 20 | ▶ | if($raw_text =~ /(.*?\n)\n(.*)/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 19 | $this->{headers} = $1; | |||||||||||||||||||||||||||
| 45 | 19 | $this->{body} = $2; | |||||||||||||||||||||||||||
| 46 | 19 | $this->{cleartext} = ''; | |||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||
| 48 | 1 | return "unable to parse message raw text."; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # get "Subject" (required) | ||||||||||||||||||||||||||||
| 52 | 19 | ▶ | if($this->{headers} =~ /^Subject:\s*(.+?)$/im) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 18 | $this->{subject} = $1; | |||||||||||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||||||||||
| 55 | 1 | return "message subject not found."; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # get "x-emdis-message-type" (optional) | ||||||||||||||||||||||||||||
| 59 | 18 | ▶ | if($this->{headers} =~ /^x-emdis-message-type:\s*(\S+)\s*$/imo) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | $this->{emdis_message_type} = $1; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # attempt to parse "Subject" into MAIL_MRK:sender[:seqnum] | ||||||||||||||||||||||||||||
| 64 | 18 | my $mail_mrk = 'EMDIS'; | |||||||||||||||||||||||||||
| 65 | 18 | ▶ | if(ecs_is_configured()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $mail_mrk = $ECS_CFG->MAIL_MRK; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 18 | warn "ECS not configured, using MAIL_MRK = '$mail_mrk'\n"; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 18 | my ($mark, $sender, $seq_num); | |||||||||||||||||||||||||||
| 72 | 18 | ▶ | if($this->{subject} =~ /$mail_mrk:(\S+?):(\d+)(:(\d+)\/(\d+))?\s*$/i) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 73 | # regular message | ||||||||||||||||||||||||||||
| 74 | 9 | $this->{is_ecs_message} = 1; | |||||||||||||||||||||||||||
| 75 | 9 | $this->{is_meta_message} = ''; | |||||||||||||||||||||||||||
| 76 | 9 | $this->{is_document} = ''; | |||||||||||||||||||||||||||
| 77 | 9 | $this->{sender} = $1; | |||||||||||||||||||||||||||
| 78 | 9 | $this->{seq_num} = $2; | |||||||||||||||||||||||||||
| 79 | 9 | ▶ | $this->{part_num} = defined $4 ? $4 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 9 | ▶ | $this->{num_parts} = defined $5 ? $5 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 9 | ▶ | if($this->{part_num} > $this->{num_parts}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 1 | return "part_num is greater than num_parts: " . $this->{subject}; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | elsif($this->{subject} =~ /$mail_mrk:(\S+?):(\d+):DOC\s*$/io) { | ||||||||||||||||||||||||||||
| 86 | # document | ||||||||||||||||||||||||||||
| 87 | 1 | $this->{sender} = $1; | |||||||||||||||||||||||||||
| 88 | 1 | $this->{is_ecs_message} = ''; | |||||||||||||||||||||||||||
| 89 | 1 | $this->{is_meta_message} = ''; | |||||||||||||||||||||||||||
| 90 | 1 | $this->{is_document} = 1; | |||||||||||||||||||||||||||
| 91 | 1 | $this->{seq_num} = $2; | |||||||||||||||||||||||||||
| 92 | 1 | $this->{emdis_message_type} = 'DOC'; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | elsif($this->{subject} =~ /$mail_mrk:(\S+)\s*$/i) { | ||||||||||||||||||||||||||||
| 95 | # meta-message | ||||||||||||||||||||||||||||
| 96 | 3 | $this->{sender} = $1; | |||||||||||||||||||||||||||
| 97 | 3 | $this->{is_ecs_message} = 1; | |||||||||||||||||||||||||||
| 98 | 3 | $this->{is_meta_message} = 1; | |||||||||||||||||||||||||||
| 99 | 3 | $this->{is_document} = ''; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | # not an ECS message | ||||||||||||||||||||||||||||
| 103 | 5 | $this->{is_ecs_message} = ''; | |||||||||||||||||||||||||||
| 104 | 5 | $this->{is_meta_message} = ''; | |||||||||||||||||||||||||||
| 105 | 5 | $this->{is_document} = ''; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # get "Content-type" (optional) | ||||||||||||||||||||||||||||
| 109 | 17 | ▶ | if($this->{headers} =~ /^Content-type:\s*(.+?)\s*$/im) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 1 | $this->{content_type} = $1; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # get "From" (optional) | ||||||||||||||||||||||||||||
| 114 | 17 | ▶ | if($this->{headers} =~ /^From:\s*(.+?)\s*$/im) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 15 | $this->{from} = $1; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # get "To" (optional) | ||||||||||||||||||||||||||||
| 119 | 17 | ▶ | if($this->{headers} =~ /^To:\s*(.+?)\s*$/im) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 15 | $this->{to} = $1; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # decode quoted printable e-mails if necessary | ||||||||||||||||||||||||||||
| 124 | 17 | ▶ | if($this->{headers} =~ /^Content-Transfer-Encoding\s*:\s*quoted-printable$/im) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | $this->{headers} =~ | ||||||||||||||||||||||||||||
| 126 | 1 | s/^(Content-Transfer-Encoding)\s*:\s*quoted-printable$/$1: 8bit/im; | |||||||||||||||||||||||||||
| 127 | 1 | $this->{body} = decode_qp($this->{body}); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 17 | return $this; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 134 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 135 | 4 | ▶ | sub content_type { | ||||||||||||||||||||||||||
|
Subroutine
content_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | 4 | my $this = shift; | |||||||||||||||||||||||||||
| 137 | 4 | return $this->{content_type}; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 141 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 142 | 5 | ▶ | sub cleartext { | ||||||||||||||||||||||||||
|
Subroutine
cleartext: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 5 | my $this = shift; | |||||||||||||||||||||||||||
| 144 | 5 | return $this->{cleartext}; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 148 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 149 | 5 | ▶ | sub body { | ||||||||||||||||||||||||||
|
Subroutine
body: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 150 | 5 | my $this = shift; | |||||||||||||||||||||||||||
| 151 | 5 | return $this->{body}; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 155 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 156 | 6 | ▶ | sub from { | ||||||||||||||||||||||||||
|
Subroutine
from: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | 6 | my $this = shift; | |||||||||||||||||||||||||||
| 158 | 6 | return $this->{from}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 162 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 163 | 4 | ▶ | sub headers { | ||||||||||||||||||||||||||
|
Subroutine
headers: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 4 | my $this = shift; | |||||||||||||||||||||||||||
| 165 | 4 | return $this->{headers}; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 169 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 170 | 1 | ▶ | sub is_document { | ||||||||||||||||||||||||||
|
Subroutine
is_document: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 1 | my $this = shift; | |||||||||||||||||||||||||||
| 172 | 1 | return $this->{is_document}; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 176 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 177 | 13 | ▶ | sub is_ecs_message { | ||||||||||||||||||||||||||
|
Subroutine
is_ecs_message: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | 13 | my $this = shift; | |||||||||||||||||||||||||||
| 179 | 13 | return $this->{is_ecs_message}; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 183 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 184 | 10 | ▶ | sub is_meta_message { | ||||||||||||||||||||||||||
|
Subroutine
is_meta_message: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 185 | 10 | my $this = shift; | |||||||||||||||||||||||||||
| 186 | 10 | return $this->{is_meta_message}; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 190 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 191 | 2 | ▶ | sub num_parts { | ||||||||||||||||||||||||||
|
Subroutine
num_parts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 192 | 2 | my $this = shift; | |||||||||||||||||||||||||||
| 193 | 2 | return $this->{num_parts}; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 197 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 198 | 2 | ▶ | sub part_num { | ||||||||||||||||||||||||||
|
Subroutine
part_num: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 199 | 2 | my $this = shift; | |||||||||||||||||||||||||||
| 200 | 2 | return $this->{part_num}; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 204 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 205 | 4 | ▶ | sub raw_text { | ||||||||||||||||||||||||||
|
Subroutine
raw_text: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 206 | 4 | my $this = shift; | |||||||||||||||||||||||||||
| 207 | 4 | return $this->{raw_text}; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 211 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 212 | 9 | ▶ | sub sender { | ||||||||||||||||||||||||||
|
Subroutine
sender: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 213 | 9 | my $this = shift; | |||||||||||||||||||||||||||
| 214 | 9 | return $this->{sender}; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 218 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 219 | 7 | ▶ | sub seq_num { | ||||||||||||||||||||||||||
|
Subroutine
seq_num: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 220 | 7 | my $this = shift; | |||||||||||||||||||||||||||
| 221 | 7 | return $this->{seq_num}; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 225 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 226 | 5 | ▶ | sub subject { | ||||||||||||||||||||||||||
|
Subroutine
subject: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 227 | 5 | my $this = shift; | |||||||||||||||||||||||||||
| 228 | 5 | return $this->{subject}; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 232 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 233 | 6 | ▶ | sub to { | ||||||||||||||||||||||||||
|
Subroutine
to: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 234 | 6 | my $this = shift; | |||||||||||||||||||||||||||
| 235 | 6 | return $this->{to}; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 239 | # Accessor method (read-only). | ||||||||||||||||||||||||||||
| 240 | 2 | ▶ | sub full_msg { | ||||||||||||||||||||||||||
|
Subroutine
full_msg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 241 | 2 | my $this = shift; | |||||||||||||||||||||||||||
| 242 | 2 | return $this->{headers} . "\n" . $this->{body}; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 246 | # save raw message to file | ||||||||||||||||||||||||||||
| 247 | # returns empty string if successful, otherwise returns error message | ||||||||||||||||||||||||||||
| 248 | sub save_to_file | ||||||||||||||||||||||||||||
| 249 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
save_to_file: called CC=4 SCAR=14.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 250 | 1 | my $err = ''; | |||||||||||||||||||||||||||
| 251 | 1 | my $arg1 = shift; | |||||||||||||||||||||||||||
| 252 | 1 | my ($filename,$msg); | |||||||||||||||||||||||||||
| 253 | 1 | ▶ | if(ref $arg1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | # invoked as instance method | ||||||||||||||||||||||||||||
| 255 | 1 | $msg = $arg1; | |||||||||||||||||||||||||||
| 256 | 1 | $filename = shift; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | else { | ||||||||||||||||||||||||||||
| 259 | # invoked as class method | ||||||||||||||||||||||||||||
| 260 | 0 | $filename = $arg1; | |||||||||||||||||||||||||||
| 261 | 0 | my $raw_text = shift; | |||||||||||||||||||||||||||
| 262 | 0 | $msg = new EMDIS::ECS::Message($raw_text); | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | 1 | ▶ | open MSGFILE, ">$filename" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | or return "Unable to create file $filename: $!"; | ||||||||||||||||||||||||||||
| 266 | 1 | ▶ | print MSGFILE $msg->full_msg() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | or $err = "Unable to write file $filename: $!"; | ||||||||||||||||||||||||||||
| 268 | 1 | close MSGFILE; | |||||||||||||||||||||||||||
| 269 | 1 | chmod $EMDIS::ECS::FILEMODE, $filename; | |||||||||||||||||||||||||||
| 270 | 1 | return $err; # return error message (if any) | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 274 | # read message from file | ||||||||||||||||||||||||||||
| 275 | # returns object reference if successful, otherwise returns error message | ||||||||||||||||||||||||||||
| 276 | sub read_from_file | ||||||||||||||||||||||||||||
| 277 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read_from_file: called CC=7 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 278 | 1 | my $err = ''; | |||||||||||||||||||||||||||
| 279 | 1 | my $arg1 = shift; | |||||||||||||||||||||||||||
| 280 | 1 | my ($filename,$raw_text,$this); | |||||||||||||||||||||||||||
| 281 | 1 | ▶ | if(ref $arg1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | # invoked as instance method | ||||||||||||||||||||||||||||
| 283 | 0 | $this = $arg1; | |||||||||||||||||||||||||||
| 284 | 0 | $filename = shift; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | else { | ||||||||||||||||||||||||||||
| 287 | # invoked as class method | ||||||||||||||||||||||||||||
| 288 | 1 | $filename = $arg1; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # read file | ||||||||||||||||||||||||||||
| 292 | 1 | ▶ | open(MSGFILE, "< $filename") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | or return "Unable to open file $filename: $!"; | ||||||||||||||||||||||||||||
| 294 | 1 | ▶ | $raw_text = join('', <MSGFILE>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | or $err = "Unable to read file $filename: $!"; | ||||||||||||||||||||||||||||
| 296 | 1 | close MSGFILE; | |||||||||||||||||||||||||||
| 297 | 1 | ▶ | return $err if $err; # return error message (if any) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # attempt to construct object | ||||||||||||||||||||||||||||
| 300 | 1 | my $newmsg; | |||||||||||||||||||||||||||
| 301 | 1 | ▶ | if(ref $arg1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 0 | $newmsg = $this->new($raw_text); # re-define this object | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | else { | ||||||||||||||||||||||||||||
| 305 | 1 | $newmsg = new EMDIS::ECS::Message($raw_text); | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # set 'cleartext' attribute of message object | ||||||||||||||||||||||||||||
| 309 | $newmsg->{cleartext} = $newmsg->{body} | ||||||||||||||||||||||||||||
| 310 | 1 | ▶ | if ref $newmsg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 1 | return $newmsg; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 316 | # read and decrypt message from encrypted file | ||||||||||||||||||||||||||||
| 317 | # returns object reference if successful, otherwise returns error message | ||||||||||||||||||||||||||||
| 318 | sub read_from_encrypted_file | ||||||||||||||||||||||||||||
| 319 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
read_from_encrypted_file: not called CC=16 SCAR=56.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 320 | 0 | my $err = ''; | |||||||||||||||||||||||||||
| 321 | 0 | my $arg1 = shift; | |||||||||||||||||||||||||||
| 322 | 0 | my ($filename,$raw_text,$this); | |||||||||||||||||||||||||||
| 323 | 0 | ▶ | if(ref $arg1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | # invoked as instance method | ||||||||||||||||||||||||||||
| 325 | 0 | $this = $arg1; | |||||||||||||||||||||||||||
| 326 | 0 | $filename = shift; | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | else { | ||||||||||||||||||||||||||||
| 329 | # invoked as class method | ||||||||||||||||||||||||||||
| 330 | 0 | $filename = $arg1; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | # read encrypted file | ||||||||||||||||||||||||||||
| 334 | 0 | my $newmsg = read_from_file($filename); | |||||||||||||||||||||||||||
| 335 | 0 | ▶ | return $newmsg unless ref $newmsg; # check for error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | 0 | ▶ | return "not an ECS message" unless $newmsg->is_ecs_message or $newmsg->is_document; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$newmsg->is_ecs_message or $newmsg->is_document
MC/DC0%0/2
$newmsg->is_ecs_message or $newmsg->is_document
$newmsg->is_ecs_message
$newmsg->is_document
Truth table0%0/3
A or B
A= $newmsg->is_ecs_messageB= $newmsg->is_document
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # read relevant node info from node_tbl | ||||||||||||||||||||||||||||
| 339 | 0 | my $node_tbl = $main::ECS_NODE_TBL; | |||||||||||||||||||||||||||
| 340 | 0 | my $was_locked = $node_tbl->LOCK; | |||||||||||||||||||||||||||
| 341 | 0 | ▶ | if(not $was_locked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | 0 | ▶ | $node_tbl->lock() # lock node_tbl | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | or return "unable to lock node_tbl: " . | ||||||||||||||||||||||||||||
| 344 | $node_tbl->ERROR; | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | 0 | my $node = $node_tbl->read($newmsg->sender); | |||||||||||||||||||||||||||
| 347 | 0 | ▶ | if(not $was_locked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 0 | $node_tbl->unlock(); # unlock node_tbl | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | 0 | ▶ | if(not $node) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 0 | return "node not found: " . $newmsg->sender; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # decrypt message into temp file | ||||||||||||||||||||||||||||
| 355 | 0 | my $decr_filename = "$filename.asc"; | |||||||||||||||||||||||||||
| 356 | 0 | for ($node->{encr_typ}) { | |||||||||||||||||||||||||||
| 357 | 0 | ▶ | /PGP2/i and do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | $err = pgp2_decrypt( | ||||||||||||||||||||||||||||
| 359 | $filename, | ||||||||||||||||||||||||||||
| 360 | $decr_filename, | ||||||||||||||||||||||||||||
| 361 | (/verify/i ? $node->{encr_sig} : undef), | ||||||||||||||||||||||||||||
| 362 | 0 | ▶ | $node->{encr_out_passphrase}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | 0 | last; | |||||||||||||||||||||||||||
| 364 | }; | ||||||||||||||||||||||||||||
| 365 | 0 | ▶ | /OpenPGP/i and do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | $err = openpgp_decrypt( | ||||||||||||||||||||||||||||
| 367 | $filename, | ||||||||||||||||||||||||||||
| 368 | $decr_filename, | ||||||||||||||||||||||||||||
| 369 | (/verify/i ? $node->{encr_sig} : undef), | ||||||||||||||||||||||||||||
| 370 | 0 | ▶ | $node->{encr_out_passphrase}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | 0 | last; | |||||||||||||||||||||||||||
| 372 | }; | ||||||||||||||||||||||||||||
| 373 | 0 | $err = "unrecognized encr_typ: $node->{encr_typ}\n"; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | 0 | ▶ | if($err) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | 0 | unlink $decr_filename; | |||||||||||||||||||||||||||
| 377 | 0 | chomp($err); | |||||||||||||||||||||||||||
| 378 | 0 | return $err; | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # read message cleartext from temp file | ||||||||||||||||||||||||||||
| 382 | 0 | my $fh = new IO::File; | |||||||||||||||||||||||||||
| 383 | 0 | ▶ | return "unable to open file: $decr_filename" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | unless $fh->open("< $decr_filename"); | ||||||||||||||||||||||||||||
| 385 | 0 | my @cleartext = $fh->getlines(); | |||||||||||||||||||||||||||
| 386 | 0 | close $fh; | |||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | # remove temp file | ||||||||||||||||||||||||||||
| 389 | 0 | unlink $decr_filename; | |||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | # set 'cleartext' message attribute | ||||||||||||||||||||||||||||
| 392 | 0 | $newmsg->{cleartext} = join('', @cleartext); | |||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 0 | return $newmsg; | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 1 | 1; | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | __DATA__ | ||||||||||||||||||||||||||||