1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
/*
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 <stdlib.h>
#include <string.h>
/* Remove trailing \n from a string */
void remtrailn(char* line) {
/* If the last character is a \n, change it into the null
character (i.e. end of string). */
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];
/* Copy source string into temporary string */
strcpy(strtmp, source);
/* Copy the first i characters back into the source string */
strxfrm(source, strtmp, i);
source[i] = '\0';
/* Copy the string to be inserted into the source string */
strcat(source, str);
/* Copy the remainder of the temporary string into the source,
string, skipping over n characters. */
strcat(source, strtmp + i + n);
}
/* Converts a string to lowercase */
void strtolower(char* destination, char* source) {
int i;
/* Loop through each character, converting it into
lowercase. */
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 pointer */
FILE *fp;
/* String to read each line of file into */
char line[1024];
/* Pointer to store character location searches in */
char *strchrp;
/* For keeping track of where we are */
int offset = 0;
/* Attempt to open the file */
fp = fopen("phonebook.conf", "r");
if (fp == NULL) {
printf("Error opening blasms.conf.\n");
return;
}
/* Loop through each line of the file */
while (fgets(line, 1024, fp) != NULL) {
/* Remove trailing newline */
remtrailn(line);
/* Attempt to find the end of the first word */
if ((strchrp = strchr(line + offset, ' ')) != NULL) {
/* Convert position of end of first word into an int */
offset = strchrp - line;
/* Check if the first word is the same as the given telnum */
if ((strlen(telnum) == offset) && !strncmp(telnum, line, offset)) {
/* Find the second word (i.e. the name) */
if ((strchrp = strchr(line + offset + 1, ' ')) != NULL) {
line[strchrp - line] = '\0';
}
/* Add " (" to the given telnum */
strcat(telnum, " (");
/* Add the found name to the given telnum */
strcat(telnum, line + offset + 1);
/* Add ")" to the given telnum */
strcat(telnum, ")");
/* Leave this procedure */
return;
}
}
}
}
/* Set a sender's number as part of their name */
void settelnum(char* telnum, char* sms, int *offsetptr) {
/* File pointer */
FILE *fp;
/* String to read each line of file into */
char line[1024];
/* Name to lookup */
char name[1000];
/* Current name in phonebook.conf */
char curname[1000];
/* Pointer to store character location searches in */
char *strchrp;
/* Keep track of where we are, starting from the
current point in the calling function. */
int offset = *offsetptr;
/* Some loop iteration variables */
int i, j;
/* Attempt to open the phonebook */
fp = fopen("phonebook.conf", "r");
if (fp == NULL) {
printf("Error opening blasms.conf.\n");
return;
}
/* Set entire given SMS as the target name for now */
strcpy(name, sms + offset + 1);
/* Trim the target name to just the first word of the SMS */
if ((strchrp = strchr(name, ' ')) != NULL) {
offset = strchrp - name;
name[offset] = '\0';
}
/* If the name is a wildcard, set it as the telnum
for the calling function and leave this function. */
if (!strcmp(name, "*")) {
strcpy(telnum, name);
/* Advance the offset to go past this wildcard,
for the calling function. */
*offsetptr = *offsetptr + 2;
return;
}
/* Convert the name to lowercase for easier comparison */
strtolower(name, name);
/* Loop through each line of the file to find a matching name */
while (fgets(line, 1024, fp) != NULL) {
/* Remove trailing newline */
remtrailn(line);
/* Convert the line to lowercase for easier comparison */
strtolower(line, line);
/* Find the end of the first word (i.e. the number which
we don't want for now. */
for (i = 0; i < strlen(line); i++) {
if (line[i] == ' ') {
break;
}
}
/* Move past the first space */
offset = i + 1;
/* Loop through each character in the line, skipping the first
word (the number). */
for (i = 0, j = 0; i < strlen(line + offset - 1); i++, j++) {
/* If this isn't the end of the word or line, build a name
for comparison. */
if (line[i + offset] != ' ' && line[i + offset] != '\0') {
curname[j] = line[i + offset];
/* Otherwise, the word or line ended, so check for a match. */
} else {
/* Terminate the current name string */
curname[j] = '\0';
/* Check to see if the names are a match... */
if (!strcmp(name, curname)) {
/* If they match, copy the number from the
line into telnum */
strncpy(telnum, line, offset - 1);
/* Terminate the string */
telnum[offset - 1] = '\0';
/* Advance the offset paste the name for the
calling function */
*offsetptr = *offsetptr + strlen(name) + 1;
return;
/* ...if they aren't a match, start building a new
name from this line (as it might have multiple
names for the current number). */
} else {
j = -1;
}
}
}
}
/* If the name wasn't found, then we don't want to send an SMS,
so exit the program. */
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];
/* Unmodified telephone number length */
int telnumlen;
/* 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[11];
/* strchr pointer */
char *strchrp;
/* Command match / telnum set */
short int match = 0;
/* Stores the positions in systemcmd of wildcards,
for if multiple phonebook looks are to be done. */
struct {
/* Each individual position */
int pos;
} wildcard[1000];
/* Number of wildcards (i.e. number of wildcard structs used) */
int wildcards = 0;
/* Print some usage information if --help was used, or if exactly
two arguments weren't given. */
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;
}
/* Copy first argument into telnum, so it can be manipulated */
strcpy(telnum, argv[1]);
/* Print the arguments (usually number/date) */
printf("Phone number: %s\n", telnum);
printf("Date : %s\n", argv[2]);
/* Get the actual SMS content from standard input */
fgets(sms, 500, stdin);
/* Make sure we got something */
if (sms != NULL) {
/* Remove trailing newline */
remtrailn(sms);
printf("Contents : %s\n", sms);
}
/* Open configuration file */
fp = fopen("blasms.conf", "r");
/* Make sure file opened OK */
if (fp == NULL) {
printf("Error opening blasms.conf.\n");
return 1;
}
/* Read each line from the file */
while (fgets(line, 1024, fp) != NULL) {
/* Remove traliing newline */
remtrailn(line);
/* Try to find the end of the first word (i.e. the first space) */
if ((strchrp = strchr(line, ' ')) != NULL) {
/* Position = address of space - address of start of line */
offset = strchrp - line;
if (offset > 10) {
printf("Error, command longer than 10 characters in blasms.conf.\n");
return 1;
}
/* Copy that first word into configcmd */
strxfrm(configcmd, line, offset);
configcmd[offset] = '\0';
/* If the command was "default", set it as the default
command and move to next line in file. */
if (!strcmp(configcmd, "default")) {
strcpy(defaultcmd, line + offset + 1);
continue;
}
/* If the SMS command doesn't end here, continue. (i.e. it
definitely doesn't match the config file command. */
if (sms[offset] != ' ') {
continue;
}
/* If it does end here, copy it to smscommand */
strxfrm(smscommand, sms, offset);
smscommand[offset] = '\0';
/* If the commands are the same, stop reading the file */
if (!strcmp(smscommand, configcmd)) {
match = 1;
break;
}
}
}
/* match was set if a command was found, so set the systemcmd
to be the remainder of the current config file line */
if (match) {
strcpy(systemcmd, line + offset + 1);
/* If not, use the defaultcmd as set above. */
} else {
strcpy(systemcmd, defaultcmd);
offset = -1;
}
/* Reset match to 0 as we use it again */
match = 0;
/* Go through each character of systemcmd, looking for
recognised macros */
for (i = 0; i < strlen(systemcmd); i++) {
/* If a % is found, we might be about to find a macro. */
if (systemcmd[i] == '%') {
/* Check the next character for recognised macros */
switch (systemcmd[i+1]) {
/* Sender number with name lookup */
case 'N':
/* Attempt to look up number to find a name */
setname(telnum);
/* Insert number (or number + name) into systemcmd */
replacestr(systemcmd, i, 2, telnum);
break;
/* Sender number only */
case 'n':
/* Insert number into systemcmd */
replacestr(systemcmd, i, 2, telnum);
break;
/* Date */
case 'd':
/* Insert date into systemcmd */
replacestr(systemcmd, i, 2, argv[2]);
break;
/* SMS content, minus the initial command (if any) */
case 's':
/* Insert SMS content into systemcmd */
replacestr(systemcmd, i, 2, sms + offset + 1);
break;
/* Interpret second word of SMS as a name to be looked up */
case 'P':
/* If name hasn't already been found, try to find it */
if (!match) {
settelnum(telnum, sms, &offset);
/* If found, no need to try again. */
match = 1;
}
/* If telnum was a wildcard, keep track of its position. */
if (!strcmp(telnum, "*")) {
wildcard[wildcards].pos = i;
wildcards++;
}
/* Insert telnum into systemcmd */
replacestr(systemcmd, i, 2, telnum);
break;
/* Intepret the second word (i.e. after the command) of the SMS
as a filename, attempt to open the file pending/filename, treating
the first word of it as a number, attempting to look up its name,
and insert a colon between it and the rest of the file. */
case 'M':
/* Build the path */
strcpy(telnum, "pending/");
strcat(telnum, sms + offset + 1);
/* Open the file */
fp = fopen(telnum, "r");
/* If the file didn't open, abort this macro. */
if (fp == NULL) {
printf("Error opening message file.\n");
break;
}
/* If the first line can't be read, abort this macro. */
if (fgets(line, 1024, fp) == NULL) {
printf("Error reading message file.\n");
break;
}
/* Close the file after reading the first line */
fclose(fp);
/* Attempt to find the end of the first word */
if ((strchrp = strchr(line + offset, ' ')) != NULL) {
/* Remove trailing newline */
remtrailn(line);
/* Copy the first word into telnum */
strncpy(telnum, line, strchrp - line);
telnum[strchrp - line] = '\0';
/* Set the length of the unmodified number */
telnumlen = strlen(telnum);
/* Attempt to look up the number */
setname(telnum);
/* If a first word couldn't be determined, abort this macro. */
} else {
printf("Error, malformed message file.\n");
break;
}
/* Insert the number (or number + name) into systemcmd */
replacestr(systemcmd, i, 2, telnum);
/* Insert a colon */
replacestr(systemcmd, i + strlen(telnum), 0, ":");
/* Insert the remainder of the file */
replacestr(systemcmd, i + strlen(telnum) + 1, 0, line + telnumlen);
break;
}
}
}
/* Execute a single command, or multiple command, depending on
whether the systemcmd contained wildcards. */
if (wildcards) {
/* Try to open the phonebook to look up each number to send to */
fp = fopen("phonebook.conf", "r");
/* Exit if the file couldn't be opened */
if (fp == NULL) {
printf("Error opening phonebook.conf.\n");
return 1;
}
/* Read each line in the file */
while (fgets(line, 1024, fp) != NULL) {
/* Attempt to find the first word (hopefully a number) */
if ((strchrp = strchr(line, ' ')) != NULL) {
/* Set initial offset in systemcmd (1, as in a single *) */
offset = 1;
/* Copy systemcmd into wildcardcmd for manipulation */
strcpy(wildcardcmd, systemcmd);
line[strchrp - line] = '\0';
/* Loop through each wildcard that we tracked earlier */
for (i = 0; i < wildcards; i++) {
/* Insert the first word of the phonebook (hopefully a number) into
wildcardcmd at each wildcard position. Adjust the wildcard position
each time to take into account the length of the previous inserted
number. */
replacestr(wildcardcmd, wildcard[i].pos + (int)strlen(line) * i - i, 1, line);
}
printf("Executing: %s\n", wildcardcmd);
/* Execute the command */
system(wildcardcmd);
}
}
} else {
printf("Executing: %s\n", systemcmd);
/* Execute command */
system(systemcmd);
}
return 0;
}
|