home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MXMS_160.LZH / MSGHELP.C < prev    next >
C/C++ Source or Header  |  1991-06-26  |  8KB  |  226 lines

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