From 8134bf8f534a19de35fb39a1eed47eb75f27a47c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sat, 25 Apr 2009 13:32:30 +0000 Subject: [PATCH] * swap I/O to the new functions --- webcit/calendar.c | 10 +-- webcit/messages.c | 23 +++--- webcit/msg_renderers.c | 2 +- webcit/notes.c | 173 +++++++++++++++++++++++++++-------------- webcit/vcard_edit.c | 58 +++++++------- webcit/webcit.c | 2 +- webcit/webcit.h | 6 +- 7 files changed, 164 insertions(+), 110 deletions(-) diff --git a/webcit/calendar.c b/webcit/calendar.c index 02c8e817b..57b98b400 100644 --- a/webcit/calendar.c +++ b/webcit/calendar.c @@ -1146,10 +1146,10 @@ void load_ical_object(long msgnum, int unread, * as one of the other MIME parts, attempt to load it now. */ if ((Data == NULL) && (!IsEmptyStr(relevant_partnum))) { - relevant_source = load_mimepart(msgnum, relevant_partnum); + Data = load_mimepart(msgnum, relevant_partnum); } - if ((relevant_source != NULL) || (Data != NULL)) { + if (Data != NULL) { relevant_source = (char*) ChrPtr(Data); process_ical_object(msgnum, unread, from, @@ -1157,11 +1157,7 @@ void load_ical_object(long msgnum, int unread, which_kind, CallBack, calv); - if (Data != NULL) - FreeStrBuf (&Data); - else - free(relevant_source); - + FreeStrBuf (&Data); } icalmemory_free_ring(); diff --git a/webcit/messages.c b/webcit/messages.c index 9b16e2a94..f10f3bbdf 100644 --- a/webcit/messages.c +++ b/webcit/messages.c @@ -1588,26 +1588,23 @@ void mimepart(int force_download) /* * Read any MIME part of a message, from the server, into memory. */ -char *load_mimepart(long msgnum, char *partnum) +StrBuf *load_mimepart(long msgnum, char *partnum) { - char buf[SIZ]; off_t bytes; - char content_type[SIZ]; - char *content; + StrBuf *Buf; + Buf = NewStrBuf(); serv_printf("DLAT %ld|%s", msgnum, partnum); - serv_getln(buf, sizeof buf); - if (buf[0] == '6') { - bytes = extract_long(&buf[4], 0); - extract_token(content_type, &buf[4], 3, '|', sizeof content_type); - - content = malloc(bytes + 2); - serv_read(content, bytes); + StrBuf_ServGetlnBuffered(Buf); + if (GetServerStatus(Buf, NULL) == 6) { + StrBufCutLeft(Buf, 4); + bytes = StrBufExtract_long(Buf, 0, '|'); - content[bytes] = 0; /* null terminate for good measure */ - return(content); + StrBuf_ServGetBLOBBuffered(Buf, bytes); + return(Buf); } else { + FreeStrBuf(&Buf); return(NULL); } } diff --git a/webcit/msg_renderers.c b/webcit/msg_renderers.c index e7d13ff10..356fdceb2 100644 --- a/webcit/msg_renderers.c +++ b/webcit/msg_renderers.c @@ -495,7 +495,7 @@ void render_MIME_VCard(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundC } /* In all cases, display the full card */ - display_vcard(Buf, ChrPtr(Mime->Data), 0, 1, NULL, Mime->msgnum); + display_vcard(Buf, Mime->Data, 0, 1, NULL, Mime->msgnum); FreeStrBuf(&Mime->Data); Mime->Data = Buf; } diff --git a/webcit/notes.c b/webcit/notes.c index 9e00be85f..a4e570645 100644 --- a/webcit/notes.c +++ b/webcit/notes.c @@ -141,83 +141,142 @@ void display_vnote_div(struct vnote *v) { /* * Fetch a message from the server and extract a vNote from it */ -struct vnote *vnote_new_from_msg(long msgnum) { - char buf[1024]; +struct vnote *vnote_new_from_msg(long msgnum,int unread) +{ + StrBuf *Buf; + StrBuf *Data; + const char *bptr; + int Done = 0; + char from[128] = ""; + char uid_from_headers[256]; char mime_partnum[256]; char mime_filename[256]; char mime_content_type[256]; char mime_disposition[256]; int mime_length; char relevant_partnum[256]; - char *relevant_source = NULL; - char uid_from_headers[256]; - int in_body = 0; - int body_line_len = 0; - int body_len = 0; + int phase = 0; /* 0 = citadel headers, 1 = mime headers, 2 = body */ + char msg4_content_type[256] = ""; + char msg4_content_encoding[256] = ""; + int msg4_content_length = 0; struct vnote *vnote_from_body = NULL; - relevant_partnum[0] = 0; - uid_from_headers[0] = 0; - sprintf(buf, "MSG4 %ld", msgnum); /* we need the mime headers */ - serv_puts(buf); - serv_getln(buf, sizeof buf); - if (buf[0] != '1') return NULL; - - while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) { - if (!strncasecmp(buf, "exti=", 5)) { - safestrncpy(uid_from_headers, &buf[5], sizeof uid_from_headers); + relevant_partnum[0] = '\0'; + serv_printf("MSG4 %ld", msgnum); /* we need the mime headers */ + Buf = NewStrBuf(); + StrBuf_ServGetlnBuffered(Buf); + if (GetServerStatus(Buf, NULL) != 1) { + FreeStrBuf (&Buf); + return NULL; + } + while ((StrBuf_ServGetlnBuffered(Buf)>=0) && !Done) { + if ( (StrLength(Buf)==3) && + !strcmp(ChrPtr(Buf), "000")) { + Done = 1; + break; } - else if (!strncasecmp(buf, "part=", 5)) { - 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/vnote")) { - strcpy(relevant_partnum, mime_partnum); + bptr = ChrPtr(Buf); + switch (phase) { + case 0: + if (!strncasecmp(bptr, "exti=", 5)) { + safestrncpy(uid_from_headers, &(ChrPtr(Buf)[5]), sizeof uid_from_headers); } - } - else if ((in_body) && (IsEmptyStr(relevant_partnum)) && (!IsEmptyStr(uid_from_headers))) { - // Convert an old-style note to a vNote - if (!vnote_from_body) { - vnote_from_body = vnote_new(); - vnote_from_body->uid = strdup(uid_from_headers); - vnote_from_body->color_red = pastel_palette[3][0]; - vnote_from_body->color_green = pastel_palette[3][1]; - vnote_from_body->color_blue = pastel_palette[3][2]; - vnote_from_body->body = malloc(32768); - vnote_from_body->body[0] = 0; - body_len = 0; + else if (!strncasecmp(bptr, "part=", 5)) { + extract_token(mime_filename, &bptr[5], 1, '|', sizeof mime_filename); + extract_token(mime_partnum, &bptr[5], 2, '|', sizeof mime_partnum); + extract_token(mime_disposition, &bptr[5], 3, '|', sizeof mime_disposition); + extract_token(mime_content_type, &bptr[5], 4, '|', sizeof mime_content_type); + mime_length = extract_int(&bptr[5], 5); + + if ( (!strcasecmp(mime_content_type, "text/calendar")) + || (!strcasecmp(mime_content_type, "application/ics")) + || (!strcasecmp(mime_content_type, "text/vtodo")) + ) { + strcpy(relevant_partnum, mime_partnum); + } } - body_line_len = strlen(buf); - if ((body_len + body_line_len + 10) < 32768) { - strcpy(&vnote_from_body->body[body_len++], " "); - strcpy(&vnote_from_body->body[body_len], buf); - body_len += body_line_len; + else if (!strncasecmp(bptr, "from=", 4)) { + extract_token(from, bptr, 1, '=', sizeof(from)); } - } - else if (IsEmptyStr(buf)) { - in_body = 1; + else if ((phase == 0) && (!strncasecmp(bptr, "text", 4))) { + phase = 1; + } + break; + case 1: + if (!IsEmptyStr(bptr)) { + if (!strncasecmp(bptr, "Content-type: ", 14)) { + safestrncpy(msg4_content_type, &bptr[14], sizeof msg4_content_type); + striplt(msg4_content_type); + } + else if (!strncasecmp(bptr, "Content-transfer-encoding: ", 27)) { + safestrncpy(msg4_content_encoding, &bptr[27], sizeof msg4_content_encoding); + striplt(msg4_content_type); + } + else if ((!strncasecmp(bptr, "Content-length: ", 16))) { + msg4_content_length = atoi(&bptr[16]); + } + break; + } + else { + phase++; + + if ((msg4_content_length > 0) + && ( !strcasecmp(msg4_content_encoding, "7bit")) + && ((!strcasecmp(mime_content_type, "text/calendar")) + || (!strcasecmp(mime_content_type, "application/ics")) + || (!strcasecmp(mime_content_type, "text/vtodo")) + ) + ) + { + } + } + case 2: + Data = NewStrBufPlain(NULL, msg4_content_length * 2); + if (msg4_content_length > 0) { + StrBuf_ServGetBLOBBuffered(Data, msg4_content_length); + phase ++; + } + else { + StrBufAppendBuf(Data, Buf, 0); + StrBufAppendBufPlain(Data, "\r\n", 1, 0); + } + case 3: + StrBufAppendBuf(Data, Buf, 0); } } + FreeStrBuf(&Buf); - if (!IsEmptyStr(relevant_partnum)) { - relevant_source = load_mimepart(msgnum, relevant_partnum); - if (relevant_source != NULL) { - struct vnote *v = vnote_new_from_str(relevant_source); - free(relevant_source); - return(v); - } + /* If MSG4 didn't give us the part we wanted, but we know that we can find it + * as one of the other MIME parts, attempt to load it now. + */ + if ((Data == NULL) && (!IsEmptyStr(relevant_partnum))) { + Data = load_mimepart(msgnum, relevant_partnum); } - if (vnote_from_body) { - return(vnote_from_body); + + if ((IsEmptyStr(relevant_partnum)) && (StrLength(Data) > 0 )) { + if (!IsEmptyStr(uid_from_headers)) { + // Convert an old-style note to a vNote + vnote_from_body = vnote_new(); + vnote_from_body->uid = strdup(uid_from_headers); + vnote_from_body->color_red = pastel_palette[3][0]; + vnote_from_body->color_green = pastel_palette[3][1]; + vnote_from_body->color_blue = pastel_palette[3][2]; + vnote_from_body->body = malloc(StrLength(Data) + 1); + vnote_from_body->body[0] = 0; + memcpy(vnote_from_body->body, ChrPtr(Data), StrLength(Data) + 1); + return vnote_from_body; + } + else { + struct vnote *v = vnote_new_from_str(ChrPtr(Data)); + return(v); + } } return NULL; } + /* * Serialize a vnote and write it to the server */ @@ -282,7 +341,7 @@ void ajax_update_note(void) { } // If we get to this point it's an update, not a delete - v = vnote_new_from_msg(msgnum); + v = vnote_new_from_msg(msgnum, 0); if (!v) { begin_ajax_response(); wprintf("Cannot locate a vNote within message %d\n", msgnum); @@ -343,7 +402,7 @@ void display_note(message_summary *Msg, int unread) { memset(&TP, 0, sizeof(WCTemplputParams)); TP.Filter.ContextType = CTX_VNOTE; - v = vnote_new_from_msg(Msg->msgnum); + v = vnote_new_from_msg(Msg->msgnum, unread); if (v) { // display_vnote_div(v); TP.Context = v; diff --git a/webcit/vcard_edit.c b/webcit/vcard_edit.c index ef96784de..5846fa80a 100644 --- a/webcit/vcard_edit.c +++ b/webcit/vcard_edit.c @@ -46,7 +46,7 @@ void display_addressbook(long msgnum, char alpha) { ///char mime_disposition[SIZ]; //int mime_length; char vcard_partnum[SIZ]; - char *vcard_source = NULL; + StrBuf *vcard_source = NULL; message_summary summ;////TODO: this will leak memset(&summ, 0, sizeof(summ)); @@ -71,7 +71,7 @@ void display_addressbook(long msgnum, char alpha) { wprintf("[%s]", _("edit")); } - free(vcard_source); + FreeStrBuf(&vcard_source); } } @@ -113,7 +113,7 @@ void fetch_ab_name(message_summary *Msg, char **namebuf) { char mime_disposition[SIZ]; int mime_length; char vcard_partnum[SIZ]; - char *vcard_source = NULL; + StrBuf *vcard_source = NULL; int i, len; message_summary summ;/// TODO this will lak @@ -150,7 +150,7 @@ void fetch_ab_name(message_summary *Msg, char **namebuf) { /* Grab the name off the card */ display_vcard(WC->WBuf, vcard_source, 0, 0, namebuf, Msg->msgnum); - free(vcard_source); + FreeStrBuf(&vcard_source); } } if (*namebuf != NULL) { @@ -539,7 +539,7 @@ void display_parsed_vcard(StrBuf *Target, struct vCard *v, int full, long msgnum * \param msgnum Citadel message pointer */ void display_vcard(StrBuf *Target, - const char *vcard_source, + StrBuf *vcard_source, char alpha, int full, char **storename, @@ -551,7 +551,7 @@ void display_vcard(StrBuf *Target, StrBuf *Buf2; char this_alpha = 0; - v = vcard_load((char*)vcard_source); ///TODO + v = VCardLoad(vcard_source); if (v == NULL) return; @@ -697,8 +697,9 @@ void do_addrbook_view(addrbookent *addrbook, int num_ab) { * to start with a blank card. */ void do_edit_vcard(long msgnum, char *partnum, char *return_to, const char *force_room) { + StrBuf *Buf; char buf[SIZ]; - char *serialized_vcard = NULL; + StrBuf *serialized_vcard = NULL; size_t total_len = 0; struct vCard *v; int i; @@ -770,32 +771,31 @@ void do_edit_vcard(long msgnum, char *partnum, char *return_to, const char *forc strcat(whatuser, &buf[5]); } } - - sprintf(buf, "DLAT %ld|%s", msgnum, partnum); - serv_puts(buf); - serv_getln(buf, sizeof buf); - if (buf[0] != '6') { - convenience_page("770000", "Error", &buf[4]); + Buf = NewStrBuf(); + serv_printf(buf, "DLAT %ld|%s", msgnum, partnum); + StrBuf_ServGetlnBuffered(Buf); + if (GetServerStatus(Buf, NULL) != 6) { + convenience_page("770000", "Error", &(ChrPtr(Buf)[4])); return; } - - total_len = atoi(&buf[4]); - serialized_vcard = malloc(total_len + 2); + + StrBufCutLeft(Buf, 4); + total_len = StrBufExtract_long(Buf, 0, '|'); - serv_read(serialized_vcard, total_len); - serialized_vcard[total_len] = 0; + StrBuf_ServGetBLOBBuffered(Buf, total_len); - v = vcard_load(serialized_vcard); - free(serialized_vcard); + v = VCardLoad(Buf); + FreeStrBuf(&Buf); /* Populate the variables for our form */ i = 0; while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) { - value = vcard_get_prop(v, "", 0, i++, 0); - char prp[256]; /* property name */ char prm[256]; /* parameters */ + value = vcard_get_prop(v, "", 0, i++, 0); + + extract_token(prp, key, 0, ';', sizeof prp); safestrncpy(prm, key, sizeof prm); remove_token(prm, 0, ';'); @@ -1078,6 +1078,7 @@ void submit_vcard(void) { struct vCard *v; char *serialized_vcard; char buf[SIZ]; + StrBuf *Buf; int i; if (!havebstr("ok_button")) { @@ -1098,11 +1099,12 @@ void submit_vcard(void) { } /** Make a vCard structure out of the data supplied in the form */ - - snprintf(buf, sizeof buf, "begin:vcard\r\n%s\r\nend:vcard\r\n", - bstr("extrafields") + Buf = NewStrBuf(); + StrBufPrintf(Buf, "begin:vcard\r\n%s\r\nend:vcard\r\n", + bstr("extrafields") ); - v = vcard_load(buf); /** Start with the extra fields */ + v = VCardLoad(Buf); /** Start with the extra fields */ + FreeStrBuf(&Buf); if (v == NULL) { safestrncpy(WC->ImportantMessage, _("An error has occurred."), @@ -1183,7 +1185,7 @@ void submit_vcard(void) { void display_vcard_photo_img(void) { long msgnum = 0L; - char *vcard; + StrBuf *vcard; struct vCard *v; char *photosrc; const char *contentType; @@ -1192,7 +1194,7 @@ void display_vcard_photo_img(void) msgnum = StrTol(WCC->UrlFragment2); vcard = load_mimepart(msgnum,"1"); - v = vcard_load(vcard); + v = VCardLoad(vcard); photosrc = vcard_get_prop(v, "PHOTO", 1,0,0); FlushStrBuf(WCC->WBuf); diff --git a/webcit/webcit.c b/webcit/webcit.c index a47f5f31e..03877576d 100644 --- a/webcit/webcit.c +++ b/webcit/webcit.c @@ -346,7 +346,7 @@ void output_static(const char *what) * titlebarmsg text to display in the title bar * messagetext body of the box */ -void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext) +void convenience_page(const char *titlebarcolor, const char *titlebarmsg, const char *messagetext) { hprintf("HTTP/1.1 200 OK\n"); output_headers(1, 1, 2, 0, 0, 0); diff --git a/webcit/webcit.h b/webcit/webcit.h index 7a991ce5f..77451ed53 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -594,7 +594,7 @@ void serv_read(char *buf, int bytes); void SetAccessCommand(long Oper); void do_addrbook_view(addrbookent *addrbook, int num_ab); void fetch_ab_name(message_summary *Msg, char **namebuf); -void display_vcard(StrBuf *Target, const char *vcard_source, char alpha, int full, char **storename, long msgnum); +void display_vcard(StrBuf *Target, StrBuf *vcard_source, char alpha, int full, char **storename, long msgnum); void jsonMessageList(void); void new_summary_view(void); void getseen(void); @@ -643,7 +643,7 @@ char *memreadline(char *start, char *buf, int maxlen); char *memreadlinelen(char *start, char *buf, int maxlen, int *retlen); long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen); void remove_token(char *source, int parmnum, char separator); -char *load_mimepart(long msgnum, char *partnum); +StrBuf *load_mimepart(long msgnum, char *partnum); void MimeLoadData(wc_mime_attachment *Mime); int pattern2(char *search, char *patn); void do_edit_vcard(long, char *, char *, const char *); @@ -656,7 +656,7 @@ void folders(void); void display_addressbook(long msgnum, char alpha); void offer_start_page(StrBuf *Target, WCTemplputParams *TP); -void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext); +void convenience_page(const char *titlebarcolor, const char *titlebarmsg, const char *messagetext); void output_html(const char *, int, int, StrBuf *, StrBuf *); void do_listsub(void); ssize_t write(int fd, const void *buf, size_t count); -- 2.30.2