summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames <31272717+gpd-pocket-hacker@users.noreply.github.com>2020-11-09 16:10:37 +0000
committerJames <31272717+gpd-pocket-hacker@users.noreply.github.com>2020-11-09 16:24:06 +0000
commitbfac22dc79164bab10185188e12cee313ab96de9 (patch)
tree4cee4dd9631304042982ddb0a248a541d9b6af12
parent6a27a521f99c7babc9d000465b661f66b54ee101 (diff)
downloadgalaxy_tools-bfac22dc79164bab10185188e12cee313ab96de9.tar.gz
galaxy_tools-bfac22dc79164bab10185188e12cee313ab96de9.tar.bz2
galaxy_tools-bfac22dc79164bab10185188e12cee313ab96de9.zip
add email support
-rw-r--r--.gitignore1
-rw-r--r--Makefile26
-rw-r--r--email.c112
-rw-r--r--email.h2
-rw-r--r--net_rx.c14
-rw-r--r--rx.c29
-rw-r--r--serial_rx.c14
-rw-r--r--test_email.c25
8 files changed, 193 insertions, 30 deletions
diff --git a/.gitignore b/.gitignore
index ae0d207..360db0e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,4 +5,5 @@ serial_arm
net_arm
serial_keypad
net_keypad
+test_email
*.orig
diff --git a/Makefile b/Makefile
index aa4c00f..b55dec5 100644
--- a/Makefile
+++ b/Makefile
@@ -3,19 +3,14 @@ PKG_CONFIG=pkg-config
CURSES_CFLAGS=$(shell ${PKG_CONNFIG} --cflags ncursesw)
CURSES_LIBS=$(shell ${PKG_CONFIG} --libs ncursesw)
+#OPENWRT_BASE=/root/projects/openwrt/gl-inet-mt300n-v2/master-d20f4fc
+#TOOLCHAIN=${OPENWRT_BASE}/staging_dir/toolchain-mipsel_24kc_gcc-7.3.0_musl
+#CC=${TOOLCHAIN}/bin/mipsel-openwrt-linux-musl-gcc
+#STAGING_DIR=${OPENWRT_BASE}/staging_dir/target-mipsel_24kc_musl
#export STAGING_DIR
+#CFLAGS=-I${STAGING_DIR}/include -I${TOOLS_DIR}/include
-#TOOLS_DIR=/root/projects/openwrt/tpl-wdr3600/trunk-50149/staging_dir/toolchain-mips_34kc_gcc-5.3.0_musl-1.1.16
-#STAGING_DIR=/root/projects/openwrt/tpl-wdr3600/trunk-50149/staging_dir/target-mips_34kc_musl-1.1.16/
-
-#TOOLS_DIR=/root/projects/openwrt/tpl-wdr3600/trunk-47381/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_musl-1.1.11
-#STAGING_DIR=/root/projects/openwrt/tpl-wdr3600/trunk-47381/staging_dir/target-mips_34kc_musl-1.1.11
-
-#CC=${TOOLS_DIR}/bin/mips-openwrt-linux-gcc
-
-CFLAGS=-I${STAGING_DIR}/include -I${TOOLS_DIR}/include
-
-PROGS=net_rx net_arm serial_rx serial_arm serial_keypad net_keypad
+PROGS=net_rx net_arm serial_rx serial_arm serial_keypad net_keypad #test_email
PREFIX=/usr/local
@@ -25,10 +20,11 @@ install: ${PROGS}
mkdir -p ${DESTDIR}/${PREFIX}/bin
for i in ${PROGS}; do install -m 755 $$i ${DESTDIR}/${PREFIX}/bin/$$i; done
-net_rx:util.o sia.o net_rx.o rx.o
-net_arm:util.o sia.o net_arm.o arm.o
-serial_rx:util.o sia.o serial_rx.o rx.o
+net_rx:util.o sia.o net_rx.o rx.o email.o
+net_arm:util.o sia.o net_arm.o arm.o
+serial_rx:util.o sia.o serial_rx.o rx.o email.o
serial_arm:util.o sia.o serial_arm.o arm.o
+test_email:test_email.o email.o
serial_keypad:util.o keypad.o serial_keypad.o
${CC} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${CURSES_LIBS} ${LIBS}
@@ -37,7 +33,7 @@ net_keypad:util.o keypad.o net_keypad.o
${CC} ${CFLAGS} ${LDFLAGS} -o $@ $^ ${CURSES_LIBS} ${LIBS}
tidy:
- astyle -A3 -s2 --attach-extern-c -L -c -w -Y -m0 -f -p -H -U -k3 -xj -xd sia.c sia.h util.c util.h serial_rx.c net_rx.c serial_arm.c net_arm.c arm.c keypad.c net_keypad.c serial_keypad.c rx.c
+ astyle -A3 -s2 --attach-extern-c -L -c -w -Y -m0 -f -p -H -U -k3 -xj -xd sia.c sia.h util.c util.h serial_rx.c net_rx.c serial_arm.c net_arm.c arm.c keypad.c net_keypad.c serial_keypad.c rx.c email.c test_email.c
clean:
/bin/rm -f *.o
diff --git a/email.c b/email.c
new file mode 100644
index 0000000..31c22b8
--- /dev/null
+++ b/email.c
@@ -0,0 +1,112 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <syslog.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "email.h"
+
+
+
+static int daemonish (int nochdir, int noclose)
+{
+ if (!nochdir && chdir ("/"))
+ return -1;
+
+ if (!noclose) {
+ int fd, failed = 0;
+
+ if ((fd = open ("/dev/null", O_RDWR)) < 0) return -1;
+
+ if (dup2 (fd, 0) < 0 || dup2 (fd, 1) < 0 || dup2 (fd, 2) < 0)
+ failed++;
+
+ if (fd > 2) close (fd);
+
+ if (failed) return -1;
+ }
+
+ switch (fork()) {
+ case 0:
+ break;
+
+ case -1:
+ return -1;
+
+ default:
+ return 1;;
+ }
+
+ if (setsid() < 0) return -1;
+
+ switch (fork()) {
+ case 0:
+ break;
+
+ case -1:
+ return -1;
+
+ default:
+ _exit (0);
+ }
+
+ return 0;
+}
+
+
+static void write_complete (int fd, const void *_buf, size_t len)
+{
+ ssize_t writ;
+ const char *buf = _buf;
+
+ while (len) {
+ writ = write (fd, buf, len);
+
+ if (writ <= 0) return;
+
+ len -= writ;
+ buf += writ;
+ }
+}
+
+
+static void write_str (int fd, const char *str)
+{
+ write_complete (fd, str, strlen (str));
+}
+
+void send_email (const char *to, const char *subject, const char *body)
+{
+ int pipes[2];
+
+ if (daemonish (0, 1)) return;
+
+ pipe (pipes);
+
+
+ switch (fork()) {
+ case 0:
+ close (pipes[1]);
+ dup2 (pipes[0], 0);
+ close (pipes[0]);
+
+ execl ("/usr/lib/sendmail", "sendmail", to, (char *) 0);
+ _exit (1);
+
+ case -1:
+ _exit (1);
+ }
+
+ close (pipes[0]);
+
+ write_str (pipes[1], "Subject: ");
+ write_str (pipes[1], subject);
+ write_str (pipes[1], "\n\n");
+ write_str (pipes[1], body);
+ write_str (pipes[1], "\n\n");
+ close (pipes[1]);
+
+ _exit (0);
+}
diff --git a/email.h b/email.h
new file mode 100644
index 0000000..af81eee
--- /dev/null
+++ b/email.h
@@ -0,0 +1,2 @@
+extern void send_email(const char *to,const char *subject, const char *body);
+
diff --git a/net_rx.c b/net_rx.c
index 1ea0993..15e181b 100644
--- a/net_rx.c
+++ b/net_rx.c
@@ -10,13 +10,13 @@
#include "util.h"
#include "sia.h"
-extern int new_block (int fd, SIA_Block *b, int log);
+extern int new_block (int fd, SIA_Block *b, int log, const char *email);
extern void periodic_task (void);
static int usage (const char *name)
{
fprintf (stderr, "Usage:\n");
- fprintf (stderr, "%s [ -l ] [ -p listen_port ]\n\n", name);
+ fprintf (stderr, "%s [ -l ] [ -p listen_port ] [ -m email address]\n\n", name);
fprintf (stderr, "listen_port defaults to 10002\n");
return 1;
@@ -38,8 +38,10 @@ int main (int argc, char *argv[])
unsigned char buf[SIA_MAX_BLOCK_LENGTH];
unsigned ptr = 0;
+ const char *email = NULL;
- while ((opt = getopt (argc, argv, "p:l")) != -1) {
+
+ while ((opt = getopt (argc, argv, "p:lm:")) != -1) {
switch (opt) {
case 'p':
port = atoi (optarg);
@@ -49,6 +51,10 @@ int main (int argc, char *argv[])
log++;
break;
+ case 'm':
+ email = optarg;
+ break;
+
default: /* '?' */
return usage (argv[0]);
}
@@ -117,7 +123,7 @@ int main (int argc, char *argv[])
break;
default: /*Valid block */
- new_block (afd, &b, log);
+ new_block (afd, &b, log, email);
ptr = 0;
break;
}
diff --git a/rx.c b/rx.c
index 09d969f..2087f99 100644
--- a/rx.c
+++ b/rx.c
@@ -5,17 +5,34 @@
#include <syslog.h>
#include "sia.h"
+#include "email.h"
-static void msg (char *account, char *event, char *ascii, int log)
+static void msg (char *account, char *event, char *ascii, int log, const char *email)
{
+ char body[256];
+ char subject[256];
+
+
+ snprintf (body, sizeof (body) - 1, "%s %64s %s", account, event, ascii);
+ body[sizeof (body) - 1] = 0;
+
if (log)
- syslog (LOG_WARNING, "SIA: %s %64s %s", account, event, ascii);
+ syslog (LOG_WARNING, "SIA: %s", body);
+
+ printf ("%s\n", body);
+
+
+ if (!email) return;
+
+ if (strstr (body, "HEARTBT")) return;
+
+ snprintf (subject, sizeof (subject) - 1, "Galaxy SIA from %s", account);
- printf ("%s %64s %s\n", account, event, ascii);
+ send_email (email, subject, body);
}
-int new_block (int fd, SIA_Block *b, int log)
+int new_block (int fd, SIA_Block *b, int log, const char *email)
{
static int have_ascii_messages = 0; /*SIA level 3 doesn't have ascii, SIA level 4 does */
@@ -39,7 +56,7 @@ int new_block (int fd, SIA_Block *b, int log)
memcpy (event, b->data, len);
event[len] = 0;
- if (!have_ascii_messages) msg (account, event, "", log);
+ if (!have_ascii_messages) msg (account, event, "", log, email);
break;
@@ -48,7 +65,7 @@ int new_block (int fd, SIA_Block *b, int log)
memcpy (ascii, b->data, len);
ascii[len] = 0;
- msg (account, event, ascii, log);
+ msg (account, event, ascii, log, email);
break;
}
diff --git a/serial_rx.c b/serial_rx.c
index 8ef66ec..6165a01 100644
--- a/serial_rx.c
+++ b/serial_rx.c
@@ -9,14 +9,14 @@
#include "sia.h"
-extern int new_block (int fd, SIA_Block *b, int log);
+extern int new_block (int fd, SIA_Block *b, int log, const char *email);
//extern void periodic_task(void);
static int usage (const char *name)
{
fprintf (stderr, "Usage:\n");
- fprintf (stderr, "%s [ -l ] [ -b baud ] -p serial_device\n\n", name);
+ fprintf (stderr, "%s [ -l ] [ -b baud ] -p serial_device [ -m email address ]\n\n", name);
fprintf (stderr, "baud defaults to 9600\n");
return 1;
@@ -32,12 +32,13 @@ int main (int argc, char *argv[])
SIA_Block b;
ssize_t red;
int log = 0;
+ const char *email = NULL;
unsigned char buf[SIA_MAX_BLOCK_LENGTH];
unsigned ptr = 0;
- while ((opt = getopt (argc, argv, "p:b:l:")) != -1) {
+ while ((opt = getopt (argc, argv, "p:b:lm:")) != -1) {
switch (opt) {
case 'p':
port = optarg;
@@ -47,11 +48,14 @@ int main (int argc, char *argv[])
baud = atoi (optarg);
break;
+ case 'm':
+ email = optarg;
+ break;
+
case 'l':
log++;
break;
-
default: /* '?' */
return usage (argv[0]);
}
@@ -91,7 +95,7 @@ int main (int argc, char *argv[])
break;
default: /*Valid block */
- new_block (fd, &b, log);
+ new_block (fd, &b, log, email);
ptr = 0;
break;
}
diff --git a/test_email.c b/test_email.c
new file mode 100644
index 0000000..9478f26
--- /dev/null
+++ b/test_email.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "email.h"
+
+
+
+
+int main (int argc, char *argv[])
+{
+
+ if (argc != 4) {
+ fprintf (stderr, "Usage:\n");
+ fprintf (stderr, "\n");
+ fprintf (stderr, "test_email <to> <subject> <body>\n");
+ exit (1);
+ }
+
+
+ send_email (argv[1], argv[2], argv[3]);
+
+
+ return 0;
+}
+