home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MAXMAILP.ZIP / MSGHELP.C < prev    next >
C/C++ Source or Header  |  1991-01-02  |  9KB  |  269 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    MSGHELP.C   : Maximus message handler routines                        */
  4. /*                  Version 1.0 by Craig Derouen                            */
  5. /*                    Currently Maximus only handles old fido style         */
  6. /*                    1.MSG message format, so only will here as well.      */
  7. /*                    But that is expected to change soon!                  */
  8. /*                    And I will add routines to support it.                */
  9. /*                                                                          */
  10. /*                   Alll message calls handling FIDO style messages        */
  11. /*                   will have "fido" embedded in the function name         */
  12. /*                   so its easy to distinguish. The new Maximus            */
  13. /*                   format will have "max" in replace of "fido"            */
  14. /*                   when it is stable. Actually Maximus will still         */
  15. /*                   support the old style on an area by area basis.        */
  16. /*                                                                          */
  17. /*                   All these functions are as self-contained as           */
  18. /*                   possible.                                              */
  19. /****************************************************************************/
  20. #if defined OS2
  21.   #define INCL_DOS
  22.   #define INCL_DOSDATETIME
  23.   #include <os2.h>
  24. #endif
  25.  
  26. #include "MaxMail.h"
  27.  
  28. static int     fido_init = FALSE;
  29. static char    *msgbuffer;
  30.  
  31.  
  32. /* You MUST call this first to initialize things */
  33. int fidomsg_init()
  34. {
  35.    msgbuffer = (char *) malloc(((unsigned) MAXFMSGSIZ));
  36.    if (msgbuffer == NULL)  
  37.       return(MEMORY_ALLOC_ERR);
  38.    fido_init = TRUE;
  39.    return(0);
  40. }
  41.  
  42. /* Return the highest message in an area. -1 if error
  43.    msgpath:  DOS path for messages (has a trailing \)  */
  44. #if defined OS2
  45. int find_fidohigh(char *msgpath)
  46. {
  47.    register    int  highest=0;
  48.    int      h;
  49.    char    apath[66];
  50.  
  51.    FILEFINDBUF c_file;
  52.    USHORT HANDLE;
  53.    unsigned count;
  54.  
  55.    HANDLE = 0xffff;
  56.    count = 1;
  57.  
  58.    if (!fido_init)
  59.       return(NOT_INITIALIZED);
  60.    sprintf(apath,"%s*.MSG",msgpath);
  61.  
  62.    /* done = _dos_findfirst(apath,0,&c_file); */
  63.    DosFindFirst((char far *) apath, (USHORT far *) &HANDLE,
  64.    0x0, (FILEFINDBUF far *) &c_file, sizeof(c_file),
  65.    (unsigned far *) &count,0L);
  66.  
  67.    if (!count)  {
  68.       /* Invalid Path and/or empty directory!! */
  69.       return(-1);
  70.    }
  71.    while (count!=0 && highest < MAXFMSGS){
  72.       h = atoi(c_file.achName);
  73.       if (h > highest) highest = h;
  74.       DosFindNext(HANDLE, (FILEFINDBUF far *) &c_file, sizeof(c_file),
  75.       (unsigned far *) &count);
  76.    }
  77.    DosFindClose(HANDLE);
  78.    return(highest);
  79. }
  80. #else
  81. int find_fidohigh(char *msgpath)
  82. {
  83.    register    int  done,highest=0;
  84.    int      h;
  85.    struct  find_t  c_file;
  86.    char    apath[66];
  87.  
  88.    if (!fido_init)
  89.       return(NOT_INITIALIZED);
  90.    sprintf(apath,"%s*.MSG",msgpath);
  91.    done = _dos_findfirst(apath,0,&c_file);
  92.    if (done)  {
  93.       /* Invalid Path and/or empty directory!! */
  94.       return(-1);
  95.    }
  96.    while (!done && highest < MAXFMSGS){
  97.       h = atoi(c_file.name);
  98.       if (h>highest) highest=h;
  99.        done=_dos_findnext(&c_file);
  100.    }
  101.    return(highest);
  102. }
  103. #endif
  104.  
  105. /* Read a Fido style message to a file
  106.    i:      Message #
  107.    flags:  Types of Messages and whether to output to screen also
  108.    path:   Message path (with trailing \)
  109.    output: Output file to write to
  110. */
  111.  
  112. int file_fidomsg(int i,int flags,FILE *output,struct msgupd_st *msgupd,struct msghead *MSGHD)
  113. {
  114.    int         infp,j,m,n,start;
  115.    char        msg_name[65];
  116.    char        msg_line[81];
  117.    struct      _msg        amsg;
  118. #if !defined OS2
  119.    struct      find_t      c_file;
  120. #endif
  121.    long           size;
  122.    char        temp[9];
  123.  
  124. #if defined OS2
  125.    FILEFINDBUF c_file;
  126.    USHORT HANDLE = 0xffff;
  127.    unsigned count = 1;
  128. #endif
  129.  
  130.    if (!fido_init)
  131.       return(NOT_INITIALIZED);
  132.    j=n=0;
  133.    sprintf(msg_name,"%s%u%s",msgupd->msgpath,i,".MSG");
  134.  
  135. #if defined OS2
  136.    if (DosFindFirst((char far *) msg_name, (USHORT far *) &HANDLE,
  137.    0x0, (FILEFINDBUF far *) &c_file, sizeof(c_file),
  138.    (unsigned far *) &count,0L) !=0)
  139.       return(FILE_SRCH_ERR);
  140.  
  141.    size=c_file.cbFile - ((long) sizeof(struct _msg));
  142.    DosFindClose(HANDLE);
  143. #else
  144.    if (_dos_findfirst(msg_name,0,&c_file) !=0)
  145.       return(FILE_SRCH_ERR);
  146.  
  147.    size=c_file.size - ((long) sizeof(struct _msg));
  148. #endif
  149.    infp=sopen(msg_name,O_RDONLY|O_BINARY,SH_DENYNO,S_IREAD);
  150.    if (infp < 0) 
  151.       return(FILE_OPEN_ERR);
  152.  
  153.    j=read(infp,(char *) &amsg,sizeof(struct _msg));
  154.    if (j != sizeof(struct _msg)) 
  155.       return(FILE_READ_ERR);
  156.  
  157.    j=read(infp,msgbuffer,size);
  158.    if (j != size) 
  159.       return(FILE_READ_ERR);
  160.  
  161.    close(infp);
  162.    if ( (flags & PRIVATE) && (amsg.attr & MSGPRIVATE)) {
  163.       if (strcmpi(amsg.to,MSGHD->ourname) != 0)
  164.          return(PVTMSG_ERR);        /* No messages to read */
  165.    }
  166.  
  167.    if (infp) {        /* We had a valid MSG file */
  168.       fprintf(output,"\n\nMESSAGE #:   %u  of %u",i,msgupd->himsg);
  169.       if (amsg.up)
  170.          fprintf(output,"  (replies: #%d)",amsg.up);
  171.       fprintf(output,"\n");
  172.       fprintf(output,"FROM:        %s",amsg.from);
  173.       strcpy(MSGHD->from,amsg.from);
  174.       strcpy(MSGHD->subject,amsg.subj);
  175.       if (amsg.attr & MSGPRIVATE) fprintf(output,"     (PRIVATE)");
  176.       if (amsg.reply)
  177.          fprintf(output," (reply to #%d)",amsg.reply);
  178.       if (strcmpi(amsg.to,MSGHD->ourname) == 0) {
  179.          fprintf(output,"\nTO:          %s       <<PERSONAL MESSAGE>>\n",amsg.to);
  180.          j = MSGHD->ourmail;
  181.          MSGHD->ourmail++;
  182.          if (MSGHD->answers == NULL) {        /* Start of list */
  183.             MSGHD->answers = (int *) calloc(1,sizeof(int));
  184.             if (MSGHD->answers == NULL)
  185.                aborterror(BADALLOC,NULL);
  186.          }
  187.          else {
  188.             MSGHD->answers = (int *) realloc(MSGHD->answers,(j + 1) * sizeof(int));
  189.             if (MSGHD->answers == NULL)
  190.                aborterror(BADALLOC,NULL);
  191.          }
  192.          MSGHD->answers[j] = i;
  193.       }
  194.       else fprintf(output,"\nTO:          %s\n",amsg.to);
  195.       fprintf(output,"Subject:     %s\n",amsg.subj);
  196.       fprintf(output,"Date:        %s\n\n",amsg.date);
  197.  
  198.        j=0;
  199.        start=0;
  200.       while (msgbuffer[j] == '\0') {
  201.          j++;
  202.          start++;
  203.       }
  204.  
  205.       for (j=start;j<=size;j++) {        /* Search for SEEN-BY, returns */
  206.          if (msgbuffer[j] == SOFT) {
  207.             if (msgbuffer[j-1] == '\n') {
  208.                msgbuffer[j-2]=' ';
  209.                msgbuffer[j-1]=' ';
  210.             }
  211.          }
  212.          if ((msgbuffer[j] == SOFT) || (msgbuffer[j]=='\r')) {
  213.             if (msgbuffer[j+1] == '\r')
  214.                msgbuffer[j]='\n';
  215.             if (msgbuffer[j+1] != '\n')
  216.                msgbuffer[j]='\n';
  217.             else msgbuffer[j]=' ';
  218.          }
  219.          if (msgbuffer[j+1] == 'S'){
  220.             if ((msgbuffer[j]=='\012') || (msgbuffer[j] == '\001')) {
  221.                for (m=0;m<=7;m++)
  222.                   temp[m]=msgbuffer[j+m+1];
  223.                temp[8]='\0';
  224.                if (strcmp(temp,SEEN) ==0) size=j-1;
  225.             }
  226.          }
  227.       }        /* End of search for SEEN-BY, returns */
  228.       for (j=start;j<=size;j++) {
  229.          if (msgbuffer[j] == '\001' ) {
  230.             while (msgbuffer[j] == '\001') {
  231.                while ((msgbuffer[j] != 0x0A) && (msgbuffer[j] !=0x0D))
  232.                   j++;
  233.                while ((msgbuffer[j] < ' ') && (msgbuffer[j] > '\001'))
  234.                   j++;
  235.             }
  236.          }
  237.          msg_line[n] = msgbuffer[j];
  238.          if (n>79) {        /* Do word-wrap */
  239.             do {
  240.                n--;
  241.                j--;
  242.             } while ((msg_line[n] != ' ') && (n>10));
  243.             msg_line[n] = '\n';
  244.          }        /* End of word-wrap */
  245.          else if (msg_line[n] != '\n') {
  246.             n++;
  247.             msg_line[n] = 0x00;
  248.          }
  249.          if ((msgbuffer[j] == '\n') || (msg_line[n] == '\n')) {
  250.             msg_line[n+1] = 0x00;
  251.             n = 0;
  252.             fputs(msg_line,output);
  253.             msg_line[0] = 0x00;
  254.             msg_line[1] = 0x00;
  255.          }
  256.       }
  257.       if (msg_line[n] != '\n') {
  258.          msg_line[n+1] = '\n';
  259.          n++;
  260.       }
  261.       msg_line[n+1] = 0x00;
  262.       fputs(msg_line,output);
  263.    }
  264.  
  265.    if (fflush(output) == EOF) 
  266.       return(FILE_WRITE_ERR);
  267.    return(i);        /* No errors!, return message number */
  268. }
  269.