Re: format and output all received: lines in an e-mail message

2019-11-20 Thread Greg Minshall
hi, Valdis!

ah, formail -- awesome!  yes, that does great.

Ken and Ralph -- thanks for your replies.  yes, awk and sed are in my
repertoire, but, well, i was wondering if i could avoid those in this
case.

cheers, Greg

ps -- the goal is a little "blame" script for e-mail that tells you how
long a given message spent moving from A to B.  (this relies, of course,
on globally synchronized clocks, but that seems much more likely to be
true today than it did when Received: lines were first introduced.)



Re: format and output all received: lines in an e-mail message

2019-11-20 Thread Valdis Klētnieks
On Wed, 20 Nov 2019 10:41:34 +0530, Greg Minshall said:
> hi.  i'd like first, of course, to thank you all for nmh!
>
> then, i'd like to use something like fmttest(1) to print out all the
> "Received:" lines in an e-mail message.  ideally, each "Received:" line
> would come out on a separate line; less ideally, but i'm sure very
> practical, a very long line would come out, with some odd ascii code
> separating the individual lines.

Well in the "not using mh tools at all" category, if you have procmail
installed, you can just use:

[~] cat Mail/inbox/1 | formail -c -x Received
 from localhost (localhost [127.0.0.1]) by turing-police.cc.vt.edu 
(8.12.6.Beta1/8.12.6.Beta1) with ESMTP id g86N6Pbs020540 for 
; Fri, 6 Sep 2002 19:06:25 -0400
 from fan.cc.vt.edu [198.82.161.8]  by localhost with POP3 
(fetchmail-5.9.0)for valdis@localhost (single-drop); Fri, 06 Sep 2002 
19:06:25 -0400 (EDT)
 from dagger.cc.vt.edu ([10.1.1.11])  by gkar.cc.vt.edu (Sun Internet Mail 
Server sims.3.5.2001.05.04.11.50.p10)  with ESMTP id 
<0h210069rhg...@gkar.cc.vt.edu> for valdis@sims-ms-daemon; Fri,  6 Sep 2002 
19:05:21 -0400 (EDT)
 from mail01.corp.tellme.com (mail01.corp.tellme.com [209.157.157.98])  by 
dagger.cc.vt.edu (Mirapoint Messaging Server MOS 3.2.0-GA)  with ESMTP id 
AQM10446; Fri, 06 Sep 2002 19:05:20 -0400 (EDT)
 from mail01.corp.tellme.com (localhost [127.0.0.1])  by localhost.tellme.com 
(Postfix) with ESMTP id 1EE5744BDD for  ; Fri, 06 Sep 
2002 16:05:19 -0700 (PDT)
 from dhcp157-180.corp.tellme.com  (dhcp157-180.corp.tellme.com 
[209.157.157.180]) by mail01.corp.tellme.com  (Postfix) with ESMTP id 
A6F5444BDA for ; Fri,  06 Sep 2002 16:05:18 -0700 (PDT)

(Note that this returns the *values* of the lines, not the line.
 | sed -e 's/^/Received: /'  if that matters to you.


pgpfZqsSToygm.pgp
Description: PGP signature


Re: format and output all received: lines in an e-mail message

2019-11-20 Thread Ralph Corderoy
Hi Paul,

> greg wrote:
> > then, i'd like to use something like fmttest(1) to print out all the
> > "Received:" lines in an e-mail message.  ideally, each "Received:"
> > line would come out on a separate line; less ideally, but i'm sure
> > very
>
> Is strict use of only MH tools a requirement for some reason?  Because
> if not, then this does a pretty good job:
> show -noshowproc |sed -n -e '/^$/q' -e '/^Received:/p' 

Fields can be continued by starting the next line with white-space,
and Received fields are often continued.  :-)

-- 
Cheers, Ralph.



Re: format and output all received: lines in an e-mail message

2019-11-20 Thread Paul Fox
greg wrote:
 > then, i'd like to use something like fmttest(1) to print out all the
 > "Received:" lines in an e-mail message.  ideally, each "Received:" line
 > would come out on a separate line; less ideally, but i'm sure very

Is strict use of only MH tools a requirement for some reason?  Because
if not, then this does a pretty good job:
show -noshowproc |sed -n -e '/^$/q' -e '/^Received:/p' 

paul
=--
paul fox, p...@foxharp.boston.ma.us (arlington, ma, where it's 35.2 degrees)




Re: format and output all received: lines in an e-mail message

2019-11-20 Thread Ralph Corderoy
Hi Greg,

> That's all I've time for at the moment, but others may chip in and this
> saves them covering some of the ground.

Ken's mhl(1) suggestion is what I would have tried.  But there's also
non-nmh solutions, e.g. sed or awk if you're familiar with those.
This prints all the Foo fields, each on a line, joining continuation lines.

#! /usr/bin/gawk -bf

BEGIN {
IGNORECASE = 1
}

pending {
if (/^[ \t]/) {
printf " %s", $0
} else {
print ""
pending = 0
}
}

/^foo[ \t]*:/ {
printf "%s:%s", FILENAME, $0
pending = 1
next
}

/^$/ {
nextfile
}

END {
if (pending)
print ""
}

-- 
Cheers, Ralph.



Re: format and output all received: lines in an e-mail message

2019-11-20 Thread Ken Hornstein
>then, i'd like to use something like fmttest(1) to print out all the
>"Received:" lines in an e-mail message.  ideally, each "Received:" line
>would come out on a separate line; less ideally, but i'm sure very
>practical, a very long line would come out, with some odd ascii code
>separating the individual lines.

The format engine may not be the best tool for this job.

It is really optimized for dealing with header fields that appear only
once (e.g., Subject) or ones that can be combined in a reasonable
way (like header fields that contain address).  So when you refer to
%{received}, you're getting everything folded together.  There's no loop
processing that you could use to say, "Run these commands for every
instance of the Received header", which is really wnat you want.  That's
not to say it wouldn't be useful, but it would require some serious
rethinking of the format engine.

But, have you considered using mhl with a custom mhl format file?  That's
a tool designed to deal with each invidiual header, and I think that
would do exactly what you want.

--Ken



Re: format and output all received: lines in an e-mail message

2019-11-20 Thread Ralph Corderoy
Hi Greg,

Keeping you CC'd.

> context: the problem is there are (typically) multiple "Received:"
> header in an e-mail messages; can try either of these
> 
> bash% fmttest -outsize max +SOMEFOLDER 660 -format "%(putstr{received});"
> bash% fmttest -outsize max +SOMEFOLDER 660 -format "%(putlit{received});"
> 
> and get either a long line, or a number of lines, but without any real
> indication of how to separate the output into individual "Received"
> lines.

Here's my input test file.

$ sed -n l mail
foo: a aa$
foo: b bb$
foo: c$
 cc$
foo:$
 dd$
foo: e$
\tee$
$

Obvious approach, as you found, gives all the values of the Received
fields in one go.

$ fmttest -outsize max -format '%{foo}' -file mail
a aa b bb c cc dd e ee
$

-dump and -trace can be useful for more insight.

$ fmttest -dump -trace -outsize max -format '%{foo}' -file mail
Instruction dump of format string: 
%{foo}
COMP, comp "foo"
DONE
COMP, comp "foo", c_flags 0x8
num=0 str=
outbuf="a aa b bb c cc dd e ee"
DONE
a aa b bb c cc dd e ee
$

Using %(putstr) shows the string register being set to the values with a
tab inserted to join them together.

$ fmttest -dump -trace -outsize max -format '%(putstr{foo})' -file mail
Instruction dump of format string: 
%(putstr{foo})
LS_COMP, comp "foo"
STR
DONE
LS_COMP, comp "foo", c_flags 0x8
num=0 str=" a aa\n\t b bb\n\t c\n cc\n\t\n dd\n\t e\n\tee"
STR
outbuf="a aa b bb c cc dd e ee"
DONE
a aa b bb c cc dd e ee
$

Using %(putlit) preserves those tabs.

$ fmttest -dump -trace -outsize max -format '%(putlit{foo})' -file mail
Instruction dump of format string: 
%(putlit{foo})
LS_COMP, comp "foo"
STRLIT
DONE
LS_COMP, comp "foo", c_flags 0x8
num=0 str=" a aa\n\t b bb\n\t c\n cc\n\t\n dd\n\t e\n\tee"
STRLIT
outbuf=" a aa\n\t b bb\n\t c\n cc\n\t\n dd\n\t e\n\tee"
DONE
 a aa
 b bb
 c
 cc

 dd
 e
ee
$

But \n\t can't be used to split them back into the original distinct
field values because that may occur within a value, as with my ee test
input.

That's all I've time for at the moment, but others may chip in and this
saves them covering some of the ground.

-- 
Cheers, Ralph.



format and output all received: lines in an e-mail message

2019-11-20 Thread Greg Minshall
hi.  i'd like first, of course, to thank you all for nmh!

then, i'd like to use something like fmttest(1) to print out all the
"Received:" lines in an e-mail message.  ideally, each "Received:" line
would come out on a separate line; less ideally, but i'm sure very
practical, a very long line would come out, with some odd ascii code
separating the individual lines.

any hints?

cheers, Greg


context: the problem is there are (typically) multiple "Received:"
header in an e-mail messages; can try either of these

bash% fmttest -outsize max +SOMEFOLDER 660 -format "%(putstr{received});"
bash% fmttest -outsize max +SOMEFOLDER 660 -format "%(putlit{received});"

and get either a long line, or a number of lines, but without any real
indication of how to separate the output into individual "Received"
lines.