procmail
[Top] [All Lists]

Re: splitting and forwarding messages

1997-12-08 01:42:36
On Sun, 7 Dec 1997 23:13:22 -0800 (PST), Vince LaMonica
<vjl(_at_)cullasaja(_dot_)com> wrote:
I have a 200 character limit on my pager. But I have wordy contacts who go
over that limit. What I would like to do is have a recipe split up
messages addressed to my pager into 200 character (max) messages. So if
someone sent me a 500 char page, it would be sent to my pager as 2 200
char pages and 1 100 char page.

This stuff about forwarding to pagers is a recurring topic on this
list. I've tried to find a good summary of all the issues but there
always seems to be some tiny twist to what people would like to have
implemented. As a general comment for future generations, the Procmail
part is usually trivial and the problem reduces to writing a good
program (shell script or otherwise) for formatting the text precisely
the way you want it, and spitting it out in suitable chunks.

Here's something to split up the body of the message into smaller
chunks and do a shell script on each chunk. 

  tr '\012' ' ' | fold -s -w 200 | while read line; do ... ; done

The -s option to fold says to only wrap lines on whitespace if
possible. (Not all versions of fold have this.) This is a cheap trick
but works nicely as long as messages are free-form text-only. In the
end, you might be better off writing your own awk or perl script to
strip all kinds of redundant formatting from a message (indentation,
quoting, changing tabs to spaces, compressing redundant whitespace,
etc) and chunk it. A good reformatter would recognize when only part
of a message is indented, and keep the indentation on that (or at
least preserve line breaks on such sections). (There's a program
called par which attempts to canonicalize line formatting but I've
found it a bit awkward to use. <http://www.cs.berkeley.edu/~amc/Par/>)

Anyhow, here's how you might construct a suitable minimalized message
for forwarding to a pager using the above shell script snippet:

    # Create a duplicate of the message to forward to the pager.
    # This will be reformatted and have most headers stripped off.

    :0c
    {
        # Construct header with only From: and Subject: retained
        :0hw
        HEADER=| formail -XFrom: -XSubject:

        # Reformat body as 200-character lines and send each 
        # as a separate message with the preconstructed minimal header
        :0bw
        | tr '\012' ' ' | fold -s -w 200 | while read line; do
            echo -e "$HEADER\n\n$line" | \
            $SENDMAIL $SENDMAILFLAGS pageraddress(_at_)wherever(_dot_)com ; done
    }

If your version of echo doesn't understand \n to mean newline (and/or
the -e option to enable this escape processing), you need to tweak
this. 
  (You might need to anyway -- this is mostly untested. In my limited
testing, I found the messages would arrive in more or less random
order. Inserting pauses in the script should help to some extent, but
could lead to other problems and is not an ideal solution anyhow.)

I don't know if the header trimming is required; some pager gateways
appear to count the headers as part of the message, while others
don't. Again, for future generations, details like this are relevant
to include when you ask about how to do this.
        
/* era */

-- 
 Paparazzi of the Net: No matter what you do to protect your privacy,
  they'll hunt you down and spam you. <http://www.iki.fi/~era/spam/>

<Prev in Thread] Current Thread [Next in Thread>