Subversion Repositories Okuu

Rev

Rev 3 | Rev 7 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 5
Line 1... Line 1...
1
/* $Id: news.c 3 2024-09-10 19:21:48Z nishi $ */
1
/* $Id: news.c 5 2024-09-11 00:26:25Z nishi $ */
2
 
2
 
3
#define OK_NEWS_SRC
3
#define OK_NEWS_SRC
4
#include "ok_news.h"
4
#include "ok_news.h"
5
 
5
 
6
#include "ok_util.h"
6
#include "ok_util.h"
Line 10... Line 10...
10
#include <string.h>
10
#include <string.h>
11
#include <stdlib.h>
11
#include <stdlib.h>
12
#include <stdint.h>
12
#include <stdint.h>
13
#include <sys/stat.h>
13
#include <sys/stat.h>
14
 
14
 
-
 
15
#include <sys/socket.h>
-
 
16
#include <netinet/tcp.h>
-
 
17
#include <arpa/inet.h>
-
 
18
 
-
 
19
#ifdef __FreeBSD__
-
 
20
#include <netinet/in.h>
-
 
21
#endif
-
 
22
 
-
 
23
extern char* nntpserver;
-
 
24
extern char* nntpuser;
-
 
25
extern char* nntppass;
-
 
26
extern char* nntppath;
-
 
27
extern char* nntpfrom;
-
 
28
extern char* nntpgroup;
-
 
29
extern char* nntpcount;
-
 
30
extern int nntpport;
-
 
31
 
15
struct news_entry news_entry;
32
struct news_entry news_entry;
16
 
33
 
-
 
34
void ok_close(int sock);
-
 
35
 
17
void ok_news_init(void){
36
void ok_news_init(void){
18
	news_entry.from = NULL;
37
	news_entry.from = NULL;
19
	news_entry.content = NULL;
38
	news_entry.content = NULL;
20
}
39
}
21
 
40
 
22
int ok_news_read(const char* path){
41
int ok_news_read(const char* path){
23
 
-
 
24
	if(news_entry.from != NULL){
42
	if(news_entry.from != NULL){
25
		free(news_entry.from);
43
		free(news_entry.from);
26
		news_entry.from = NULL;
44
		news_entry.from = NULL;
27
	}
45
	}
28
	if(news_entry.content != NULL){
46
	if(news_entry.content != NULL){
Line 201... Line 219...
201
		return 0;
219
		return 0;
202
	}else{
220
	}else{
203
		return 1;
221
		return 1;
204
	}
222
	}
205
}
223
}
-
 
224
 
-
 
225
int ok_news_parse(int sock){
-
 
226
	char c;
-
 
227
	int sta = 0;
-
 
228
	bool st = false;
-
 
229
	while(1){
-
 
230
		if(recv(sock, &c, 1, 0) <= 0){
-
 
231
			return -1;
-
 
232
		}
-
 
233
		if(c == '\n') break;
-
 
234
		if(!st){
-
 
235
			if('0' <= c && c <= '9'){
-
 
236
				sta *= 10;
-
 
237
				sta += c - '0';
-
 
238
			}else if(c == ' '){
-
 
239
				st = true;
-
 
240
			}
-
 
241
		}
-
 
242
	}
-
 
243
	return sta == 0 ? -1 : sta;
-
 
244
}
-
 
245
 
-
 
246
int ok_news_write(const char* nick, const char* message){
-
 
247
	int nt_sock;
-
 
248
	struct sockaddr_in nt_addr;
-
 
249
	if((nt_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0){
-
 
250
		fprintf(stderr, "Socket creation failure\n");
-
 
251
		return 1;
-
 
252
	}
-
 
253
 
-
 
254
	bzero((char*)&nt_addr, sizeof(nt_addr));
-
 
255
	nt_addr.sin_family = PF_INET;
-
 
256
	nt_addr.sin_addr.s_addr = inet_addr(nntpserver);
-
 
257
	nt_addr.sin_port = htons(nntpport);
-
 
258
 
-
 
259
	int yes = 1;
-
 
260
 
-
 
261
	if(setsockopt(nt_sock, IPPROTO_TCP, TCP_NODELAY, (char*)&yes, sizeof(yes)) < 0) {
-
 
262
		fprintf(stderr, "setsockopt failure");
-
 
263
		ok_close(nt_sock);
-
 
264
		return 1;
-
 
265
	}
-
 
266
 
-
 
267
	if(connect(nt_sock, (struct sockaddr*)&nt_addr, sizeof(nt_addr)) < 0){
-
 
268
		fprintf(stderr, "Connection failure\n");
-
 
269
		ok_close(nt_sock);
-
 
270
		return 1;
-
 
271
	}
-
 
272
 
-
 
273
	int sta;
-
 
274
 
-
 
275
	sta = ok_news_parse(nt_sock);
-
 
276
	if(sta == 200 || sta == 201){
-
 
277
		char construct[1024];
-
 
278
		if(nntpuser != NULL){
-
 
279
			sprintf(construct, "AUTHINFO USER %s\r\n", nntpuser);
-
 
280
			send(nt_sock, construct, strlen(construct), 0);
-
 
281
			sta = ok_news_parse(nt_sock);
-
 
282
			if(sta != 381){
-
 
283
				goto cleanup;
-
 
284
			}
-
 
285
		}
-
 
286
		if(nntppass != NULL){
-
 
287
			sprintf(construct, "AUTHINFO PASS %s\r\n", nntppass);
-
 
288
			send(nt_sock, construct, strlen(construct), 0);
-
 
289
			if(sta != 281){
-
 
290
				goto cleanup;
-
 
291
			}
-
 
292
		}
-
 
293
		send(nt_sock, "MODE READER\r\n", 4 + 1 + 6 + 2, 0);	
-
 
294
		sta = ok_news_parse(nt_sock);
-
 
295
		if(sta == 200 || sta == 201){
-
 
296
			send(nt_sock, "POST\r\n", 4 + 2, 0);
-
 
297
			sta = ok_news_parse(nt_sock);
-
 
298
			if(sta == 340){
-
 
299
				sprintf(construct, "From: %s\r\n", nntpfrom);
-
 
300
				send(nt_sock, construct, strlen(construct), 0);
-
 
301
				sprintf(construct, "Newsgroups: %s\r\n", nntpgroup);
-
 
302
				send(nt_sock, construct, strlen(construct), 0);
-
 
303
				sprintf(construct, "Subject: Message from %s\r\n", nick);
-
 
304
				send(nt_sock, construct, strlen(construct), 0);
-
 
305
				send(nt_sock, "\r\n", 2, 0);
-
 
306
				char c;
-
 
307
				int i;
-
 
308
				bool first = true;
-
 
309
				for(i = 0; message[i] != 0; i++){
-
 
310
					if(message[i] == '\n'){
-
 
311
						send(nt_sock, "\r\n", 2, 0);
-
 
312
						first = true;
-
 
313
					}else{
-
 
314
						if(first && message[i] == '.'){
-
 
315
							send(nt_sock, message + i, 1, 0);
-
 
316
						}
-
 
317
						send(nt_sock, message + i, 1, 0);
-
 
318
						first = false;
-
 
319
					}
-
 
320
				}
-
 
321
				if(!first) send(nt_sock, "\r\n", 2, 0);
-
 
322
				send(nt_sock, ".\r\n", 3, 0);
-
 
323
				sta = ok_news_parse(nt_sock);
-
 
324
				if(sta != 240) goto cleanup;
-
 
325
			}else{
-
 
326
				goto cleanup;
-
 
327
			}
-
 
328
		}else{
-
 
329
			goto cleanup;
-
 
330
		}
-
 
331
	}else{
-
 
332
		goto cleanup;
-
 
333
	}
-
 
334
 
-
 
335
	ok_close(nt_sock);
-
 
336
	return 0;
-
 
337
cleanup:
-
 
338
	ok_close(nt_sock);
-
 
339
	return 1;
-
 
340
}