File: trn-artchk.c

package info (click to toggle)
trn4 4.0-test77-18
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 4,016 kB
  • sloc: ansic: 48,332; sh: 6,795; tcl: 1,696; yacc: 662; perl: 108; makefile: 26
file content (341 lines) | stat: -rw-r--r-- 7,915 bytes parent folder | download | duplicates (12)
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
/* trn-artchk.c
*/
/* This software is copyrighted as detailed in the LICENSE file. */


/* A program to check an article's validity and print warnings if problems
** are found.
**
** Usage: trn-artchk <article> <maxLineLen> <newsgroupsFile> <activeFile>
*/

#include "EXTERN.h"
#include "common.h"
#include "util2.h"
#include "util3.h"
#ifdef SUPPORT_NNTP
#include "nntpclient.h"
#include "nntpinit.h"
#endif

#define MAXNGS 100

#ifdef SUPPORT_NNTP
int server_connection _((void));
int nntp_handle_timeout _((void));

char* server_name;
char* nntp_auth_file;
#endif /* SUPPORT_NNTP */

int debug = 0;

char* homedir;
char* dotdir;

char nullstr[1] = "";
char ess[2] = "s";

int
main(argc, argv)
int argc;
char* argv[];
{
    FILE* fp;
    FILE* fp_active = NULL;
    FILE* fp_ng = NULL;
    bool check_active = FALSE;
    bool check_ng = FALSE;
    char buff[LBUFLEN];
    char* cp;
    char* cp2;
    char* ngptrs[MAXNGS];
    int nglens[MAXNGS];
    int foundactive[MAXNGS];
    int i, col, max_col_len, line_num = 0, ngcnt = 0, ngleft;
    int found_newsgroups = 0;

    homedir = getenv("HOME");
    if (homedir == NULL)
	homedir = getenv("LOGDIR");
    dotdir = getenv("DOTDIR");
    if (!dotdir)
	dotdir = homedir;

    if (argc != 5 || !(max_col_len = atoi(argv[2]))) {
	fprintf(stderr, "\
Usage: trn-artchk <article> <maxLineLen> <newsgroupsFile> <activeFile>\n");
	exit(1);
    }

    if ((fp = fopen(argv[1], "r")) == NULL) {
	fprintf(stderr, "trn-artchk: unable to open article `%s'.\n", argv[1]);
	exit(1);
    }

    /* Check the header for proper format and report on the newsgroups */
    while (fgets(buff, LBUFLEN, fp)) {
	line_num++;
	buff[strlen(buff)-1] = '\0';
	if (!*buff)
	    break;
	if (*buff == ' ' || *buff == '\t')
	    continue;
	if (!(cp = index(buff, ':'))) {
	    printf("\nERROR: line %d is an invalid header line:\n%s\n",
		   line_num, buff);
	    break;
	}
	if (cp[1] != ' ' && cp[1] != '\0') {
	    printf("\n\
ERROR: header on line %d does not have a space after the colon:\n%s\n",
		   line_num, buff);
	}
	if (cp - buff == 10 && strnEQ(buff, "Newsgroups", 10)) {
	    found_newsgroups = 1;
	    for (cp = buff + 11; *cp == ' '; cp++)
		;
	    if (index(cp, ' ')) {
		printf("\n\
ERROR: the \"Newsgroups:\" line has spaces in it that MUST be removed. The\n\
only allowable space is the one separating the colon (:) from the contents.\n\
Use a comma (,) to separate multiple newsgroup names.\n");
		continue;
	    }
	    while (*cp) {
		if (!(cp2 = index(cp, ',')))
		    cp2 = cp + strlen(cp);
		else
		    *cp2++ = '\0';
		if (ngcnt < MAXNGS) {
		    nglens[ngcnt] = strlen(cp);
		    foundactive[ngcnt] = 0;
		    ngptrs[ngcnt] = safemalloc(nglens[ngcnt]+1);
		    strcpy(ngptrs[ngcnt], cp);
		    ngcnt++;
		}
		cp = cp2;
	    }
	    if (!ngcnt) {
		printf("\n\
ERROR: the \"Newsgroups:\" line lists no newsgroups.\n");
		continue;
	    }
	}
    }
    if (!found_newsgroups) {
	printf("\nERROR: the \"Newsgroups:\" line is missing from the header.\n");
    }

    /* Check the body of the article for long lines */
    while (fgets(buff, LBUFLEN, fp)) {
	line_num++;
	col = strlen(buff)-1;
	if (buff[col] != '\n')
	    printf("\n\
Warning: line %d has no trailing newline character and may get lost.\n",
		   line_num);
	else
	    buff[col] = '\0';
	col = 0;
	for (cp = buff; *cp; cp++) {
	    if (*cp == '\t')
		col += 8 - (col%8);
	    else
		col++;
	}
	if (col > max_col_len) {
	    printf("\n\
Warning: posting exceeds %d columns.  Line %d is the first long one:\n%s\n",
		   max_col_len, line_num, buff);
	    break;
	}
    }
#ifdef SUPPORT_NNTP
    cp = getenv("NNTPSERVER");
    if (!cp) {
	cp = filexp(SERVER_NAME);
	if (FILE_REF(cp))
	    cp = nntp_servername(cp);
    }
    if (strNE(cp,"local")) {
	server_name = savestr(cp);
	cp = index(server_name, ';');
#ifndef DECNET
	if (!cp)
	    cp = index(server_name, ':');
#endif
	if (cp) {
	    *cp = '\0';
	    nntplink.port_number = atoi(cp+1);
	}
	nntp_auth_file = filexp(NNTP_AUTH_FILE);
	if ((cp = getenv("NNTP_FORCE_AUTH")) != NULL
	 && (*cp == 'y' || *cp == 'Y'))
	    nntplink.flags |= NNTP_FORCE_AUTH_NEEDED;
	if (init_nntp() < 0)
	    server_name = NULL;
    }
#endif
    if (ngcnt) {
	struct stat st;
	if (stat(argv[3], &st) != -1)
	    check_ng = st.st_size > 0 && (fp_ng = fopen(argv[3], "r")) != NULL;
#ifdef SUPPORT_NNTP
	else if (server_name && server_connection())
	    check_ng = TRUE;
#endif
	if (stat(argv[4], &st) != -1)
	    check_active = st.st_size > 0 && (fp_active = fopen(argv[4], "r")) != NULL;
#ifdef SUPPORT_NNTP
	else if (server_name && server_connection())
	    check_active = TRUE;
#endif
    }
    if (ngcnt && (check_ng || check_active)) {
	/* Print a note about each newsgroup */
	printf("\nYour article's newsgroup%s:\n", PLURAL(ngcnt));
	if (!check_active) {
	    for (i = 0; i < ngcnt; i++) {
		foundactive[i] = 1;
	    }
	}
	else if (fp_active != NULL) {
	    ngleft = ngcnt;
	    while (fgets(buff, LBUFLEN, fp_active)) {
		if (!ngleft)
		    break;
		for (i = 0; i < ngcnt; i++) {
		    if (!foundactive[i]) {
			if ((buff[nglens[i]] == '\t' || buff[nglens[i]] == ' ')
			  && strnEQ(ngptrs[i], buff, nglens[i])) {
			    foundactive[i] = 1;
			    ngleft--;
			}
		    }
		}
	    }
	    fclose(fp_active);
	}
#ifdef SUPPORT_NNTP
	else if (server_name) {
	    int listactive_works = 1;
	    for (i = 0; i < ngcnt; i++) {
		if (listactive_works) {
		    sprintf(ser_line, "list active %s", ngptrs[i]);
		    if (nntp_command(ser_line) <= 0)
			break;
		    if (nntp_check() > 0) {
			while (nntp_gets(ser_line, sizeof ser_line) >= 0) {
			    if (nntp_at_list_end(ser_line))
				break;
			    foundactive[i] = 1;
			}
		    }
		    else if (*ser_line == NNTP_CLASS_FATAL) {
			listactive_works = FALSE;
			i--;
		    }
		}
		else {
		    sprintf(ser_line, "GROUP %s", ngptrs[i]);
		    if (nntp_command(ser_line) <= 0)
			break;
		    if (nntp_check() > 0)
			foundactive[i] = 1;
		}
	    }
	}
	if (check_ng && fp_ng == NULL) {
	    fp_ng = fopen(argv[3], "w+");
	    unlink(argv[3]);
	    if (fp_ng != NULL) {
		for (i = 0; i < ngcnt; i++) {
		    /* issue a description list command */
		    sprintf(ser_line, "XGTITLE %s", ngptrs[i]);
		    if (nntp_command(ser_line) <= 0)
			break;
		    /*$$ use list newsgroups if this fails...? */
		    if (nntp_check() > 0) {
			/* write results to fp_ng */
			while (nntp_gets(ser_line, sizeof ser_line) >= 0) {
			    if (nntp_at_list_end(ser_line))
				break;
			    fprintf(fp_ng, "%s\n", ser_line);
			}
		    }
		}
		fseek(fp_ng, 0L, 0);
	    }
	}
#endif
	if (fp_ng != NULL) {
	    ngleft = ngcnt;
	    while (fgets(buff, LBUFLEN, fp_ng)) {
		if (!ngleft)
		    break;
		for (i = 0; i < ngcnt; i++) {
		    if (foundactive[i] && ngptrs[i]) {
			if ((buff[nglens[i]] == '\t' || buff[nglens[i]] == ' ')
			  && strnEQ(ngptrs[i], buff, nglens[i])) {
			    cp = &buff[nglens[i]];
			    *cp++ = '\0';
			    while (*cp == ' ' || *cp == '\t')
				cp++;
			    if (cp[0] == '?' && cp[1] == '?')
				cp = "[no description available]\n";
			    printf("%-23s %s", buff, cp);
			    free(ngptrs[i]);
			    ngptrs[i] = 0;
			    ngleft--;
			}
		    }
		}
	    }
	    fclose(fp_ng);
	}
	for (i = 0; i < ngcnt; i++) {
	    if (!foundactive[i]) {
		printf("%-23s ** invalid news group -- check spelling **\n",
		   ngptrs[i]);
		free(ngptrs[i]);
	    }
	    else if (ngptrs[i]) {
		printf("%-23s [no description available]\n", ngptrs[i]);
		free(ngptrs[i]);
	    }
	}
    }

#ifdef SUPPORT_NNTP
    nntp_close(TRUE);
    if (server_name)
	cleanup_nntp();
#endif

    return 0;
}

#ifdef SUPPORT_NNTP
int
server_connection()
{
    static int server_stat = 0;
    if (!server_stat) {
	if (nntp_connect(server_name,0) > 0)
	    server_stat = 1;
	else
	    server_stat = -1;
    }
    return server_stat == 1;
}
#endif

#ifdef SUPPORT_NNTP
int
nntp_handle_timeout()
{
    fputs("\n503 Server timed out.\n",stderr);
    return -2;
}
#endif