Instantly share code, notes, and snippets.

Embed
What would you like to do?
Procmail config and PHP script for sending email source address and subject to given callsigns through APRS-IS.
#LOGFILE=/home/nonoo/procmail.log
#VERBOSE=YES
:0
* ^List-ID: <aprs.nonoo.hu>$
| /home/nonoo/bin/mail2aprs.php
:0
* ^List-ID: <aprs.nonoo.hu>$
/dev/null
:0:
! nonoo@nonoo.hu
#!/usr/bin/php5
<?php
$aprs_callsign = 'CALLSIGN-15';
$aprs_passcode = 0;
$aprs_dsts = array('CALLSIGN-1', 'CALLSIGN-2');
// Reading the message from stdin
$f = fopen('php://stdin', 'r');
while ($line = fgets($f)) {
if (preg_match('/^Subject:/', $line))
$msg = trim(str_replace('Subject: ', '', $line));
if (preg_match('/^X-Original-Sender:/', $line))
$from = trim(str_replace('X-Original-Sender: ', '', $line));
if (isset($msg) && isset($from))
break;
}
fclose($f);
if (!isset($msg) || !isset($from))
die();
$msg = "$from: $msg";
$msg = substr($msg, 0, 67); // Trimming string to max. 67 chars
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket) {
$result = socket_connect($socket, 'hun.aprs2.net', 14580);
if ($result) {
// Authenticating
$tosend = "user $aprs_callsign pass $aprs_passcode\n";
socket_write($socket, $tosend, strlen($tosend));
$authstartat = time();
$authenticated = false;
while ($msgin = socket_read($socket, 1000, PHP_NORMAL_READ)) {
if (strpos($msgin, "$aprs_callsign verified") !== FALSE) {
$authenticated = true;
break;
}
// Timeout handling
if (time()-$authstartat > 5)
break;
}
if ($authenticated) {
foreach ($aprs_dsts as $dst) {
$tosend = "$aprs_callsign>APRS,TCPIP*::" . str_pad($dst, 9, ' ') . ":$msg\n";
socket_write($socket, $tosend, strlen($tosend));
}
}
}
socket_close($socket);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment