Sometimes, especially when I'm using my cell phone as a modem, I want
to get online, download my new mail, upload whatever mail I have ready
to send, and get offline as soon as possible.

Here's the system I use for this at the moment.  I run this shell
script to control everything; pon and poff are the Debian commands to
turn PPP on and off.  This reminds me of Fidonet and UUCP.

#!/bin/sh
if [ $# -ne 1 ] ; then
        echo "Usage: $0 outgoingmail" >&2
        exit 1
fi
: ${PROVIDER=sfmeer}
pon "$PROVIDER"
wfg /var/run/pppd.log
rsync -e ssh -avzz kirk.dnaco.net:/var/mail/kragen /home/kragen/priv/mail/currentmbox &
rsync -e ssh -avzz panacea.canonical.org:/var/mail/kragen 
/home/kragen/priv/mail/currentmbox.panacea &
ssh panacea.canonical.org python mailmsg.py send < "$1" &
wait; wait; wait; poff "$PROVIDER"


mailmsg.py is the program (posted here previously) I use to send mail;
it takes a file that may have zero or more mail messages encapsulated
inside of it, extracts them, and sends them.  wfg stands for "wait for
file to grow", and is the following program:

/* wait for file to grow.
 *
 * Give this program a filename on the command line.  It will open the file,
 * seek to the end, and check about ten times a second to see if the file has 
 * grown.  If so, it will exit. 
 *
 * This is useful in shell scripts.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

int main(int argc, char **argv) 
{
  int file;
  struct timeval tenth_of_a_second;

  tenth_of_a_second.tv_sec = 0;
  tenth_of_a_second.tv_usec = 100*1000;  /* 100 ms */
  
  if (argc != 2) {
    fprintf(stderr, "Usage: %s filename\n", argv[0]);
    return -1;
  }
  file = open(argv[1], O_RDONLY);
  if (file < 0) {
    perror(argv[1]);
    return -1;
  }
  if (lseek(file, 0, SEEK_END) == (off_t)-1) {
    perror("lseek");
    return -1;
  }
  for (;;) {
    char c;
    int rv = read(file, &c, 1);
    if (rv < 0) {
      perror("read");
      return -1;
    } else if (rv == 1) {
      return 0;
    } else if (rv == 0) {
      struct timeval interval = tenth_of_a_second;
      select(0, 0, 0, 0, &interval);
    } else {
      perror("inturnul errawr");
      return -1;
    }
  }
}


/var/run/pppd.log is created by the following shell script in
/etc/ppp/ip-up.d:
#!/bin/sh
date >> /var/run/pppd.log

This setup works reasonably well, but it has some problems:
- the first time PPP comes up after bootup, /var/run/pppd.log doesn't exist,
  so wfg exits, so exchmail fails; I usually touch it when that happens.
- with extremely unreliable connections with dynamic IP addresses,
  such as my Sprint cellphone, occasionally the following sequence of
  events happens:
  - PPP comes up
  - /var/run/pppd.log grows
  - wfg exits
  - the rsync commands and the ssh command form their connections to their
    respective endpoints and send their initial requests
  - PPP goes down
  - PPP comes back up with a different IP address
  Now, the rsync and ssh commands will hang forever without accomplishing
  any useful work, which is extremely undesirable when I'm paying by the
  minute.  In this case, I generally hit ^C and try again.

Reply via email to