|
|
1.1 root 1: /*
2: * This software is Copyright 1987 by Stan Barber.
3: *
4: * Permission is hereby granted to copy, reproduce, redistribute or otherwise
5: * use this software as long as: there is no monetary profit gained
6: * specifically from the use or reproduction or this software, it is not
7: * sold, rented, traded or otherwise marketed, and this copyright notice is
8: * included prominently in any copy made.
9: *
10: * The author make no claims as to the fitness or correctness of this software
11: * for any use whatsoever, and it is provided as is. Any use of this software
12: * is at the user's own risk.
13: *
14: * this is a file full of functions used by the various news client programs
15: */
16: #ifdef SCCSID
17: static char *SccsId = "@(#)nntp.c 2.2 10/15/87";
18: #endif /* SCCSID */
19:
20: #include "params.h"
21:
22: static char *nntpserver;
23: static char ser_line[256];
24: static char active_file_name[512];
25: static char article_file_name[512];
26: static char last_group[256];
27: /*
28: * open_server open a connection to the server
29: */
30: open_server()
31: {
32: int response;
33:
34: /* open connection to nntpserver if appropriate */
35:
36: nntpserver = getserverbyfile(SERVER_FILE);
37: if (nntpserver == NULL) {
38: fprintf(stderr, "Can't get the name of the news server from %s\n",
39: SERVER_FILE);
40: fprintf(stderr, "Either fix this file, or put NNTPSERVER in your environment.");
41: return -1;
42: }
43: response = server_init(nntpserver);
44: if (response < 0) {
45: fprintf(stderr, "Couldn't connect to %s news server, try again later.\n",
46: nntpserver);
47: return -1;
48: }
49: if (handle_server_response(response, nntpserver) < 0) {
50: fprintf(stderr, "Cannot handle response from nntpserver.\n");
51: return -1;
52: }
53: return 0;
54: }
55:
56: /*
57: * open_active gets the active file and returns an open file descriptor to
58: * the calling program
59: */
60:
61: FILE *
62: open_active()
63: {
64: FILE *openfp;
65: bzero(active_file_name, sizeof(active_file_name));
66: put_server("LIST"); /* tell server we want the active file */
67: (void) get_server(ser_line, sizeof(ser_line));
68: if (*ser_line != CHAR_OK) { /* and then see if that's ok */
69: xerror("Can't get active file from server: \n%s\n", ser_line);
70: }
71: strcpy(active_file_name, "/tmp/nsact.XXXXXX");
72: (void) mktemp(active_file_name); /* make a temporary name */
73: openfp = fopen(active_file_name, "w+"); /* and get ready */
74: if (openfp == NULL)
75: return NULL;
76:
77: while (get_server(ser_line, sizeof(ser_line)) >= 0) { /* while */
78: if (ser_line[0] == '.' && strlen(ser_line) == 1)
79: /* there's another line */
80: break; /* get it and write it to */
81: fputs(ser_line, openfp);
82: putc('\n', openfp);
83: }
84:
85: fseek(openfp, 0L, 0); /* just get to the beginning */
86:
87: return openfp;
88: }
89:
90: /*
91: * active_name() returns the name of the temporary file that contains the
92: * name of the current active file.
93: */
94:
95: char *
96: active_name()
97: {
98: if (active_file_name[0] == '\0')
99: return NULL;
100: return &active_file_name[0];
101: }
102:
103: /*
104: * set_group() set the current group returns NULL if failure "string" if
105: * successful
106: */
107:
108: char *
109: set_group(newsgroup)
110: char *newsgroup;
111: {
112: char nntpbfr[256];
113: if (newsgroup == NULL || *newsgroup == '\0')
114: return NULL;
115: if (strcmp(newsgroup, last_group)) {
116: (void) sprintf(nntpbfr, "GROUP %s", newsgroup);
117: put_server(nntpbfr);
118: (void) get_server(ser_line, sizeof(ser_line));
119: if (*ser_line != CHAR_OK)
120: return NULL;
121: strcpy(last_group, newsgroup);
122: }
123: return &ser_line[0];
124: }
125:
126: /*
127: * getarticle() returns an open file descriptor to the requested article.
128: */
129:
130: FILE *
131: getarticle(newsgroup, number, command)
132: char *newsgroup, *command;
133: int number;
134: {
135: FILE *fp;
136: char nntpbfr[256];
137: bzero(article_file_name, sizeof(article_file_name));
138: if (set_group(newsgroup) == NULL)
139: return NULL;
140: strcpy(article_file_name, "/tmp/nsart.XXXXXX");
141: if (mktemp(article_file_name) == NULL)
142: return NULL;
143: (void) sprintf(nntpbfr, "%s %ld", command, number);
144: put_server(nntpbfr);
145: (void) get_server(ser_line, sizeof(ser_line));
146: if (*ser_line != CHAR_OK)
147: return NULL;
148: if ((fp = fopen(article_file_name, "w+")) == NULL) {
149: /* and get ready */
150: sync_server();
151: return NULL;
152: }
153: while (get_server(ser_line, sizeof(ser_line)) >= 0) { /* while */
154: if (ser_line[0] == '.' && strlen(ser_line) == 1)
155: /* there's another line */
156: break; /* get it and write it to */
157: fputs(ser_line, fp); /* the temp file */
158: putc('\n', fp);
159: }
160: fseek(fp, 0L, 0); /* just get to the beginning */
161: return fp;
162: }
163: /*
164: * article_name() returns the name of the temporary file that contains the
165: * name of the current article file.
166: */
167:
168: char *
169: article_name()
170: {
171: if (article_file_name[0] == '\0')
172: return NULL;
173: return &article_file_name[0];
174: }
175:
176: /*
177: * group_name() returns the name of the last group accessed from nntp
178: */
179:
180: char *
181: group_name()
182: {
183: if (last_group[0] == '\0')
184: return NULL;
185: return &last_group[0];
186: }
187:
188: /*
189: * getartbyid retrieves an article by id number and returns an open file
190: * descriptor for that article
191: */
192:
193: FILE *
194: getartbyid(id)
195: char *id;
196: {
197: FILE *fp;
198: char nntpbfr[256];
199: (void) sprintf(nntpbfr, "ARTICLE %s", id);
200: put_server(nntpbfr);
201: (void) get_server(ser_line, sizeof(ser_line));
202: if (*ser_line != CHAR_OK) {
203: fprintf(stderr, "Cannot fetch article %s\n", id);
204: return NULL;
205: }
206: strcpy(article_file_name, "/tmp/nsart.XXXXXX");
207: if (mktemp(article_file_name) == NULL)
208: return NULL;
209: if ((fp = fopen(article_file_name, "w+")) == NULL) {
210: /* and get ready */
211: sync_server();
212: return NULL;
213: }
214: while (get_server(ser_line, sizeof(ser_line)) >= 0) { /* while */
215: if (ser_line[0] == '.' && strlen(ser_line) == 1)
216: /* there's another line */
217: break; /* get it and write it to */
218: fputs(ser_line, fp); /* the temp file */
219: putc('\n', fp);
220: }
221: fseek(fp, 0L, 0); /* just get to the beginning */
222: return fp;
223: }
224:
225: /*
226: * sync_server gobbles up the rest of the server output until it sees the .
227: * on the beginning of a line by itself
228: */
229:
230: void
231: sync_server()
232: {
233: while (get_server(ser_line, sizeof(ser_line)) >= 0) { /* while */
234: if (ser_line[0] == '.' && strlen(ser_line) == 1)
235: /* there's another line */
236: break; /* get it and throw it away */
237: }
238:
239: }
240:
241: /*
242: * strindex returns location of tx in sx
243: */
244: int
245: strindex(sx, tx)
246: char *sx, *tx;
247: {
248: int i, n;
249: n = strlen(tx);
250: for (i = 0; sx[i] != '\0'; i++)
251: if (strncmp(sx + i, tx, n) == 0)
252: return i;
253: return -1;
254: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.