home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC
/
SRC15B43.ZIP
/
NEWS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-09-28
|
51KB
|
1,864 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"
#include "retcode.h"
extern unsigned _stklen = 8500;
#define SHARE_LEVEL 10
#define WAIT_TIME 10
#define TRIES 100
#define MT_DESQVIEW 0x01
#define MT_WINDOWS 0x02
#define MT_OS2 0x04
#define MT_NB 0x40
#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 MAX_LOG 800
#define free_Mail_Socket(SOCK) if (SOCK!=NULL) { \
farfree(SOCK->sock); farfree(SOCK); SOCK=NULL; }
configrec syscfg;
static int NNTP_stat;
unsigned short reply;
unsigned long reparticle;
unsigned long cur_daten;
char serverhost[81], newsrc[81];
char maindir[181], tmpdir[201], net_data[181];
char *version = "Freeware PPP News Retrieval " VERSION;
char cur_gname[STRING], tpktname[121];
char POPNAME[STR], POPDOMAIN[STR];
char msgto[STRING], msgarea[STRING];
long cur_numa, cur_first, cur_last;
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];
int instance, crossposts, multitasker = 0, SOCK_DELAY;
unsigned short sy;
typedef struct {
char groupname[60];
long lastread;
char subtype[8];
} 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(0, "\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(0, "\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 dv_pause(void)
{
__emit__(0xb8, 0x1a, 0x10, 0xcd, 0x15);
__emit__(0xb8, 0x00, 0x10, 0xcd, 0x15);
__emit__(0xb8, 0x25, 0x10, 0xcd, 0x15);
}
void win_pause(void)
{
__emit__(0x55, 0xb8, 0x80, 0x16, 0xcd, 0x2f, 0x5d);
}
int get_dos_version(void)
{
_AX = 0x3000;
geninterrupt(0x21);
if (_AX % 256 >= 10) {
multitasker |= MT_OS2;
}
return (_AX);
}
int get_dv_version(void)
{
int v;
if (multitasker & MT_OS2)
return 0;
_AX = 0x2b01;
_CX = 0x4445;
_DX = 0x5351;
geninterrupt(0x21);
if (_AL == 0xff) {
return 0;
} else {
v = _BX;
multitasker |= MT_DESQVIEW;
return v;
}
}
int get_win_version(void)
{
int v = 0;
__emit__(0x55, 0x06, 0x53);
_AX = 0x352f;
geninterrupt(0x21);
_AX = _ES;
if (_AX | _BX) {
_AX = 0x1600;
geninterrupt(0x2f);
v = _AX;
if (v % 256 <= 1)
v = 0;
}
__emit__(0x5b, 0x07, 0x5d);
if (v != 0)
multitasker |= MT_WINDOWS;
return (v);
}
int get_nb_version(void)
{
_AX = 0;
geninterrupt(0x2A);
return (_AH);
}
void detect_multitask(void)
{
get_dos_version();
get_win_version();
get_dv_version();
if (multitasker < 2)
if (get_nb_version())
multitasker = MT_NB;
}
void giveup_timeslice(void)
{
if (multitasker) {
switch (multitasker) {
case 1:
case 3:
dv_pause();
break;
case 2:
case 4:
case 5:
case 6:
case 7:
win_pause();
break;
default:
break;
}
}
}
void output(char *fmt, ...)
{
va_list v;
char s[512];
va_start(v, fmt);
vsprintf(s, fmt, v);
va_end(v);
fputs(s, stderr);
}
void backline(void)
{
int i;
output(" ");
for (i = wherex(); i > 0; i--)
output("\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_write(int handle, void *buffer, unsigned long len)
{
if (handle == -1) {
return (-1);
}
return (write(handle, buffer, (unsigned) len));
}
int sh_open(char *path, int file_access, unsigned fmode)
{
int handle, count, share;
char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
share = SH_DENYRW;
} else {
share = SH_DENYWR;
}
handle = open(path, file_access | share, fmode);
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, fmode);
while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
if (count % 2)
delay(WAIT_TIME);
else
giveup_timeslice();
count++;
handle = open(path, file_access | share, fmode);
}
}
}
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));
}
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));
}
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 *) strchr(fmode, 'w')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT | O_TRUNC;
} else
if (((char *) strchr(fmode, 'a')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT;
} else {
md = O_RDONLY;
}
if (((char *) strchr(fmode, 'b')) != NULL) {
md |= O_BINARY;
}
if (((char *) strchr(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 *) strchr(fmode, 'a')) != NULL)
sh_lseek(fd, 0L, SEEK_END);
f = fdopen(fd, fmode);
if (!f) {
close(fd);
}
} else
f = 0;
return (f);
}
size_t fsh_write(void *ptr, size_t size, size_t n, FILE * stream)
{
if (stream == NULL) {
return (0);
}
return (fwrite(ptr, size, n, stream));
}
void trim_log(void)
{
int num_lines, total_lines, kill_lines;
FILE *old_log, *new_log;
char ol[101], nl[101], s[121];
sprintf(ol, "%s\\NEWS.LOG", net_data);
sprintf(nl, "%s\\NEWS.ZZZ", net_data);
old_log = fopen(ol, "r");
new_log = fopen(nl, "a");
total_lines = 0;
if (old_log != NULL) {
while (!(fgets(s, 100, old_log) == NULL))
++total_lines;
rewind(old_log);
if (total_lines < MAX_LOG) {
if (old_log != NULL)
fclose(old_log);
if (new_log != NULL)
fclose(new_log);
unlink(nl);
return;
}
kill_lines = total_lines - MAX_LOG;
num_lines = 0;
while (!(fgets(s, 100, old_log) == NULL) && (num_lines < kill_lines))
num_lines++;
while ((strstr(s, "NEWS session") == NULL) && (num_lines < total_lines)) {
fgets(s, 81, old_log);
num_lines++;
}
fputs(s, new_log);
while ((!(fgets(s, 100, old_log) == NULL)))
fputs(s, new_log);
}
if (old_log != NULL)
fclose(old_log);
if (new_log != NULL)
fclose(new_log);
unlink(ol);
rename(nl, ol);
}
void ssm(char *s)
{
int f, i, i1;
char s1[161];
shortmsgrec sm;
configrec syscfg;
sprintf(s1, "CONFIG.DAT");
f = sh_open1(s1, O_RDONLY | O_BINARY);
if (f < 0)
return;
sh_read(f, (void *) (&syscfg), sizeof(configrec));
sh_close(f);
sprintf(s1, "%sSMW.DAT", syscfg.datadir);
f = sh_open(s1, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
if (f < 0)
return;
i = (int) (filelength(f) / sizeof(shortmsgrec));
i1 = i - 1;
if (i1 >= 0) {
sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
sh_read(f, (void *) &sm, sizeof(shortmsgrec));
while ((sm.tosys == 0) && (sm.touser == 0) && (i1 > 0)) {
--i1;
sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
sh_read(f, (void *) &sm, sizeof(shortmsgrec));
}
if ((sm.tosys) || (sm.touser))
++i1;
} else
i1 = 0;
sm.tosys = 0;
sm.touser = 1;
strncpy(sm.message, s, 80);
sm.message[80] = 0;
sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
sh_write(f, (void *) &sm, sizeof(shortmsgrec));
sh_close(f);
}
static unsigned char *trimstr1(unsigned char *s)
{
int i;
static char *whitespace = " \r\n\t";
i = strlen(s);
while ((i > 0) && (strchr(whitespace, s[i - 1])))
--i;
while ((i > 0) && (strchr(whitespace, *s))) {
memmove(s, s + 1, --i);
}
s[i] = 0;
return (s);
}
int log_it(int send_ssm, char *fmt,...)
{
va_list v;
char s[512];
FILE *fp;
sprintf(s, "%s\\NEWS.LOG", net_data);
if ((fp = fsh_open(s, "at")) == NULL)
return 1;
va_start(v, fmt);
vsprintf(s, fmt, v);
va_end(v);
fputs(s, fp);
fputs(s, stderr);
if (fp != NULL)
fclose(fp);
trimstr1(s);
if (send_ssm)
ssm(s);
return 0;
}
int count_lines(char *s)
{
FILE *fp;
char fn[201];
unsigned int nl = 0;
const int NEWLINE = '\n';
int c;
strcpy(fn, s);
if ((fp = fsh_open(fn, "rt")) == NULL) {
output("\n ■ Cannot open %s", fn);
return 0;
}
while ((c = getc(fp)) != EOF) {
if (c == NEWLINE)
++nl;
}
if (fp != NULL)
fclose(fp);
return nl;
}
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;
char s[255];
va_start(v, fmt);
vsprintf(s, fmt, v);
va_end(v);
fputs(s, stderr);
check_packets();
cursor('R');
exit(exitlevel);
}
void write_groups(int display)
{
int i;
FILE *groupfp;
if ((groupfp = fsh_open(newsrc, "wt")) == NULL) {
output("\n ■ Unable to open %s!", newsrc);
return;
} else {
if (display)
output("\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 %s\n", grouprec[i].groupname,
grouprec[i].lastread, grouprec[i].subtype);
}
}
if (groupfp != NULL)
fclose(groupfp);
}
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;
ngroups = count_lines(newsrc);
if (!ngroups)
return;
grouprec = (GROUPFILEREC *) farmalloc((ngroups + 1) * sizeof(GROUPFILEREC));
if (!grouprec) {
ngroups = 0;
output("\n ■ Unable to allocate memory reading NEWS.RC!");
return;
}
if ((groupfp = fsh_open(newsrc, "rt")) == NULL) {
farfree(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) {
strcpy(grouprec[i].subtype, strupr(ss));
++i;
}
}
}
}
}
ngroups = i;
if (groupfp != NULL)
fclose(groupfp);
for (i = 0; i < ngroups; i++) {
if ((grouprec[i].subtype) && (atoi(grouprec[i].subtype) != 0)) {
sprintf(fn, "%s\\N%s.NET", net_data, grouprec[i].subtype);
if (exist(fn)) {
groupfp = fsh_open(fn, "rt");
ok = 0;
while ((fgets(tmp, 25, groupfp)) && !ok) {
sn = atoi(tmp);
if (sn == 32767)
ok = 1;
}
if (groupfp != NULL)
fclose(groupfp);
if (!ok)
log_it(1, "\n ■ @32767 not listed as a subscriber in N%s.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 %ld", &reply, &reparticle);
if (reply == 411)
return 1;
else {
if (reply == 211) {
sscanf(_temp_buffer, "%hu %ld %ld %ld %s",
&reply, &cur_numa, &cur_first, &cur_last, &cur_gname);
return 0;
} else {
if (reply == 480)
return 2;
else
log_it(0, "\n ■ Unknown cgroup response : %s", _temp_buffer);
return 1;
}
}
SOCK_READ_ERR(NNTP_sock);
return -1;
}
int cstat(Mail_Socket * NNTP_sock, long l)
{
char *p, *q;
static char _temp_buffer[STRING];
sock_printf(NNTP_sock->sock, "STAT %ld", l);
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 %ld", &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(0, "\n ■ Unknown cstat error : %s", _temp_buffer);
SOCK_READ_ERR(NNTP_sock);
return -1;
}
void qexit(Mail_Socket * NNTP_sock, int n)
{
static char _temp_buffer[STR];
if (n >= 10)
log_it(0, "\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(0, "\n ■ Tried to quit host but failed");
sock_err:
cursor('R');
check_packets();
fcloseall();
free_Mail_Socket(NNTP_sock);
exit(n);
}
void treat(char *str)
{
char *obuf, *nbuf;
if (*str) {
for (obuf = str, nbuf = str; *obuf && obuf; ++obuf) {
if (((*obuf >= 32) && (*obuf <= 126)) || (*obuf == 10) || (*obuf == 9)) {
*nbuf++ = *obuf;
}
}
}
*nbuf = 0;
}
void name_packet(char *pktname)
{
int ok;
struct stat info;
unsigned int 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], ch;
unsigned int curpos, count, part;
char spin[26];
int i, place, len, spooltodisk;
unsigned long msgsize;
struct stat statbuf;
FILE *tpktfp = NULL;
static char _temp_buffer[BIGSTR];
strcpy(spin, "||//--\\\\");
place = count = spooltodisk = 0;
len = strlen(spin);
if ((strlen(grouprec[cug].subtype) == 1) &&
(grouprec[cug].subtype[0] == '0'))
spooltodisk = 1;
part = 1;
if (spooltodisk)
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 %ld", 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 %ld", &reply, &reparticle);
if (reply == 423)
return -1;
else {
if (reply == 222) {
if (!spooltodisk) {
if ((tpktfp = fsh_open(tpktname, "wt+")) == NULL) {
fprintf(stderr, "\n ■ Unable to create temporary packet %s", tpktname);
return -1;
} else {
strcpy(_temp_buffer, "\n");
fputs(_temp_buffer, tpktfp);
}
} else {
if (tpktfp != NULL)
fclose(tpktfp);
if ((tpktfp = fsh_open(tpktname, "at+")) == NULL) {
log_it(0, "\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 : %ld\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 (!spooltodisk)
output("\n ■ Receiving message - <Esc> aborts - <Space> skips group [-]");
else
output("\n ■ Appending to %s%s - <Esc> aborts - <Space> skips group [-]", s1, s2);
msgsize = 0L;
curpos = 0L;
while (1) {
if (kbhit()) {
ch = (getch());
switch (ch) {
case 27:
output("\r ■ Abort detected... please wait until message is completed [-]");
*abort = 1;
break;
case 32:
output("\r ■ Skipping group... please wait until message is completed [-]");
*abort = 2;
break;
default:
break;
}
}
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) {
output("\b\b%c]", spin[place++]);
place %= len;
count = 0;
}
if ((msgsize > 30000L) && (!spooltodisk)) {
strcpy(_temp_buffer, "\nContinued in next message...\n");
fputs(_temp_buffer, tpktfp);
if (tpktfp != NULL)
fclose(tpktfp);
sprintf(tpktname, "%s\\INPUT%d.MSG", tmpdir, part++);
if (stat(tpktname, &statbuf) == 0)
unlink(tpktname);
if ((tpktfp = fsh_open(tpktname, "wt+")) == NULL) {
fprintf(stderr, "\n ■ Unable to create temporary packet %s", tpktname);
return -1;
}
backline();
output("\r ■ Breaking into %d%s part [ ]", (
part - 1), ordinal_text(part - 1));
strcpy(_temp_buffer, "\nContinued from previous message...\n");
fputs(_temp_buffer, tpktfp);
msgsize = strlen(_temp_buffer);
}
}
if (tpktfp != NULL)
fclose(tpktfp);
output("\b\bX]");
return 1;
}
}
if (tpktfp != NULL)
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 %ld", 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 %ld", &reply, &reparticle);
if (reply == 423)
return 0;
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 1;
}
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(0, "\n ■ Unknown chead error : %s", _temp_buffer);
}
SOCK_READ_ERR(NNTP_sock);
return 0;
}
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(0, "\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 %ld", &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(0, "\n ■ Unknown cnext error : %s", _temp_buffer);
SOCK_READ_ERR(NNTP_sock);
return -1;
}
char *stripspace(char *str)
{
char *obuf, *nbuf;
if (str) {
for (obuf = str, nbuf = str; *obuf; ++obuf) {
if (!isspace(*obuf))
*nbuf++ = *obuf;
}
*nbuf = NULL;
}
return (str);
}
int sendauthinfo(Mail_Socket * NNTP_sock)
{
char s[MAXPATH], line[121], name[21], pass[31], *ss;
static char _temp_buffer[BIGSTR];
FILE *fp;
output("\n ■ Server requested authentication information.");
sprintf(s, "%s\\NET.INI", maindir);
if ((fp = fsh_open(s, "rt")) == NULL) {
output("\n ■ Unable to open %s.", s);
return 1;
}
while (fgets(line, 80, fp)) {
ss = NULL;
stripspace(line);
if ((line[0] == ';') || (line[0] == '\n') || (line[0] == '['))
continue;
if (strlen(line) == 0)
continue;
if (strnicmp(line, "NEWSNAME", 8) == 0) {
ss = strtok(line, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
name[0] = 0;
strcpy(name, ss);
}
}
if (strnicmp(line, "NEWSPASS", 8) == 0) {
ss = strtok(line, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
pass[0] = 0;
strcpy(pass, ss);
}
}
}
if (fp != NULL)
fclose(fp);
if ((name[0] == 0) || (pass[0] == 0)) {
output("\n ■ NEWSNAME or NEWSPASS not properly defined!");
return 1;
}
while (1) {
sprintf(_temp_buffer, "authinfo user %s", name);
sock_printf(NNTP_sock->sock, _temp_buffer);
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 %s", &reply, s);
if (reply == 381) {
sprintf(_temp_buffer, "authinfo pass %s", pass);
sock_printf(NNTP_sock->sock, _temp_buffer);
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 %s", &reply, s);
if (reply == 281) {
output("\n ■ Authentication accepted. Continuing news retrieval session.");
return 0;
} else {
output("\n ■ Authentication failed. Aborting news retrieval session.");
return 1;
}
} else {
output("\n ■ Unknown response \"%hu\" from server.", reply);
return 1;
}
}
SOCK_READ_ERR(NNTP_sock);
return -1;
}
int saveactive(Mail_Socket * NNTP_sock, char *fn)
{
char s[181], group[256], act;
long to, from;
int count, done;
FILE *fp;
static char _temp_buffer[BIGSTR];
if ((fp = fsh_open(fn, "w")) == NULL) {
perror(fn);
return -1;
}
done = 0;
while (!done) {
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 %ld", &reply, &reparticle);
if (reply == 215) {
count = 0;
while (1) {
if (count++ % 25 == 0)
output("\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 %ld %ld %c", group, &to, &from, &act);
sprintf(s, "%s %ld\n", group, to);
fsh_write(s, sizeof(char), strlen(s), fp);
}
log_it(0, "\n ■ %d total newsgroups available on %s", count, serverhost);
if (fp != NULL)
fclose(fp);
return 0;
} else {
if (reply == 480) {
if (fp != NULL)
fclose(fp);
return 1;
} else {
log_it(0, "\n ■ Unknown saveactive error : %s", _temp_buffer);
if (fp != NULL)
fclose(fp);
done = 1;
}
}
}
SOCK_READ_ERR(NNTP_sock);
return -1;
}
int checkx(int cug)
{
char *ptr, *ptr2, *ptr3, buf[256];
int i, max;
/*
ptr = strtok(cur_organization, "*");
trimstr1(ptr);
if (strncmpi(ptr, syscfg.systemname, strlen(syscfg.systemname)) == 0) {
log_it(0, "\n ■ Skipping message - originally posted on %s.", syscfg.systemname);
return 0;
}
*/
sprintf(buf, "%s!%s", POPNAME, POPDOMAIN);
if (strstr(cur_path, buf) != 0) {
log_it(0, "\n ■ Local post \"%s\" skipped.", cur_subject);
return 0;
}
ptr = cur_newsgroups;
while (*ptr == ' ')
ptr++;
max = 0;
while (1) {
if (*ptr == 0)
break;
if (*ptr == ',') {
ptr++;
++max;
}
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 (strcmpi(buf, grouprec[i].groupname) == 0) {
output("\n ■ Skipping message - already posted in %s.",
grouprec[i].groupname);
return 0;
}
}
if ((crossposts > 0) && (max > crossposts)) {
output("\n ■ Skipping message - crossposted to more than %hu newsgroups.",
crossposts);
return 0;
}
}
return 1;
}
int postnews(Mail_Socket * NNTP_sock, int cug)
{
char s[181], s1[12], s2[5], fn[201], *ss;
int f1, nlines, len, tlen;
FILE *fp;
static char _temp_buffer[STR];
struct ffblk ff;
sprintf(fn, "%s\\OUTBOUND\\%s.*", net_data, grouprec[cug].subtype);
f1 = findfirst(fn, &ff, 0);
if (f1 != 0) {
log_it(0, "\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(1, "\n ■ No posting allowed to %s!", grouprec[cug].groupname);
else
log_it(0, "\n ■ Remote error: %s", _temp_buffer);
return 0;
}
output("\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)) {
if (fp != NULL)
fclose(fp);
output("\n ■ Connection reset by host");
return 1;
}
tlen += sock_fastwrite(NNTP_sock->sock, &_temp_buffer[tlen], len - tlen);
}
backline();
output("\r ■ Lines sent : %d", ++nlines);
}
if (fp != NULL)
fclose(fp);
sock_printf(NNTP_sock->sock, ".\n");
output("\n ■ Awaiting acknowledgement - may take several minutes...");
log_it(0, "\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(0, "%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(0, "\n ■ %s%s not accepted by server - nothing posted", s1, s2);
} else
log_it(0, "\n ■ Remote error: %s", _temp_buffer);
} else {
if (unlink(s) == 0)
log_it(0, "\n ■ Deleted sent message - %s", s);
}
f1 = findnext(&ff);
}
SOCK_READ_ERR(NNTP_sock);
return 0;
}
void get_subtype(int sub, char *where)
{
int ok, which;
char fn[181], s[81], net_name[21], *ss;
FILE *fp;
where[0] = 0;
which = sub;
sprintf(fn, "%sSUBS.XTR", syscfg.datadir);
if ((fp = fsh_open(fn, "r")) == NULL)
return;
ok = 0;
while (fgets(s, 80, fp)) {
if (s[0] == '!') {
if (which == atoi(&s[1]))
ok = 1;
}
if (ok && (s[0] == '$')) {
ss = strtok(s, " ");
strcpy(net_name, &ss[1]);
ss = strtok(NULL, " ");
if (fp != NULL)
fclose(fp);
if ((stricmp(net_name, "FILENET") == 0)) {
trimstr1(ss);
strcpy(where, ss);
return;
} else
return;
}
}
if (fp != NULL)
fclose(fp);
}
unsigned short max_on_sub(int cug)
{
int i, num_subs;
char fn[MAXPATH], subtype[12];
unsigned short max;
subboardrec sub;
FILE *fp;
if ((strlen(grouprec[cug].subtype) == 1) && (grouprec[cug].subtype[0] == '0'))
return 0;
sprintf(fn, "%sSUBS.DAT", syscfg.datadir);
if ((fp = fsh_open(fn, "rb")) != NULL) {
fseek(fp, 0L, SEEK_END);
num_subs = (int) (ftell(fp) / sizeof(subboardrec));
for (i = 0; i < num_subs; i++) {
output("\b\b\b\b%-4d", i);
fseek(fp, (long) i * sizeof(subboardrec), SEEK_SET);
fread(&sub, sizeof(subboardrec), 1, fp);
get_subtype(i, subtype);
if (stricmp(subtype, grouprec[cug].subtype) == 0) {
max = sub.maxmsgs;
output("\b\b\b\b%s (%s) - %hu posts.", sub.name, subtype, max);
fclose(fp);
return max;
}
}
fclose(fp);
} else
output("\n ■ Unable to read %s!", fn);
return 0;
}
int getnews(char *hostname)
{
int f1, nup, nug, cug, abort, ok, done, firstrun, parts;
char *p, fn[201], mailname[121], reline[121], s[21], s1[21], pktname[121];
char ch, orig_subj[STRING];
unsigned int text_len;
unsigned short temptype;
long new_articles, max_articles;
net_header_rec nh;
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)
farfree(grouprec);
sprintf(fn, "%s\\NEWSRC", net_data);
log_it(0, "\n ■ Creating newsgroup list : %s... ", fn);
if (saveactive(nntp_sock, fn) == 1) {
if (sendauthinfo(nntp_sock) == 0)
saveactive(nntp_sock, fn);
}
nntp_shutdown(nntp_sock);
free_Mail_Socket(nntp_sock);
return 0;
}
nug = nup = 1;
while ((cug < ngroups) && (!abort)) {
if (nup) {
nup = 0;
if (grouprec[cug].subtype) {
if (fp != NULL)
fclose(fp);
write_groups(0);
name_packet(pktname);
if ((fp = fsh_open(pktname, "wb")) == NULL) {
log_it(0, "\n ■ Unable to create %s!", pktname);
qexit(nntp_sock, 1);
} else {
fnsplit(pktname, NULL, NULL, s, s1);
output("\n ■ Creating new packet : %s%s", s, s1);
}
} else {
if (fp != NULL)
fclose(fp);
}
}
if (nug) {
nug = 0;
cur_article = ++grouprec[cug].lastread;
while (1) {
if (cgroup(nntp_sock, grouprec[cug].groupname)) {
if (cgroup(nntp_sock, grouprec[cug].groupname) == 2) {
if (sendauthinfo(nntp_sock) == 0)
continue;
else
return 1;
}
log_it(1, "\n ■ Error accessing newsgroup \"%s\".",
grouprec[cug].groupname);
if (++bogus >= 10) {
log_it(0, "\n ■ Too many invalid newsgroups... NEWS.RC error!");
qexit(nntp_sock, 10);
}
nug = 1;
break;
} else
break;
}
if (!nug) {
log_it(0, "\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(0, "no new articles.");
nug = 1;
} else {
new_articles = cur_last - grouprec[cug].lastread;
if (new_articles)
log_it(0, "%ld new article%s.", new_articles + 1,
new_articles == 1L ? "" : "s");
else {
log_it(0, "no new articles.");
nug = 1;
}
if (new_articles > 250L) {
max_articles = 0L;
output("\n ■ Maximum posts - ");
max_articles = (long) max_on_sub(cug);
if (max_articles && (new_articles > max_articles)) {
log_it(0, "\n ■ Requesting most recent %ld articles", max_articles);
grouprec[cug].lastread = cur_last - max_articles;
}
}
}
postnews(nntp_sock, cug);
if (!nug) {
if (cur_numa == 0) {
log_it(0, "\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) == -1) {
output("\n ■ End of new articles in %s",
grouprec[cug].groupname);
nug = 1;
write_groups(1);
}
}
while (kbhit()) {
ch = (getch());
switch (ch) {
case 27:
log_it(0, "\n ■ Aborting news retrieval session...");
if (fp != NULL)
fclose(fp);
nntp_shutdown(nntp_sock);
free_Mail_Socket(nntp_sock);
return 1;
case 32:
output("\n ■ Skipping group %s... ", grouprec[cug].groupname);
write_groups(1);
nug = 1;
break;
default:
break;
}
}
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]) {
if (strlen(cur_replyto) > 45)
cur_replyto[45] = 0;
}
strncpy(_temp_buffer, orig_subj, 56);
_temp_buffer[56] = '\0';
output("\n ■ [%ld/%ld] : %s ", grouprec[cug].lastread,
cur_last, _temp_buffer);
abort = 0;
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;
}
firstrun = 1;
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) {
output("\n ■ Truncating %lu bytes from input file",
text_len - 32000L);
text_len = 32000;
}
if ((p = (char *) malloc(32767L)) == NULL) {
log_it(0, "\n ■ Insufficient memory to read entire message");
qexit(nntp_sock, 1);
}
sh_read(f1, (void *) p, text_len);
sh_close(f1);
temptype = atoi(grouprec[cug].subtype);
nh.tosys = sy;
nh.touser = 0;
nh.fromsys = 32767;
nh.fromuser = 0;
if (!temptype) {
nh.main_type = main_type_new_post;
nh.minor_type = 0;
} else {
nh.main_type = main_type_pre_post;
nh.minor_type = temptype;
}
nh.list_len = 0;
++cur_daten;
nh.daten = cur_daten;
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);
if (nh.main_type == main_type_new_post)
nh.length += strlen(grouprec[cug].subtype) + 1;
if (firstrun) {
firstrun = 0;
if (strncmpi(cur_articleid, "re: ", 4) == 0) {
strncpy(reline, cur_articleid, 60);
sprintf(cur_articleid, "Re: %s\r\n\r\n", reline);
}
}
if (!cur_date[0]) {
strncpy(cur_date, ctime(&(time_t) nh.daten), 24);
cur_date[24] = '\0';
}
strcat(cur_date, "\r\n");
nh.length += strlen(cur_date);
nh.length += strlen(cur_articleid);
fsh_write(&nh, sizeof(net_header_rec), 1, fp);
if (nh.main_type == main_type_new_post)
fsh_write(grouprec[cug].subtype, sizeof(char), strlen(grouprec[cug].subtype) +1, fp);
fsh_write(cur_subject, sizeof(char), strlen(cur_subject) +1, fp);
fsh_write(mailname, sizeof(char), strlen(mailname), fp);
fsh_write(cur_date, sizeof(char), strlen(cur_date), fp);
fsh_write(cur_articleid, sizeof(char), strlen(cur_articleid), fp);
fsh_write(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;
}
cur_article = ++grouprec[cug].lastread;
if ((filelength(fileno(fp)) > 250000L) && grouprec[cug].subtype)
nup = 1;
if (abort == 2) {
write_groups(1);
nug = 1;
abort = 0;
}
}
if (nug) {
++cug;
if (((strlen(grouprec[cug].subtype) == 1) &&
(grouprec[cug].subtype[0] == '0')) && (grouprec[cug].subtype)) {
nup = 1;
} else {
if ((grouprec[cug - 1].subtype) &&
((strlen(grouprec[cug].subtype) == 1) &&
(grouprec[cug].subtype[0] == '0'))) {
if (fp != NULL)
fclose(fp);
}
}
}
}
if (fp != NULL)
fclose(fp);
if (abort)
log_it(0, "\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[MAXPATH], *ss;
FILE *fp;
struct tm *time_now;
struct date dt;
struct time tm;
time_t some;
cursor('S');
cursor('H');
output("\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(0, "\n ■ WWIV_INSTANCE set to %d. Can only be 1..999!", instance);
instance = 1;
}
} else
instance = 1;
gettime(&tm);
getdate(&dt);
cur_daten = dostounix(&dt, &tm);
SOCK_DELAY = 120;
get_dir(maindir, 0);
strcpy(net_data, argv[1]);
strcpy(serverhost, argv[2]);
detect_multitask();
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(0, "\n ■ Retrieving current newsgroup listing from %s",
serverhost);
else
log_it(0, "\n ■ %u newsgroup%s defined in %s", ngroups,
ngroups == 1 ? "" : "s", strlwr(newsrc));
crossposts = 10;
POPNAME[0] = 0;
POPDOMAIN[0] = 0;
sprintf(fn, "%s\\NET.INI", maindir);
if ((fp = fsh_open(fn, "rt")) == NULL)
output("\n ■ Unable to read %s", fn);
else {
while (fgets(s, 80, fp)) {
ss = NULL;
stripspace(s);
if ((s[0] == ';') || (s[0] == '\n') || (s[0]== '['))
continue;
if (strlen(s) == 0)
continue;
if ((strnicmp(s, "POPNAME", 7) == 0) && (POPNAME[0] == 0)) {
ss = strtok(s, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
strcpy(POPNAME, ss);
}
}
if (strnicmp(s, "FWDNAME", 7) == 0) {
ss = strtok(s, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
strcpy(POPNAME, ss);
}
}
if ((strnicmp(s, "DOMAIN", 6) == 0) && (POPDOMAIN[0] == 0)) {
ss = strtok(s, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
strcpy(POPDOMAIN, ss);
}
}
if (strnicmp(s, "FWDDOM", 6) == 0) {
ss = strtok(s, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
strcpy(POPDOMAIN, ss);
}
}
if (strnicmp(s, "XPOSTS", 6) == 0) {
ss = strtok(s, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
crossposts = atoi(ss);
if (crossposts > 99)
crossposts = 10;
}
}
}
if (fp != NULL)
fclose(fp);
}
if (getnews(serverhost))
err_exit(EXIT_FAILURE, "\n ■ Exiting program.");
if (grouprec)
farfree(grouprec);
grouprec = NULL;
cd_to(maindir);
log_it(0, "\n ■ NEWS succesfully completed processing %d newsgroups", ngroups);
trim_log();
err_exit(EXIT_SUCCESS, "\n ■ Normal program termination");
}