Saturday, June 29, 2019

Automating the Winlink

So, no big surprise, I'm lazy.  I realize at some point I'll quit checking Winlink regularly.  Of course, that is exactly the time someone will send me something interesting.

So how to make my Winlink messages visible with no effort.  Of course, put them in the email inbox.

I already have pat running on my LAN so checking Winlink is a simple matter of pointing my browser. My email client checks for email from my provider every so often, so the first step is to have pat check for inbound Winlink messages every so often.  This is no big deal, simply put a pat connect command on the cron tab and away we go.

I decided to check via telnet since from time to time I will swipe my Winlink radio for some other service. I do intend to fix that in the near future so I can be assured the RF path is available.

I decided to log the results.  Since the dialog from the RMS is usually pretty boring, I only keep the most recent.  Errors, tho, I want to see whether something happened when I wasn't looking.  So I wrote a little script:


So, now any Winlink messages are sitting on the Raspberry Pi, how to get them to the inbox.

pat puts everything of interest in the ~/.wl2k directory tree.  Received messages are put in the ~/.wl2k/mailbox/(call)/in folder.  The messages are stored in plain text, so it would be a simple matter to mail them.  Rather than try to keep track of wich messages have been mailed, I decided to move the processed messages into Winlink's archive folder.

The Linux mail command makes this pretty straightforward.  Note that your ISP or email provider may have some hoops to jump through in order to make this work. In my case, it was pretty easy.  The only slightly tricky part was fishing the subject line from the original messages so when they got to my email client I knew what I was looking at.

The Raspbian implementation of bash seemed to be a little different than those with which I am familiar, and I am not that much of a bash maestro anyway, so I decided to script it in php.  This does mean you need to install php, but that is pretty easy.

The following is my script:

<?php
  /* Send incoming Winlink to email, then move to archive
   *
   *  jjmcd - 190628
   */

  /* Location of Winlink mailbox */
$mailbox="/home/winlink/.wl2k/mailbox/WB8RCR";
/* Address messages will be coming from */
$from_address="wb8rcr@chartermi.net";
/* Address to send messages to */
$to_address="wb8rcr@arrl.net";
/* Subject prefix to ease processing on receiving end */
$mail_prefix="PAT01:: ";

$list = dir($mailbox . "/in");
/* Loop through files in inbox */
while ( false !== ($file = $list->read()))
  {
    if ( substr($file,0,1)!="." ) /* Ignore this directory */
      {
        $got=0;
        /* Read the file until we find the subject line */
        $f = fopen($mailbox . "/in/" . $file,"r");
        while ( $line=fgets($f,1000) )
          if ( !feof($f) )
            {
              if ( substr($line,0,7)=="Subject" )
                if ( !$got )
                  {
                    /* Strip off the "Subject:" part */
                    $subj=substr($line,9,strlen($line)-11);
                    /* And remember we found it */
                    $got=1;
                  }
            }
        fclose($f);
        if ( $got )
          {
            /* Build the mail command */
            $command = "mail -s \"" . $mail_prefix . $subj .
                     "\" " . "-r \"" . $from_address . "\" " .
                     " \"" . $to_address . "\" <" . $mailbox .
                     "/in/" . $file;
            exec($command);
            /* Move the processed file to archive */
            $mvcommand = "mv " .  $mailbox . "/in/" . $file . " "
              . $mailbox . "/archive/";
            exec($mvcommand);
          }
      }
  }
$list->close();
?>

If you are lucky, the only things you might want to change are the first few lines. After that, the script does a directory listing of the inbox (ignoring the . and .. files). It then loops through that list, opening each file and finding the Subject: line. It pulls the string Subject: off that line and adds $mail_prefix (the mailer will add Subject: back).

The mail command is formed and sent to the shell, then the mv command is also sent to the shell. The script then goes on to the next file.

This script is then scheduled on the crontab shortly after the mail was retrieved from the RMS.  I suppose it might have been better to do both in one script, but it can be easier to take a step at a time.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.