Don't remember if I got this script from the Datalist script library for
Dialog or from elsewhere. I didn't write it.
Seems the script is wrong, as you pointed out. It finds the first line
*containing* "-----BEGIN PGP SIGNED" as a /substring/. It should check
that substring starts at column 1. I thought the PGP sig block was
always at the end of the message, so just delete everything from and
after the "-----BEGIN PGP SIGNATURE----" line.
Examples:
http://www.heavensgate.com/misc/bg2pgp.txt
https://philzimmermann.com/text/PGP_10thAnniversary.txt
Those have a PGP message block delimiter line as the first line in the
message and starting at column 1. Seems I only need to check if the
first line's entire string is the PGP message block delimiter line, not
by having to check all the lines in the message.
One example doesn't have a following "Hash: <type>" line, so I should
delete it only if a prior delete got rid of the PGP message block
delimiter line.
Looking for the pgp_sig_start substring anywhere within message.text
also seems wrong. That will look for the substring anywhere in the
message, not for it to start in column 1 and not only after the PGP
message block delimiter line. I don't know if the delete() string
command can be modified to look for the position of a concatenation of
EOL + pgp_sig_start (or, if EOL isn't recorded at the end of strings
then maybe NUL + pgp_sig_start if strings are NUL terminated). That
position + 1 (one character after the EOL or NUL) would mark the string
position where the PGP sig block started.
https://www.rossde.com/PGP/pgp_signatures.html
That shows the "-----BEGIN PGP SIGNED MESSAGE-----" delimiter line is
always in line 1 (starting in column 1) and the PGP sig block is always
at the end of the message.
I'm assuming in Delphi that message.strings[] is an array of strings
(don't know if terminated by EOL or NUL) where each string is a line in
the message with an index value starting at 0 (not 1). Seems the whole
"delete PGP sig" function should be conditional on first finding the PGP
message block delimiter line, and that every line from and after the
starting PGP sig delimiter can get deleted. Maybe the following works:
// Check if line 0 starts a PGP message block.
if message.strings(0) = '-----BEGIN PGP SIGNED MESSAGE-----' then
begin
// Delete PGP message block delimiter line.
message.delete(0);
// Replace with a comment that PGP sig was removed.
message.insert(0, '*****PGP SIGNATURE REMOVED*****'); // Optional.
// If present as the next line, delete the Hash specifier.
if message.strings(1) = 'Hash: ' then
message,delete(1);
// Delete the PGP signature block at the end.
sig_found := 0;
for i := 1 to message.count - 1 do
begin
if message.strings(i) = '-----BEGIN PGP SIGNATURE-----' then
sig_found := 1;
// If sig start delimiter line found, delete it and the rest.
if sig_found then message.delete(i);
end;
end;
end;
The 'if' checks if the first line is the PGP message block delimiter
line; else, there's no point in checking if the next line is a Hash line
and also no point looking for a PGP sig block. Everything rests on
finding that required delimiter line that delineates the start of the
PGP message block.
The for-loop checks for if and when the PGP sig block starts. While I
could've added another for-loop inside that for-loop to do the deletes,
I didn't see much impact in retesting each further line. I could
possibly short-circuit the 'if' by doing a boolean AND between the
string (line) value and sig_found assuming Pascal/Delphi supports short
circuiting of Boolean operators (e.g., FALSE AND ..., don't need to test
anything beyond FALSE since the entire Boolean will be FALSE).
If there can be mixed content where the PGP message block starts later
in the message (not in the first line) then I'll have to use the
for-loop to walk through lines in the message. If there can be
non-signed content after the PGP sig block, yeah, then I have to find
the length of just the sig block and delete just those lines.
I don't do Delphi, so I've probably made mistakes. I haven't touched
Pascal for over 40 years and that was before it got objectified.