📄 nntpcli.c
字号:
start_timer(&np->nntpcli_t);
return;
}
}
fsocket.sin_addr.s_addr = resolve(np->name);
if(fsocket.sin_addr.s_addr == 0) { /* No IP address found */
if (nntptrace >= 2)
printf("NNTP can't resolve host '%s'\n", np->name);
/* Try again later */
start_timer(&np->nntpcli_t);
return;
}
fsocket.sin_family = AF_INET;
fsocket.sin_port = IPPORT_NNTP;
s = socket(AF_INET,SOCK_STREAM,0);
if(connect(s,(struct sockaddr *)&fsocket,SOCKSIZE) == -1){
cp = sockerr(s);
logmsg(s,"NNTP %s Connect failed: %s",psocket(&fsocket),
cp != NULL ? cp : "");
if (nntptrace >= 2)
printf("NNTP %s Connect failed: %s\n",psocket(&fsocket),
cp != NULL ? cp : "");
goto quit;
}
network = fdopen(s,"r+t");
/* Eat the banner */
i = getreply(network);
if(i == -1 || i >= 400) {
logmsg(fileno(network),"NNTP %s bad reply on banner (response was %d)",psocket(&fsocket),i);
if (nntptrace >= 1)
printf("NNTP %s bad reply on banner (response was %d)\n",psocket(&fsocket),i);
goto quit;
}
if (mlock(Newsdir, "nntp")) {
if (nntptrace >= 2)
printf("NNTP %s Connect failed: cannot lock nntp.dat\n", psocket(&fsocket));
goto quit;
}
sprintf(buf,"%s/nntp.dat",Newsdir);
if((fp = fopen(buf,APPEND_TEXT)) == NULL) {
logmsg(fileno(network),"NNTP %s Connect failed: Cannot open %s",psocket(&fsocket),
buf);
if (nntptrace >= 1)
printf("NNTP %s Connect failed: Cannot open %s\n",psocket(&fsocket), buf);
rmlock(Newsdir, "nntp");
goto quit;
}
rewind(fp);
/* for(pos=0L; fgets(buf,NNTPMAXLEN,fp) != NULL;pos=ftell(fp)) { */
for(; fgets(buf,NNTPMAXLEN,fp) != NULL;) {
if((cp = strchr(buf,' ')) == NULL)
continue; /* something wrong with this line, skip it */
*cp = '\0';
if(stricmp(buf,np->name) == 0) {
rip(cp+1);
lastdate = strdup(cp+1);
break;
}
}
fclose(fp);
rmlock(Newsdir, "nntp");
if(lastdate == NULL)
lastdate = strdup("700101 000000");
/* snapshot the time for use later in re-writing nntp.dat */
time(&t);
ltm = localtime(&t);
/* Get a list of new message-id's */
if (np->groups) {
if (nntptrace >= 3)
printf("==>NEWNEWS %s %s\n", np->groups, lastdate);
fprintf(network,"NEWNEWS %s %s\n", np->groups, lastdate);
} else {
if (nntptrace >= 3)
printf("==>NEWNEWS %s %s\n", Nntpgroups != NULL ? Nntpgroups : "*", lastdate);
fprintf(network,"NEWNEWS %s %s\n",Nntpgroups != NULL ? Nntpgroups : "*", lastdate);
}
free(lastdate);
/* Get the response */
if((i = getreply(network)) != 230) { /* protocol error */
logmsg(fileno(network),"NNTP %s protocol error (response was %d)",psocket(&fsocket),i);
if (nntptrace >= 1)
printf("NNTP %s protocol error (response was %d)\n",psocket(&fsocket),i);
goto quit;
}
if((tmpf = tmpfile()) == NULL) {
if (nntptrace >= 1)
printf("NNTP %s Cannot open temp file\n", psocket(&fsocket));
goto quit;
}
if(gettxt(network,tmpf) == -1) {
logmsg(fileno(network), "NNTP %s giving up: gettxt() failure",psocket(&fsocket));
if (nntptrace >= 1)
printf("NNTP %s giving up: gettxt() failure\n",psocket(&fsocket));
fclose(tmpf);
goto quit;
}
/* Open the history file */
if (mlock(Newsdir, "history")) {
if (nntptrace >= 1)
printf("NNTP %s giving up: couldn't lock history file\n", psocket(&fsocket));
fclose(tmpf);
goto quit;
}
sprintf(buf,"%s/history",Newsdir);
if((fp = fopen(buf,APPEND_TEXT)) == NULL) {
logmsg(fileno(network),"NNTP %s Connect failed: Cannot open %s",psocket(&fsocket), buf);
if (nntptrace >= 1)
printf("NNTP %s Connect failed: Cannot open %s\n",psocket(&fsocket), buf);
fclose(tmpf);
goto quit;
}
/* search through the history file for matching message id's */
rewind(tmpf);
while(fgets(tbuf,NNTPMAXLEN,tmpf) != NULL) {
i = 0;
rewind(fp);
while(fgets(buf,NNTPMAXLEN,fp) != NULL) {
if(stricmp(buf,tbuf) == 0) {
i = 1;
break;
}
kwait(NULL);
}
if(i == 0) { /* not found, get the article */
if(getarticle(network,tbuf) == -1) {
logmsg(fileno(network),"NNTP %s Giving up: could not get article",psocket(&fsocket));
if (nntptrace >= 2)
printf("NNTP %s Giving up: could not get article\n",psocket(&fsocket));
fclose(fp);
rmlock(Newsdir, "history");
fclose(tmpf);
goto quit;
}
fprintf(fp,"%s",tbuf); /* add the new message id */
}
}
fclose(fp);
rmlock(Newsdir, "history");
fclose(tmpf);
if (nntptrace >= 3)
printf("==>QUIT\n");
fprintf(network,"QUIT\n");
/* Eat the response */
getreply(network);
/* NOW, update the nntp.dat file */
if (mlock(Newsdir, "nntp")) {
if (nntptrace >= 2)
printf("NNTP %s Could not lock nntp.dat for update\n", psocket(&fsocket));
goto quit;
}
sprintf(buf,"%s/nntp.dat",Newsdir);
fp = fopen(buf,READ_TEXT);
sprintf(buf, "%s/nntp.tmp",Newsdir);
if ((tmpf = fopen(buf, WRITE_TEXT)) == NULL)
if (nntptrace >= 1)
printf("NNTP %s Cannot create temp file '%s'\n", psocket(&fsocket), buf);
if (fp == NULL || tmpf == NULL) {
logmsg(fileno(network),"NNTP %s Could not update %s", psocket(&fsocket), buf);
if (nntptrace >= 2)
printf("NNTP %s Could not update %s\n",psocket(&fsocket), buf);
if (fp)
fclose(fp);
if (tmpf)
fclose(tmpf);
rmlock(Newsdir, "nntp");
goto quit;
}
while (fgets(tbuf, sizeof(tbuf), fp))
if (strnicmp(tbuf, np->name, strlen(np->name)))
fputs(tbuf, tmpf);
fprintf(tmpf,"%s %02d%02d%02d %02d%02d%02d\n",np->name,ltm->tm_year%100,ltm->tm_mon+1,
ltm->tm_mday,ltm->tm_hour,ltm->tm_min,ltm->tm_sec);
fclose(fp);
fclose(tmpf);
sprintf(buf, "%s/nntp.dat", Newsdir);
sprintf(tbuf, "%s/nntp.tmp", Newsdir);
unlink(buf);
rename(tbuf, buf);
rmlock(Newsdir, "nntp");
quit:
if (nntptrace >= 3)
printf("NNTP daemon exiting\n");
fclose(network);
/* Restart timer */
start_timer(&np->nntpcli_t);
return;
}
static int
gettxt(network,fp)
FILE *network;
FILE *fp;
{
char buf[NNTPMAXLEN];
int nlines;
for (nlines = 0; fgets(buf,NNTPMAXLEN,network) != NULL; ++nlines) {
if (nntptrace >= 4)
printf("<==%s", buf);
if(strcmp(buf,".\n") == 0) {
if (nntptrace >= 3)
printf("NNTP received %d lines\n", nlines);
return 0;
}
/* check for escaped '.' characters */
if(strcmp(buf,"..\n") == 0)
fputs(".\n",fp);
else
fputs(buf,fp);
}
if (nntptrace >= 1)
printf("NNTP receive error after %d lines\n", nlines);
return -1;
}
static int
getreply(network)
FILE *network;
{
char buf[NNTPMAXLEN];
int response;
while(fgets(buf,NNTPMAXLEN,network) != NULL) {
/* skip informative messages and blank lines */
if(buf[0] == '\0' || buf[0] == '1')
continue;
sscanf(buf,"%d",&response);
if (nntptrace >= 3)
printf("<==%s\n", buf);
return response;
}
if (nntptrace >= 3)
printf("==No response\n");
return -1;
}
static int
getarticle(network,msgid)
FILE *network;
char *msgid;
{
char buf[NNTPMAXLEN], froml[NNTPMAXLEN], newgl[NNTPMAXLEN];
FILE *fp, *tmpf;
int r;
char *cp;
extern int Smtpquiet;
if (nntptrace >= 3)
printf("==>ARTICLE %s", msgid);
fprintf(network,"ARTICLE %s", msgid);
r = getreply(network);
if(r == -1 || r >= 500)
return -1;
if(r >= 400)
return 0;
if((tmpf = tmpfile()) == NULL) {
if (nntptrace >= 1)
printf("NNTP Cannot open temp file for article\n");
return -1;
}
if(gettxt(network,tmpf) == -1) {
fclose(tmpf);
return -1;
}
/* convert the article into mail format */
rewind(tmpf);
froml[0] = '\0';
newgl[0] = '\0';
while(fgets(buf,NNTPMAXLEN,tmpf) != NULL) {
if(strncmp(buf,"From: ",6) == 0) {
struct timeb t;
ftime(&t);
rip(&buf[6]);
sprintf(froml,"From %s %ld\n",&buf[6], t.time);
if(newgl[0] != '\0')
break;
}
if(strncmp(buf,"Newsgroups: ",12) == 0) {
strcpy(newgl,&buf[12]);
if(froml[0] != '\0')
break;
}
/* invalid article - missing 'From:' line or 'Newsgroups:' line */
if(strcmp(buf,"\n") == 0 && (froml[0] == '\0' || newgl[0] == '\0')) {
/* fclose(fp); */
fclose(tmpf);
return 0;
}
}
sprintf(buf,"%s/",News_spool ? News_spool : Mailspool);
for(cp=newgl;;++cp) {
if(*cp == '.') {
#ifdef __TURBOC__
mkdir(buf); /* create a subdirectory, if necessary */
#else
mkdir(buf,0755); /* create a subdirectory, if necessary */
#endif
strcat(buf,"/");
continue;
}
if(*cp == ',' || *cp == '\n') {
char tempdir[80], prefix[20], *p;
strcpy(tempdir, buf);
if ((p = strrchr(tempdir, '/')) != NULL) {
*p++ = '\0';
strcpy(prefix, p);
}
if (mlock(tempdir, prefix)) {
if (nntptrace >= 2)
printf("NNTP group '%s' is locked\n", buf);
return -1;
}
strcat(buf,".txt");
/* open the mail file */
if (nntptrace >= 3)
printf("Writing article to '%s'\n", buf);
if((fp = fopen(buf,APPEND_TEXT)) != NULL) {
fputs(froml,fp);
rewind(tmpf);
while(fgets(buf,NNTPMAXLEN,tmpf) != NULL) {
/* for UNIX mail compatiblity */
if(strncmp(buf,"From ",5) == 0)
putc('>',fp);
fputs(buf,fp);
}
putc('\n',fp);
fclose(fp);
}
rmlock(tempdir, prefix);
if (*cp == '\n')
break;
else
sprintf(buf,"%s/",News_spool ? News_spool : Mailspool);
continue;
}
buf[strlen(buf)+1] = '\0';
buf[strlen(buf)] = strchr(validchars, tolower(*cp)) ? *cp : '_';
}
fclose(tmpf);
strcpy(buf,msgid); /* Get a copy we can munge */
rip(buf); /* remove trailing new-line */
rip(newgl); /* ditto */
#ifdef notdef
printf("New news arrived: %s, article %s%c\n",newgl,buf,Smtpquiet?' ':'\007');
#else
printf("New news arrived: %s, article %s\n",newgl,buf);
#endif
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -