summaryrefslogtreecommitdiff
path: root/blasms.c
blob: c8127a08cd7275db9240cb7e2436c0a7b03217a7 (plain)
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
/*
    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 */
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];
    /* 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[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;
                case 'M':
                    strcpy(telnum, "pending/");
                    strcat(telnum, sms + offset + 1);

                    fp = fopen(telnum, "r");

                    if (fp == NULL) {
                        printf("Error opening message file.\n");
                        break;
                    }

                    if (fgets(line, 1024, fp) == NULL) {
                        printf("Error reading message file.\n");
                        break;
                    }

                    fclose(fp);

                    if ((strchrp = strchr(line + offset, ' ')) != NULL) {
                        remtrailn(line);
                        strncpy(telnum, line, strchrp - line);
                        telnum[strchrp - line] = '\0';
                        telnumlen = strlen(telnum);
                        setname(telnum);
                    } else {
                        printf("Error, malformed message file.\n");
                        break;
                    }

                    replacestr(systemcmd, i, 2, telnum);
                    replacestr(systemcmd, i + strlen(telnum), 0, ":");
                    replacestr(systemcmd, i + strlen(telnum) + 1, 0, line + telnumlen);

                    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;
}