EMDIS-ECS

 view release on metacpan or  search on metacpan

lib/EMDIS/ECS/Message.pm  view on Meta::CPAN


# ----------------------------------------------------------------------
# Accessor method (read-only).
sub sender {
    my $this = shift;
    return $this->{sender};
}

# ----------------------------------------------------------------------
# Accessor method (read-only).
sub seq_num {
    my $this = shift;
    return $this->{seq_num};
}

# ----------------------------------------------------------------------
# Accessor method (read-only).
sub subject {
    my $this = shift;
    return $this->{subject};
}

# ----------------------------------------------------------------------
# Accessor method (read-only).
sub to {
    my $this = shift;
    return $this->{to};
}

# ----------------------------------------------------------------------
# Accessor method (read-only).
sub full_msg {
    my $this = shift;
    return $this->{headers} . "\n" . $this->{body};
}

# ----------------------------------------------------------------------
# save raw message to file
# returns empty string if successful, otherwise returns error message
sub save_to_file
{
    my $err = '';
    my $arg1 = shift;
    my ($filename,$msg);
    if(ref $arg1) {
        # invoked as instance method
        $msg = $arg1;
        $filename = shift;
    }
    else {
        # invoked as class method
        $filename = $arg1;
        my $raw_text = shift;
        $msg = new EMDIS::ECS::Message($raw_text);
    }
    open MSGFILE, ">$filename"
        or return "Unable to create file $filename: $!";
    print MSGFILE $msg->full_msg()
        or $err = "Unable to write file $filename: $!";
    close MSGFILE;
    chmod $EMDIS::ECS::FILEMODE, $filename;
    return $err;  # return error message (if any)
}

# ----------------------------------------------------------------------
# read message from file
# returns object reference if successful, otherwise returns error message
sub read_from_file
{
    my $err = '';
    my $arg1 = shift;
    my ($filename,$raw_text,$this);
    if(ref $arg1) {
        # invoked as instance method
        $this = $arg1;
        $filename = shift;
    }
    else {
        # invoked as class method
        $filename = $arg1;
    }

    # read file
    open(MSGFILE, "< $filename")
        or return "Unable to open file $filename: $!";
    $raw_text = join('', <MSGFILE>)
        or $err = "Unable to read file $filename: $!";
    close MSGFILE;
    return $err if $err;  # return error message (if any)

    # attempt to construct object
    my $newmsg;
    if(ref $arg1) {
        $newmsg = $this->new($raw_text);  # re-define this object
    }
    else {
        $newmsg = new EMDIS::ECS::Message($raw_text);
    }

    # set 'cleartext' attribute of message object
    $newmsg->{cleartext} = $newmsg->{body}
        if ref $newmsg;

    return $newmsg;
}

# ----------------------------------------------------------------------
# read and decrypt message from encrypted file
# returns object reference if successful, otherwise returns error message
sub read_from_encrypted_file
{
    my $err = '';
    my $arg1 = shift;
    my ($filename,$raw_text,$this);
    if(ref $arg1) {
        # invoked as instance method
        $this = $arg1;
        $filename = shift;
    }
    else {
        # invoked as class method



( run in 1.291 second using v1.01-cache-2.11-cpan-39bf76dae61 )