-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjcblock.c
1182 lines (1023 loc) · 33.7 KB
/
jcblock.c
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Program name: jcblock
*
* File name: jcblock.c
*
* Copyright: Copyright 2008 Walter S. Heath
*
* Copy permission:
* 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 may view a copy of the GNU General Public License at:
* <http://www.gnu.org/licenses/>.
*
* Description:
* A program to block telemarketing (junk) calls.
* This program connects to a serial port modem and listens for
* the caller ID string that is sent between the first and second
* rings. It records the string in file callerID.dat. It then
* reads strings from file whitelist.dat and scans them against
* the caller ID string for a match. If it finds a match it accepts
* the call. If a match is not found, it reads strings from file
* blacklist.dat and scans them against the caller ID string for a
* match. If it finds a match to a string in the blacklist, it sends
* an off-hook command (ATH1) to the modem, followed by an on-hook
* command (ATH0). This terminates the junk call.
*
* For more details, see the README and UPDATES files.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <time.h>
#include <signal.h>
#include "common.h"
#define DEBUG
// Comment out the following define if you don't have ALSA audio
// support. Then compile with:
// gcc -o jcblock jcblock.c truncate.c -lm
// The program will then have all capabilities except the star (*) key
// feature.
#define DO_TONES
// Comment out the following define if you don't have an answering
// machine attached to the same telephone line.
#define ANS_MACHINE
// Comment out the following define if you don't want truncation of
// records older than nine months from files blacklist.dat and
// callerID.dat. Then remove truncate.c from the gcc compile command.
#define DO_TRUNCATE
#define CALLERID_YES 1
#define CALLERID_NO 0
#define OPEN_PORT_BLOCKED 1
#define OPEN_PORT_POLLED 0
// Default serial port specifier.
char *serialPort = "/dev/ttyS0";
int fd; // the serial port
FILE *fpWh; // whitelist.dat file
static struct termios options;
static time_t pollTime, pollStartTime;
static bool modemInitialized = FALSE;
static bool inBlockedReadCall = FALSE;
static int numRings;
static void cleanup( int signo );
int send_modem_command(int fd, char *command );
static bool check_blacklist( char *callstr );
static bool write_blacklist( char *callstr );
static bool check_whitelist( char * callstr );
static void open_port( int mode );
static void close_open_port( int doCallerID );
int init_modem(int fd, int doCallerID );
static char *copyright = "\n"
"jcblock Copyright (C) 2008 Walter S. Heath\n"
"This program comes with absolutely no warranty.\n"
"This is free software, distributed under terms\n"
"of the GNU Public License described at:\n"
"<http://www.gnu.org/licenses/>.\n\n";
// Main function
int main(int argc, char **argv)
{
int optChar;
// Set Ctrl-C and kill terminator signal catchers
signal( SIGINT, cleanup );
signal( SIGKILL, cleanup );
// See if a serial port argument was specified
if( argc > 1 )
{
while( ( optChar = getopt( argc, argv, "p:h" ) ) != EOF )
{
switch( optChar )
{
case 'p':
serialPort = optarg;
break;
case 'h':
default:
fprintf( stderr, "Usage: jcblock [-p /dev/<portID>]\n" );
fprintf( stderr, "Default serial port is: /dev/ttyS0.\n" );
fprintf( stderr, "For another port, use the -p option.\n" );
_exit(-1);
}
}
}
// Display copyright notice
printf( "%s", copyright );
#ifdef DO_TONES
// Initialize the the star (*) key tones operation
tonesInit();
#endif
// Open or create a file to append caller ID strings to
if( (fpCa = fopen( "./callerID.dat", "a+" ) ) == NULL )
{
printf("fopen() of callerID.dat failed\n");
return;
}
// Open the whitelist file (for reading & writing)
if( (fpWh = fopen( "./whitelist.dat", "r+" ) ) == NULL )
{
printf("fopen() of whitelist.dat failed. A whitelist is not required.\n" );
}
// Open the blacklist file (for reading & writing)
if( (fpBl = fopen( "./blacklist.dat", "r+" ) ) == NULL )
{
printf("fopen() of blacklist.dat failed. A blacklist must exist.\n" );
return;
}
// Open the serial port
open_port( OPEN_PORT_BLOCKED );
// Initialize the modem
if( init_modem(fd, CALLERID_YES ) != 0 )
{
printf("init_modem() failed\n");
close(fd);
fclose(fpCa);
fclose(fpBl);
fclose(fpWh);
#ifdef DO_TONES
tonesClose();
#endif
fflush(stdout);
sync();
return;
}
modemInitialized = TRUE;
printf("Waiting for a call...\n");
// Wait for calls to come in...
wait_for_response(fd);
close( fd );
fclose(fpCa);
fclose(fpBl);
fclose(fpWh);
#ifdef DO_TONES
tonesClose();
#endif
fflush(stdout);
sync();
}
//
// Initialize the modem.
// The 'doCallerID' argument allows initialization with
// or without sending the caller ID command (AT+VCID=1).
//
int init_modem(int fd, int doCallerID )
{
// Reset the modem
#ifdef DEBUG
printf("sending ATZ command...\n");
#endif
if( send_modem_command(fd, "ATZ\r") != 0 )
{
return(-1);
}
sleep(1); // needed
// If operating in a non-US telephone system region,
// insert an appropriate "AT+GCI=XX\r" modem command here.
// See the README file for details.
if( doCallerID )
{
// Tell modem to return caller ID
#ifdef DEBUG
printf("sending AT+VCID=1 command...\n");
#endif
if( send_modem_command(fd, "AT+VCID=1\r") != 0 )
{
return(-1);
}
}
return(0);
}
//
// Send command string to the modem
//
int send_modem_command(int fd, char *command )
{
char buffer[255]; // Input buffer
char *bufptr; // Current char in buffer
int nbytes; // Number of bytes read
int tries; // Number of tries so far
int i;
// Send an AT command followed by a CR
if( write(fd, command, strlen(command) ) != strlen(command) )
{
printf("send_modem_command: write() failed\n" );
}
for( tries = 0; tries < 20; tries++ )
{
// Read characters into our string buffer until we get a CR or NL
bufptr = buffer;
inBlockedReadCall = TRUE;
while( (nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0 )
{
bufptr += nbytes;
if( bufptr[-1] == '\n' || bufptr[-1] == '\r' )
break;
}
inBlockedReadCall = FALSE;
// Null terminate the string and see if we got an OK response
*bufptr = '\0';
// Scan for string "OK"
if( strstr( buffer, "OK" ) != NULL )
{
#ifdef DEBUG
printf("got command OK\n");
#endif
return( 0 );
}
}
#ifdef DEBUG
printf("did not get command OK\n");
#endif
return( -1 );
}
//
// Wait (forever!) for calls...
//
int wait_for_response(fd)
{
char buffer[255]; // Input buffers
char buffer2[255];
int nbytes2; // bytes in buffer2
char buffer3[255];
char bufRing[10]; // RING input buffer
int nbytes; // Number of bytes read
int i, j;
struct tm *tmPtr;
time_t currentTime;
int currentYear;
char curYear[4];
// Get a string of characters from the modem
while(1)
{
#ifdef DEBUG
// Flush anything in stdout (needed if stdout is redirected to
// a disk file).
fflush(stdout); // flush C library buffers to kernel buffers
sync(); // flush kernel buffers to disk
#endif
// Block until at least one character is available.
// After first character is received, continue reading
// characters until inter-character timeout (VTIME)
// occurs (or VMIN characters are received, which
// shouldn't happen, since VMIN is set larger than
// the longest string expected).
inBlockedReadCall = TRUE;
nbytes = read( fd, buffer, 250 );
inBlockedReadCall = FALSE;
// Replace '\n' and '\r' characters with '-' characters
for( i = 0; i < nbytes; i++ )
{
if( ( buffer[i] == '\n' ) || ( buffer[i] == '\r' ) )
{
buffer[i] = '-';
}
}
// Put a '\n' at its end and null-terminate it
buffer[nbytes] = '\n';
buffer[nbytes + 1] = 0;
#ifdef DEBUG
printf("nbytes: %d, str: %s", nbytes, buffer );
#endif
// A string was received. If its a 'RING' string, just ignore it.
if( strstr( buffer, "RING" ) != NULL )
{
continue;
}
// Ignore a string "AT+VCID=1" returned from the modem.
if( strncmp( buffer, "AT+VCID=1", 9 ) == 0 )
{
continue;
}
// Caller ID data was received after the first ring.
numRings = 1;
// A caller ID string was constructed.
// If space(' ') characters are not present before and after all
// equal('=') characters, insert them (some modems don't insert
// them!).
for( i = 0, j = 0; i < nbytes + 1; i++ )
{
if( buffer[i] == '=' )
{
if( buffer[i - 1] != ' ' ) // If space before is missing...
{
buffer2[j++] = ' ';
buffer2[j++] = buffer[i];
if( buffer[i + 1] != ' ' ) // If space after is missing...
{
buffer2[j++] = ' ';
}
}
else // If space before is there...
{
buffer2[j++] = buffer[i];
}
}
else // If this char is not a '='...
{
buffer2[j++] = buffer[i];
}
}
nbytes2 = j; // number of bytes in buffer2
//
// The DATE field does not contain the year. Compute the year
// and insert it.
if( time( ¤tTime ) == -1 )
{
printf("time() failed\n" );
return -1;
}
tmPtr = localtime( ¤tTime );
currentYear = tmPtr->tm_year -100; // years since 2000
if( sprintf( curYear, "%02d", currentYear ) != 2 )
{
printf( "sprintf() failed\n" );
return -1;
}
// Zero a new buffer with room for the year.
for( i = 0; i < 100; i++ )
{
buffer3[i] = 0;
}
// Fill it but leave room for the year
for( i = 0; i < 13; i++ )
{
buffer3[i] = buffer2[i];
}
for( i = 13; i < nbytes2; i++ )
{
buffer3[i + 2] = buffer2[i];
}
// Insert the year characters.
buffer3[13] = curYear[0];
buffer3[14] = curYear[1];
// Close and re-open file 'callerID.dat' (in case it was
// edited while the program was running!).
fclose(fpCa);
if( (fpCa = fopen( "./callerID.dat", "a+" ) ) == NULL )
{
printf("re-fopen() of callerID.dat failed\n");
return(-1);
}
// Write the record to the file
if( fputs( (const char *)buffer3, fpCa ) == EOF )
{
printf("fputs( (const char *)buffer3, fpCa ) failed\n");
return(-1);
}
// Flush the record to the file
if( fflush(fpCa) == EOF )
{
printf("fflush(fpCa) failed\n");
return(-1);
}
// If a whitelist.dat file was present, compare the
// caller ID string to entries in the whitelist. If a match
// is found, accept the call and bypass the blacklist check.
if( fpWh != NULL )
{
if( check_whitelist( buffer3 ) == TRUE )
{
// Caller ID match was found (or an error occurred),
// so accept the call
continue;
}
}
// Compare the caller ID string to entries in the blacklist. If
// a match is found, answer (i.e., terminate) the call.
if( check_blacklist( buffer3 ) == TRUE )
{
// Blacklist entry was found.
//
#ifdef DO_TRUNCATE
// The following function truncates (removes old) entries
// in data files -- if thirty days have elapsed since the
// last time it truncated. Entries in callerID.dat are removed
// if they are older than nine months. Entries in blacklist.dat
// are removed if they have not been used to terminate a call
// within the last nine months.
// Note: it is not necessary for this function to run for the
// main program to operate normally. You may remove it if you
// don't want automatic file truncation. All of its code is in
// truncate.c.
truncate_records();
#endif // end DO_TRUNCATE
continue;
}
#ifdef DO_TONES
// At this point the phone will ring until the call has been
// answered or the caller hangs up (RING strings stop arriving).
// Listen for a star (*) key press by polling the microphone. If
// a press is detected (within the timed window), build and add
// an entry to the blacklist for this call.
else
{
// Get current time (seconds since Unix Epoch)
if( (pollStartTime = time( NULL ) ) == -1 )
{
printf("time() failed(1)\n");
continue;
}
// Reinitialize the serial port for polling
close(fd);
open_port( OPEN_PORT_POLLED );
// Now poll until 'RING' strings stop arriving.
// Note: seven seconds is just longer than the
// inter-ring time (six seconds).
while( (pollTime = time( NULL )) < pollStartTime + 7 )
{
if( ( nbytes = read( fd, bufRing, 1 ) ) > 0 )
{
if(bufRing[0] == 'R')
{
pollStartTime = time( NULL );
numRings++; // count the ring
}
}
usleep( 100000 ); // 100 msec
}
// Reinitialize the serial port for blocked operation
close(fd);
open_port( OPEN_PORT_BLOCKED );
#ifdef ANS_MACHINE
// If the call is answered after three rings, poll for a
// touchtone star (*) key press. Note that if an answering
// machine is connected to the line, the star feature is only
// available if the call is answered after the third ring.
// This is necessary to avoid conflict with answering machines.
// The answering machine *must be* set to answer on the fourth
// or later ring. See the README and UPDATES files for further
// details.
if( numRings == 3 )
{
#else
// If no answering machine is connected to the same telephone
// line, the star key feature is available for calls answered after
// three or more rings.
if( TRUE )
{
#endif
//
// Send an off-hook modem command so the mic can pick up the tones
// generated by the star (*) key press. When the command is sent
// the listener hears a "click". That indicates the start of the
// timed window when a star (*) key press will be accepted.
send_modem_command(fd, "ATH1\r"); // off hook
// Send on-hook and off-hook commands to produce two more clicks
// to aid the listener in detecting the start of the window.
// Note that, due to the hardware, the third click is delayed.
// If you like, you can omit these two commands and just sound
// one click to signal the start of the detection window.
send_modem_command(fd, "ATH0\r"); // on hook
send_modem_command(fd, "ATH1\r"); // off hook
// Get current time (seconds since Unix Epoch)
if( (pollStartTime = time( NULL ) ) == -1 )
{
printf("time() failed(2)\n");
continue;
}
// Poll for star (*) key press within the timeout window
// (ten seconds)
while( (pollTime = time( NULL )) < pollStartTime + 10 )
{
if( tonesPoll() == TRUE )
{
// Write a caller ID entry to blacklist.dat.
write_blacklist( buffer3 );
break;
}
}
// Re-initialize the modem to return caller ID.
// This also produces two clicks to signal the
// end of the tone detection window.
send_modem_command(fd, "ATZ\r");
send_modem_command(fd, "AT+VCID=1\r");
continue;
}
}
#endif // end DO_TONES
} // end of while()
}
//
// Compare strings in the 'whitelist.dat' file to fields in the
// received caller ID string. If a whitelist string is present
// (or an error occurred), return TRUE; otherwise return FALSE.
//
static bool check_whitelist( char *callstr )
{
char whitebuf[100];
char whitebufsave[100];
char *whitebufptr;
char call_date[10];
char *dateptr;
char *strptr;
int i;
long file_pos_last, file_pos_next;
// Close and re-open the whitelist.dat file. Note: this
// seems to be necessary to be able to write records
// back into the file. The write works the first time
// after the file is opened but not subsequently! :-(
// This also allows whitelist changes made while the
// program is running to be recognized.
//
fclose( fpWh );
// Re-open for reading and writing
if( (fpWh = fopen( "./whitelist.dat", "r+" ) ) == NULL )
{
printf("Re-open of whitelist.dat file failed\n" );
return(TRUE); // accept the call
}
// Disable buffering for whitelist.dat writes
setbuf( fpWh, NULL );
// Seek to beginning of list
fseek( fpWh, 0, SEEK_SET );
// Save the file's current access location
if( file_pos_next = ftell( fpWh ) == -1L )
{
printf("ftell(fpWh) failed\n");
return(TRUE); // accept the call
}
// Read and process records from the file
while( fgets( whitebuf, sizeof( whitebuf ), fpWh ) != NULL )
{
// Save the start location of the string just read and get
// the location of the start of the next string in the file.
file_pos_last = file_pos_next;
file_pos_next = ftell( fpWh );
// Ignore lines that start with a '#' character (comment lines)
if( whitebuf[0] == '#' )
continue;
// Ignore lines containing just a '\n'
if( whitebuf[0] == '\n' )
{
continue;
}
// Ignore records that are too short (don't have room for the date)
if( strlen( whitebuf ) < 26 )
{
printf("ERROR: whitelist.dat record is too short to hold date field.\n");
printf(" record: %s", whitebuf);
printf(" record is ignored (edit file and fix it).\n");
continue;
}
// Save the string (for writing back to the file later)
strcpy( whitebufsave, whitebuf );
// Make sure a '?' char is present in the string
if( ( strptr = strstr( whitebuf, "?" ) ) == NULL )
{
printf("ERROR: all whitelist.dat entry first fields *must be*\n");
printf(" terminated with a \'?\' character!! Entry is:\n");
printf(" %s", whitebuf);
printf(" Entry was ignored!\n");
continue;
}
// Make sure the '?' character is within the first twenty characters
if( (int)( strptr - whitebuf ) > 18 )
{
printf("ERROR: terminator '?' is not within first 20 characters\n" );
printf(" %s", whitebuf);
printf(" Entry was ignored!\n");
continue;
}
// Get a pointer to the search token in the string
if( ( whitebufptr = strtok( whitebuf, "?" ) ) == NULL )
{
printf("whitebuf strtok() failed\n");
return(TRUE); // accept the call
}
// Scan the call string for the whitelist entry
if( strstr( callstr, whitebufptr ) != NULL )
{
#ifdef DEBUG
printf("whitelist entry matches: %s\n", whitebuf );
#endif
// Make sure the 'DATE = ' field is present
if( (dateptr = strstr( callstr, "DATE = " ) ) == NULL )
{
printf( "DATE field not found in caller ID!\n" );
return(TRUE); // accept the call
}
// Get the current date from the caller ID string
strncpy( call_date, &dateptr[7], 6 );
// Terminate the string
call_date[6] = 0;
// Update the date in the whitebufsave record
strncpy( &whitebufsave[19], call_date, 6 );
// Write the record back to the whitelist.dat file
fseek( fpWh, file_pos_last, SEEK_SET );
if( fputs( whitebufsave, fpWh ) == EOF )
{
printf("fputs(whitebufsave, fpWh) failed\n" );
return(TRUE); // accept the call
}
// Flush the string to the file
if( fflush(fpWh) == EOF )
{
printf("fflush(fpWh) failed\n");
return(TRUE); // accept the call
}
// Force kernel file buffers to the disk
// (probably not necessary)
sync();
// A whitelist.dat entry matched, so return TRUE
return(TRUE); // accept the call
}
} // end of while()
// No whitelist.dat entry matched, so return FALSE.
return(FALSE);
}
//
// Compare strings in the 'blacklist.dat' file to fields in the
// received caller ID string. If a blacklist string is present,
// send off-hook (ATH1) and on-hook (ATH0) to the modem to
// terminate the call...
//
static bool check_blacklist( char *callstr )
{
char blackbuf[100];
char blackbufsave[100];
char *blackbufptr;
char call_date[10];
char *dateptr;
char *strptr;
int i;
long file_pos_last, file_pos_next;
char yearStr[10];
// Close and re-open the blacklist.dat file. Note: this
// seems to be necessary to be able to write records
// back into the file. The write works the first time
// after the file is opened but not subsequently! :-(
// This also allows blacklist changes made while the
// program is running to be recognized.
//
fclose( fpBl );
// Re-open for reading and writing
if( (fpBl = fopen( "./blacklist.dat", "r+" ) ) == NULL )
{
printf("re-open fopen( blacklist) failed\n" );
return(FALSE);
}
// Disable buffering for blacklist.dat writes
setbuf( fpBl, NULL );
// Seek to beginning of list
fseek( fpBl, 0, SEEK_SET );
// Save the file's current access location
if( file_pos_next = ftell( fpBl ) == -1L )
{
printf("ftell(fpBl) failed\n");
return(FALSE);
}
// Read and process records from the file
while( fgets( blackbuf, sizeof( blackbuf ), fpBl ) != NULL )
{
// Save the start location of the string just read and get
// the location of the start of the next string in the file.
file_pos_last = file_pos_next;
file_pos_next = ftell( fpBl );
// Ignore lines that start with a '#' character (comment lines)
if( blackbuf[0] == '#' )
continue;
// Ignore lines containing just a '\n'
if( blackbuf[0] == '\n' )
{
continue;
}
// Ignore records that are too short (don't have room for the date)
if( strlen( blackbuf ) < 26 )
{
printf("ERROR: blacklist.dat record is too short to hold date field.\n");
printf(" record: %s", blackbuf );
printf(" record is ignored (edit file and fix it).\n");
continue;
}
// Save the string (for writing back to the file later)
strcpy( blackbufsave, blackbuf );
// Make sure a '?' char is present in the string
if( ( strptr = strstr( blackbuf, "?" ) ) == NULL )
{
printf("ERROR: all blacklist.dat entry first fields *must be*\n");
printf(" terminated with a \'?\' character!! Entry is:\n");
printf(" %s", blackbuf);
printf(" Entry was ignored!\n");
continue;
}
// Make sure the '?' character is within the first twenty characters
// (could not be if the previous record was only partially written).
if( (int)( strptr - blackbuf ) > 18 )
{
printf("ERROR: terminator '?' is not within first 20 characters\n" );
printf(" %s", blackbuf);
printf(" Entry was ignored!\n");
continue;
}
// Get a pointer to the search token in the string
if( ( blackbufptr = strtok( blackbuf, "?" ) ) == NULL )
{
printf("blackbuf strtok() failed\n");
return(FALSE);
}
// Scan the call string for the blacklist entry
if( strstr( callstr, blackbufptr ) != NULL )
{
#ifdef DEBUG
printf("blacklist entry matches: %s\n", blackbuf );
#endif
// At this point, the modem is in data mode. It must
// be returned to command mode to send it the off-hook
// and on-hook commands. For the modem used, command
// 'AT+++' did not work. The only way I could find to
// put it back in command mode was to close, open and
// reinitialize the connection. This clears the DTR line
// which resets the modem to command mode. To accomplish
// this in time (before the next ring), the caller ID
// command is not sent. Later, the modem is again
// reinitialized with caller ID activated. This is all
// kind of crude, but it works...
close_open_port( CALLERID_NO );
usleep( 250000 ); // quarter second
// Send off hook command
#ifdef DEBUG
printf("sending off hook\n");
#endif
send_modem_command(fd, "ATH1\r");
sleep(1);
// Send on hook command
#ifdef DEBUG
printf("sending on hook\n");
#endif
send_modem_command(fd, "ATH0\r");
sleep(1);
// Now, to prepare for the next call, close and reopen
// the port with caller ID activated.
close_open_port( CALLERID_YES );
// Make sure the 'DATE = ' field is present
if( (dateptr = strstr( callstr, "DATE = " ) ) == NULL )
{
printf( "DATE field not found in caller ID!\n" );
return(FALSE);
}
// Get the current date from the caller ID string
strncpy( call_date, &dateptr[7], 6 );
// Terminate the string
call_date[6] = 0;
// Update the date in the blackbufsave record
strncpy( &blackbufsave[19], call_date, 6 );
// Write the record back to the blacklist.dat file
fseek( fpBl, file_pos_last, SEEK_SET );
if( fputs( blackbufsave, fpBl ) == EOF )
{
printf("fputs(blackbufsave, fpBl) failed\n" );
return(FALSE);
}
// Flush the string to the file
if( fflush(fpBl) == EOF )
{
printf("fflush(fpBl) failed\n");
return(FALSE);
}
// Force kernel file buffers to the disk
// (probably not necessary)
sync();
// A blacklist.dat entry matched, so return TRUE
return(TRUE);
}
} // end of while()
/* A blacklist.dat entry was not matched, so return FALSE */
return(FALSE);
}
//
// Add a record to the blacklist.dat file.
// Extract the NAME or NMBR field from the callerID record and use it to
// construct a blacklist.dat entry. Then append it to the blacklist.dat file.
// Return TRUE if an entry was made; FALSE on an error.
// Note:
// If you enter blacklist.dat records manually with some editors (e.g., vi
// or gedit), the editor adds a '\n' character at the end of the file when
// the file is closed if you didn't! Some editors don't do this (e.g., emacs).
// The '\n' character can be in the last or second to last location since the
// stored length is always even.
// This function starts the record it constructs with a '\n'. It checks to see
// if your editor added a '\n' at the end of the file. If one is present, it
// writes the first character of the new record over it. If not, it appends
// the new record to the end of the file.
//
bool write_blacklist( char *callstr )
{
char blackbuf[100];
char blacklistEntry[80];
char readbuf[10];
char *srcDesc = "*-KEY ENTRY";
char *nameStr, *nmbrStr, *nmbrStrEnd;
int nameStrLength, nmbrStrLength;
int i;
char yearStr[10];
// Close and re-open the blacklist.dat file. Note: this
// seems to be necessary to be able to write records
// back into the file. The write works the first time
// after the file is opened but not subsequently! :-(
// This also allows blacklist changes made while the
// program is running to be recognized.
//
fclose( fpBl );
// Re-open for reading and writing
if( (fpBl = fopen( "./blacklist.dat", "r+" ) ) == NULL )
{
printf("write_blacklist: re-open fopen() failed\n" );
return(FALSE);
}
// Disable buffering for blacklist.dat writes
setbuf( fpBl, NULL );
// Build a blacklist entry from the caller ID string.
// First fill the build array with ' ' chars.
for(i = 0; i < 80; i++)
{
blacklistEntry[i] = ' ';
}
// If the NAME field does not contain "Cell Phone", use it as the
// match string. If it does, use the call's number instead.
// Note: Cell phone calls generally contain a "generic" NAME
// field: "Cell Phone XX", where XX is the state ID (e.g., MI for
// Michigan). If that field was used in the blacklist record, all
// cell phone calls from that state would be blocked! So we use the
// call's number instead in those cases.
//
// For some caller ID strings, the NMBR and NAME fields are not the
// standard lengths (10 and 15, respectively). So we need to calculate
// their positions and lengths.
//
// Find the start of the "NAME = " string.
if( ( nameStr = strstr( callstr, "NAME = " ) ) == NULL )
{
printf( "write_blacklist: strstr(..., \"NAME = \" ) failed\n" );
return FALSE;
}
// Find the start of the "NMBR = " string.
if( ( nmbrStr = strstr( callstr, "NMBR = " ) ) == NULL )
{
printf( "write_blacklist: strstr(..., \"NMBR = \" ) failed\n" );
return FALSE;
}
// While here, find a pointer to the character after the NMBR
// string field (subtract 2 from nameStr for the "--" separater)
nmbrStrEnd = nameStr - 2;
// Find the start of the NAME string field.
nameStr += strlen( "NAME = " );