/*
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 .
*/
/*
Copyright 2010 Luke Bratch
*/
#include
#include
#include
/* Remove trailing \n */
void remtrailn(char* line) {
if (line[strlen(line) - 1] == '\n') {
line[strlen(line) - 1] = '\0';
}
}
/* Replace n characters in source with str at i */
void replacestr(char* source, int i, int n, 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 + n);
}
/* Converts a string to lowercase */
void strtolower(char* destination, char* source) {
int i;
for (i = 0; i < strlen(source) + 1; i++) {
destination[i] = tolower(source[i]);
}
}
/* 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 blasms.conf.\n");
return;
}
while (fgets(line, 1024, fp) != NULL) {
remtrailn(line);
if ((strchrp = strchr(line + offset, ' ')) != NULL) {
offset = strchrp - line;
if ((strlen(telnum) == offset) && !strncmp(telnum, line, offset)) {
if ((strchrp = strchr(line + offset + 1, ' ')) != NULL) {
line[strchrp - line] = '\0';
}
strcat(telnum, " (");
strcat(telnum, line + offset + 1);
strcat(telnum, ")");
return;
}
}
}
}
/* Set a sender's number as part of their name */
void settelnum(char* telnum, char* sms, int *offsetptr) {
FILE *fp;
char line[1024];
char name[1000];
char curname[1000];
char *strchrp;
int offset = *offsetptr;
int i, j;
fp = fopen("phonebook.conf", "r");
if (fp == NULL) {
printf("Error opening blasms.conf.\n");
return;
}
strcpy(name, sms + offset + 1);
if ((strchrp = strchr(name, ' ')) != NULL) {
offset = strchrp - name;
name[offset] = '\0';
}
if (!strcmp(name, "*")) {
strcpy(telnum, name);
*offsetptr = *offsetptr + 2;
return;
}
strtolower(name, name);
while (fgets(line, 1024, fp) != NULL) {
remtrailn(line);
strtolower(line, line);
for (i = 0; i < strlen(line); i++) {
if (line[i] == ' ') {
break;
}
}
offset = i + 1;
for (i = 0, j = 0; i < strlen(line + offset - 1); i++, j++) {
if (line[i + offset] != ' ' && line[i + offset] != '\0') {
curname[j] = line[i + offset];
} else {
curname[j] = '\0';
if (!strcmp(name, curname)) {
strncpy(telnum, line, offset - 1);
telnum[offset - 1] = '\0';
*offsetptr = *offsetptr + strlen(name) + 1;
return;
} else {
j = -1;
}
}
}
}
printf("Error: %s not found in phonebook.conf.\n", name);
exit(1);
}
int main(int argc, char *argv[]) {
/* Telephone number (may contain name)*/
char telnum[100];
/* SMS content */
char sms[500];
/* SMS command */
char smscommand[11];
/* Command to execute */
char systemcmd[1000];
/* Dynamic systemcmd for wildcard matches */
char wildcardcmd[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 / telnum set */
short int match = 0;
struct {
int pos;
} wildcard[1000];
int wildcards = 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"
"blasms.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"
" %%P - attempts to replace the second word in the SMS with a destination number\n"
" from phonebook.conf - SMS not processed if entry not found\n"
"\n"
"An example blasms.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("blasms.conf", "r");
if (fp == NULL) {
printf("Error opening blasms.conf.\n");
return 1;
}
while (fgets(line, 1024, fp) != NULL) {
remtrailn(line);
if ((strchrp = strchr(line, ' ')) != NULL) {
offset = strchrp - line;
if (offset > 10) {
printf("Error, command longer than 10 characters in blasms.conf.\n");
return 1;
}
strxfrm(configcmd, line, offset);
configcmd[offset] = '\0';
if (!strcmp(configcmd, "default")) {
strcpy(defaultcmd, line + offset + 1);
continue;
}
/* If the SMS command doesn't end here, continue */
if (sms[offset] != ' ') {
continue;
}
strxfrm(smscommand, sms, offset);
smscommand[offset] = '\0';
if (!strcmp(smscommand, configcmd)) {
match = 1;
break;
}
}
}
if (match) {
strcpy(systemcmd, line + offset + 1);
} else {
strcpy(systemcmd, defaultcmd);
offset = -1;
}
match = 0;
for (i = 0; i < strlen(systemcmd); i++) {
if (systemcmd[i] == '%') {
switch (systemcmd[i+1]) {
case 'N':
setname(telnum);
replacestr(systemcmd, i, 2, telnum);
break;
case 'n':
replacestr(systemcmd, i, 2, telnum);
break;
case 'd':
replacestr(systemcmd, i, 2, argv[2]);
break;
case 's':
replacestr(systemcmd, i, 2, sms + offset + 1);
break;
case 'P':
if (!match) {
settelnum(telnum, sms, &offset);
match = 1;
}
if (!strcmp(telnum, "*")) {
wildcard[wildcards].pos = i;
wildcards++;
}
replacestr(systemcmd, i, 2, telnum);
break;
}
}
}
if (wildcards) {
fp = fopen("phonebook.conf", "r");
if (fp == NULL) {
printf("Error opening phonebook.conf.\n");
return 1;
}
while (fgets(line, 1024, fp) != NULL) {
if ((strchrp = strchr(line, ' ')) != NULL) {
offset = 1;
strcpy(wildcardcmd, systemcmd);
line[strchrp - line] = '\0';
for (i = 0; i < wildcards; i++) {
replacestr(wildcardcmd, wildcard[i].pos + (int)strlen(line) * i - i, 1, line);
}
printf("Executing: %s\n", wildcardcmd);
system(wildcardcmd);
}
}
} else {
printf("Executing: %s\n", systemcmd);
system(systemcmd);
}
return 0;
}