home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC15B17.ZIP
/
NEWS.C
< prev
next >
Wrap
Text File
|
1997-03-22
|
41KB
|
1,473 lines
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <process.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <dos.h>
#include <share.h>
#include <dir.h>
#include <alloc.h>
#include <sys\stat.h>
#include <math.h>
#include "tcp.h"
#include "pop.h"
#include "net.h"
#include "vardec.h"
#include "version.h"
extern unsigned _stklen = 16384;
#define NGROUPS 100
#define BIGSTR 4096
#define STRING 512
#define STR 129
#define WAIT_TIME 10
#define TRIES 100
#define NUL '\0'
#define LAST(s) s[strlen(s)-1]
#define NNTP_PORT 119
#define free_Mail_Socket(SOCK) if (SOCK!=NULL) { \
farfree(SOCK->sock); farfree(SOCK); SOCK=NULL; }
tcp_Socket nntpsock;
configrec syscfg;
subboardrec *subboards;
static int NNTP_stat;
unsigned short reply;
unsigned long reparticle;
char serverhost[81],
newsrc[81],
fromline[121],
smtppath[81];
char maindir[181],
tmpdir[201],
net_data[181];
char *version = "Freeware PPP News Retrieval "VERSION;
char cur_gname[STRING],
tpktname[121];
char msgto[STRING],
msgarea[STRING];
unsigned long cur_numa,
cur_first,
cur_last;
unsigned long cur_article;
char cur_articleid[STRING];
char cur_path[STRING],
cur_from[STRING],
cur_subject[STRING],
cur_replyto[STRING];
char cur_newsgroups[STRING],
cur_message_ID[STRING],
cur_organization[STRING],
cur_references[STRING],
cur_lines[STR],
cur_date[STRING];
unsigned short sy;
int instance,
SOCK_DELAY;
typedef struct {
char groupname[60];
unsigned long lastread;
unsigned short subtype;
} GROUPFILEREC;
GROUPFILEREC *grouprec;
int ngroups;
int bogus = 0;
#define SOCK_READ_ERR(SOCK) sock_err: \
switch (NNTP_stat) { \
case 1 : \
write_groups(1); \
log_it("\n ! Connection closed : %s", sockerr(NNTP_sock->sock)); \
free_Mail_Socket(SOCK); \
fcloseall(); \
cursor('R'); \
exit(EXIT_FAILURE); \
case -1: \
write_groups(1); \
log_it("\n ! Session error: %s", sockerr(NNTP_sock->sock)); \
free_Mail_Socket(SOCK); \
fcloseall(); \
cursor('R'); \
exit(EXIT_FAILURE); \
} \
char *texth[] = {"th", "st", "nd", "rd"};
char *ordinal_text(int number)
{
if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3)
number = 0;
return texth[number];
}
static unsigned cursize;
void cursor(int tmp)
{
union REGS inregs, outregs;
switch (toupper(tmp)) {
case 'S': /* Save */
inregs.h.ah = 3;
inregs.h.bh = 0;
int86(0x10, &inregs, &outregs);
cursize = outregs.x.cx;
break;
case 'R': /* Restore */
inregs.h.ah = 1;
inregs.x.cx = cursize;
int86(0x10, &inregs, &outregs);
break;
case 'H': /* Hide */
inregs.h.ah = 1;
inregs.h.ch = 0x20;
int86(0x10, &inregs, &outregs);
break;
case 'N': /* Normal */
inregs.h.ah = 1;
inregs.h.ch = 6;
inregs.h.cl = 7;
int86(0x10, &inregs, &outregs);
break;
}
}
void backline(void)
{
int i;
fprintf(stderr, " ");
for (i = wherex(); i > 0; i--)
fprintf(stderr, "\b \b");
}
void cd_to(char *s)
{
char *s1;
int i, db;
s1 = s;
i = strlen(s1) - 1;
db = (s1[i] == '\\');
if (i == 0)
db = 0;
if ((i == 2) && (s1[1] == ':'))
db = 0;
if (db)
s1[i] = 0;
chdir(s1);
if (s[1] == ':')
setdisk(s[0] - 'A');
}
void get_dir(char *s, int be)
{
strcpy(s, "X:\\");
s[0] = 'A' + getdisk();
getcurdir(0, s + 3);
if (be) {
if (s[strlen(s) - 1] != '\\')
strcat(s, "\\");
}
}
int sh_open(char *path, int file_access, unsigned mode)
{
int handle, count, share;
char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (mode & S_IWRITE))
share=SH_DENYRW;
else
share=SH_DENYWR;
handle=open(path,file_access|share,mode);
if (handle<0) {
count=1;
fnsplit(path,drive,dir,file,ext);
if (access(path,0)!=-1) {
delay(WAIT_TIME);
handle=open(path,file_access|share,mode);
while (((handle<0) && (errno==EACCES)) && (count<TRIES)) {
delay(WAIT_TIME);
count++;
handle=open(path,file_access|share,mode);
}
}
}
return(handle);
}
int sh_open1(char *path, int access)
{
unsigned fmode;
fmode = 0;
if ((access & O_RDWR) || (access & O_WRONLY))
fmode |= S_IWRITE;
if ((access & O_RDWR) || (access & O_RDONLY))
fmode |= S_IREAD;
return (sh_open(path, access, fmode));
}
long sh_lseek(int handle, long offset, int fromwhere)
{
if (handle == -1) {
return (-1L);
}
return (lseek(handle, offset, fromwhere));
}
FILE *fsh_open(char *path, char *fmode)
{
FILE *f;
int count, share, md, fd;
char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
share = SH_DENYWR;
md = 0;
if (((char *) _fstrchr(fmode, 'w')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT | O_TRUNC;
} else
if (((char *) _fstrchr(fmode, 'a')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT;
} else {
md = O_RDONLY;
}
if (((char *) _fstrchr(fmode, 'b')) != NULL) {
md |= O_BINARY;
}
if (((char *) _fstrchr(fmode, '+')) != NULL) {
md &= ~O_RDONLY;
md |= O_RDWR;
share = SH_DENYRD;
}
fd = open(path, md | share, S_IREAD | S_IWRITE);
if (fd < 0) {
count = 1;
fnsplit(path, drive, dir, file, ext);
if ((access(path, 0)) != -1) {
delay(WAIT_TIME);
fd = open(path, md | share, S_IREAD | S_IWRITE);
while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
delay(WAIT_TIME);
count++;
fd = open(path, md | share, S_IREAD | S_IWRITE);
}
}
}
if (fd > 0) {
if (((char *) _fstrchr(fmode, 'a')) != NULL)
sh_lseek(fd, 0L, SEEK_END);
f = fdopen(fd, fmode);
if (!f) {
close(fd);
}
} else
f = 0;
return (f);
}
int sh_close(int f)
{
if (f != -1)
close(f);
return (-1);
}
int sh_read(int handle, void *buf, unsigned len)
{
if (handle == -1) {
return (-1);
}
return (read(handle, buf, len));
}
int log_it(char *fmt,...)
{
va_list v;
char fn[201];
FILE *fp;
sprintf(fn, "%s\\NEWS.LOG", net_data);
if ((fp = fsh_open(fn, "at")) == NULL)
return 1;
va_start(v, fmt);
vfprintf(fp, fmt, v);
vfprintf(stderr, fmt, v);
va_end(v);
fclose(fp);
return 0;
}
long count_lines(char *s)
{
FILE *fp;
char fn[201];
long int nl = 0;
const int NEWLINE = '\n';
int c;
strcpy(fn, s);
fp = fsh_open(fn, "rt");
if (fp == NULL) {
printf("\n ■ Cannot open %s", fn);
return 0;
}
while ((c = getc(fp)) != EOF) {
if (c == NEWLINE)
++nl;
}
fclose(fp);
return nl;
}
static unsigned char *trimstr1(unsigned char *s)
{
int i;
static char *whitespace = " \r\n\t";
i = strlen(s);
while ((i > 0) && (_fstrchr(whitespace, s[i - 1])))
--i;
while ((i > 0) && (_fstrchr(whitespace, *s))) {
memmove(s, s + 1, --i);
}
s[i] = 0;
return (s);
}
void usleep(void)
{
tcp_tick(NULL); /* occasionally process something */
}
int exist(char *s)
{
int i;
struct ffblk ff;
i = findfirst(s, &ff, 0);
if (i)
return (0);
else
return (1);
}
void rename_pend(char *dir, char *file)
{
char s[181], s1[181];
int i, ok;
sprintf(s, "%s\\%s", dir, file);
ok = 0;
for (i = 0; i < 1000 && !ok; i++) {
sprintf(s1, "%s\\P0-%u.NET", dir, i);
if (!exist(s1)) {
rename(s, s1);
ok = 1;
}
}
}
void check_packets(void)
{
char s[181];
int f1;
struct ffblk ff;
sprintf(s, "%s\\P0-*.%3.3d", net_data, instance);
f1 = findfirst(s, &ff, 0);
while (f1 == 0) {
rename_pend(net_data, ff.ff_name);
f1 = findnext(&ff);
}
}
void err_exit(int exitlevel, char *fmt,...)
{
va_list v;
va_start(v, fmt);
vfprintf(stderr, fmt, v);
va_end(v);
check_packets();
cursor('R');
exit(exitlevel);
}
void write_groups(int display)
{
int i;
FILE *groupfp;
groupfp = fsh_open(newsrc, "wt");
if (!groupfp) {
fprintf(stderr, "\n ■ Unable to open %s!", newsrc);
return;
}
else {
if (display)
fprintf(stderr, "\n ■ Updating pointers in %s...", newsrc);
}
for (i = 0; i < ngroups; i++) {
if ((grouprec[i].groupname[0]) && (stricmp(grouprec[i].groupname, "newsrc") != 0)) {
fprintf(groupfp, "%s %ld %hu\n", grouprec[i].groupname,
grouprec[i].lastread, grouprec[i].subtype);
}
}
fclose(groupfp);
}
void far *mallocx(unsigned long l)
{
void *x;
char huge *xx;
if (!l)
l = 1;
x = farmalloc(l);
if (!x) {
fprintf(stderr, "\n ■ Unable to allocate %lu bytes of memory", l);
cursor('R');
exit(EXIT_FAILURE);
}
xx = (char huge *) x;
while (l) {
if (l > 32768L) {
memset((void *) xx, 0, 32768L);
l -= 32768L;
xx += 32768L;
} else {
memset((void *) xx, 0, l);
break;
}
}
return (x);
}
void far *malloca(unsigned long nbytes)
{
void *buf;
buf = malloc(nbytes + 1);
if (buf == NULL)
fprintf(stderr, "\n ■ Unable to allocate sufficient memory (%ld bytes)", nbytes);
return(buf);
}
void nntp_shutdown(Mail_Socket *NNTP_sock)
{
sock_printf(NNTP_sock->sock, "QUIT");
sock_close(NNTP_sock->sock);
sock_wait_closed(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_err:
return;
}
void read_groups(void)
{
int i = 0, ok;
unsigned short sn;
char *ss, fn[101], tmp[101];
FILE *groupfp;
if (grouprec)
free((void *) grouprec);
grouprec = NULL;
ngroups = count_lines(newsrc);
if (!ngroups)
return;
grouprec = (GROUPFILEREC *) mallocx((ngroups+1) * sizeof(GROUPFILEREC));
if (!grouprec) {
ngroups=0;
fprintf(stderr, "\n ■ Unable to allocate memory for reading NEWS.RC!");
return;
}
groupfp = fsh_open(newsrc, "rt");
if (!groupfp) {
free((void *) grouprec);
ngroups=0;
return;
}
while (fgets(tmp, 120, groupfp)) {
if (tmp[0]) {
if (strncmpi(tmp, "newsrc", 6) == 0) {
strcpy(grouprec[i].groupname, "newsrc");
++i;
} else {
ss = strtok(tmp, " ");
strcpy(grouprec[i].groupname, ss);
ss = strtok(NULL, " ");
if (ss != NULL) {
grouprec[i].lastread = atol(ss);
ss = strtok(NULL, " \n");
if (ss != NULL) {
grouprec[i].subtype = atoi(ss);
++i;
}
}
}
}
}
ngroups=i;
fclose(groupfp);
for (i = 0; i < ngroups; i++) {
if (grouprec[i].subtype) {
sprintf(fn, "%s\\N%hu.NET", net_data, grouprec[i].subtype);
if (!exist(fn))
log_it("\n ■ N%u.NET does not exist in %s!", grouprec[i].subtype,
net_data);
else {
groupfp = fsh_open(fn, "rt");
ok = 0;
while ((fgets(tmp, 25, groupfp)) && !ok) {
sn = atoi(tmp);
if (sn == 32767)
ok = 1;
}
fclose(groupfp);
if (!ok)
log_it("\n ■ @32767 not list as a subscriber in N%d.NET!",
grouprec[i].subtype);
}
}
}
}
int cgroup(Mail_Socket *NNTP_sock, char *s)
{
static char _temp_buffer[STRING];
sock_printf(NNTP_sock->sock, "GROUP %s", s);
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
if (reply == 411)
return -1;
else
if (reply == 211) {
sscanf(_temp_buffer, "%hu %lu %lu %lu %s",
&reply, &cur_numa, &cur_first, &cur_last, &cur_gname);
return 0;
} else
log_it("\n ■ Unknown cgroup response : %s", _temp_buffer);
SOCK_READ_ERR(NNTP_sock);
return -1;
}
int cstat(Mail_Socket *NNTP_sock, unsigned long i)
{
char *p, *q;
static char _temp_buffer[STRING];
sock_printf(NNTP_sock->sock, "STAT %lu", i);
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
if (reply == 423)
return -1;
else
if (reply == 223) {
cur_article = reparticle;
p = _temp_buffer;
q = cur_articleid;
while ((p < _temp_buffer + STRING - 2) && (*p != '<'))
p++;
while ((p < _temp_buffer + STRING - 2) && (*p != '>'))
*q++ = *p++;
*q++ = '>';
*q++ = '\0';
return 0;
} else
log_it("\n ■ Unknown cstat error : %s", _temp_buffer);
SOCK_READ_ERR(NNTP_sock);
return -1;
}
int qexit(Mail_Socket *NNTP_sock, int n)
{
static char _temp_buffer[STR];
if (n >= 10)
log_it("\n ! Fatal NEWS error: check configuration!");
fflush(stdout);
sock_printf(NNTP_sock->sock, "QUIT");
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (*_temp_buffer != '2')
log_it("\n ■ Tried to quit host but failed");
cursor('R');
check_packets();
if (n) {
fcloseall();
free_Mail_Socket(NNTP_sock);
exit(n);
}
SOCK_READ_ERR(NNTP_sock);
return 0;
}
void treat(char *str)
{
char *obuf, *nbuf;
if (*str) {
for (obuf = str, nbuf = str; *obuf && obuf; ++obuf) {
if (((*obuf >= 32) && (*obuf <= 126)) || (*obuf == 10)) {
*nbuf++ = *obuf;
}
}
}
}
void name_packet(char *pktname)
{
int ok;
struct stat info;
unsigned i;
ok = 0;
for (i = 0; ((i < 1000) && (!ok)); i++) {
sprintf(pktname, "%s\\P0-%u.%3.3d", net_data, i, instance);
if (stat(pktname, &info) == -1)
ok = 1;
}
}
int savebody(Mail_Socket *NNTP_sock, int cug, int *abort)
{
char s1[21], s2[21];
unsigned int curpos, count, part;
char spin[26];
int i, place, len;
unsigned long msgsize;
struct stat statbuf;
FILE *tpktfp = NULL;
static char _temp_buffer[BIGSTR];
strcpy(spin, "||//--\\\\");
place = count = 0;
len = strlen(spin);
part = 1;
if (grouprec[cug].subtype == 0)
sprintf(tpktname, "%s\\NEWS%d.UUE", tmpdir, cug);
else {
sprintf(tpktname, "%s\\INPUT%d.MSG", tmpdir, part++);
if (stat(tpktname, &statbuf) == 0)
unlink(tpktname);
}
sock_printf(NNTP_sock->sock, "BODY %lu", cur_article);
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
if (reply == 423)
return -1;
else {
if (reply == 222) {
if (grouprec[cug].subtype) {
if ((tpktfp = fsh_open(tpktname, "wt+")) == NULL) {
log_it("\n ■ Unable to create temporary packet %s", tpktname);
qexit(NNTP_sock, 1);
}
} else {
if (tpktfp)
fclose(tpktfp);
if ((tpktfp = fsh_open(tpktname, "at+")) == NULL) {
log_it("\n ■ Unable to create temporary packet %s", tpktname);
qexit(NNTP_sock, 1);
} else {
fputs("\n", tpktfp);
for (i = 0; i < 79; i++)
fputs("-", tpktfp);
fputs("\n", tpktfp);
sprintf(_temp_buffer, "Art : %lu\n", cur_article);
fputs(_temp_buffer, tpktfp);
sprintf(_temp_buffer, "Date : %s\n", cur_date);
fputs(_temp_buffer, tpktfp);
sprintf(_temp_buffer, "From : %s\n", cur_replyto);
fputs(_temp_buffer, tpktfp);
sprintf(_temp_buffer, "Subj : %s\n\n", cur_subject);
fputs(_temp_buffer, tpktfp);
}
}
fnsplit(tpktname, NULL, NULL, s1, s2);
if (grouprec[cug].subtype)
fprintf(stderr, "\n ■ Receiving message, <Esc> aborts [-]");
else
fprintf(stderr, "\n ■ Appending to %s%s, <Esc> aborts [-]", s1, s2);
msgsize = 0L;
curpos = 0L;
while (1) {
if (kbhit()) {
if (getch() == 27) {
fprintf(stderr, "\r ■ Abort detected... please wait until message is completed [-]");
*abort = 1;
}
}
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0) {
break;
} else {
treat(_temp_buffer);
strcat(_temp_buffer, "\n");
curpos = strlen(_temp_buffer);
fputs(_temp_buffer, tpktfp);
msgsize += curpos;
}
if (count++ > 5) {
fprintf(stderr, "\b\b%c]", spin[place++]);
place %= len;
count = 0;
}
if ((msgsize > 30000L) && grouprec[cug].subtype) {
if (tpktfp)
fclose(tpktfp);
sprintf(tpktname, "%s\\INPUT%d.MSG", tmpdir, part++);
if (stat(tpktname, &statbuf) == 0)
unlink(tpktname);
if ((tpktfp = fsh_open(tpktname, "wt+")) == 0) {
log_it("\n ■ Unable to create temporary packet %s", tpktname);
qexit(NNTP_sock, 1);
}
backline();
fprintf(stderr, "\r ■ Breaking into %d%s part [ ]", (
part - 1), ordinal_text(part - 1));
msgsize = 0L;
}
}
if (tpktfp)
fclose(tpktfp);
fprintf(stderr, "\b\bX]");
return 1;
}
}
if (tpktfp)
fclose(tpktfp);
SOCK_READ_ERR(NNTP_sock);
return 0;
}
int extract(char *to, char *key, char *from)
{
if (!strnicmp(from, key, strlen(key))) {
from += strlen(key);
while (*from == ' ')
from++;
strcpy(to, from);
return 1;
} else
return 0;
}
int chead(Mail_Socket *NNTP_sock)
{
static char _temp_buffer[BIGSTR];
*cur_path = 0;
*cur_from = 0;
*cur_replyto = 0;
*cur_subject = 0;
*cur_newsgroups = 0;
*cur_message_ID = 0;
*cur_references = 0;
*cur_date = 0;
sock_printf(NNTP_sock->sock, "HEAD %lu", cur_article);
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
if (reply == 423)
return -1;
else
if (reply == 221)
while (1) {
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (strlen(_temp_buffer) > 1024)
_temp_buffer[1024] = 0;
if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0) {
if (cur_replyto[0] == 0)
strcpy(cur_replyto, cur_from);
return 0;
}
extract(cur_path, "Path:", _temp_buffer) ||
extract(cur_from, "From:", _temp_buffer) ||
extract(cur_subject, "Subject:", _temp_buffer) ||
extract(cur_replyto, "Reply-To:", _temp_buffer) ||
extract(cur_newsgroups, "Newsgroups:", _temp_buffer) ||
extract(cur_organization, "Organization:", _temp_buffer) ||
extract(cur_message_ID, "Message-ID:", _temp_buffer) ||
extract(cur_references, "References:", _temp_buffer) ||
extract(cur_lines, "Lines:", _temp_buffer) ||
extract(cur_date, "Date:", _temp_buffer);
} else {
log_it("\n ■ Unknown chead error : %s", _temp_buffer);
}
SOCK_READ_ERR(NNTP_sock);
return -1;
}
Mail_Socket *netsocket(char *host)
{
Mail_Socket *NNTP_sock = NULL;
longword h;
static char _temp_buffer[STR];
if (!(h = resolve(host)))
err_exit(EXIT_FAILURE, "\n ■ Could not resolve %s... aborting", host);
if ((NNTP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket))) == NULL)
err_exit(EXIT_FAILURE,"\n ■ Insufficient memory for socket... aborting.");
if ((NNTP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket))) == NULL) {
farfree(NNTP_sock);
err_exit(EXIT_FAILURE, "\n ■ Insufficient memory for socket... aborting.");
}
if (!tcp_open(NNTP_sock->sock, 0, h, NNTP_PORT, NULL)) {
free_Mail_Socket(NNTP_sock);
err_exit(EXIT_FAILURE, "\n ■ TCP socket open failed... aborting");
}
sock_mode(NNTP_sock->sock, TCP_MODE_ASCII);
sock_wait_established(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
switch (reply) {
case 200:
log_it("\n ■ Connection to %s accepted...", host);
break;
case 502:
free_Mail_Socket(NNTP_sock);
err_exit(EXIT_FAILURE, "\n ■ Connection to %s refused... try again later.", host);
break;
case 503:
free_Mail_Socket(NNTP_sock);
err_exit(EXIT_FAILURE, "\n ■ NNTP service unavailable. Connection to %s refused.", host);
break;
default:
free_Mail_Socket(NNTP_sock);
err_exit(EXIT_FAILURE, "\n ■ Unknown NNTP error. Connection to %s failed.", host);
break;
}
SOCK_READ_ERR(NNTP_sock);
return (NNTP_sock);
}
int cnext(Mail_Socket *NNTP_sock)
{
char *p, *q;
static char _temp_buffer[STRING];
sock_printf(NNTP_sock->sock, "NEXT");
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
if (reply == 421)
return -1;
else
if (reply == 223) {
cur_article = reparticle;
p = _temp_buffer;
q = cur_articleid;
while ((p < _temp_buffer + STRING - 2) && (*p != '<'))
p++;
while ((p < _temp_buffer + STRING - 2) && (*p != '>'))
*q++ = *p++;
*q++ = '>';
*q++ = '\0';
return 0;
} else {
log_it("\n ■ Unknown cnext error : %s", _temp_buffer);
}
SOCK_READ_ERR(NNTP_sock);
return -1;
}
int saveactive(Mail_Socket *NNTP_sock, char *fn)
{
char s[181], group[256], act;
unsigned long to, from;
int count;
FILE *fp;
static char _temp_buffer[BIGSTR];
if ((fp = fsh_open(fn, "w")) == NULL) {
perror(fn);
return -1;
}
sock_printf(NNTP_sock->sock, "LIST");
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
sscanf(_temp_buffer, "%hu %lu", &reply, &reparticle);
if (reply == 215) {
count = 0;
while (1) {
if (count++ % 25 == 0)
fprintf(stderr, "\b\b\b\b\b\b\b%-7d", count);
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0)
break;
sscanf(_temp_buffer, "%s %lu %lu %c", group, &to, &from, &act);
sprintf(s, "%s %lu\n", group, to);
fprintf(fp, s);
}
log_it("\n ■ %d total newsgroups available on %s", count, serverhost);
fclose(fp);
return 0;
} else
log_it("\n ■ Unknown saveactive error : %s", _temp_buffer);
SOCK_READ_ERR(NNTP_sock);
fclose(fp);
return -1;
}
int checkx(int cug)
{
char *ptr, *ptr2, *ptr3, buf[256];
int i;
if (cug == 0)
return 0;
ptr = strtok(cur_organization, "*");
trimstr1(ptr);
if (strcmp(ptr, syscfg.systemname) == 0) {
log_it("\n ■ Skipping post from %s", syscfg.systemname);
return 1;
}
ptr = cur_newsgroups;
while (*ptr == ' ')
ptr++;
while (1) {
if (*ptr == 0)
break;
if (*ptr == ',')
ptr++;
while (*ptr == ' ')
ptr++;
ptr2 = ptr;
ptr3 = buf;
while (*ptr2 != 0 && *ptr2 != ',')
*ptr3++ = *ptr2++;
*ptr3 = 0;
while (*(--ptr3) == ' ')
*ptr3 = 0;
ptr = ptr2;
for (i = 0; i < cug; i++) {
if (strcmp(buf, grouprec[i].groupname) == 0) {
log_it("\n ■ Skipping message - crossposted to %s",
grouprec[i].groupname);
return 1;
}
}
}
return 0;
}
void good_name(char *name)
{
char *ss;
int i;
trimstr1(name);
if ((strcspn(name," ")==strlen(name)) && (strcspn(name,"(")==strlen(name)))
if (_fstrchr(name, '@') != NULL)
return;
if (strcspn(name, "<") != strlen(name)) {
ss = strtok(name, "<");
if (ss)
ss = strtok(NULL, ">");
strcpy(name, ss);
if (name[0] == 0)
strcpy(name, "Unknown");
return;
}
if ((i = strcspn(name, "(")) != strlen(name)) {
if (name[i - 1] == 32) {
ss = strtok(name, " ");
if (ss)
strcpy(name, ss);
} else {
ss = strtok(name, "(");
if (ss)
strcpy(name, ss);
}
if (name[0] == 0)
strcpy(name, "Unknown");
return;
}
}
int postnews(Mail_Socket *NNTP_sock, int cug)
{
char s[181], s1[12], s2[5], fn[201], *ss;
int f1, nlines, in_header, len, tlen;
FILE *fp;
static char _temp_buffer[STR];
long clock;
struct ffblk ff;
sprintf(fn, "%s\\OUTBOUND\\%hu.*", net_data, grouprec[cug].subtype);
f1 = findfirst(fn, &ff, 0);
if (f1 != 0) {
log_it("\n ■ No outbound news articles to post...");
return 1;
}
while (f1 == 0) {
sock_printf(NNTP_sock->sock, "POST");
sock_wait_input(NNTP_sock->sock, SOCK_DELAY, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (*_temp_buffer != '3') {
if (atoi(_temp_buffer) == 440)
log_it("\n ■ No posting allowed to %s!", grouprec[cug].groupname);
else
log_it("\n ■ Remote error: %s", _temp_buffer);
return 0;
}
fprintf(stderr, "\n ■ Posting article to %s\n", grouprec[cug].groupname);
sprintf(s, "%s\\OUTBOUND\\%s", net_data, ff.ff_name);
fp = fsh_open(s, "rb");
nlines = 0;
while (fgets(_temp_buffer, 120, fp) != NULL) {
if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0) {
_temp_buffer[1] = '.';
_temp_buffer[2] = 0;
}
tlen = 0;
len = strlen(_temp_buffer);
while (tlen < len) {
if (!tcp_tick(NNTP_sock->sock)) {
fclose(fp);
fprintf(stderr, "\n ■ Connection reset by host");
return 1;
}
tlen += sock_fastwrite(NNTP_sock->sock, &_temp_buffer[tlen], len-tlen);
}
backline();
fprintf(stderr, "\r ■ Lines sent : %d", ++nlines);
}
fclose(fp);
sock_printf(NNTP_sock->sock, ".\n");
fprintf(stderr, "\n ■ Awaiting acknowledgement - may take several minutes...");
log_it("\n ■ Server response : ");
sock_wait_input(NNTP_sock->sock, 180, NULL, &NNTP_stat);
sock_gets(NNTP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
log_it("%s", _temp_buffer);
if (*_temp_buffer != '2') {
if (atoi(_temp_buffer) == 441) {
ss = strtok(_temp_buffer, " ");
ss = strtok(NULL, " ");
if (atoi(ss) == 435)
unlink(s);
fnsplit(s, NULL, NULL, s1, s2);
log_it("\n ■ %s%s not accepted by server - nothing posted", s1, s2);
} else
log_it("\n ■ Remote error: %s", _temp_buffer);
} else {
if (unlink(s) == 0)
log_it("\n ■ Deleted sent message - %s", s);
}
f1 = findnext(&ff);
}
SOCK_READ_ERR(NNTP_sock);
return 0;
}
int get_subtype(int sub)
{
int ok;
char fn[181], s[81], net_name[21], *ss;
FILE *fp;
sprintf(fn, "%sSUBS.XTR", syscfg.datadir);
fp = fsh_open(fn, "r");
if (!fp)
return 0;
ok = 0;
while (fgets(s, 80, fp)) {
if (s[0] == '!') {
if (sub == atoi(&s[1]))
ok = 1;
}
if (ok && (s[0] == '$')) {
ss = strtok(s, " ");
strcpy(net_name, &ss[1]);
ss = strtok(NULL, " ");
fclose(fp);
if ((stricmp(net_name, "FILENET") == 0) && (isdigit(ss[0])))
return atoi(ss);
else
return 0;
}
}
fclose(fp);
return 0;
}
int max_on_sub(int cug)
{
int i, f, max_subs, num_subs, stype;
char s[121];
if (grouprec[cug].subtype == 0)
return 0;
max_subs = syscfg.max_subs;
subboards = (subboardrec *) mallocx(max_subs * sizeof(subboardrec));
sprintf(s, "%sSUBS.DAT", syscfg.datadir);
f = sh_open1(s, O_RDONLY | O_BINARY);
if (f < 0) {
fprintf(stderr, "\n ■ %s NOT FOUND", s);
return 0;
}
num_subs = (sh_read(f, subboards, (max_subs * sizeof(subboardrec))))/
sizeof(subboardrec);
f = sh_close(f);
for (i = 0; i < num_subs; i++) {
fprintf(stderr, "\b\b\b\b%-4d", i);
stype = get_subtype(i);
if (stype && (grouprec[cug].subtype == stype)) {
if (subboards)
free(subboards);
fprintf(stderr, "\b\b\b\b%hu messages.", subboards[i].maxmsgs);
return subboards[i].maxmsgs;
}
}
if (subboards)
free(subboards);
return 0;
}
int getnews(char *hostname)
{
int f1, nup, nug, cug, abort, ok, done, new_articles, max_articles, parts;
char *p, fn[201], mailname[121], reline[121], s[21], s1[21], pktname[121];
char orig_subj[STRING];
unsigned int text_len;
net_header_rec nh;
struct date dt;
struct time tm;
struct ffblk ff;
static FILE *fp;
Mail_Socket *nntp_sock;
static char _temp_buffer[STR];
abort = cug = 0;
sock_init();
nntp_sock = netsocket(hostname);
if (stricmp(grouprec[0].groupname, "newsrc") == 0) {
grouprec[0].groupname[0] = 0;
write_groups(0);
if (grouprec)
free(grouprec);
sprintf(fn, "%s\\NEWSRC", net_data);
log_it("\n ■ Creating newsgroup list : %s... ", fn);
saveactive(nntp_sock, fn);
nntp_shutdown(nntp_sock);
free_Mail_Socket(nntp_sock);
return 0;
}
nug = nup = 1;
while ((cug < ngroups) && (!abort)) {
if (kbhit()) {
if (getch() == 27) {
log_it("\n ■ Aborting news retrieval session...");
if (fp)
fclose(fp);
grouprec[cug].lastread = cur_article;
write_groups(1);
nntp_shutdown(nntp_sock);
free_Mail_Socket(nntp_sock);
return 1;
}
}
if (nup) {
nup = 0;
if (grouprec[cug].subtype) {
if (fp)
fclose(fp);
write_groups(0);
name_packet(pktname);
if ((fp = fsh_open(pktname, "wb")) == NULL) {
log_it("\n ■ Unable to create %s!", pktname);
qexit(nntp_sock, 1);
} else {
fnsplit(pktname, NULL, NULL, s, s1);
fprintf(stderr, "\n ■ Creating new packet : %s%s", s, s1);
}
} else {
if (fp) {
fclose(fp);
}
}
}
if (nug) {
cur_article = ++grouprec[cug].lastread;
nug = 0;
if (cgroup(nntp_sock, grouprec[cug].groupname)) {
log_it("\n ■ %s is an invalid or deleted newsgroup...",
grouprec[cug].groupname);
grouprec[cug].groupname[0] = 0;
if (++bogus >= 10) {
log_it("\n ■ More than 10 invalid newsgroups... possible NEWS.RC error!");
qexit(nntp_sock, 10);
}
nug = 1;
continue;
}
log_it("\n ■ Requesting %s... ", grouprec[cug].groupname);
if (grouprec[cug].lastread < cur_first)
grouprec[cug].lastread = cur_first;
if (grouprec[cug].lastread > cur_last) {
grouprec[cug].lastread = cur_last;
log_it("no new articles.");
nug = 1;
} else {
new_articles = cur_last - grouprec[cug].lastread + 1;
log_it("%lu new article%s. ",
cur_last - grouprec[cug].lastread + 1,
new_articles == 1 ? "" : "s");
if (new_articles > 100) {
fprintf(stderr, "\n ■ Checking maximum articles allowed on sub : ");
max_articles = max_on_sub(cug);
if (max_articles && (new_articles > max_articles)) {
log_it("\n ■ Requesting most recent %d articles", max_articles);
grouprec[cug].lastread = cur_last - max_articles;
}
}
}
postnews(nntp_sock, cug);
if (!nug) {
if (cur_numa == 0) {
log_it("\n ■ No articles available in %s...",
grouprec[cug].groupname);
nug = 1;
continue;
} else {
if (cstat(nntp_sock, grouprec[cug].lastread) &&
cstat(nntp_sock, cur_last))
nug = 1;
}
}
} else {
if (cnext(nntp_sock)) {
fprintf(stderr, "\n ■ End of new articles in %s",
grouprec[cug].groupname);
nug = 1;
write_groups(1);
}
}
if (!nug) {
if ((!chead(nntp_sock)) || (!checkx(cug))) {
if (cur_subject[0]) {
treat(cur_subject);
if (strlen(cur_subject) > 65)
cur_subject[65] = '\0';
} else
strcpy(cur_subject, "No subject");
strcpy(orig_subj, cur_subject);
if (cur_from[0]) {
treat(cur_from);
if (strlen(cur_from) > 45)
cur_from[45] = 0;
} else
strcpy(cur_from, "Unknown");
if (cur_replyto[0]) {
/* treat(cur_replyto); */
/* good_name(cur_replyto); */
if (strlen(cur_replyto) > 45)
cur_replyto[45] = 0;
}
strncpy(_temp_buffer, orig_subj, 55);
_temp_buffer[55] = '\0';
fprintf(stderr, "\n ■ [%lu/%lu] : %s ", cur_article,
cur_last, _temp_buffer);
ok = savebody(nntp_sock, cug, &abort);
if (ok && grouprec[cug].subtype) {
strcpy(msgarea, grouprec[cug].groupname);
sprintf(msgto, "All");
sprintf(fn, "%s\\INPUT*.MSG", tmpdir);
done = 1;
parts = 0;
if (findfirst(fn, &ff, 0) == 0) {
parts = 1;
done = 0;
}
while (!done) {
sprintf(fn, "%s\\%s", tmpdir, ff.ff_name);
f1 = sh_open1(fn, O_RDONLY | O_BINARY);
text_len = (unsigned int) filelength(f1);
if (text_len > 32000L) {
fprintf(stderr, "\n ■ Truncating %ld bytes from input file",
text_len - 32000L);
text_len = 32000L;
}
p = (char *) malloca(32767);
if (!p) {
log_it("\n ■ Insufficient memory to read entire message");
qexit(nntp_sock, 1);
}
sh_read(f1, (void *) p, text_len);
sh_close(f1);
nh.tosys = sy;
nh.touser = 0;
nh.fromsys = 32767L;
nh.fromuser = 0;
nh.main_type = main_type_pre_post;
nh.minor_type = grouprec[cug].subtype;
nh.list_len = 0;
gettime(&tm);
getdate(&dt);
nh.daten = dostounix(&dt, &tm);
nh.method = 0;
if (parts > 1) {
sprintf(_temp_buffer, "%d%s ", parts, ordinal_text(parts));
strcat(_temp_buffer, orig_subj);
strcpy(cur_subject, _temp_buffer);
if (strlen(cur_subject) > 72)
cur_subject[72] = '\0';
else
cur_subject[strlen(cur_subject)] = '\0';
}
nh.length = text_len + strlen(cur_subject) + 1;
strncpy(mailname, cur_replyto, 46);
strcat(mailname, "\r\n");
nh.length += strlen(mailname);
nh.length += 24 + 2;
strncpy(reline, cur_articleid, 60);
sprintf(cur_articleid, "RE: %s\r\n\r\n", reline);
nh.length += strlen(cur_articleid);
fwrite(&nh, sizeof(net_header_rec), 1, fp);
fwrite(cur_subject, sizeof(char), strlen(cur_subject) +1, fp);
fwrite(mailname, sizeof(char), strlen(mailname), fp);
strncpy(_temp_buffer, ctime(&(time_t) nh.daten), 24);
_temp_buffer[24] = '\0';
strcat(_temp_buffer, "\r\n");
fwrite(_temp_buffer, sizeof(char), strlen(_temp_buffer), fp);
sprintf(_temp_buffer, "%s\n\n", cur_articleid);
fwrite(cur_articleid, sizeof(char), strlen(cur_articleid), fp);
fwrite(p, sizeof(char), text_len, fp);
if (p)
free(p);
unlink(fn);
if (findnext(&ff) == 0) {
done = 0;
++parts;
} else
done = 1;
}
}
grouprec[cug].lastread = cur_article;
} else
grouprec[cug].lastread = cur_article + 1;
if ((filelength(fileno(fp)) > 250000L) && grouprec[cug].subtype)
nup = 1;
}
if (nug) {
++cug;
if ((grouprec[cug - 1].subtype == 0) && (grouprec[cug].subtype)) {
nup = 1;
} else {
if ((grouprec[cug - 1].subtype) && (grouprec[cug].subtype == 0)) {
if (fp) {
fclose(fp);
}
}
}
}
}
if (fp)
fclose(fp);
if (abort)
log_it("\n ■ Session aborted from keyboard");
write_groups(1);
nntp_shutdown(nntp_sock);
free_Mail_Socket(nntp_sock);
return 0;
}
void main(int argc, char *argv[])
{
int f;
char s[201], fn[201], *ss;
FILE *fp;
struct tm *time_now;
time_t some;
cursor('S');
cursor('H');
fprintf(stderr, "\n ■ %s", version);
if (argc != 4)
err_exit(EXIT_FAILURE, "\n ■ Invalid arguments for %s", argv[0]);
ss = getenv("WWIV_INSTANCE");
if (ss) {
instance = atoi(ss);
if ((instance <= 0) || (instance >= 1000)) {
log_it("\n ■ WWIV_INSTANCE set to %d. Can only be 1..999!", instance);
instance = 1;
}
} else
instance = 1;
SOCK_DELAY = 120;
get_dir(maindir, 0);
strcpy(net_data, argv[1]);
strcpy(serverhost, argv[2]);
time(&some);
time_now = localtime(&some);
strftime(s, 80, "\n\nNEWS session beginning on %A, %B %d, %Y at %H:%M %p",
time_now);
sprintf(fn, "%s\\NEWS.LOG", net_data);
if ((fp = fsh_open(fn, "at")) != NULL) {
fprintf(fp, s);
fclose(fp);
}
sprintf(tmpdir, "%s\\SPOOL", net_data);
sy = atoi(argv[3]);
sprintf(s, "%s\\CONFIG.DAT", maindir);
f = sh_open1(s, O_RDONLY | O_BINARY);
if (f < 0)
err_exit(EXIT_FAILURE, "\n ■ %s NOT FOUND.", s);
sh_read(f, (void *) (&syscfg), sizeof(configrec));
sh_close(f);
sprintf(newsrc, "%s\\NEWS.RC", net_data);
read_groups();
if (ngroups == 0)
err_exit(EXIT_FAILURE, "\n ■ Unable to access newsgroup file: %s!", newsrc);
if (stricmp(grouprec[0].groupname, "newsrc") == 0)
log_it("\n ■ Retrieving current newsgroup listing from %s",
serverhost);
else
log_it("\n ■ %d newsgroup%s defined in %s", ngroups,
ngroups == 1 ? "" : "s", strlwr(newsrc));
if (getnews(serverhost))
err_exit(EXIT_FAILURE, "\n ■ Exiting program.");
if (grouprec)
free(grouprec);
grouprec = NULL;
cd_to(maindir);
log_it("\n ■ NEWS succesfully completed processing %d newsgroups", ngroups);
err_exit(EXIT_SUCCESS, "\n ■ Normal program termination");
}