home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
b
/
bmh02src.zip
/
NNTP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-18
|
7KB
|
282 lines
/*
nntp.c : Paul Healy, EI9GL.
920816 : Added this header, bmh version.
Todo:
. check that the date is ok for nntp
. generally check file writes for success
* NNTP Server/Client - See RFC977
* Jeffrey R. Comstock. - NR0D - Bloomington, Minnesota USA
* Copyright 1990 Jeffrey R. Comstock, All Rights Reserved.
* Permission granted for non-commercial copying and use, provided
* this notice is retained.
*
* DB3FL 9107xx: heavily rewritten and bug fixing in file-handling
*
* ported to NOS by PE1NMB - 920120
*
* Mods by PA0GRI and WG7J
*/
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <io.h>
#include <dir.h>
#include "mailer.h"
#include "current.h"
#include "nntp.h"
#include "send.h"
#include "lock.h"
#include "rc.h"
#include "smtp.h"
#include "misc.h"
static int
makedir(char *path)
{
if (path == NULL)
return -1;
if (access(path, 0) == -1) {
fprintf(stderr, "bmh: making directory %s\n", path);
if (mkdir(path) == -1) {
perror(path);
return -1;
}
}
return 0;
}
/*
* creating path to a new newsgroup
*/
int
makepath(char *path)
{
char *cp = path;
if (*cp == '/')
cp++;
while (1) {
cp = strchr(cp, '/');
if ( cp == NULL )
return makedir(path);
else
*cp = '\0';
if (makedir(path) == -1)
return -1;
if (cp != NULL) {
*cp = '/';
cp++;
}
}
}
static int
addpath(char *group)
{
char path[256], *cp = path;
FILE *pointerfp = fopenlocked(getrc(newsdir), "pointer", "", "a");
int ret;
sprintf(path, "%s/%s", getrc(newsdir), group);
while (*cp) {
if (*cp == '.')
*cp = '/';
cp++;
}
ret = makepath(path);
if (pointerfp == NULL) {
fprintf(stderr, "nntp: can't open pointer file\n");
return -1;
}
fprintf(pointerfp, "%s %s\n", group, path);
fcloselocked(pointerfp, getrc(newsdir), "pointer");
return ret;
}
static int
addhistory(char *id, char *group, long num)
{
FILE *fp = fopenlocked(getrc(newsdir), "history", "", "a");
struct tm *stm;
long currtime;
if (fp == NULL) {
fprintf(stderr, "nntp: error opening %s/history\n", getrc(newsdir));
return -1;
}
time(&currtime);
stm = localtime(&currtime);
fprintf(fp, "%s %2.2d%2.2d%2.2d %2.2d%2.2d%2.2d %s/%d\n",
id,
stm->tm_year, stm->tm_mon+1, stm->tm_mday,
stm->tm_hour, stm->tm_min, stm->tm_sec,
group, num);
fcloselocked(fp, getrc(newsdir), "history");
return 0;
}
static char *
getpath(char *group, char *path, int maxpath)
{
FILE *fp = fopenlocked(getrc(newsdir), "pointer", "", "r");
int grplen = strlen(group);
char *result = path, tmp[256];
sprintf(path, "%s/%s", getrc(newsdir), news2dir(strcpy(tmp, group)));
if (fp == NULL)
return result;
while (fgets(path, maxpath, fp) != NULL)
if (strncmp(group, path, grplen) == 0) {
rip(path);
result = &path[grplen+1];
break;
}
fcloselocked(fp, getrc(newsdir), "pointer");
return result;
}
static long
updateactivelist(char *group)
{
FILE *activefp = fopenlocked(getrc(newsdir), "active", "", "r+"), *tmpfp;
long number = 0;
char buf[512], filename[256];
int grplen = strlen(group);
if ( (activefp == NULL) &&
( (activefp = fopenlocked(getrc(newsdir), "active", "", "w+")) == NULL)
) {
fprintf(stderr, "post: can't open active file\n");
return -1L;
}
if ((tmpfp = tempfile("post", filename, "w")) == NULL) {
fcloselocked(activefp, getrc(newsdir), "active");
fprintf(stderr, "post: can't open temporary file\n");
return -1;
}
while (fgets(buf, sizeof(buf), activefp) != NULL)
if (strncmp(group, buf, grplen) == 0) {
long low;
char posting, moderated = ' ';
sscanf(&buf[grplen+1], "%ld %ld %c %c",
&number, &low, &posting, &moderated);
fprintf(tmpfp,"%s %5.5ld %5.5ld %c %c\n",
group, ++number, low, posting, moderated);
}
else
fputs(buf, tmpfp);
if (number == 0) {
addpath(group);
number = 1;
fprintf(tmpfp, "%s 00001 00001 y\n", group);
}
rewind(tmpfp);
rewind(activefp);
dupfile(tmpfp, activefp);
fcloselocked(activefp, getrc(newsdir), "active");
fclose(tmpfp);
unlink(filename);
return number;
}
static int
xferart(FILE *fpin, char *from, char *group, char *msgid)
{
long num;
if ((num = updateactivelist(group)) > 0L) {
char Num[20], pathname[256], *path;
FILE *fp;
sprintf(Num, "%ld", num);
if ( (fp = fopenlocked(path = getpath(group, pathname, sizeof(pathname)),
Num, "", "w")) == NULL) {
fprintf(stderr, "post: can't open %s/%s\n", path, Num);
return -1;
}
dupfile(fpin, fp);
fcloselocked(fp, path, Num);
addhistory(msgid, group, num);
return 0;
}
else {
fprintf(stderr, "post: can't update active list\n");
return -1;
}
}
int
post(FILE *fpin, char *newsgroup, char *subject)
{
char msgid[256], filename[256], from[256];
int ret = -1;
FILE *fp;
if ( makepath(getrc(newsdir)) == -1 )
return -1;
sprintf(msgid, "<%ld@%s>", get_msgid(getrc(mqueue)), getrc(hostname));
sprintf(from, "%s@%s", getrc(username), getrc(hostname));
if ((fp = tempfile("post", filename, "w")) == NULL)
return -1;
fprintf(fp, "Path: %s\n", getrc(username) );
fprintf(fp, "Sender: nntp@%s\n", getrc(hostname));
fprintf(fp, "Newsgroup: %s\n", newsgroup);
fprintf(fp, "Date: %s", ptime(time(NULL)));
fprintf(fp, "Msgid: %s\n", msgid);
fprintf(fp, "From: %s", from);
if (getrc(fullname) != NULL)
fprintf(fp, " (%s)", getrc(fullname) );
fputc('\n', fp);
fprintf(fp, "Subject: %s\n", subject);
if (getrc(replyto) != NULL)
fprintf(fp, "Reply-To: %s\n", getrc(replyto));
if (getrc(organ) != NULL)
fprintf(fp,"Organization: %s\n", getrc(organ));
fputc('\n', fp);
dupfile(fpin, fp);
rewind(fp);
ret = xferart(fp, from, newsgroup, msgid);
fclose(fp);
unlink(filename);
if (ret == -1)
fprintf(stderr, "post: failed to send article\n");
return ret;
}