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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
/*
knnntpclient.cpp
KNode, the KDE newsreader
Copyright (c) 1999-2001 the KNode authors.
See file AUTHORS for details
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*/
#include <stdlib.h>
#include <klocale.h>
#include <tqtextcodec.h>
#include <tqmutex.h>
#include "kngroupmanager.h"
#include "knnntpclient.h"
#include "utilities.h"
KNNntpClient::KNNntpClient(int NfdPipeIn, int NfdPipeOut, TQMutex& nntpMutex)
: KNProtocolClient(NfdPipeIn,NfdPipeOut), mutex(nntpMutex)
{}
KNNntpClient::~KNNntpClient()
{}
// examines the job and calls the suitable handling method
void KNNntpClient::processJob()
{
switch (job->type()) {
case KNJobData::JTLoadGroups :
doLoadGroups();
break;
case KNJobData::JTFetchGroups :
doFetchGroups();
break;
case KNJobData::JTCheckNewGroups :
doCheckNewGroups();
break;
case KNJobData::JTfetchNewHeaders :
case KNJobData::JTsilentFetchNewHeaders :
doFetchNewHeaders();
break;
case KNJobData::JTfetchArticle :
doFetchArticle();
break;
case KNJobData::JTpostArticle :
doPostArticle();
break;
case KNJobData::JTfetchSource :
doFetchSource();
break;
default:
#ifndef NDEBUG
qDebug("knode: KNNntpClient::processJob(): mismatched job");
#endif
break;
}
}
void KNNntpClient::doLoadGroups()
{
KNGroupListData *target = static_cast<KNGroupListData *>(job->data());
sendSignal(TSloadGrouplist);
if (!target->readIn(this))
job->setErrorString(i18n("Unable to read the group list file"));
}
void KNNntpClient::doFetchGroups()
{
KNGroupListData *target = static_cast<KNGroupListData *>(job->data());
sendSignal(TSdownloadGrouplist);
errorPrefix = i18n("The group list could not be retrieved.\nThe following error occurred:\n");
progressValue = 100;
predictedLines = 30000; // rule of thumb ;-)
if (!sendCommandWCheck("LIST",215)) // 215 list of newsgroups follows
return;
char *s, *line;
TQString name;
KNGroup::tqStatus status;
bool subscribed;
while (getNextLine()) {
line = getCurrentLine();
if (line[0]=='.') {
if (line[1]=='.')
line++; // collapse double period into one
else
if (line[1]==0)
break; // message complete
}
s = strchr(line,' ');
if(!s) {
#ifndef NDEBUG
qDebug("knode: retrieved broken group-line - ignoring");
#endif
} else {
s[0] = 0; // cut string
name = TQString::fromUtf8(line);
if (target->subscribed.tqcontains(name)) {
target->subscribed.remove(name); // group names are unique, we wont find it again anyway...
subscribed = true;
} else
subscribed = false;
while (s[1]!=0) s++; // the last character determines the moderation status
switch (s[0]) {
case 'n' : status = KNGroup::readOnly;
break;
case 'y' : status = KNGroup::postingAllowed;
break;
case 'm' : status = KNGroup::moderated;
break;
default : status = KNGroup::unknown;
}
target->groups->append(new KNGroupInfo(name,TQString(),false,subscribed,status));
}
doneLines++;
}
if (!job->success() || job->canceled())
return; // stopped...
TQSortedVector<KNGroupInfo> tempVector;
target->groups->toVector(&tempVector);
tempVector.sort();
if (target->getDescriptions) {
errorPrefix = i18n("The group descriptions could not be retrieved.\nThe following error occurred:\n");
progressValue = 100;
doneLines = 0;
predictedLines = target->groups->count();
sendSignal(TSdownloadDesc);
sendSignal(TSprogressUpdate);
int rep;
if (!sendCommand("LIST NEWSGROUPS",rep))
return;
if (rep == 215) { // 215 informations follows
TQString description;
KNGroupInfo info;
int pos;
while (getNextLine()) {
line = getCurrentLine();
if (line[0]=='.') {
if (line[1]=='.')
line++; // collapse double period into one
else
if (line[1]==0)
break; // message complete
}
s = line;
while (*s != '\0' && *s != '\t' && *s != ' ') s++;
if (*s == '\0') {
#ifndef NDEBUG
qDebug("knode: retrieved broken group-description - ignoring");
#endif
} else {
s[0] = 0; // terminate groupname
s++;
while (*s == ' ' || *s == '\t') s++; // go on to the description
name = TQString::fromUtf8(line);
if (target->codecForDescriptions) // some countries use local 8 bit characters in the tag line
description = target->codecForDescriptions->toUnicode(s);
else
description = TQString::fromLocal8Bit(s);
info.name = name;
if ((pos=tempVector.bsearch(&info))!=-1)
tempVector[pos]->description = description;
}
doneLines++;
}
}
if (!job->success() || job->canceled())
return; // stopped...
}
target->groups->setAutoDelete(false);
tempVector.toList(target->groups);
target->groups->setAutoDelete(true);
sendSignal(TSwriteGrouplist);
if (!target->writeOut())
job->setErrorString(i18n("Unable to write the group list file"));
}
void KNNntpClient::doCheckNewGroups()
{
KNGroupListData *target = static_cast<KNGroupListData *>(job->data());
sendSignal(TSdownloadNewGroups);
errorPrefix = i18n("New groups could not be retrieved.\nThe following error occurred:\n");
progressValue = 100;
predictedLines = 30; // rule of thumb ;-)
TQCString cmd;
cmd.sprintf("NEWGROUPS %.2d%.2d%.2d 000000",target->fetchSince.year()%100,target->fetchSince.month(),target->fetchSince.day());
if (!sendCommandWCheck(cmd,231)) // 231 list of new newsgroups follows
return;
char *s, *line;
TQString name;
KNGroup::tqStatus status;
TQSortedList<KNGroupInfo> tmpList;
tmpList.setAutoDelete(true);
while (getNextLine()) {
line = getCurrentLine();
if (line[0]=='.') {
if (line[1]=='.')
line++; // collapse double period into one
else
if (line[1]==0)
break; // message complete
}
s = strchr(line,' ');
if(!s) {
#ifndef NDEBUG
qDebug("knode: retrieved broken group-line - ignoring");
#endif
} else {
s[0] = 0; // cut string
name = TQString::fromUtf8(line);
while (s[1]!=0) s++; // the last character determines the moderation status
switch (s[0]) {
case 'n' : status = KNGroup::readOnly;
break;
case 'y' : status = KNGroup::postingAllowed;
break;
case 'm' : status = KNGroup::moderated;
break;
default : status = KNGroup::unknown;
}
tmpList.append(new KNGroupInfo(name,TQString(),true,false,status));
}
doneLines++;
}
if (!job->success() || job->canceled())
return; // stopped...
if (target->getDescriptions) {
errorPrefix = i18n("The group descriptions could not be retrieved.\nThe following error occurred:\n");
progressValue = 100;
doneLines = 0;
predictedLines = tmpList.count()*3;
sendSignal(TSdownloadDesc);
sendSignal(TSprogressUpdate);
cmd = "LIST NEWSGROUPS ";
TQStrList desList;
char *s;
int rep;
for (KNGroupInfo *group=tmpList.first(); group; group=tmpList.next()) {
if (!sendCommand(cmd+group->name.utf8(),rep))
return;
if (rep != 215) // 215 informations follows
break;
desList.clear();
if (!getMsg(desList))
return;
if (desList.count()>0) { // group has a description
s = desList.first();
while (*s !=- '\0' && *s != '\t' && *s != ' ') s++;
if (*s == '\0') {
#ifndef NDEBUG
qDebug("knode: retrieved broken group-description - ignoring");
#endif
} else {
while (*s == ' ' || *s == '\t') s++; // go on to the description
if (target->codecForDescriptions) // some countries use local 8 bit characters in the tag line
group->description = target->codecForDescriptions->toUnicode(s);
else
group->description = TQString::fromLocal8Bit(s);
}
}
}
}
sendSignal(TSloadGrouplist);
if (!target->readIn()) {
job->setErrorString(i18n("Unable to read the group list file"));
return;
}
target->merge(&tmpList);
sendSignal(TSwriteGrouplist);
if (!target->writeOut()) {
job->setErrorString(i18n("Unable to write the group list file"));
return;
}
}
void KNNntpClient::doFetchNewHeaders()
{
KNGroup* target=static_cast<KNGroup*>(job->data());
char* s;
int first=0, last=0, oldlast=0, toFetch=0, rep=0;
TQCString cmd;
target->setLastFetchCount(0);
sendSignal(TSdownloadNew);
errorPrefix=i18n("No new articles could be retrieved for\n%1/%2.\nThe following error occurred:\n")
.tqarg(account.server()).tqarg(target->groupname());
cmd="GROUP ";
cmd+=target->groupname().utf8();
if (!sendCommandWCheck(cmd,211)) { // 211 n f l s group selected
return;
}
currentGroup = target->groupname();
progressValue = 90;
s = strchr(getCurrentLine(),' ');
if (s) {
s++;
s = strchr(s,' ');
}
if (s) {
s++;
first=atoi(s);
target->setFirstNr(first);
s = strchr(s,' ');
}
if (s) {
last=atoi(s);
} else {
TQString tmp=i18n("No new articles could be retrieved.\nThe server sent a malformatted response:\n");
tmp+=getCurrentLine();
job->setErrorString(tmp);
closeConnection();
return;
}
if(target->lastNr()==0) { //first fetch
if(first>0)
oldlast=first-1;
else
oldlast=first;
} else
oldlast=target->lastNr();
toFetch=last-oldlast;
//qDebug("knode: last %d oldlast %d toFetch %d\n",last,oldlast,toFetch);
if(toFetch<=0) {
//qDebug("knode: No new Articles in group\n");
target->setLastNr(last); // don't get stuck when the article numbers wrap
return;
}
if(toFetch>target->maxFetch()) {
toFetch=target->maxFetch();
//qDebug("knode: Fetching only %d articles\n",toFetch);
}
progressValue = 100;
predictedLines = toFetch;
// get list of additional headers provided by the XOVER command
// see RFC 2980 section 2.1.7
TQStrList headerformat;
cmd = "LIST OVERVIEW.FMT";
if ( sendCommand( cmd, rep ) && rep == 215 ) {
TQStrList tmp;
if (getMsg(tmp)) {
for(TQCString s = tmp.first(); !s.isNull(); s = tmp.next()) {
s = s.stripWhiteSpace();
// remove the mandatory xover header
if (s == "Subject:" || s == "From:" || s == "Date:" || s == "Message-ID:"
|| s == "References:" || s == "Bytes:" || s == "Lines:")
continue;
else
headerformat.append(s);
}
}
}
//qDebug("knode: KNNntpClient::doFetchNewHeaders() : xover %d-%d", last-toFetch+1, last);
cmd.sprintf("xover %d-%d",last-toFetch+1,last);
if (!sendCommand(cmd,rep))
return;
// no articles in selected range...
if (rep==420) { // 420 No article(s) selected
target->setLastNr(last);
return;
} else if (rep!=224) { // 224 success
handleErrors();
return;
}
TQStrList headers;
if (!getMsg(headers)) {
return;
}
progressValue = 1000;
sendSignal(TSprogressUpdate);
sendSignal(TSsortNew);
mutex.lock();
target->insortNewHeaders(&headers, &headerformat, this);
target->setLastNr(last);
mutex.unlock();
}
void KNNntpClient::doFetchArticle()
{
KNRemoteArticle *target = static_cast<KNRemoteArticle*>(job->data());
TQCString cmd;
sendSignal(TSdownloadArticle);
errorPrefix = i18n("Article could not be retrieved.\nThe following error occurred:\n");
progressValue = 100;
predictedLines = target->lines()->numberOfLines()+10;
if (target->collection()) {
TQString groupName = static_cast<KNGroup*>(target->collection())->groupname();
if (currentGroup != groupName) {
cmd="GROUP ";
cmd+=groupName.utf8();
if (!sendCommandWCheck(cmd,211)) // 211 n f l s group selected
return;
currentGroup = groupName;
}
}
if (target->articleNumber() != -1) {
cmd.setNum(target->articleNumber());
cmd.prepend("ARTICLE ");
} else {
cmd = "ARTICLE " + target->messageID()->as7BitString(false);
}
if (!sendCommandWCheck(cmd,220)) { // 220 n <a> article retrieved - head and body follow
int code = atoi(getCurrentLine());
if ( code == 430 || code == 423 ) { // 430 no such article found || 423 no such article number in this group
TQString msgId = target->messageID()->as7BitString( false );
// strip of '<' and '>'
msgId = msgId.mid( 1, msgId.length() - 2 );
job->setErrorString( errorPrefix + getCurrentLine() +
i18n("<br><br>The article you requested is not available on your news server."
"<br>You could try to get it from <a href=\"http://groups.google.com/groups?selm=%1\">groups.google.com</a>.")
.tqarg( msgId ) );
}
return;
}
TQStrList msg;
if (!getMsg(msg))
return;
progressValue = 1000;
sendSignal(TSprogressUpdate);
target->setContent(&msg);
target->parse();
}
void KNNntpClient::doPostArticle()
{
KNLocalArticle *art=static_cast<KNLocalArticle*>(job->data());
sendSignal(TSsendArticle);
if (art->messageID(false)!=0) {
int rep;
if (!sendCommand(TQCString("STAT ")+art->messageID(false)->as7BitString(false),rep))
return;
if (rep==223) { // 223 n <a> article retrieved - request text separately
#ifndef NDEBUG
qDebug("knode: STAT successful, we have probably already sent this article.");
#endif
return; // the article is already on the server, lets put it silently into the send folder
}
}
if(!sendCommandWCheck("POST", 340)) // 340 send article to be posted. End with <CR-LF>.<CR-LF>
return;
if (art->messageID(false)==0) { // article has no message ID => search for a ID in the response
TQCString s = getCurrentLine();
int start = s.tqfindRev(TQRegExp("<[^\\s]*@[^\\s]*>"));
if (start != -1) { // post response includes a recommended id
int end = s.tqfind('>',start);
art->messageID()->from7BitString(s.mid(start,end-start+1));
art->assemble();
#ifndef NDEBUG
qDebug("knode: using the message-id recommended by the server: %s",s.mid(start,end-start+1).data());
#endif
}
}
if (!sendMsg(art->encodedContent(true)))
return;
if (!checkNextResponse(240)) // 240 article posted ok
return;
}
void KNNntpClient::doFetchSource()
{
KNRemoteArticle *target = static_cast<KNRemoteArticle*>(job->data());
sendSignal(TSdownloadArticle);
errorPrefix = i18n("Article could not be retrieved.\nThe following error occurred:\n");
progressValue = 100;
predictedLines = target->lines()->numberOfLines()+10;
TQCString cmd = "ARTICLE " + target->messageID()->as7BitString(false);
if (!sendCommandWCheck(cmd,220)) // 220 n <a> article retrieved - head and body follow
return;
TQStrList msg;
if (!getMsg(msg))
return;
progressValue = 1000;
sendSignal(TSprogressUpdate);
target->setContent(&msg);
}
bool KNNntpClient::openConnection()
{
currentGroup = TQString();
TQString oldPrefix = errorPrefix;
errorPrefix=i18n("Unable to connect.\nThe following error occurred:\n");
if (!KNProtocolClient::openConnection())
return false;
progressValue = 30;
int rep;
if (!getNextResponse(rep))
return false;
if ( ( rep < 200 ) || ( rep > 299 ) ) { // RFC977: 2xx - Command ok
handleErrors();
return false;
}
progressValue = 50;
if (!sendCommand("MODE READER",rep))
return false;
if (rep==500) {
#ifndef NDEBUG
qDebug("knode: \"MODE READER\" command not recognized.");
#endif
} else
if ( ( rep < 200 ) || ( rep > 299 ) ) { // RFC977: 2xx - Command ok
handleErrors();
return false;
}
progressValue = 60;
// logon now, some newsserver send a incomplete group list otherwise
if (account.needsLogon() && !account.user().isEmpty()) {
//qDebug("knode: user: %s",account.user().latin1());
TQCString command = "AUTHINFO USER ";
command += account.user().local8Bit();
if (!KNProtocolClient::sendCommand(command,rep))
return false;
if (rep==381) { // 381 PASS required
//qDebug("knode: Password required");
if (!account.pass().length()) {
job->setErrorString(i18n("Authentication failed.\nCheck your username and password."));
job->setAuthError(true);
return false;
}
//qDebug("knode: pass: %s",account.pass().latin1());
command = "AUTHINFO PASS ";
command += account.pass().local8Bit();
if (!KNProtocolClient::sendCommand(command,rep))
return false;
if (rep==281) { // 281 authorization success
#ifndef NDEBUG
qDebug("knode: Authorization successful");
#endif
} else {
#ifndef NDEBUG
qDebug("knode: Authorization failed");
#endif
job->setErrorString(i18n("Authentication failed.\nCheck your username and password.\n\n%1").tqarg(getCurrentLine()));
job->setAuthError(true);
closeConnection();
return false;
}
} else {
if (rep==281) { // 281 authorization success
#ifndef NDEBUG
qDebug("knode: Authorization successful");
#endif
} else {
if ((rep==482)||(rep==500)) { //482 Authentication rejected
#ifndef NDEBUG
qDebug("knode: Authorization failed"); // we don't care, the server can refuse the info
#endif
} else {
handleErrors();
return false;
}
}
}
}
progressValue = 70;
errorPrefix = oldPrefix;
return true;
}
// authentication on demand
bool KNNntpClient::sendCommand(const TQCString &cmd, int &rep)
{
if (!KNProtocolClient::sendCommand(cmd,rep))
return false;
if (rep==480) { // 480 requesting authorization
//qDebug("knode: Authorization requested");
if (!account.user().length()) {
job->setErrorString(i18n("Authentication failed.\nCheck your username and password."));
job->setAuthError(true);
closeConnection();
return false;
}
//qDebug("knode: user: %s",account.user().data());
TQCString command = "AUTHINFO USER ";
command += account.user().local8Bit();
if (!KNProtocolClient::sendCommand(command,rep))
return false;
if (rep==381) { // 381 PASS required
//qDebug("knode: Password required");
if (!account.pass().length()) {
job->setErrorString(i18n("Authentication failed.\nCheck your username and password.\n\n%1").tqarg(getCurrentLine()));
job->setAuthError(true);
closeConnection();
return false;
}
//qDebug("knode: pass: %s",account.pass().data());
command = "AUTHINFO PASS ";
command += account.pass().local8Bit();
if (!KNProtocolClient::sendCommand(command,rep))
return false;
}
if (rep==281) { // 281 authorization success
#ifndef NDEBUG
qDebug("knode: Authorization successful");
#endif
if (!KNProtocolClient::sendCommand(cmd,rep)) // retry the original command
return false;
} else {
job->setErrorString(i18n("Authentication failed.\nCheck your username and password.\n\n%1").tqarg(getCurrentLine()));
job->setAuthError(true);
closeConnection();
return false;
}
}
return true;
}
void KNNntpClient::handleErrors()
{
if (errorPrefix.isEmpty())
job->setErrorString(i18n("An error occurred:\n%1").tqarg(getCurrentLine()));
else
job->setErrorString(errorPrefix + getCurrentLine());
int code = atoi(getCurrentLine());
// close the connection only when necessary:
// 430 no such article found
// 411 no such news group
// 423 no such article number in this group
if ((code != 430)&&(code != 411)&&(code != 423))
closeConnection();
}
//--------------------------------
|