-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcallval.c
200 lines (166 loc) · 4.58 KB
/
callval.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
/*
* July 10, 2007 - JNOS never had any callsign validation. Now it does !
* with kind permission from Barry (K2MF), plus a few mods for compiler.
*
* Oct 19, 2010 - Added tactical callsign validation by Bob (K6FSH), so
* renamed the original callcheck to org_callcheck, then created a new
* callcheck to include both functions (if that makes any sense).
*/
#include "global.h"
#include "proc.h"
#ifdef MBX_CALLCHECK
#include <string.h>
#include <ctype.h>
/* Universal callsign validation function. It is assumed that a string
* containing only alphanumeric characters is being passed to it.
* - 08/94 K2MF */
static int org_callcheck (char *str)
{
int i, len;
int digits = 0, firstdigit = 0, seconddigit = 0;
int thirddigit = 0, fourthdigit = 0, fifthdigit = 0;
len = strlen(str);
if(len < 3 || len > 6)
/* Callsign less than 3 or greater than 6 characters */
return 0; /* invalid */
for(i = 0; i < len; i++) {
if(!isalnum(str[i]))
/* Character is not alphanumeric */
return 0; /* invalid */
if(isdigit(str[i])) {
/* Got a digit */
if(++digits > 2)
/* More than 2 digits in callsign */
return 0; /* invalid */
switch(i) {
case 0:
/* 1st character */
if(len == 3)
/* Callsign only 3
* characters */
return 0; /* invalid */
firstdigit++;
break;
case 1:
/* 2nd character */
if(firstdigit || len == 3)
/* 1st character also a
* digit or callsign only
* 3 characters */
return 0; /* invalid */
seconddigit++;
break;
case 2:
if(seconddigit && len == 3)
/* 2nd character also a
* digit and callsign only
* 3 characters */
return 0; /* invalid */
thirddigit++;
break;
case 3:
/* 4th character */
if(firstdigit || len == 4)
/* 1st character also a
* digit or callsign only
* 4 characters */
return 0; /* invalid */
fourthdigit++;
break;
case 4:
/* 5th character */
if(firstdigit || len == 5)
/* 1st character also a
* digit or callsign only
* 5 characters */
return 0; /* invalid */
fifthdigit++;
break;
case 5:
/* Digit in 6th character */
return 0; /* invalid */
}
}
}
if(!thirddigit && (firstdigit || !seconddigit))
/* The 3rd character is not a digit and the 1st character
* is a digit or the 2nd character is not a digit */
return 0; /* invalid */
/* String is a valid callsign (it passed all the tests!) */
return 1;
}
#ifdef MBX_TAC_CALLCHECK
/* Compare the call (pointed to by callP) to the list contained in
* the file ./TacCalls. Return 1 if a match is found, otherwise 0.
*
* Format of the TacCalls file is:
* Each line can contain-
* 1 alphanumeric string, no more than 6 characters long and
* beginning with the 1st character on the line.
*
* Optional comments are allowed on lines that begin with '#' or
* on the same line as a call separated by
* 1 or more SPACE, TAB or '#' characters.
* All lines should be terminated with a '\n', '\r' or '\0'
* - K6FSH 2010-08-05
*/
#define LINELEN 256
#define TAC_CALL_SIZE 6
static int tac_callcheck (char *callP)
{
int lenCall, lenLine;
char line[LINELEN];
FILE *tf;
lenCall = strlen(callP);
/* Check size of tactical call */
if(lenCall > TAC_CALL_SIZE)
return 0; /* invalid */
if ( ( tf= fopen("./TacCalls", READ_TEXT) ) == NULLFILE )
return 0; // Open error
/* Read lines from the TacCalls file. */
while(fgets(line, sizeof(line), tf) != NULLCHAR)
{
pwait(NULL); /* be nice */
/* skip comment lines and blank lines */
if( (*line == '#') || (*line == '\n') ||
(*line == '\r') || (*line == '\0') ||
(*line == '\t') )
continue;
/* Truncate all after 1st SPACE, TAB, '#', LF or CR */
lenLine= strcspn (line, " \t#\n\r");
line[lenLine] = '\0';
if (lenLine > TAC_CALL_SIZE)
{
fclose(tf);
return 0;
}
if ( strncmpi(callP, line, lenLine)== 0 ) /* Compare ignoring case */
{ /* found it! */
fclose(tf);
return 1;
}
else
continue; /* not a match, try another */
}
/* No match was found */
fclose(tf);
return 0;
}
#endif /* end of MBX_TAC_CALLCHECK */
/* Oct19, 2010, Maiko, Restructured callcheck function */
int callcheck (char *str)
{
if (!org_callcheck (str))
{
#ifdef MBX_TAC_CALLCHECK
/*
* If the callsign check above fails, allow the connection if the
* name is contained in the TacCalls file - K6FSH 2010-05-28
*/
if (!tac_callcheck (str))
#endif
return 0;
}
return 1;
}
#endif /* end of MBX_CALLCHECK */