summaryrefslogtreecommitdiff
path: root/blasms.c
diff options
context:
space:
mode:
Diffstat (limited to 'blasms.c')
-rw-r--r--blasms.c210
1 files changed, 210 insertions, 0 deletions
diff --git a/blasms.c b/blasms.c
new file mode 100644
index 0000000..da38fbe
--- /dev/null
+++ b/blasms.c
@@ -0,0 +1,210 @@
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ Copyright 2010 Luke Bratch <l_bratch@yahoo.co.uk>
+*/
+
+#include <stdio.h>
+#include <string.h>
+
+/* Remove trailing \n */
+void remtrailn(char* line) {
+ if (line[strlen(line) - 1] == '\n') {
+ line[strlen(line) - 1] = '\0';
+ }
+}
+
+/* Replace source[i] with str */
+void replacestr(char* source, int i, char* str) {
+ /* Temporary string for building */
+ char strtmp[1000];
+
+ strcpy(strtmp, source);
+ strxfrm(source, strtmp, i);
+ source[i] = '\0';
+ strcat(source, str);
+ strcat(source, strtmp + i + 2);
+}
+
+/* Set a sender's name as part of their number */
+void setname(char* telnum) {
+ FILE *fp;
+ char line[1024];
+ char *strchrp;
+ int offset = 0;
+
+ fp = fopen("phonebook.conf", "r");
+
+ if (fp == NULL) {
+ printf("Error opening smsparse.conf.\n");
+ return;
+ }
+
+ while (fgets(line, 1024, fp) != NULL) {
+ remtrailn(line);
+ if ((strchrp = strchr(line, ' ')) != NULL) {
+ offset = strchrp - line;
+ if ((strlen(telnum) == offset) && !strncmp(telnum, line, offset)) {
+ //printf("Name match: %s\n", line);
+ strcat(telnum, " (");
+ strcat(telnum, line + offset + 1);
+ strcat(telnum, ")");
+ }
+ }
+ }
+}
+
+int main(int argc, char *argv[]) {
+ /* Telephone number (may contain name)*/
+ char telnum[100];
+ /* SMS content */
+ char sms[500];
+ /* SMS command */
+ char smscommand[8];
+ /* Command to execute */
+ char systemcmd[1000];
+ /* Default command to execute */
+ char defaultcmd[1000];
+ /* Length of command */
+ int offset = 0;
+ /* Loop counter */
+ int i = 0;
+ /* Config file pointer */
+ FILE *fp;
+ /* Config file line */
+ char line[1024];
+ /* Config file command */
+ char configcmd[8];
+ /* strchr pointer */
+ char *strchrp;
+ /* Command match */
+ short int match = 0;
+
+ if (argc != 3 || !strcmp(argv[argc - 1], "--help")) {
+ fprintf(stderr, "Usage: %s sender-number send-date\n"
+ "\n"
+ "SMS content should be passed using stdin.\n"
+ "\n"
+ "smsparse.conf is the configuration file and should be in the format:\n"
+ " default SYSTEM COMMANDS\n"
+ " CMD1 SYSTEM COMMANDS\n"
+ " CMD2 SYSTEM COMMANDS\n"
+ " etc.\n"
+ "\n"
+ "Where SYSTEM COMMANDS can be any command to be executed. The line starting\n"
+ "with \'default\' must be present, and is the default command in the absense of a\n"
+ "recognised command. CMD1, CMD2, etc. are commands which can be matched at the\n"
+ "start of SMSes, in order to execute commands other than the default. Macros\n"
+ "are available, allowing for text replacement. The macros available are:\n"
+ " %%N - replaced with the sender-number, and attempts to set a sender name (see\n"
+ " below)\n"
+ " %%n - replaced with the sender-number\n"
+ " %%d - replaced with the send-date\n"
+ " %%s - replaced with the SMS content\n"
+ "\n"
+ "An example smsparse.conf might be:\n"
+ " default echo \\\"SMS from %%N at %%d: %%s\\\"\n"
+ " LS ls %%s\n"
+ "\n"
+ "Sender names are looked up in phonebook.conf, which should be in the format:\n"
+ " NUMBER1 NAME1\n"
+ " NUMBER2 NAME2\n"
+ " etc.\n"
+ "\n"
+ "An example phonebook.conf might be:\n"
+ " +447777123456 John\n"
+ " +447713987654 Bill\n",
+ argv[0]);
+ return 1;
+ }
+
+ strcpy(telnum, argv[1]);
+
+ printf("Phone number: %s\n", telnum);
+ printf("Date : %s\n", argv[2]);
+
+ fgets(sms, 500, stdin);
+
+ if (sms != NULL) {
+ remtrailn(sms);
+ printf("Contents : %s\n", sms);
+ }
+
+ fp = fopen("smsparse.conf", "r");
+
+ if (fp == NULL) {
+ printf("Error opening smsparse.conf.\n");
+ return 1;
+ }
+
+ while (fgets(line, 1024, fp) != NULL) {
+ remtrailn(line);
+ //printf("%s\n", line);
+ if ((strchrp = strchr(line, ' ')) != NULL) {
+ offset = strchrp - line;
+ strxfrm(configcmd, line, offset);
+ configcmd[offset] = '\0';
+ if (!strcmp(configcmd, "default")) {
+ strcpy(defaultcmd, line + offset + 1);
+ //printf("Default command: %s\n", defaultcmd);
+ continue;
+ }
+ strxfrm(smscommand, sms, offset);
+ smscommand[offset] = '\0';
+ //printf("SMS command: %s\n", smscommand);
+ if (!strcmp(smscommand, configcmd)) {
+ match = 1;
+ break;
+ }
+ }
+ }
+
+ if (match) {
+ //printf("Command match!\n");
+ strcpy(systemcmd, line + offset + 1);
+ } else {
+ //printf("Using default command.\n");
+ strcpy(systemcmd, defaultcmd);
+ }
+
+ //printf("Command: %s\n", systemcmd);
+
+ for (i = 0; i < strlen(systemcmd); i++) {
+ if (systemcmd[i] == '%') {
+ switch (systemcmd[i+1]) {
+ case 'N':
+ setname(telnum);
+ replacestr(systemcmd, i, telnum);
+ break;
+ case 'n':
+ replacestr(systemcmd, i, telnum);
+ break;
+ case 'd':
+ replacestr(systemcmd, i, argv[2]);
+ break;
+ case 's':
+ replacestr(systemcmd, i, sms);
+ break;
+ }
+ }
+ }
+
+ printf("Executing: %s\n", systemcmd);
+
+ system(systemcmd);
+
+ return 0;
+}