home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 2 BBS
/
02-BBS.zip
/
MAXMAILP.ZIP
/
MSGHELP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-01-02
|
9KB
|
269 lines
/****************************************************************************/
/* */
/* MSGHELP.C : Maximus message handler routines */
/* Version 1.0 by Craig Derouen */
/* Currently Maximus only handles old fido style */
/* 1.MSG message format, so only will here as well. */
/* But that is expected to change soon! */
/* And I will add routines to support it. */
/* */
/* Alll message calls handling FIDO style messages */
/* will have "fido" embedded in the function name */
/* so its easy to distinguish. The new Maximus */
/* format will have "max" in replace of "fido" */
/* when it is stable. Actually Maximus will still */
/* support the old style on an area by area basis. */
/* */
/* All these functions are as self-contained as */
/* possible. */
/****************************************************************************/
#if defined OS2
#define INCL_DOS
#define INCL_DOSDATETIME
#include <os2.h>
#endif
#include "MaxMail.h"
static int fido_init = FALSE;
static char *msgbuffer;
/* You MUST call this first to initialize things */
int fidomsg_init()
{
msgbuffer = (char *) malloc(((unsigned) MAXFMSGSIZ));
if (msgbuffer == NULL)
return(MEMORY_ALLOC_ERR);
fido_init = TRUE;
return(0);
}
/* Return the highest message in an area. -1 if error
msgpath: DOS path for messages (has a trailing \) */
#if defined OS2
int find_fidohigh(char *msgpath)
{
register int highest=0;
int h;
char apath[66];
FILEFINDBUF c_file;
USHORT HANDLE;
unsigned count;
HANDLE = 0xffff;
count = 1;
if (!fido_init)
return(NOT_INITIALIZED);
sprintf(apath,"%s*.MSG",msgpath);
/* done = _dos_findfirst(apath,0,&c_file); */
DosFindFirst((char far *) apath, (USHORT far *) &HANDLE,
0x0, (FILEFINDBUF far *) &c_file, sizeof(c_file),
(unsigned far *) &count,0L);
if (!count) {
/* Invalid Path and/or empty directory!! */
return(-1);
}
while (count!=0 && highest < MAXFMSGS){
h = atoi(c_file.achName);
if (h > highest) highest = h;
DosFindNext(HANDLE, (FILEFINDBUF far *) &c_file, sizeof(c_file),
(unsigned far *) &count);
}
DosFindClose(HANDLE);
return(highest);
}
#else
int find_fidohigh(char *msgpath)
{
register int done,highest=0;
int h;
struct find_t c_file;
char apath[66];
if (!fido_init)
return(NOT_INITIALIZED);
sprintf(apath,"%s*.MSG",msgpath);
done = _dos_findfirst(apath,0,&c_file);
if (done) {
/* Invalid Path and/or empty directory!! */
return(-1);
}
while (!done && highest < MAXFMSGS){
h = atoi(c_file.name);
if (h>highest) highest=h;
done=_dos_findnext(&c_file);
}
return(highest);
}
#endif
/* Read a Fido style message to a file
i: Message #
flags: Types of Messages and whether to output to screen also
path: Message path (with trailing \)
output: Output file to write to
*/
int file_fidomsg(int i,int flags,FILE *output,struct msgupd_st *msgupd,struct msghead *MSGHD)
{
int infp,j,m,n,start;
char msg_name[65];
char msg_line[81];
struct _msg amsg;
#if !defined OS2
struct find_t c_file;
#endif
long size;
char temp[9];
#if defined OS2
FILEFINDBUF c_file;
USHORT HANDLE = 0xffff;
unsigned count = 1;
#endif
if (!fido_init)
return(NOT_INITIALIZED);
j=n=0;
sprintf(msg_name,"%s%u%s",msgupd->msgpath,i,".MSG");
#if defined OS2
if (DosFindFirst((char far *) msg_name, (USHORT far *) &HANDLE,
0x0, (FILEFINDBUF far *) &c_file, sizeof(c_file),
(unsigned far *) &count,0L) !=0)
return(FILE_SRCH_ERR);
size=c_file.cbFile - ((long) sizeof(struct _msg));
DosFindClose(HANDLE);
#else
if (_dos_findfirst(msg_name,0,&c_file) !=0)
return(FILE_SRCH_ERR);
size=c_file.size - ((long) sizeof(struct _msg));
#endif
infp=sopen(msg_name,O_RDONLY|O_BINARY,SH_DENYNO,S_IREAD);
if (infp < 0)
return(FILE_OPEN_ERR);
j=read(infp,(char *) &amsg,sizeof(struct _msg));
if (j != sizeof(struct _msg))
return(FILE_READ_ERR);
j=read(infp,msgbuffer,size);
if (j != size)
return(FILE_READ_ERR);
close(infp);
if ( (flags & PRIVATE) && (amsg.attr & MSGPRIVATE)) {
if (strcmpi(amsg.to,MSGHD->ourname) != 0)
return(PVTMSG_ERR); /* No messages to read */
}
if (infp) { /* We had a valid MSG file */
fprintf(output,"\n\nMESSAGE #: %u of %u",i,msgupd->himsg);
if (amsg.up)
fprintf(output," (replies: #%d)",amsg.up);
fprintf(output,"\n");
fprintf(output,"FROM: %s",amsg.from);
strcpy(MSGHD->from,amsg.from);
strcpy(MSGHD->subject,amsg.subj);
if (amsg.attr & MSGPRIVATE) fprintf(output," (PRIVATE)");
if (amsg.reply)
fprintf(output," (reply to #%d)",amsg.reply);
if (strcmpi(amsg.to,MSGHD->ourname) == 0) {
fprintf(output,"\nTO: %s <<PERSONAL MESSAGE>>\n",amsg.to);
j = MSGHD->ourmail;
MSGHD->ourmail++;
if (MSGHD->answers == NULL) { /* Start of list */
MSGHD->answers = (int *) calloc(1,sizeof(int));
if (MSGHD->answers == NULL)
aborterror(BADALLOC,NULL);
}
else {
MSGHD->answers = (int *) realloc(MSGHD->answers,(j + 1) * sizeof(int));
if (MSGHD->answers == NULL)
aborterror(BADALLOC,NULL);
}
MSGHD->answers[j] = i;
}
else fprintf(output,"\nTO: %s\n",amsg.to);
fprintf(output,"Subject: %s\n",amsg.subj);
fprintf(output,"Date: %s\n\n",amsg.date);
j=0;
start=0;
while (msgbuffer[j] == '\0') {
j++;
start++;
}
for (j=start;j<=size;j++) { /* Search for SEEN-BY, returns */
if (msgbuffer[j] == SOFT) {
if (msgbuffer[j-1] == '\n') {
msgbuffer[j-2]=' ';
msgbuffer[j-1]=' ';
}
}
if ((msgbuffer[j] == SOFT) || (msgbuffer[j]=='\r')) {
if (msgbuffer[j+1] == '\r')
msgbuffer[j]='\n';
if (msgbuffer[j+1] != '\n')
msgbuffer[j]='\n';
else msgbuffer[j]=' ';
}
if (msgbuffer[j+1] == 'S'){
if ((msgbuffer[j]=='\012') || (msgbuffer[j] == '\001')) {
for (m=0;m<=7;m++)
temp[m]=msgbuffer[j+m+1];
temp[8]='\0';
if (strcmp(temp,SEEN) ==0) size=j-1;
}
}
} /* End of search for SEEN-BY, returns */
for (j=start;j<=size;j++) {
if (msgbuffer[j] == '\001' ) {
while (msgbuffer[j] == '\001') {
while ((msgbuffer[j] != 0x0A) && (msgbuffer[j] !=0x0D))
j++;
while ((msgbuffer[j] < ' ') && (msgbuffer[j] > '\001'))
j++;
}
}
msg_line[n] = msgbuffer[j];
if (n>79) { /* Do word-wrap */
do {
n--;
j--;
} while ((msg_line[n] != ' ') && (n>10));
msg_line[n] = '\n';
} /* End of word-wrap */
else if (msg_line[n] != '\n') {
n++;
msg_line[n] = 0x00;
}
if ((msgbuffer[j] == '\n') || (msg_line[n] == '\n')) {
msg_line[n+1] = 0x00;
n = 0;
fputs(msg_line,output);
msg_line[0] = 0x00;
msg_line[1] = 0x00;
}
}
if (msg_line[n] != '\n') {
msg_line[n+1] = '\n';
n++;
}
msg_line[n+1] = 0x00;
fputs(msg_line,output);
}
if (fflush(output) == EOF)
return(FILE_WRITE_ERR);
return(i); /* No errors!, return message number */
}