File: NNTPSource.m

package info (click to toggle)
lusernet.app 0.4.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 560 kB
  • ctags: 97
  • sloc: objc: 8,335; sh: 1,049; makefile: 63
file content (493 lines) | stat: -rw-r--r-- 11,407 bytes parent folder | download | duplicates (5)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
copyright 2002 Alexander Malmberg <alexander@malmberg.org>
*/

#include <string.h>

#include <Foundation/NSObject.h>
#include <Foundation/NSString.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSValue.h>
#include <Foundation/NSException.h>

#include <Foundation/NSBundle.h>

#include "MsgDB.h"

#include "NNTPSource.h"
#include "GUISource.h"
#include "NNTPSourceGUI.h"

#include "NNTPServer.h"


typedef struct nntpsource_group_s group_t;


@implementation NNTPSource (private)

-(void) recreateNNTPServer
{
	const char *host,*sport;
	host=[mdb msg_getMetaHeader: "Host" : mmid];
	sport=[mdb msg_getMetaHeader: "Port" : mmid];

	if (cur_host && !strcmp(host,cur_host) && (sport==cur_sport || !strcmp(sport,cur_sport)))
		return;

	if (cur_host)
		free(cur_host);
	if (host)
	{
		cur_host=strdup(host);
		if (!cur_host) abort(); /* TODO */
	}
	else
		cur_host=NULL;

	if (cur_sport)
		free(cur_sport);
	if (sport)
	{
		cur_sport=strdup(sport);
		if (!cur_sport) abort(); /* TODO */
	}
	else
		cur_sport=NULL;

	if (server)
	{
		[server enableConnect: 0];
		[server closeAllConnections];
		DESTROY(server);
	}
	if (host)
	{
		if (sport && atoi(sport)>0 && atoi(sport)<65536)
			server=[[NNTPServer alloc] initWithHost: host port: atoi(sport)];
		else
			server=[[NNTPServer alloc] initWithHost: host];
		[server setReceiver: self];
		[server enableConnect: 1];
	}
}


-(void) syncGroupHeaders: (int)gnum
{
	char buf1[32],buf2[32];
	snprintf(buf1,sizeof(buf1),"G%i-num",gnum);
	snprintf(buf2,sizeof(buf2),"%i",groups[gnum].last_num);
	[mdb msg_setMetaHeader: buf1 value: buf2 : mmid];
}


-(void) syncAllGroupHeaders
{
	int i;
	char buf1[32],buf2[32];

	for (i=0;i<num_groups;i++)
	{
		snprintf(buf1,sizeof(buf1),"G%i-name",i);
		[mdb msg_setMetaHeader: buf1 value: [NNTPServer getGroupName: groups[i].idx] : mmid];
		snprintf(buf1,sizeof(buf1),"G%i-num",i);
		snprintf(buf2,sizeof(buf2),"%i",groups[i].last_num);
		[mdb msg_setMetaHeader: buf1 value: buf2 : mmid];
	}
	snprintf(buf1,sizeof(buf1),"%i",num_groups);
	[mdb msg_setMetaHeader: "Groups" value: buf1 : mmid];
}


-(void) nntp_serverDate: (const char *)date qid: (unsigned int)qid { }
-(void) nntp_headerId: (const char *)mid  data: (const unsigned char *)data : (int)length  qid: (unsigned int)qid { }
-(void) nntp_articleRange: (int)group : (int)num  data: (const unsigned char *)data : (int)length  qid: (unsigned int)qid { }
-(void) nntp_articleId: (const char *)mid  data: (const unsigned char *)data : (int)length  qid: (unsigned int)qid
{
	unsigned char *d;
	msg_id_t mdb_id;
	mdb_id=[mdb midForId: mid];
	if (!mdb_id)
		mdb_id=[mdb createMessageWithId: mid  source: self];

	if (length==-1)
	{
		[mdb setMessageData: NULL length: -1 : mdb_id];
		return;
	}
	d=malloc(length);
	if (!d) abort();
	memcpy(d,data,length);
	[mdb setMessageData: d length: length : mdb_id];
}
-(void) nntp_groupList: (const unsigned char *)data : (int)length  qid: (unsigned int)qid { }


-(void) nntp_fail: (NSString *)reason qid: (unsigned int)qid
{
	[[mdb notificationCenter]
		postNotificationName: MsgDB_LogMessageNotification
		object: mdb
		userInfo:
			[NSDictionary
				dictionaryWithObject: [NSString stringWithFormat: @"'%@' failed: %@",[server qidDescription: qid],reason]
				forKey: @"Message"]];
}
-(void) nntp_fail_connect: (NSString *)reason
{
	[[mdb notificationCenter]
		postNotificationName: MsgDB_LogMessageNotification
		object: mdb
		userInfo:
			[NSDictionary
				dictionaryWithObject: [NSString stringWithFormat: _(@"Failed to connect: %@"),reason]
				forKey: @"Message"]];
}
-(void) nntp_message: (NSString *)msg
{
	[[mdb notificationCenter]
		postNotificationName: MsgDB_LogMessageNotification
		object: mdb
		userInfo:
			[NSDictionary
				dictionaryWithObject: msg
				forKey: @"Message"]];
}
-(void) nntp_progress: (int)bytes : (unsigned int)qid
{
	[[mdb notificationCenter]
		postNotificationName: MsgDB_LogMessageNotification
		object: mdb
		userInfo:
			[NSDictionary
				dictionaryWithObject:
					[NSString stringWithFormat: _(@"Downloaded %ikb for '%@'"),
						bytes/1024,[server qidDescription: qid]]
				forKey: @"Message"]];
}

-(void) nntp_groupInfo: (int)group : (int)num : (int)first : (int)last qid: (unsigned int)qid
{
	int i;
	for (i=0;i<num_groups;i++)
		if (group==groups[i].idx)
			break;
	if (i==num_groups)
	{
		if (first!=-1 || last!=-1)
			[[mdb notificationCenter]
				postNotificationName: MsgDB_LogMessageNotification
				object: mdb
				userInfo:
					[NSDictionary
						dictionaryWithObject: [NSString stringWithFormat: _(@"Group '%s' contains message %i - %i."),
							[NNTPServer getGroupName: group],
							first,last]
						forKey: @"Message"]];
		else
			[[mdb notificationCenter]
				postNotificationName: MsgDB_LogMessageNotification
				object: mdb
				userInfo:
					[NSDictionary
						dictionaryWithObject: [NSString stringWithFormat: _(@"Group '%s' doesn't exist."),
							[NNTPServer getGroupName: group],
							first,last]
						forKey: @"Message"]];
		return;
	}
	if (num==-1) return;

	if (last<=groups[i].last_num)
	{
		[[mdb notificationCenter]
			postNotificationName: MsgDB_LogMessageNotification
			object: mdb
			userInfo:
				[NSDictionary
					dictionaryWithObject: [NSString stringWithFormat: _(@"No new messages in '%s'."),
						[NNTPServer getGroupName: groups[i].idx]]
					forKey: @"Message"]];
		return;
	}
	if (groups[i].last_num>=first) first=groups[i].last_num+1;

	[[mdb notificationCenter]
		postNotificationName: MsgDB_LogMessageNotification
		object: mdb
		userInfo:
			[NSDictionary
				dictionaryWithObject: [NSString stringWithFormat: _(@"%i new messages in '%s', retrieving headers..."),
					last-first+1,[NNTPServer getGroupName: groups[i].idx]]
				forKey: @"Message"]];

//	printf("requesting headers for %i-%i=%i\n",first,last,last-first+1);
	[server getHeaderRange: first : last group: group priority: 0];
}

-(void) nntp_headerRange: (int)group : (int)first : (int)last : (int)num
	: (char **)list partial: (BOOL)partial qid: (unsigned int)qid
{
static const char *fields[8]=
{0,"Real-Subject","Real-From","Real-Date","Real-Message-ID",
"Real-References","Real-Bytes","Real-Lines"};

/* TODO: can't be static since different NNTPSource:s might have different
mdb:s. should probably keep in an instance variable. */
	int field_nums[8];

	int i;
	int gnum;

	if (!num) return;

//	printf("got %i headers in\n",num);
	for (gnum=0;gnum<num_groups;gnum++)
		if (group==groups[gnum].idx)
			break;
	if (gnum==num_groups) abort();
	groups[gnum].last_num=last;
	[self syncGroupHeaders: gnum];

	for (i=0;i<8;i++)
		if (fields[i])
			field_nums[i]=[mdb getMetaHeaderNum: fields[i]];

	for (i=0;i<num;i++)
	{ /* TODO: optimize? */
		int j;
		msg_id_t mid=0;
		char *c,*b;

		/* find the Message-ID */
		for (j=4,c=list[i];*c && j;c++) if (*c=='\t') j--;
		if (!*c) continue;
		for (b=c;*b && *b!='\t';b++) ;
		if (!*b) continue;
		*b=0;

		mid=[mdb createMessageWithId: c  source: self];
		if (!mid) /* the message already exists */
		{
			mid=[mdb midForId: c];
			[mdb msg_setSource: self : mid];
		}
		*b='\t';

		j=0;
		for (c=list[i];*c;j++)
		{
			b=c;
			for (;*c && *c!='\t';c++) ;
			if (*c=='\t')
				*c++=0;
			if (*b)
			{
				if (j<8)
				{
					const char *f;
					f=fields[j];
					if (f)
						[mdb msg_setMetaHeaderWithNumber: field_nums[j] value: b : mid];
				}
				else
				{
#if 0 /* ignore, we probably aren't interested in it anyway */
				/* Since there have been 8 fields before this one, there must
				  have been at least 8 tabs. Thus, we can safely copy Real- right
				  before the extra header. */
					char *f;
					b-=5;
					memcpy(b,"Real-",5);
					f=strchr(b,':');
					if (!f) continue;
					*f=0;
					f+=2;
					[mdb msg_setMetaHeader: b value: f : mid];
#endif
				}
			}
		}
		[mdb msg_addToFolder: [NNTPServer getGroupName: group] : mid];
	}
//	printf("done adding\n");

	if (partial)
	{
		int last_msg_num=strtol(list[num-1],NULL,10);
		[[mdb notificationCenter]
			postNotificationName: MsgDB_LogMessageNotification
			object: mdb
			userInfo:
				[NSDictionary
					dictionaryWithObject: [NSString stringWithFormat:
						_(@"Got headers in '%s': %5i/%5i (%5i) ..."),
						[NNTPServer getGroupName: groups[gnum].idx],
						last_msg_num-first+1,last-first+1,num]
					forKey: @"Message"]];
	}
	else
	{ /* TODO: try to keep track of actual number of messages? */
		[[mdb notificationCenter]
			postNotificationName: MsgDB_LogMessageNotification
			object: mdb
			userInfo:
				[NSDictionary
					dictionaryWithObject: [NSString stringWithFormat:
						_(@"Got headers in '%s': %5i/%5i (%5i) Done."),
						[NNTPServer getGroupName: groups[gnum].idx],
						last-first+1,last-first+1,num]
					forKey: @"Message"]];
	}
}


-(void) nntp_postArticle: (BOOL)success  qid: (unsigned int)qid
{
	NSNumber *n=[[NSNumber alloc] initWithInt: qid];
	NSObject *w;

	w=[request_map objectForKey: n];
	if (!w)
		[NSException raise: @"NNTPSource internal error" format: @"!w in nntp_postArticle:qid:"];
	RETAIN(w);
	[request_map removeObjectForKey: n];
	DESTROY(n);
	[w postedArticle: success];
	DESTROY(w);
}

@end


@implementation NNTPSource

-(msg_id_t) mid
{
	return mmid;
}

-initWithMsg: (msg_id_t)mid db: (MsgDB *)amdb
{
	self=[super init];
	if (!self) return nil;
	mmid=mid;
	mdb=amdb;
	{
		int i;
		const char *c;
		char buf[32];

		c=[mdb msg_getMetaHeader: "Groups" : mmid];
		if (c)
			num_groups=atoi(c);
		else
			num_groups=0;

		groups=malloc(sizeof(group_t)*num_groups);
		if (!groups) return nil;

		for (i=0;i<num_groups;i++)
		{
			snprintf(buf,sizeof(buf),"G%i-name",i);
			c=[mdb msg_getMetaHeader: buf : mmid];
			if (!c) return nil;
			groups[i].idx=[NNTPServer getGroupNum: c];

			snprintf(buf,sizeof(buf),"G%i-num",i);
			c=[mdb msg_getMetaHeader: buf : mmid];
			if (!c)
				groups[i].last_num=0;
			else
				groups[i].last_num=atoi(c);
		}
	}
	[self recreateNNTPServer];
	request_map=[[NSMutableDictionary alloc] init];
	return self;
}

-(void) disconnect
{
	if (server)
	{
		[server enableConnect: 0];
		[server closeAllConnections];
	}
}

-(void) dealloc
{
//	printf("dealloc source %p\n",self);
	if (server)
	{
		[server enableConnect: 0];
		[server closeAllConnections];
		[server setReceiver: nil];
		[server killAllConnections];
		[server release];
	}

	if (cur_host)
		free(cur_host);
	if (cur_sport)
		free(cur_sport);
	cur_host=cur_sport=NULL;

	DESTROY(request_map);

	[super dealloc];
}

-(void) update
{
	int i;
	if (!server) return;
	for (i=0;i<num_groups;i++)
		[server getGroupInfo: groups[i].idx];
}

/* TODO: gcc issue: error message for this has messed up type (unsigned) */
-(unsigned int) getMessage: (msg_id_t)mid  priority: (int)pri
{
	const char *c,*d;
	int size;

	c=[mdb msg_getMessageID: mid];
	if (!server || !c)
	{
		[mdb setMessageData: NULL length: -1 : mid];
		return 0;
	}

	d=[mdb msg_getHeader: "Bytes" : mid];
	if (d)
		size=atoi(d);
	else
		size=3000;
	return [server getArticleById: c  size: size  priority: pri];
}

-(BOOL) cancelGetMessage: (msg_id_t)mid  id: (unsigned int)d
{
	if ([server cancelQid: d  kill: NO])
		return YES;
	return NO;
}


-(void) postArticle: (unsigned char *)data  length: (int)length
	sender: (NSObject *)sender
{
	unsigned int qid;
	NSNumber *n;
	qid=[server postArticle: data  length: length  priority: 2];
	n=[[NSNumber alloc] initWithInt: qid];
	[request_map setObject: sender forKey: n];
	DESTROY(n);
}


@end