* Replaced serv_gets() with serv_getln() - which now requires the caller
[citadel.git] / webcit / messages.c
index 4edb1e3396bd6b3f68136a83b910dc775d28a0a7..df8d2b59723b1fbd6800f222f3c571bd1fc0b551 100644 (file)
@@ -91,25 +91,57 @@ char buf[];
 }
 
 
+/*
+ * Turn a vCard "n" (name) field into something displayable.
+ */
+void vcard_n_prettyize(char *name)
+{
+       char *original_name;
+       int i;
+
+       original_name = strdup(name);
+       for (i=0; i<5; ++i) {
+               if (strlen(original_name) > 0) {
+                       if (original_name[strlen(original_name)-1] == ' ') {
+                               original_name[strlen(original_name)-1] = 0;
+                       }
+                       if (original_name[strlen(original_name)-1] == ';') {
+                               original_name[strlen(original_name)-1] = 0;
+                       }
+               }
+       }
+       strcpy(name, "");
+       for (i=0; i<strlen(original_name); ++i) {
+               if (original_name[i] == ';') {
+                       strcat(name, ", ");
+               }
+               else {
+                       name[strlen(name)+1] = 0;
+                       name[strlen(name)] = original_name[i];
+               }
+       }
+       free(original_name);
+}
+
+
+
+
 /* display_vcard() calls this after parsing the textual vCard into
  * our 'struct vCard' data object.
  * This gets called instead of display_parsed_vcard() if we are only looking
  * to extract the person's name instead of displaying the card.
  */
 void fetchname_parsed_vcard(struct vCard *v, char *storename) {
-       int i;
+       char *name;
 
        strcpy(storename, "");
-       if (v->numprops) for (i=0; i<(v->numprops); ++i) {
-               if (!strcasecmp(v->prop[i].name, "n")) {
-                       strcpy(storename, v->prop[i].value);
-                       if ((strlen(storename)>0) && (storename[0] != ';')) {
-                               while(storename[strlen(storename)-1] == ';') {
-                                       storename[strlen(storename)-1] = 0;
-                               }
-                       }
-               }
+
+       name = vcard_get_prop(v, "n", 1, 0, 0);
+       if (name != NULL) {
+               strcpy(storename, name);
+               /* vcard_n_prettyize(storename); */
        }
+
 }
 
 
@@ -137,12 +169,16 @@ void display_parsed_vcard(struct vCard *v, int full) {
        int pass;
 
        char displayname[SIZ];
+       char title[SIZ];
+       char org[SIZ];
        char phone[SIZ];
        char mailto[SIZ];
 
        strcpy(displayname, "");
        strcpy(phone, "");
        strcpy(mailto, "");
+       strcpy(title, "");
+       strcpy(org, "");
 
        if (!full) {
                wprintf("<TD>");
@@ -151,7 +187,9 @@ void display_parsed_vcard(struct vCard *v, int full) {
                        escputs(name);
                }
                else if (name = vcard_get_prop(v, "n", 1, 0, 0), name != NULL) {
-                       escputs(name);
+                       strcpy(displayname, name);
+                       vcard_n_prettyize(displayname);
+                       escputs(displayname);
                }
                else {
                        wprintf("&nbsp;");
@@ -160,16 +198,16 @@ void display_parsed_vcard(struct vCard *v, int full) {
                return;
        }
 
-       wprintf("<TABLE bgcolor=#888888>");
+       wprintf("<div align=center><table bgcolor=#aaaaaa width=50%%>");
        for (pass=1; pass<=2; ++pass) {
 
                if (v->numprops) for (i=0; i<(v->numprops); ++i) {
 
                        thisname = strdup(v->prop[i].name);
-                       extract_token(firsttoken, thisname, 0, ';');
+                       extract_token(firsttoken, thisname, 0, ';', sizeof firsttoken);
        
                        for (j=0; j<num_tokens(thisname, ';'); ++j) {
-                               extract_token(buf, thisname, j, ';');
+                               extract_token(buf, thisname, j, ';', sizeof buf);
                                if (!strcasecmp(buf, "encoding=quoted-printable")) {
                                        is_qp = 1;
                                        remove_token(thisname, j, ';');
@@ -203,6 +241,7 @@ void display_parsed_vcard(struct vCard *v, int full) {
                        if (!strcasecmp(firsttoken, "n")) {
                                if (strlen(displayname) == 0) {
                                        strcpy(displayname, thisvalue);
+                                       vcard_n_prettyize(displayname);
                                }
                        }
        
@@ -210,6 +249,16 @@ void display_parsed_vcard(struct vCard *v, int full) {
                        else if (!strcasecmp(firsttoken, "fn")) {
                                strcpy(displayname, thisvalue);
                        }
+
+                       /* title */
+                       else if (!strcasecmp(firsttoken, "title")) {
+                               strcpy(title, thisvalue);
+                       }
+       
+                       /* organization */
+                       else if (!strcasecmp(firsttoken, "org")) {
+                               strcpy(org, thisvalue);
+                       }
        
                        else if (!strcasecmp(firsttoken, "email")) {
                                if (strlen(mailto) > 0) strcat(mailto, "<br />");
@@ -225,7 +274,7 @@ void display_parsed_vcard(struct vCard *v, int full) {
                                if (strlen(phone) > 0) strcat(phone, "<br />");
                                strcat(phone, thisvalue);
                                for (j=0; j<num_tokens(thisname, ';'); ++j) {
-                                       extract_token(buf, thisname, j, ';');
+                                       extract_token(buf, thisname, j, ';', sizeof buf);
                                        if (!strcasecmp(buf, "tel"))
                                                strcat(phone, "");
                                        else if (!strcasecmp(buf, "work"))
@@ -245,10 +294,11 @@ void display_parsed_vcard(struct vCard *v, int full) {
                                if (pass == 2) {
                                        wprintf("<TR><TD>Address:</TD><TD>");
                                        for (j=0; j<num_tokens(thisvalue, ';'); ++j) {
-                                               extract_token(buf, thisvalue, j, ';');
+                                               extract_token(buf, thisvalue, j, ';', sizeof buf);
                                                if (strlen(buf) > 0) {
                                                        escputs(buf);
-                                                       wprintf("<br />");
+                                                       if (j<3) wprintf("<br />");
+                                                       else wprintf(" ");
                                                }
                                        }
                                        wprintf("</TD></TR>\n");
@@ -264,6 +314,8 @@ void display_parsed_vcard(struct vCard *v, int full) {
                                /* ignore */
                        }
                        else {
+
+                               /*** Don't show extra fields.  They're ugly.
                                if (pass == 2) {
                                        wprintf("<TR><TD>");
                                        escputs(thisname);
@@ -271,6 +323,7 @@ void display_parsed_vcard(struct vCard *v, int full) {
                                        escputs(thisvalue);
                                        wprintf("</TD></TR>\n");
                                }
+                               ***/
                        }
        
                        free(thisname);
@@ -283,7 +336,18 @@ void display_parsed_vcard(struct vCard *v, int full) {
                        "<IMG ALIGN=CENTER SRC=\"/static/vcard.gif\">"
                        "<FONT SIZE=+1><B>");
                        escputs(displayname);
-                       wprintf("</B></FONT></TD></TR>\n");
+                       wprintf("</B></FONT>");
+                       if (strlen(title) > 0) {
+                               wprintf("<div align=right>");
+                               escputs(title);
+                               wprintf("</div>");
+                       }
+                       if (strlen(org) > 0) {
+                               wprintf("<div align=right>");
+                               escputs(org);
+                               wprintf("</div>");
+                       }
+                       wprintf("</TD></TR>\n");
                
                        if (strlen(phone) > 0)
                                wprintf("<TR><TD>Telephone:</TD><TD>%s</TD></TR>\n", phone);
@@ -293,7 +357,7 @@ void display_parsed_vcard(struct vCard *v, int full) {
 
        }
 
-       wprintf("</TABLE>\n");
+       wprintf("</table></div>\n");
 }
 
 
@@ -323,9 +387,10 @@ void display_vcard(char *vcard_source, char alpha, int full, char *storename) {
        if (storename != NULL) {
                fetchname_parsed_vcard(v, storename);
        }
-       else if ( (alpha == 0)
-          || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
-          || ((!isalpha(alpha)) && (!isalpha(this_alpha))) ) {
+       else if (       (alpha == 0)
+                       || ((isalpha(alpha)) && (tolower(alpha) == tolower(this_alpha)) )
+                       || ((!isalpha(alpha)) && (!isalpha(this_alpha)))
+               ) {
                display_parsed_vcard(v, full);
        }
 
@@ -368,7 +433,7 @@ void read_message(long msgnum) {
        strcpy(mime_http, "");
 
        serv_printf("MSG4 %ld", msgnum);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        if (buf[0] != '1') {
                wprintf("<STRONG>ERROR:</STRONG> %s<br />\n", &buf[4]);
                return;
@@ -386,7 +451,7 @@ void read_message(long msgnum) {
        wprintf("<SPAN CLASS=\"message_header\">");
        strcpy(m_subject, "");
 
-       while (serv_gets(buf), strcasecmp(buf, "text")) {
+       while (serv_getln(buf, sizeof buf), strcasecmp(buf, "text")) {
                if (!strcmp(buf, "000")) {
                        wprintf("<I>unexpected end of message</I><br /><br />\n");
                        wprintf("</SPAN>\n");
@@ -440,10 +505,10 @@ void read_message(long msgnum) {
                }
 
                if (!strncasecmp(buf, "part=", 5)) {
-                       extract(mime_filename, &buf[5], 1);
-                       extract(mime_partnum, &buf[5], 2);
-                       extract(mime_disposition, &buf[5], 3);
-                       extract(mime_content_type, &buf[5], 4);
+                       extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
+                       extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
+                       extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
+                       extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
                        mime_length = extract_int(&buf[5], 5);
 
                        if (!strcasecmp(mime_disposition, "attachment")) {
@@ -524,18 +589,18 @@ void read_message(long msgnum) {
        wprintf("&subject=");
        if (strncasecmp(m_subject, "Re:", 3)) wprintf("Re:%20");
        urlescputs(m_subject);
-       wprintf("\">Reply</a> ");
+       wprintf("\">[Reply]</a> ");
 
        if (WC->is_room_aide)  {
        
                /* Move */
-               wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">Move </a>",
+               wprintf("<a href=\"/confirm_move_msg?msgid=%ld\">[Move]</a> ",
                        msgnum);
 
                /* Delete */
                wprintf("<a href=\"/delete_msg?msgid=%ld\" "
                        "onClick=\"return confirm('Delete this message?');\">"
-                       "Delete</a>", msgnum);
+                       "[Delete]</a>", msgnum);
                        
        }
 
@@ -549,7 +614,7 @@ void read_message(long msgnum) {
         * Learn the content type
         */
        strcpy(mime_content_type, "text/plain");
-       while (serv_gets(buf), (strlen(buf) > 0)) {
+       while (serv_getln(buf, sizeof buf), (strlen(buf) > 0)) {
                if (!strcmp(buf, "000")) {
                        wprintf("<I>unexpected end of message</I><br /><br />\n");
                        goto ENDBODY;
@@ -567,7 +632,7 @@ void read_message(long msgnum) {
 
        /* Boring old 80-column fixed format text gets handled this way... */
        else if (!strcasecmp(mime_content_type, "text/plain")) {
-               while (serv_gets(buf), strcmp(buf, "000")) {
+               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                        if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0;
                        if (buf[strlen(buf)-1] == '\r') buf[strlen(buf)-1] = 0;
                        while ((strlen(buf) > 0) && (isspace(buf[strlen(buf) - 1])))
@@ -598,7 +663,7 @@ void read_message(long msgnum) {
        else {
                wprintf("I don't know how to display %s<br />\n",
                        mime_content_type);
-               while (serv_gets(buf), strcmp(buf, "000")) { }
+               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { }
        }
 
 
@@ -613,12 +678,14 @@ void read_message(long msgnum) {
                if (part_source != NULL) {
 
                        /* If it's my vCard I can edit it */
-                       if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
-                          || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
+                       if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
+                               || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
+                               || (WC->wc_view == VIEW_ADDRESSBOOK)
+                       ) {
                                wprintf("<A HREF=\"/edit_vcard?"
                                        "msgnum=%ld&partnum=%s\">",
                                        msgnum, vcard_partnum);
-                               wprintf("(edit)</A>");
+                               wprintf("[edit]</A>");
                        }
 
                        /* In all cases, display the full card */
@@ -665,10 +732,10 @@ void summarize_message(long msgnum, int is_new) {
 
        sprintf(buf, "MSG0 %ld|3", msgnum);     /* ask for headers only with no MIME */
        serv_puts(buf);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        if (buf[0] != '1') return;
 
-       while (serv_gets(buf), strcmp(buf, "000")) {
+       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                if (!strncasecmp(buf, "from=", 5)) {
                        strcpy(summ.from, &buf[5]);
                }
@@ -749,15 +816,15 @@ void display_addressbook(long msgnum, char alpha) {
 
        sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
        serv_puts(buf);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        if (buf[0] != '1') return;
 
-       while (serv_gets(buf), strcmp(buf, "000")) {
+       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                if (!strncasecmp(buf, "part=", 5)) {
-                       extract(mime_filename, &buf[5], 1);
-                       extract(mime_partnum, &buf[5], 2);
-                       extract(mime_disposition, &buf[5], 3);
-                       extract(mime_content_type, &buf[5], 4);
+                       extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
+                       extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
+                       extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
+                       extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
                        mime_length = extract_int(&buf[5], 5);
 
                        if (!strcasecmp(mime_content_type, "text/x-vcard")) {
@@ -775,12 +842,14 @@ void display_addressbook(long msgnum, char alpha) {
                        display_vcard(vcard_source, alpha, 0, NULL);
 
                        /* If it's my vCard I can edit it */
-                       if ( (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
-                          || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))) {
+                       if (    (!strcasecmp(WC->wc_roomname, USERCONFIGROOM))
+                               || (!strcasecmp(&WC->wc_roomname[11], USERCONFIGROOM))
+                               || (WC->wc_view == VIEW_ADDRESSBOOK)
+                       ) {
                                wprintf("<A HREF=\"/edit_vcard?"
                                        "msgnum=%ld&partnum=%s\">",
                                        msgnum, vcard_partnum);
-                               wprintf("(edit)</A>");
+                               wprintf("[edit]</A>");
                        }
 
                        free(vcard_source);
@@ -805,7 +874,7 @@ void lastfirst_firstlast(char *namebuf) {
        i = num_tokens(namebuf, ' ');
        if (i < 2) return;
 
-       extract_token(lastname, namebuf, i-1, ' ');
+       extract_token(lastname, namebuf, i-1, ' ', sizeof lastname);
        remove_token(namebuf, i-1, ' ');
        strcpy(firstname, namebuf);
        sprintf(namebuf, "%s; %s", lastname, firstname);
@@ -839,15 +908,15 @@ void fetch_ab_name(long msgnum, char *namebuf) {
 
        sprintf(buf, "MSG0 %ld|1", msgnum);     /* ask for headers only */
        serv_puts(buf);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        if (buf[0] != '1') return;
 
-       while (serv_gets(buf), strcmp(buf, "000")) {
+       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                if (!strncasecmp(buf, "part=", 5)) {
-                       extract(mime_filename, &buf[5], 1);
-                       extract(mime_partnum, &buf[5], 2);
-                       extract(mime_disposition, &buf[5], 3);
-                       extract(mime_content_type, &buf[5], 4);
+                       extract_token(mime_filename, &buf[5], 1, '|', sizeof mime_filename);
+                       extract_token(mime_partnum, &buf[5], 2, '|', sizeof mime_partnum);
+                       extract_token(mime_disposition, &buf[5], 3, '|', sizeof mime_disposition);
+                       extract_token(mime_content_type, &buf[5], 4, '|', sizeof mime_content_type);
                        mime_length = extract_int(&buf[5], 5);
 
                        if (!strcasecmp(mime_content_type, "text/x-vcard")) {
@@ -978,6 +1047,7 @@ void do_addrbook_view(struct addrbookent *addrbook, int num_ab) {
                        wprintf("<A HREF=\"/readfwd?startmsg=%ld&is_singlecard=1",
                                addrbook[i].ab_msgnum);
                        wprintf("&maxmsgs=1&summary=0&alpha=%s\">", bstr("alpha"));
+                       vcard_n_prettyize(addrbook[i].ab_name);
                        escputs(addrbook[i].ab_name);
                        wprintf("</A></TD>\n");
                        ++displayed;
@@ -1001,12 +1071,12 @@ int load_msg_ptrs(char *servcmd)
        nummsgs = 0;
        maxload = sizeof(WC->msgarr) / sizeof(long) ;
        serv_puts(servcmd);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        if (buf[0] != '1') {
                wprintf("<EM>%s</EM><br />\n", &buf[4]);
                return (nummsgs);
        }
-       while (serv_gets(buf), strcmp(buf, "000")) {
+       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                if (nummsgs < maxload) {
                        WC->msgarr[nummsgs] = atol(buf);
                        ++nummsgs;
@@ -1089,7 +1159,7 @@ void readloop(char *oper)
        strcpy(old_msgs, "");
        if (is_summary) {
                serv_puts("GTSN");
-               serv_gets(buf);
+               serv_getln(buf, sizeof buf);
                if (buf[0] == '2') {
                        strcpy(old_msgs, &buf[4]);
                }
@@ -1481,7 +1551,7 @@ void post_message(void)
                        bstr("recp"),
                        bstr("subject") );
                serv_puts(buf);
-               serv_gets(buf);
+               serv_getln(buf, sizeof buf);
                if (buf[0] == '4') {
                        post_mime_to_server();
                        if (strlen(bstr("recp")) > 0) {
@@ -1554,7 +1624,7 @@ void display_enter(void)
 
        sprintf(buf, "ENT0 0|%s|0|0", bstr("recp"));
        serv_puts(buf);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
 
        if (!strncmp(buf, "570", 3)) {
                if (strlen(bstr("recp")) > 0) {
@@ -1675,7 +1745,7 @@ void delete_msg(void)
 
        sprintf(buf, "DELE %ld", msgid);
        serv_puts(buf);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        wprintf("<EM>%s</EM><br />\n", &buf[4]);
 
        wDumpContent(1);
@@ -1714,10 +1784,10 @@ void confirm_move_msg(void)
 
        wprintf("<SELECT NAME=\"target_room\" SIZE=5>\n");
        serv_puts("LKRA");
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
        if (buf[0] == '1') {
-               while (serv_gets(buf), strcmp(buf, "000")) {
-                       extract(targ, buf, 0);
+               while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+                       extract_token(targ, buf, 0, '|', sizeof targ);
                        wprintf("<OPTION>");
                        escputs(targ);
                        wprintf("\n");
@@ -1749,7 +1819,7 @@ void move_msg(void)
        if (!strcasecmp(bstr("yesno"), "Move")) {
                sprintf(buf, "MOVE %ld|%s", msgid, bstr("target_room"));
                serv_puts(buf);
-               serv_gets(buf);
+               serv_getln(buf, sizeof buf);
                wprintf("<EM>%s</EM><br />\n", &buf[4]);
        } else {
                wprintf("<EM>Message not moved.</EM><br />\n");
@@ -1777,9 +1847,9 @@ void do_stuff_to_msgs(void) {
 
 
        serv_puts("MSGS ALL");
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
 
-       if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
+       if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                ptr = malloc(sizeof(struct stuff_t));
                ptr->msgnum = atol(buf);
                ptr->next = stuff;
@@ -1795,7 +1865,7 @@ void do_stuff_to_msgs(void) {
 
                        if (!strcasecmp(sc, "Delete selected")) {
                                serv_printf("DELE %ld", stuff->msgnum);
-                               serv_gets(buf);
+                               serv_getln(buf, sizeof buf);
                        }
 
                }