/[mailutils]/mailutils/imap4d/fetch.c
ViewVC logotype

Diff of /mailutils/imap4d/fetch.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.29 by gray, Fri Jul 26 11:18:50 2002 UTC revision 1.30 by gray, Sat Jul 27 14:47:31 2002 UTC
# Line 52  static int fetch_uid               __P ( Line 52  static int fetch_uid               __P (
52  static int fetch_envelope0         __P ((message_t));  static int fetch_envelope0         __P ((message_t));
53  static int fetch_bodystructure0    __P ((message_t, int));  static int fetch_bodystructure0    __P ((message_t, int));
54  static int bodystructure           __P ((message_t, int));  static int bodystructure           __P ((message_t, int));
55  static int send_parameter_list     __P ((char *));  static void send_parameter_list    __P ((char *));
56  static int fetch_operation         __P ((message_t, char **, int));  static int fetch_operation         __P ((message_t, char **, int));
57  static int fetch_message           __P ((message_t, unsigned long, unsigned long));  static int fetch_message           __P ((message_t, unsigned long, unsigned long));
58  static int fetch_header            __P ((message_t, unsigned long, unsigned long));  static int fetch_header            __P ((message_t, unsigned long, unsigned long));
# Line 528  fetch_body (struct fetch_command *comman Line 528  fetch_body (struct fetch_command *comman
528    
529  /* Helper Functions: Where the Beef is.  */  /* Helper Functions: Where the Beef is.  */
530    
531    static void
532    fetch_send_header_value (header_t header, const char *name,
533                             const char *defval, int space)
534    {
535      char *buffer;
536      
537      if (space)
538        util_send (" ");
539      if (header_aget_value (header, name, &buffer) == 0)
540        {
541          util_send_qstring (buffer);
542          free (buffer);
543        }
544      else
545        util_send (defval ? defval : "NIL");
546    }
547    
548    static void
549    fetch_send_header_list (header_t header, const char *name,
550                             const char *defval, int space)
551    {
552      char *buffer;
553      
554      if (space)
555        util_send (" ");
556      if (header_aget_value (header, name, &buffer) == 0)
557        {
558          send_parameter_list (buffer);
559          free (buffer);
560        }
561      else
562        util_send (defval ? defval : "NIL");
563    }
564    
565    static void
566    fetch_send_header_address (header_t header, const char *name,
567                               const char *defval, int space)
568    {
569      char *buffer;
570      
571      if (space)
572        util_send (" ");
573      if (header_aget_value (header, name, &buffer) == 0)
574        {
575          fetch_send_address (buffer);
576          free (buffer);
577        }
578      else
579        fetch_send_address (defval ? defval : "NIL");
580    }
581    
582  /* ENVELOPE:  /* ENVELOPE:
583     The envelope structure of the message.  This is computed by the server by     The envelope structure of the message.  This is computed by the server by
584     parsing the [RFC-822] header into the component parts, defaulting various     parsing the [RFC-822] header into the component parts, defaulting various
# Line 541  fetch_body (struct fetch_command *comman Line 592  fetch_body (struct fetch_command *comman
592  static int  static int
593  fetch_envelope0 (message_t msg)  fetch_envelope0 (message_t msg)
594  {  {
   char *buffer = NULL;  
595    char *from = NULL;    char *from = NULL;
596    header_t header = NULL;    header_t header = NULL;
597    
598    message_get_header (msg, &header);    message_get_header (msg, &header);
599    
600    /* Date:  */    fetch_send_header_value (header, "Date", NULL, 0);
601    header_aget_value (header, "Date", &buffer);    fetch_send_header_value (header, "Subject", NULL, 1);
   util_send_qstring (buffer);  
   free (buffer);  
   util_send (" ");  
   
   /* Subject:  */  
   header_aget_value (header, "Subject", &buffer);  
   util_send_qstring (buffer);  
   free (buffer);  
   util_send (" ");  
602    
603    /* From:  */    /* From:  */
604    header_aget_value (header, "From", &from);    header_aget_value (header, "From", &from);
605    fetch_send_address (from);    fetch_send_address (from);
606    util_send (" ");    util_send (" ");
607    
608    /* Sender:  */    fetch_send_header_address (header, "Sender", from, 1);
609    /* Note that the server MUST default the reply-to and sender fields from    fetch_send_header_address (header, "Reply-to", from, 1);
610       the From field; a client is not expected to know to do this. */    fetch_send_header_address (header, "To", NULL, 1);
611    header_aget_value (header, "Sender", &buffer);    fetch_send_header_address (header, "Cc", NULL, 1);
612    fetch_send_address (buffer ? buffer : from);    fetch_send_header_address (header, "Bcc", NULL, 1);
613    free (buffer);    fetch_send_header_value (header, "In-Reply-To", NULL, 1);
614    util_send (" ");    fetch_send_header_value (header, "Message-ID", NULL, 1);
   
   /* Reply-To:  */  
   /* Note that the server MUST default the reply-to and sender fields from  
      the From field; a client is not expected to know to do this. */  
   header_aget_value (header, "Reply-to", &buffer);  
   fetch_send_address (buffer ? buffer : from);  
   free (buffer);  
   util_send (" ");  
   
   /* To:  */  
   header_aget_value (header, "To", &buffer);  
   fetch_send_address (buffer);  
   free (buffer);  
   util_send (" ");  
   
   /* Cc:  */  
   header_aget_value (header, "Cc", &buffer);  
   fetch_send_address (buffer);  
   free (buffer);  
   util_send (" ");  
615    
   /* Bcc:  */  
   header_aget_value (header, "Bcc", &buffer);  
   fetch_send_address (buffer);  
   free (buffer);  
   util_send (" ");  
   
   /* In-Reply-To:  */  
   header_aget_value (header, "In-Reply-To", &buffer);  
   util_send_qstring (buffer);  
   free (buffer);  
   util_send (" ");  
   
   /* Message-ID:  */  
   header_aget_value (header, "Message-ID", &buffer);  
   util_send_qstring (buffer);  
   
   free (buffer);  
616    free (from);    free (from);
617    return RESP_OK;    return RESP_OK;
618  }  }
# Line 641  fetch_bodystructure0 (message_t message, Line 645  fetch_bodystructure0 (message_t message,
645    size_t nparts = 1;    size_t nparts = 1;
646    size_t i;    size_t i;
647    int is_multipart = 0;    int is_multipart = 0;
648    
649    message_is_multipart (message, &is_multipart);    message_is_multipart (message, &is_multipart);
650    if (is_multipart)    if (is_multipart)
651      {      {
652        char *buffer = NULL;        char *buffer = NULL;
       char *s;  
       char *sp = NULL;  
653        header_t header = NULL;        header_t header = NULL;
654    
655        message_get_num_parts (message, &nparts);        message_get_num_parts (message, &nparts);
# Line 663  fetch_bodystructure0 (message_t message, Line 666  fetch_bodystructure0 (message_t message,
666    
667        message_get_header (message, &header);        message_get_header (message, &header);
668    
669    
670        /* The subtype.  */        /* The subtype.  */
671        if (header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer) == 0)        if (header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer) == 0)
672          {          {
673             s = strtok_r (buffer, " \t\r\n;", &sp);            int argc = 0;
674             if (s)            char **argv;
675               {            char *s;
676                 s = strchr (s, '/');            
677                 if (s)            argcv_get (buffer, " \t\r\n;=", NULL, &argc, &argv);
678                   *s++ = '\0';  
679               }            s = strchr (argv[0], '/');
680          }            if (s)
681        util_send (" ");              s++;
682        util_send_qstring (s);            util_send (" ");
683              util_send_qstring (s);
684    
685        /* The extension data for multipart. */            /* The extension data for multipart. */
686        if (extension)            if (extension)
         {  
           while (sp && *sp && isspace ((unsigned)*sp)) sp++;  
           /* body parameter parenthesized list: Content-type parameter list. */  
           if (sp && *sp)  
687              {              {
688                  int space = 0;
689                  char *lvalue = NULL;
690                  
691                util_send (" (");                util_send (" (");
692                {                for (i = 1; i < argc; i++)
693                  int space = 0;                  {
694                  while ((s = strtok_r (NULL, " \t\r\n;", &sp)))                    /* body parameter parenthesized list:
695                    {                       Content-type parameter list. */
696                      char *p = strchr (s, '=');                    if (lvalue)
697                      if (p)                      {
698                        *p++ = '\0';                        if (space)
                     if (space)  
                       util_send (" ");  
                     space = 1;  
                     util_send_qstring (s);  
                     if (p)  
                       {  
699                          util_send (" ");                          util_send (" ");
700                          util_unquote (&p);                        util_send_qstring (lvalue);
701                          util_send_qstring (p);                        lvalue = NULL;
702                        }                        space = 1;
703                    }                      }
704                }  
705                      switch (argv[i][0])
706                        {
707                        case ';':
708                          continue;
709                          
710                        case '=':
711                          if (++i < argc)
712                            {
713                              char *p = argv[i];
714                              util_send (" ");
715                              util_unquote (&p);
716                              util_send_qstring (p);
717                            }
718                          break;
719                          
720                        default:
721                          lvalue = argv[i];
722                        }
723                    }
724                  if (lvalue)
725                    {
726                      if (space)
727                        util_send (" ");
728                      util_send_qstring (lvalue);
729                    }
730                util_send (")");                util_send (")");
731              }              }
732            else            else
733              util_send (" NIL");              util_send (" NIL");
734              argcv_free (argc, argv);
735            free (buffer);            free (buffer);
736            }
           /* body disposition: Content-Disposition.  */  
           header_aget_value (header, MU_HEADER_CONTENT_DISPOSITION, &buffer);  
           util_send (" ");  
           send_parameter_list (buffer);  
           free (buffer);  
   
           /* body language: Content-Language.  */  
           header_aget_value (header, MU_HEADER_CONTENT_LANGUAGE, &buffer);  
           util_send (" ");  
           send_parameter_list (buffer);  
           free (buffer);  
         } /* extension */  
737        else        else
738          free (buffer);          /* No content-type header */
739            util_send (" NIL");
740    
741          /* body disposition: Content-Disposition.  */
742          fetch_send_header_list (header, MU_HEADER_CONTENT_DISPOSITION,
743                                  NULL, 1);
744          /* body language: Content-Language.  */
745          fetch_send_header_list (header, MU_HEADER_CONTENT_LANGUAGE,
746                                  NULL, 1);
747      }      }
748    else    else
749      bodystructure (message, extension);      bodystructure (message, extension);
# Line 778  static int Line 798  static int
798  bodystructure (message_t msg, int extension)  bodystructure (message_t msg, int extension)
799  {  {
800    header_t header = NULL;    header_t header = NULL;
   char *sp = NULL;  
801    char *buffer = NULL;    char *buffer = NULL;
   char *s;  
802    size_t blines = 0;    size_t blines = 0;
803    int message_rfc822 = 0;    int message_rfc822 = 0;
804    int text_plain = 0;    int text_plain = 0;
805    
806    message_get_header (msg, &header);    message_get_header (msg, &header);
807    
808    /* body type:  Content-Type    if (header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer) == 0)
      body subtype: */  
   if (header_aget_value (header, MU_HEADER_CONTENT_TYPE, &buffer) == 0  
       && (s = strtok_r (buffer, " \t\r\n;", &sp)) != NULL)  
809      {      {
810        char *p = strchr (s, '/');        int argc = 0;
811        if (strcasecmp (s, "MESSAGE/RFC822") == 0)        char **argv;
812          char *s, *p;
813              
814          argcv_get (buffer, " \t\r\n;=", NULL, &argc, &argv);
815    
816          if (strcasecmp (argv[0], "MESSAGE/RFC822") == 0)
817          message_rfc822 = 1;          message_rfc822 = 1;
818        if (strcasecmp (s, "TEXT/PLAIN") == 0)        else if (strcasecmp (argv[0], "TEXT/PLAIN") == 0)
819          text_plain = 1;          text_plain = 1;
820        if (p)  
821          *p++ = '\0';        s = strchr (argv[0], '/');
822        /* MIME media type and subtype  */        if (s)
823        util_send_qstring (s);          *s++ = 0;
824        util_send (" ");        p = argv[0];
       util_unquote (&p);  
825        util_send_qstring (p);        util_send_qstring (p);
     }  
   else  
     {  
       /* Default? If Content-Type is not present consider as text/plain.  */  
       util_send_qstring ("TEXT");  
826        util_send (" ");        util_send (" ");
827        util_send_qstring ("PLAIN");        util_send_qstring (s);
       text_plain = 1;  
     }  
   
   while (sp != NULL && *sp && isspace ((unsigned)*sp)) sp++;  
     
   /* body parameter parenthesized list: Content-type attributes */  
   if ((sp != NULL && *sp) || text_plain)  
     {  
       int space = 0;  
       int have_charset = 0;  
828    
829        util_send (" (");        /* body parameter parenthesized list: Content-type attributes */
830        if (sp)        if (argc > 1 || text_plain)
831          {          {
832            /* Content-type parameter list. */            int space = 0;
833            while ((s = strtok_r (NULL, " \t\r\n;", &sp)))            char *lvalue = NULL;
834              int have_charset = 0;
835              int i;
836              
837              util_send (" (");
838              for (i = 1; i < argc; i++)
839                {
840                  /* body parameter parenthesized list:
841                     Content-type parameter list. */
842                  if (lvalue)
843                    {
844                      if (space)
845                        util_send (" ");
846                      util_send_qstring (lvalue);
847                      lvalue = NULL;
848                      space = 1;
849                    }
850                  
851                  switch (argv[i][0])
852                    {
853                    case ';':
854                      continue;
855                      
856                    case '=':
857                      if (++i < argc)
858                        {
859                          char *p = argv[i];
860                          util_send (" ");
861                          util_unquote (&p);
862                          util_send_qstring (p);
863                        }
864                      break;
865                      
866                    default:
867                      lvalue = argv[i];
868                      if (strcasecmp (lvalue, "charset") == 0)
869                        have_charset = 1;
870    
871                    }
872                }
873              
874              if (lvalue)
875              {              {
               char *p = strchr (s, '=');  
               if (p)  
                 *p++ = '\0';  
876                if (space)                if (space)
877                  util_send (" ");                  util_send (" ");
878                util_send_qstring (s);                util_send_qstring (lvalue);
               util_send (" ");  
               util_unquote (&p);  
               if (strcasecmp (s, "charset") == 0)  
                 have_charset = 1;  
               util_send_qstring (p);  
               space = 1;  
879              }              }
880              
881              if (!have_charset && text_plain)
882                {
883                  if (space)
884                    util_send (" ");
885                  util_send ("\"CHARSET\" \"US-ASCII\"");
886                }
887              util_send (")");
888          }          }
889        if (!have_charset && text_plain)        else
890          {          util_send (" NIL");
891            if (space)        argcv_free (argc, argv);
892              util_send (" ");        free (buffer);
           util_send ("\"CHARSET\" \"US-ASCII\"");  
         }  
       util_send (")");  
893      }      }
894    else    else
895      util_send (" NIL");      {
896    free (buffer);        /* Default? If Content-Type is not present consider as text/plain.  */
897          util_send ("TEXT PLAIN (\"CHARSET\" \"US-ASCII\")");
898          text_plain = 1;
899        }
900      
901    /* body id: Content-ID. */    /* body id: Content-ID. */
902    header_aget_value (header, MU_HEADER_CONTENT_ID, &buffer);    fetch_send_header_value (header, MU_HEADER_CONTENT_ID, NULL, 1);
   util_send (" ");  
   util_send_qstring (buffer);  
   free (buffer);  
   
903    /* body description: Content-Description. */    /* body description: Content-Description. */
904    header_aget_value (header, MU_HEADER_CONTENT_DESCRIPTION, &buffer);    fetch_send_header_value (header, MU_HEADER_CONTENT_DESCRIPTION, NULL, 1);
   util_send (" ");  
   util_send_qstring (buffer);  
   free (buffer);  
905    
906    /* body encoding: Content-Transfer-Encoding. */    /* body encoding: Content-Transfer-Encoding. */
907    header_aget_value (header, MU_HEADER_CONTENT_TRANSFER_ENCODING, &buffer);    fetch_send_header_value (header, MU_HEADER_CONTENT_TRANSFER_ENCODING,
908    util_send (" ");                             "7BIT", 1);
   util_send_qstring (buffer ? buffer : "7BIT");  
   free (buffer);  
909    
910    /* body size RFC822 format.  */    /* body size RFC822 format.  */
911    {    {
# Line 910  bodystructure (message_t msg, int extens Line 945  bodystructure (message_t msg, int extens
945    if (extension)    if (extension)
946      {      {
947        /* body MD5: Content-MD5.  */        /* body MD5: Content-MD5.  */
948        header_aget_value (header, MU_HEADER_CONTENT_MD5, &buffer);        fetch_send_header_value (header, MU_HEADER_CONTENT_MD5, NULL, 1);
       util_send (" ");  
       util_send_qstring (buffer);  
       free (buffer);  
949    
950        /* body disposition: Content-Disposition.  */        /* body disposition: Content-Disposition.  */
951        header_aget_value (header, MU_HEADER_CONTENT_DISPOSITION, &buffer);        fetch_send_header_list (header, MU_HEADER_CONTENT_DISPOSITION, NULL, 1);
       util_send (" ");  
       send_parameter_list (buffer);  
       free (buffer);  
952    
953        /* body language: Content-Language.  */        /* body language: Content-Language.  */
954        header_aget_value (header, MU_HEADER_CONTENT_LANGUAGE, &buffer);        fetch_send_header_value (header, MU_HEADER_CONTENT_LANGUAGE, NULL, 1);
       util_send (" ");  
       util_send_qstring (buffer);  
       free (buffer);  
955      }      }
956    return RESP_OK;    return RESP_OK;
957  }  }
# Line 1008  fetch_operation (message_t msg, char **a Line 1034  fetch_operation (message_t msg, char **a
1034               see it as an extension. But according to IMAP4 we should               see it as an extension. But according to IMAP4 we should
1035               have send an empty string: util_send (" \"\"");               have send an empty string: util_send (" \"\"");
1036            */            */
1037            if (*section)            util_send ("[%sHEADER]", section);
             util_send ("[%sHEADER]", section);  
           else  
             util_send ("[%s", *arg);  
1038          }          }
1039        (*arg) += 7;        (*arg) += 7;
1040        fetch_header (msg, start, end);        fetch_header (msg, start, end);
# Line 1344  fetch_header_fields_not (message_t msg, Line 1367  fetch_header_fields_not (message_t msg,
1367              }              }
1368          }          }
1369    
1370          header_aget_field_value (header, i, &value);          if (header_aget_field_value (header, i, &value) == 0)
1371          /* Save the field.  */            {
1372          n = asprintf (&buffer, "%s: %s\n", name, value);              char *nl;
1373          status = stream_write (stream, buffer, n, off, &n);              
1374          off += n;              /* Save the field.  */
1375          /* count the lines.  */              n = asprintf (&buffer, "%s: %s\n", name, value);
1376          {              status = stream_write (stream, buffer, n, off, &n);
1377            char *nl = buffer;              off += n;
1378            for (;(nl = strchr (nl, '\n')); nl++)              /* count the lines.  */
1379              lines++;              for (nl = buffer;(nl = strchr (nl, '\n')); nl++)
1380          }                lines++;
1381          free (value);  
1382                free (value);
1383              }
1384          free (name);          free (name);
1385          free (buffer);          free (buffer);
1386          buffer = NULL;          buffer = NULL;
# Line 1461  fetch_send_address (char *addr) Line 1486  fetch_send_address (char *addr)
1486  }  }
1487    
1488  /* Send parameter list for the bodystructure.  */  /* Send parameter list for the bodystructure.  */
1489  static int  static void
1490  send_parameter_list (char *buffer)  send_parameter_list (char *buffer)
1491  {  {
1492    if (buffer)    int argc = 0;
1493      while (*buffer && isspace ((unsigned)*buffer)) buffer++;    char **argv;
1494      
1495      if (!buffer)
1496        {
1497          util_send ("NIL");
1498          return;
1499        }
1500    
1501    if (buffer && *buffer)    argcv_get (buffer, " \t\r\n;=", NULL, &argc, &argv);
1502      
1503      if (argc == 0)
1504        util_send ("NIL");
1505      else
1506      {      {
1507        char *sp = NULL;        char *p;
1508        char *s;        
1509        util_send ("(");        util_send ("(");
1510        s = strtok_r (buffer, " \t\r\n;", &sp);          
1511        util_send_qstring (s);        p = argv[0];
1512        while (sp && *sp && isspace ((unsigned)*sp)) sp++;        util_send_qstring (p);
1513        if (sp && *sp)  
1514          if (argc > 1)
1515          {          {
1516            int space = 0;            int i, space = 0;
1517            util_send (" (");            char *lvalue = NULL;
1518            while ((s = strtok_r (NULL, " \t\r\n;", &sp)))  
1519              util_send ("(");
1520              for (i = 1; i < argc; i++)
1521              {              {
1522                char *p = strchr (s, '=');                if (lvalue)
               if (p)  
                 *p++ = '\0';  
               if (space)  
                 util_send (" ");  
               space = 1;  
               util_send_qstring (s);  
               if (p)  
1523                  {                  {
1524                    util_send (" ");                    if (space)
1525                    util_unquote (&p);                      util_send (" ");
1526                    util_send_qstring (p);                    util_send_qstring (lvalue);
1527                      lvalue = NULL;
1528                      space = 1;
1529                    }
1530                  
1531                  switch (argv[i][0])
1532                    {
1533                    case ';':
1534                      continue;
1535                      
1536                    case '=':
1537                      if (++i < argc)
1538                        {
1539                          char *p = argv[i];
1540                          util_send (" ");
1541                          util_unquote (&p);
1542                          util_send_qstring (p);
1543                        }
1544                      break;
1545                      
1546                    default:
1547                      lvalue = argv[i];
1548                  }                  }
1549              }              }
1550              if (lvalue)
1551                {
1552                  if (space)
1553                    util_send (" ");
1554                  util_send_qstring (lvalue);
1555                }
1556            util_send (")");            util_send (")");
1557          }          }
1558        else        else
1559          util_send (" NIL");          util_send (" NIL");
1560        util_send (")");        util_send (")");
1561      }      }
1562    else    argcv_free (argc, argv);
     util_send ("NIL");  
   return 0;  
1563  }  }
1564              
1565    

Legend:
Removed from v.1.29  
changed lines
  Added in v.1.30

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26