home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 2 BBS
/
02-BBS.zip
/
VP2SRC.ZIP
/
WRITE.C
< prev
Wrap
Text File
|
1991-04-20
|
19KB
|
746 lines
/*
$Header: write.c 3.3 87/12/12 00:45:50 Bob Exp $
The Conference Mail System
This module was originally written by Bob Hartman
Sysop of FidoNet node 1:132/101
Spark Software, 427-3 Amherst St, CS 2032, Suite 232, Nashua, NH 03061
The Conference Mail System is a complete Echomail processing package. It
is a superset of the original Echomail utilities created by Jeff Rush, and
also contains ideas gleaned from the ARCmail, Renum, oMMM, MGM, and Opus
programs that were created by various software authors.
This program source code is being released with the following provisions:
1. You are free to make changes to this source code for use on your own
machine, however, altered source files may not be distributed without the
consent of Spark Software.
2. You may distribute "patches" or "diff" files for any changes that you
have made, provided that the "patch" or "diff" files are also sent to Spark
Software for inclusion in future releases of the entire package. A "diff"
file for the source archives may also contain a compiled version, provided
it is clearly marked as not being created from the original source code.
No other executable versions may be distributed without the consent of
Spark Software.
3. You are free to include portions of this source code in any program you
develop, providing: a) Credit is given to Spark Software for any code that
may is used, and b) The resulting program is free to anyone wanting to use
it, including commercial and government users.
4. There is NO technical support available for dealing with this source
code, or the accompanying executable files. This source code is provided
as is, with no warranty expressed or implied (I hate legalease). In other
words, if you don't know what to do with it, don't use it, and if you are
brave enough to use it, you're on your own.
Spark Software may be contacted by modem at (603) 888-8179 (node 1:132/101)
on the public FidoNet network, or at the address given above.
To use this code you will need Microsoft C version 4.0, and also Microsoft
Macro Assembler version 4.0.
*/
/*
$Log: write.c $
* Revision 3.3 87/12/12 00:45:50 Bob
* Source code release
*
*/
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <io.h>
#include <time.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include "fastecho.h"
#define DEBUG 0
WBUFFER wbuffs[60] = {
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 },
{ NULL, NULL, -1 }
};
extern char *open_err;
extern char *pkt_err;
extern SEACONFIG config;
extern AREAS_PTR areas[];
extern char board_name[], sysop_name[];
extern int tot_areas;
extern char bbsfile[];
extern char *arc_cmd[];
extern int nread;
extern int arc_args;
extern int last_msg;
extern int high_one;
extern int convert;
extern char seen_byline[];
extern char originline[];
extern char tearline[];
extern int max_msgs;
extern int ctrla;
extern int *msg_nums;
extern char *holder1;
extern int mail_low;
extern int mail_high;
extern char *seen_by;
extern char *origin;
extern char *tear;
extern char *holder;
extern int tearl;
extern int originl;
extern int seen_byl;
extern int msgl;
extern int seen_nets[];
extern int seen_nodes[];
extern int not_seen[];
extern int tot_seen;
extern char arealine[];
extern int areal;
extern MSG_PTR h;
extern int msgs_done;
extern struct tm *t2;
extern char pkt_dir[];
extern int num_pw;
extern PW_PTR pw[];
int to_netmail = 1;
int opus_1 = 0;
int cminternal = 1;
int last_opened;
unsigned int def_attr = 0;
PKTHDR pkt_hdr;
PACKED p_msg;
int write_it (
AREAS_PTR which,
int j)
{
if (to_netmail)
return (write_netmail (which, j));
else
return (write_packet (which, j));
}
void write_msg (int f)
{
extern long fast_lseek();
#if DEBUG
printf ("Seeking to proper location for tear line\n");
#endif
(void) fast_lseek (f, (long) (msgl+sizeof(MSG)), SEEK_SET);
/* Finish off the messgage */
finish_msg (f);
}
finish_msg (int f)
{
int n;
#if DEBUG
printf ("Writing origin line\n");
#endif
if (originl)
{
if ((n = fast_write (f, origin, originl)) != originl)
{
#if DEBUG
printf ("bad news, only wrote %d chars of %d\n", n, originl);
#endif
disk_full (f, "");
return (1);
}
}
#if DEBUG
printf ("Writing Seen-by's\n");
#endif
if (seen_byl)
{
if (fast_write (f, seen_by, seen_byl) != seen_byl)
{
disk_full (f, "");
return (1);
}
}
return (0);
}
int write_netmail (
AREAS_PTR which,
int j)
{
int f;
/* int err; */
char fname1[80];
if (j >= 0)
{
h->dest = which->node[j];
h->dest_net = which->net[j];
}
h->cost = 0;
h->attr |= def_attr;
#if DEBUG
printf ("New message for %d/%d\n", which->node[j], which->net[j]);
#endif
/* Create the message file name */
sprintf (fname1, "%s\\%d.MSG", config.mailpath, ++mail_high);
printf (" (MAIL:%04d.MSG)", mail_high);
#if DEBUG
printf ("placing it in '%s'\n", fname1);
#endif
/* Open up new message file */
if((f = open (fname1, O_WRONLY|O_CREAT|O_BINARY, S_IREAD|S_IWRITE)) == -1)
{
printf (open_err, fname1);
return (1);
}
/* Write out the header information */
if (fast_write (f, (char *) h, sizeof (MSG)) != sizeof (MSG))
{
disk_full (f, fname1);
return (1);
}
/* Write out the AREA: stuff */
if (!((which->flags & PASS_THRU) || (which->flags & ROUTETHRU)))
{
if (fast_write (f, arealine, areal) != areal)
{
disk_full (f, fname1);
return (1);
}
}
/* Write out the message text */
if (fast_write (f, (char *) holder, msgl) != msgl)
{
disk_full (f, fname1);
return (1);
}
/* Finish off the message */
if (finish_msg (f))
{
disk_full (f, fname1);
return (1);
}
/* Now close the file */
(void) fast_close (f);
++msgs_done;
return (0);
}
int write_reply (AREAS_PTR which)
{
int f;
char fname1[80];
char s[256];
#if DEBUG
printf ("New message for %d/%d\n", h->dest_net, h->dest);
#endif
/* Create the message file name */
sprintf (fname1, "%s\\%d.MSG", config.mailpath, ++mail_high);
printf (" MAIL:%04d.MSG", mail_high);
#if DEBUG
printf ("placing it in '%s'\n", fname1);
#endif
/* Open up new message file */
if((f = open (fname1, O_WRONLY|O_CREAT|O_BINARY, S_IREAD|S_IWRITE)) == -1)
{
printf (open_err, fname1);
exit (2);
}
/* Write out the header information */
if (fast_write (f, (char *) h, sizeof (MSG)) != sizeof (MSG))
{
disk_full (f, fname1);
return (1);
}
sprintf (s, "Forwarded privately in response to a message in AREA:%s\r\r", which->area_name);
/* Write out the AREA: stuff */
(void) fast_write (f, s, strlen (s));
/* Write out the message text */
if (fast_write (f, holder, msgl) != msgl)
{
disk_full (f, fname1);
return (1);
}
/* Finish off the message */
if (originl)
{
if (fast_write (f, origin, originl) != originl)
{
disk_full (f, fname1);
return (1);
}
}
/* Now add a reasonable end to the file */
if (fast_write (f, "\r\n\r\n", 5) != 5)
{
disk_full (f, fname1);
return (1);
}
/* Now close the file */
(void) fast_close (f);
++msgs_done;
return (0);
}
int write_packet (
AREAS_PTR which,
int j)
{
int f;
if (j < 0)
{
return (write1_packet ());
}
/* Open the output file */
f = open_outfile (which, j);
if(f == -1)
{
printf (pkt_err, which->net[j], which->node[j]);
return (1);
}
/* Create the header information to go out */
p_msg.dest_node = which->node[j];
p_msg.dest_net = which->net[j];
p_msg.attr = h->attr;
/* write out the packet header stuff */
(void) fast_write (f, (char *) &p_msg, sizeof (PACKED));
/* write out the date */
(void) fast_write (f, h->date, strlen (h->date)+1);
/* write out the to */
(void) fast_write (f, h->to, strlen (h->to)+1);
/* write out the from */
(void) fast_write (f, h->from, strlen (h->from)+1);
/* write out the subject */
(void) fast_write (f, h->subj, strlen (h->subj)+1);
/* write out the AREA: line */
if (!(which->flags & PASS_THRU))
{
(void) fast_write (f, arealine, areal);
}
/* Write out the message text */
if (fast_write (f, holder, msgl) != msgl)
{
disk_full (f, "");
return (1);
}
/* Finish off the message */
if (finish_msg (f))
{
disk_full (f, "");
return (1);
}
/* write out the final null */
(void) fast_write (f, "\0", 1);
++msgs_done;
return (0);
}
int write1_packet ()
{
int f;
/* Open the output file */
f = open1_outfile ();
if(f == -1)
{
printf (pkt_err, h->dest_net, h->dest);
return (1);
}
/* Create the header information to go out */
p_msg.dest_node = h->dest;
p_msg.dest_net = h->dest_net;
p_msg.attr = h->attr;
/* write out the packet header stuff */
(void) fast_write (f, (char *) &p_msg, sizeof (PACKED));
/* write out the date */
(void) fast_write (f, h->date, strlen (h->date)+1);
/* write out the to */
(void) fast_write (f, h->to, strlen (h->to)+1);
/* write out the from */
(void) fast_write (f, h->from, strlen (h->from)+1);
/* write out the subject */
(void) fast_write (f, h->subj, strlen (h->subj)+1);
/* Write out the message text */
if (fast_write (f, holder, msgl) != msgl)
{
disk_full (f, "");
return (1);
}
/* write out the final null */
(void) fast_write (f, "\r\n\r\n\0\0\0", 7);
(void) fast_close (f);
++msgs_done;
return (0);
}
char munger[100];
char *mung_path (int n)
{
char *p;
if ((n == config.zone[1]) || cminternal || !opus_1)
return (pkt_dir);
strcpy (munger, pkt_dir);
p = munger + strlen (munger) - 1;
sprintf (p, ".%03x\\", n);
return (munger);
}
int open_outfile (
AREAS_PTR which,
int j)
{
int outfile, i, done;
char template[80];
if (which->handle[j] > 0)
{
return (which->handle[j]);
}
/* First create the name of the file */
sprintf (template, "%s%04x%04x.%s", mung_path (which->zone[j]), which->net[j], which->node[j],
((opus_1 && !cminternal)?"OUT":"FS$"));
outfile = open(template, O_RDONLY|O_BINARY);
/* If outfile does not exist, then create it */
if (outfile == -1)
{
/* Attempt to create the file */
outfile = open(template, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, S_IREAD|S_IWRITE);
/* If unsuccessful we have to get rid of a handle and try again */
if (outfile == -1)
{
if ((last_opened >= 0) && (which->handle[last_opened] > 0))
{
i = last_opened;
}
else
{
done = 0;
while (!done)
{
for (i = j - 1; i >= 0; i--)
{
if (which->handle[i] > 0)
{
done = 1;
break;
}
}
if (done)
break;
for (i = which->num_nodes - 1; i > j; i--)
{
if (which->handle[i] > 0)
{
done = 1;
break;
}
}
if (done)
break;
return (-1);
}
}
(void) fast_write (which->handle[i], "\0\0", 2);
(void) fast_close (which->handle[i]);
which->handle[i] = -1;
outfile = open(template, O_RDONLY|O_BINARY);
if (outfile == -1)
{
outfile = open (template, O_WRONLY|O_CREAT|O_BINARY, S_IREAD|S_IWRITE);
if (outfile == -1)
{
return (-1);
}
else
{
last_opened = j;
}
}
else
{
(void) fast_close (outfile);
if ((outfile = open (template, O_RDWR|O_BINARY)) == -1)
{
return (-1);
}
if (fast_lseek (outfile, -2L, SEEK_END) == -1L)
{
return (-1);
}
which->handle[j] = outfile;
if (wbuffs[outfile].wbuff == NULL)
{
wbuffs[outfile].wbuff = wbuffs[outfile].wptr = _fmalloc (BIGSIZE);
if (wbuffs[outfile].wbuff != NULL)
wbuffs[outfile].wsize = 0;
}
last_opened = j;
return (outfile);
}
}
/* Otherwise fill in the structure */
pkt_hdr.dest_net = which->net[j];
pkt_hdr.dest_node = which->node[j];
for (i = 0; i < num_pw; i++)
{
/* Find out if we have a password for this guy */
if ((pw[i]->net == which->net[j]) && (pw[i]->node == which->node[j]))
{
strncpy (pkt_hdr.password, pw[i]->password, 8);
break;
}
}
fast_write(outfile,(char *)&pkt_hdr,sizeof(PKTHDR));
}
else
{
(void) fast_close (outfile);
if ((outfile = open (template, O_RDWR|O_BINARY)) == -1)
{
return (-1);
}
/* If we can't seek then we are in trouble */
if (fast_lseek (outfile,-2L,SEEK_END) == -1L)
{
return (-1);
}
}
which->handle[j] = outfile;
memset ((char *) pkt_hdr.password, 0, 8);
if (wbuffs[outfile].wbuff == NULL)
{
wbuffs[outfile].wbuff = wbuffs[outfile].wptr = _fmalloc (BIGSIZE);
if (wbuffs[outfile].wbuff != NULL)
wbuffs[outfile].wsize = 0;
}
return (outfile);
}
int open1_outfile ()
{
/* int outfile, i, done; */
int outfile, i;
char template[80];
/* First create the name of the file */
sprintf (template, "%s%04x%04x.%s", pkt_dir, h->dest_net, h->dest,
((opus_1 && !cminternal)?"OUT":"FS$"));
outfile = open(template, O_RDONLY|O_BINARY);
/* If outfile does not exist, then create it */
if (outfile == -1)
{
/* Attempt to create the file */
outfile = open(template, O_WRONLY|O_CREAT|O_BINARY, S_IREAD|S_IWRITE);
/* If unsuccessful return the bad news */
if (outfile == -1)
{
return (-1);
}
if (wbuffs[outfile].wbuff == NULL)
{
wbuffs[outfile].wbuff = wbuffs[outfile].wptr = _fmalloc (BIGSIZE);
if (wbuffs[outfile].wbuff != NULL)
wbuffs[outfile].wsize = 0;
}
/* Fill in the structure */
pkt_hdr.dest_net = h->dest_net;
pkt_hdr.dest_node = h->dest;
for (i = 0; i < num_pw; i++)
{
/* Find out if we have a password for this guy */
if ((pw[i]->net == h->dest_net) && (pw[i]->node == h->dest))
{
strncpy (pkt_hdr.password, pw[i]->password, 8);
break;
}
}
fast_write(outfile,(char *)&pkt_hdr,sizeof(PKTHDR));
}
else
{
(void) fast_close (outfile);
if ((outfile = open (template, O_RDWR|O_BINARY)) == -1)
{
return (-1);
}
/* If we can't seek then we are in trouble */
if (fast_lseek (outfile,-2L,SEEK_END) == -1L)
{
return (-1);
}
}
memset ((char *) pkt_hdr.password, 0, 8);
if (wbuffs[outfile].wbuff == NULL)
{
wbuffs[outfile].wbuff = wbuffs[outfile].wptr = _fmalloc (BIGSIZE);
if (wbuffs[outfile].wbuff != NULL)
wbuffs[outfile].wsize = 0;
}
return (outfile);
}
void disk_full (
int f,
char *fname1)
{
fast_close (f);
if (fname1[0] != '\0')
{
unlink (fname1);
}
printf ("\n%s Could not write - Disk Full???\n", fname1);
}