changeset 226:0f96b5136541

sws-email2db program written
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 10 Mar 2021 20:34:03 +0000
parents 208ae1633f6c
children cac52723c02c
files .hgignore offline/Makefile offline/sws-email2db.c
diffstat 3 files changed, 122 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Wed Mar 10 19:39:33 2021 +0000
+++ b/.hgignore	Wed Mar 10 20:34:03 2021 +0000
@@ -6,6 +6,7 @@
 ^misc/fc-pcsc-list$
 
 ^offline/sim-iccid-mkfull$
+^offline/sws-email2db$
 
 ^simtool/fc-simtool$
 
--- a/offline/Makefile	Wed Mar 10 19:39:33 2021 +0000
+++ b/offline/Makefile	Wed Mar 10 20:34:03 2021 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	sim-iccid-mkfull
+PROGS=	sim-iccid-mkfull sws-email2db
 LIBS=	../libutil/libutil.a
 INSTBIN=/opt/freecalypso/bin
 
@@ -9,6 +9,9 @@
 sim-iccid-mkfull:	sim-iccid-mkfull.o ${LIBS}
 	${CC} ${CFLAGS} -o $@ $@.o ${LIBS}
 
+sws-email2db:	sws-email2db.c
+	${CC} ${CFLAGS} -o $@ $@.c
+
 install:
 	mkdir -p ${INSTBIN}
 	install -c ${PROGS} ${INSTBIN}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/offline/sws-email2db.c	Wed Mar 10 20:34:03 2021 +0000
@@ -0,0 +1,117 @@
+/*
+ * This program reads extracts from Sysmocom webshop emails containing
+ * card provisioning data and converts these bits into our sws-card-db
+ * format.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+#define	MAX_FIELDS	32
+#define	MAX_FIELD_KW	7
+
+char *infname;
+FILE *inf;
+char linebuf[128];
+int lineno;
+
+char field_names[MAX_FIELDS][MAX_FIELD_KW+1];
+unsigned nfields;
+
+get_input_line()
+{
+	char *cp;
+
+	if (!fgets(linebuf, sizeof linebuf, inf))
+		return(0);
+	lineno++;
+	cp = index(linebuf, '\n');
+	if (!cp) {
+		fprintf(stderr, "%s line %d: too long or missing newline\n",
+			infname, lineno);
+		exit(1);
+	}
+	*cp = '\0';
+	return(1);
+}
+
+read_field_names()
+{
+	for (;;) {
+		if (!get_input_line()) {
+			fprintf(stderr, "error: %s EOFs in field name list\n",
+				infname);
+			exit(1);
+		}
+		if (!linebuf[0])
+			break;
+		if (nfields >= MAX_FIELDS) {
+			fprintf(stderr, "%s line %d: too many fields\n",
+				infname, lineno);
+			exit(1);
+		}
+		if (strlen(linebuf) > MAX_FIELD_KW) {
+			fprintf(stderr, "%s line %d: field name is too long\n",
+				infname, lineno);
+			exit(1);
+		}
+		strcpy(field_names[nfields], linebuf);
+		nfields++;
+	}
+	if (!nfields) {
+		fprintf(stderr, "error: %s header defines 0 fields\n", infname);
+		exit(1);
+	}
+}
+
+process_one_card()
+{
+	unsigned nf;
+	int rc;
+
+	for (nf = 0; nf < nfields; nf++) {
+		for (;;) {
+			rc = get_input_line();
+			if (!rc) {
+				if (!nf)
+					exit(0);
+				fprintf(stderr,
+			"error: %s EOFs in the middle of a card data block\n",
+					infname);
+				exit(1);
+			}
+			if (linebuf[0])
+				break;
+			if (nf) {
+				fprintf(stderr,
+		"%s line %d: empty line in the middle of a card data block\n",
+					infname, lineno);
+				exit(1);
+			}
+		}
+		if (nf)
+			putchar(' ');
+		printf("%s=%s", field_names[nf], linebuf);
+	}
+	putchar('\n');
+}
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s email-extract-file\n", argv[0]);
+		exit(1);
+	}
+	infname = argv[1];
+	inf = fopen(infname, "r");
+	if (!inf) {
+		perror(infname);
+		exit(1);
+	}
+	read_field_names();
+	for (;;)
+		process_one_card();
+}