File: offline.c

package info (click to toggle)
uqwk 2.21-14
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 528 kB
  • ctags: 473
  • sloc: ansic: 4,555; sh: 1,075; makefile: 126
file content (420 lines) | stat: -rw-r--r-- 8,896 bytes parent folder | download | duplicates (4)
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
/*
 * $Id: offline.c,v 1.4 2000/06/21 09:49:46 js Exp $
 */

#include <stdio.h>
#include <string.h>
#include "uqwk.h"
/*
 *  Process offline commands
 */

int QWKOffLine (bytes, fd)
int bytes;
FILE *fd;
/*
 *  Process offline commands.  Message is open on fd.  We
 *  must be careful to leave the file pointer ready for the
 *  next message.
 */
{
	FILE *pfd;
	char c, cmd[PATH_LEN];
	int	ret = 0;

	/* Open mail pipe to send results back to user */
/*
	sprintf (buf, "%s -s 'Results of your request' %s",
			MAILER_PATH, user_name);
*/
	sprintf (buf, "%s %s",
			SENDMAIL, user_name);
	if (NULL == (pfd = popen (buf, "w"))) {
		fprintf (stderr, "%s: can't popen() mail\n", progname);
		while (bytes--) fread (&c, 1, 1, fd);
		return -1;
	}

	fprintf (pfd, "Subject: Results of your request\n\nHere are the results of your mail to UQWK:\n");

	/* Get lines, process them */
	while (GetLine (&bytes, fd)) {
		/* Echo command */
		fprintf (pfd, "\nCommand: %s\n", buf);

		/* Extract command */
		if (1 != sscanf (buf, "%s", cmd)) {
			fprintf (pfd, "Malformed command.\n");
			fprintf (stderr, "%s: Malformed offline command: \"%s\"\n",
				 progname, buf);
		} else {
			fprintf (stdout, "%s: performing offline command: \"%s\"\n", progname, buf);
			/* Look up command */
			if ( !strcasecmp (cmd, "HELP") ) {
				Help(pfd);
			} else if ( !strcasecmp (cmd, "SUBSCRIBE") ) {
				if((ret = Subscribe(pfd)) == -1)
					break;
			} else if ( !strcasecmp (cmd, "UNSUBSCRIBE") ) {
				if((ret = Unsubscribe(pfd)) == -1)
					break;
			} else if ( !strcasecmp (cmd, "GROUPS") ) {
				if((ret = Groups(pfd)) == -1)
					break;
			} else if ( !strcasecmp (cmd, "ALLGROUPS") ) {
				Allgroups(pfd);
			} else if ( !strcasecmp (cmd, "SHELL") ) {
				if((ret = Shell(pfd)) == -1)
					break;
			} else if ( !strcasecmp (cmd, "CATCHUP") ) {
				if((ret = Catchup(pfd)) == -1)
					break;
			} else {
				fprintf (pfd, "No such command.  ");
				fprintf (pfd, "Send HELP for help.\n");
			}
		}
	}

	fprintf (pfd, "\nEnd of commands.\n");
	pclose (pfd);
	return ret;
}

int GetLine (bytes, fd)
int *bytes;
FILE *fd;
/*
 *  Get a line from fd, put it in buf, check for end of message
 */
{
	int i;
	unsigned char eol;

	i = 0;

	eol = QWK_EOL;
	if (slnp_mode || zip_mode) eol = '\n';

	/* Read bytes until EOL or end of message */
	while (*bytes) {
		fread (&buf[i], 1, 1, fd);
		(*bytes)--;

		/* Lose CR's from ZipNews */
		if (buf[i] == '\r') buf[i] = 0;

		if ( (buf[i] == eol) || (i == BUF_LEN-1) ) {
			buf[i] = 0;
			return (1);
		}
		i++;
	}

	/* If we got here, we ran out of bytes */
	return (0);
}

void Help (pfd)
FILE *pfd;
{
	fprintf (pfd, "\nAvailable commands:\n\n");
	fprintf (pfd, "HELP - This message.\n");
	fprintf (pfd, "SUBSCRIBE newsgroup - Subscribe to named newsgroup.\n");
	fprintf (pfd, "UNSUBSCRIBE newsgroup - Unsubscribe from newsgroup.\n");
	fprintf (pfd, "UNSUBSCRIBE ALL - Unsubscribe from all newsgroups.\n");
	fprintf (pfd, "GROUPS - List all subscribed newsgroups.\n");
	fprintf (pfd, "ALLGROUPS - List all available newsgroups.\n");
	fprintf (pfd, "CATCHUP newsgroup - Mark all articles as read.\n");
	fprintf (pfd, "SHELL command - Execute shell command\n\n");
}

int Subscribe (pfd)
FILE *pfd;
{
	struct act_ent *ap;
	struct nrc_ent *np;
	char group[PATH_LEN];

	/* Extract group name */
	if (1 != sscanf (buf, "%*s %s", group)) {
		fprintf (pfd, "Usage: SUBSCRIBE newsgroup\n");
		return (0);
	}

#if !defined(NNTP) || defined(NNTP_FULL_ACTIVE)
	/* We will need active file and .newsrc */
	if ((ReadActive() < 0) || (ReadNewsrc() < 0)) {
#else
	if (ReadNewsrc() < 0) {
#endif
		fprintf (pfd, "Sorry, couldn't read system files.\n");
		return (-1);
	}

	/* Already subscribed? */
	np = nrc_list;
	while (np != NULL) {
		if (!strcmp (group, np->name)) {
			if (np->subscribed) {
				fprintf (pfd, "Already subscribed to %s.\n",
					group);
				return (0);
			} else {
				np->subscribed = 1;
				fprintf (pfd, "Okay, re-subscribed to %s.\n",
					group);
				WriteNewsrc();
				return (0);
			}
		}
		np = np->next;
	}

	/* Find group in active file */
	if (NULL == (ap = FindActive (group))) {
		fprintf (pfd, "No such newsgroup: %s\n", group);
		return (0);
	}

	/* Okay already, add to .newsrc */
	np = (struct nrc_ent *) malloc (sizeof (struct nrc_ent));
	if (np == NULL) OutOfMemory();
	np->name = (char *) malloc (1+strlen(group));
	if (np->name == NULL) OutOfMemory();
	strcpy (np->name, group);
	np->subscribed = 1;

	/* Make subscription list - everything is read */
	if (NULL == (np->sub = (struct sub_ent *) malloc
			(sizeof (struct sub_ent)))) OutOfMemory();
	np->sub->lo = 1;
	np->sub->hi = ap->hi;
	np->sub->next = NULL;

	np->next = nrc_list;
	nrc_list = np;

	WriteNewsrc();
	fprintf (pfd, "Okay, you are now subscribed to %s.\n", group);

	return (1);
}

int Unsubscribe (pfd)
FILE *pfd;
{
	struct nrc_ent *np;
	char group[PATH_LEN];

	/* Parse group name */
	if (1 != sscanf (buf, "%*s %s", group)) {
		fprintf (pfd, "Usage: UNSUBSCRIBE newsgroup\n");
		return 0;
	}

	/* Check for ALL */
	if ( (!strcmp (group, "ALL")) || (!strcmp (group, "all")) ) {
		nrc_list = NULL;
		WriteNewsrc();
		fprintf (pfd,
		  "Okay, you are now unsubscribed from all newsgroups.\n");
		return 0;
	}

	/* We need the .newsrc file */
	if (ReadNewsrc() < 0) {
		fprintf (pfd, "Sorry, couldn't read .newsrc\n");
		return -1;
	}

	/* Look for group in newsrc */
	np = nrc_list;
	while (np != NULL) {
		if (!strcmp (group, np->name)) break;
		np = np->next;
	}

	if (np == NULL) {
		fprintf (pfd, "You are not currently subscribed to %s.\n",
		         group);
		return 0;
	}

	np->subscribed = 0;

	WriteNewsrc();
	fprintf (pfd, "Okay, you are unsubscribed from %s.\n", group);

	return 1;
}

int Groups (pfd)
FILE *pfd;
{
	struct nrc_ent *np;

	if (ReadNewsrc() < 0) {
		fprintf (pfd, "Sorry, couldn't read .newsrc\n");
		return (-1);
	}

	fprintf (pfd, "Newsgroups to which you are subscribed:\n\n");

	np = nrc_list;
	while (np != NULL) {
		if (np->subscribed) fprintf (pfd, "    %s\n", np->name);
		np = np->next;
	}
	return (1);
}

int Allgroups (pfd)
FILE *pfd;
{
	struct act_ent *ap;

	if (ReadActive() < 0) {
		fprintf (pfd, "Sorry, no newsgroups are available.\n");
		return (0);
	}

	fprintf (pfd, "List of available newsgroups:\n\n");

	ap = act_list;
	while (ap != NULL) {
		fprintf (pfd, "    %s (%d articles)\n",
			ap->name, ap->hi - ap->lo);
		ap = ap->next;
	}
	return (1);
}

int Catchup (pfd)
FILE *pfd;
{
	struct act_ent *ap;
	struct nrc_ent *np;
	struct sub_ent *sp, *tsp;
	char group[PATH_LEN];

	/* Extract group name */
	if (1 != sscanf (buf, "%*s %s", group)) {
		fprintf (pfd, "Usage: CATCHUP newsgroup\n");
		return (0);
	}

#if !defined(NNTP) || defined(NNTP_FULL_ACTIVE)
	/* We will need active file and .newsrc */
	if ((ReadActive() < 0) || (ReadNewsrc() < 0)) {
#else
	if (ReadNewsrc() < 0) {
#endif
		fprintf (pfd, "Sorry, couldn't read system files.\n");
		return (-1);
	}

	/* Not subscribed? */
	np = nrc_list;
	while (np != NULL) {
		if (!strcmp (group, np->name)) {
			if (np->subscribed) {
				break;
			} else {
				fprintf (pfd,
					"You are not subscribed to %s.\n",
					group);
				return (0);
			}
		}
		np = np->next;
	}

	if (np == NULL) {
		fprintf (pfd, "You are not subscribed to %s.\n", group);
		return (0);
	}

	/* Find group in active file */
	if (NULL == (ap = FindActive (group))) {
		fprintf (pfd, "No such newsgroup: %s\n", group);
		return (0);
	}

	/* Free subscription list */
	sp = np->sub;
	while (sp != NULL) {
		tsp = sp->next;
		free (sp);
		sp = tsp;
	}

	/* Okay already, add to .newsrc */
	np = (struct nrc_ent *) malloc (sizeof (struct nrc_ent));
	if (np == NULL) OutOfMemory();
	np->name = (char *) malloc (1+strlen(group));
	if (np->name == NULL) OutOfMemory();
	strcpy (np->name, group);
	np->subscribed = 1;

	/* Make subscription list - everything is read */
	if (NULL == (np->sub = (struct sub_ent *) malloc
			(sizeof (struct sub_ent)))) OutOfMemory();
	np->sub->lo = 1;
	np->sub->hi = ap->hi;
	np->sub->next = NULL;

	np->next = nrc_list;
	nrc_list = np;

	WriteNewsrc();
	fprintf (pfd, "Okay, you are now caught up in %s.\n", group);

	return (1);
}

int Shell (pfd)
FILE *pfd;
{
	int c;
	FILE *cfd;

	if (strlen(buf) < 7) {
		fprintf (pfd, "Usage: SHELL command\n");
		return (0);
	}

	if (NULL == (cfd = popen (&buf[6], "r"))) {
		fprintf (pfd, "Can't open pipe for command\n");
		return (-1);
	}

	while (EOF != (c = getc (cfd))) {
		if(putc( (0xff & c), pfd) == EOF)
			return(-1);
	}
	if(putc ('\n', pfd) == EOF)
		return(-1);
	pclose (cfd);
	return (1);
}

int OffLine (fd, bytes)
FILE *fd;
int bytes;
/*
 *  Offline command
 */
{
	/* Skip header */
	while(	(fgets (buf, BUF_LEN, fd) != NULL) && (bytes > 0)) {
		bytes -= strlen(buf);
		if(buf[0] == '\n' || (buf[0] == '\r' && buf[1] == '\n'))
			break;
	}


	/* Hand off to the QWK Offline function */
	return(QWKOffLine (bytes, fd));
}