* BIG rewrite of header handling and slimming of context_loop and session_loop; shuff...
[citadel.git] / webcit / messages.c
index 97d7f02fb404ccff6305882e528a8b9b93bfaac8..0a9f2c746b332965191e670047f4f336ddb74e81 100644 (file)
@@ -206,7 +206,7 @@ int load_message(message_summary *Msg,
  * printable_view      Nonzero to display a printable view
  * section             Optional for encapsulated message/rfc822 submessage
  */
-int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum) 
+int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, const StrBuf *PartNum, const StrBuf **OutMime
 {
        StrBuf *Buf;
        StrBuf *FoundCharset;
@@ -267,7 +267,7 @@ int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, co
        memset(&SubTP, 0, sizeof(WCTemplputParams));
        SubTP.Filter.ContextType = CTX_MAILSUM;
        SubTP.Context = Msg;
-       DoTemplate(tmpl, tmpllen, Target, &SubTP);
+       *OutMime = DoTemplate(tmpl, tmpllen, Target, &SubTP);
 
        DestroyMessageSummary(Msg);
        FreeStrBuf(&FoundCharset);
@@ -277,9 +277,142 @@ int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, co
 
 
 
+void
+HttpStatus(long CitadelStatus)
+{
+       long httpstatus = 502;
+       long MajorStat = MAJORCODE(CitadelStatus);
+       long MinorStat = MINORCODE(CitadelStatus);
+       
+       switch (MAJORCODE(CitadelStatus))
+       {
+       case LISTING_FOLLOWS:
+       case CIT_OK:
+               httpstatus = 201;
+               break;
+       case ERROR:
+               switch (MINORCODE(CitadelStatus))
+               {
+               case INTERNAL_ERROR:
+                       httpstatus = 403;
+                       break;
+                       
+               case TOO_BIG:
+               case ILLEGAL_VALUE:
+               case HIGHER_ACCESS_REQUIRED:
+               case MAX_SESSIONS_EXCEEDED:
+               case RESOURCE_BUSY:
+               case RESOURCE_NOT_OPEN:
+               case NOT_HERE:
+               case INVALID_FLOOR_OPERATION:
+               case FILE_NOT_FOUND:
+               case ROOM_NOT_FOUND:
+                       httpstatus = 409;
+                       break;
+
+               case MESSAGE_NOT_FOUND:
+               case ALREADY_EXISTS:
+                       httpstatus = 403;
+                       break;
+
+               case NO_SUCH_SYSTEM:
+                       httpstatus = 502;
+                       break;
+
+               default:
+               case CMD_NOT_SUPPORTED:
+               case PASSWORD_REQUIRED:
+               case ALREADY_LOGGED_IN:
+               case USERNAME_REQUIRED:
+               case NOT_LOGGED_IN:
+               case SERVER_SHUTTING_DOWN:
+               case NO_SUCH_USER:
+               case ASYNC_GEXP:
+                       httpstatus = 502;
+                       break;
+               }
+               break;
 
+       default:
+       case BINARY_FOLLOWS:
+       case SEND_BINARY:
+       case START_CHAT_MODE:
+       case ASYNC_MSG:
+       case MORE_DATA:
+       case SEND_LISTING:
+               httpstatus = 502; /* aeh... whut? */
+               break;
+       }
 
 
+}
+
+/*
+ * Unadorned HTML output of an individual message, suitable
+ * for placing in a hidden iframe, for printing, or whatever
+ *
+ * msgnum_as_string == Message number, as a string instead of as a long int
+ */
+void handle_one_message(void) 
+{
+       long CitStatus;
+       int CopyMessage = 0;
+       const StrBuf *Destination;
+       void *vLine;
+       const StrBuf *Mime;
+       long msgnum = 0L;
+       wcsession *WCC = WC;
+       const StrBuf *Tmpl;
+       StrBuf *CmdBuf = NULL;
+
+       //msgnum = StrTol(WCC->UrlFragment3);
+       //gotoroom(WCC->UrlFragment2);
+       switch (WCC->Hdr->eReqType)
+       {
+       case eGET:
+       case ePOST:
+               Tmpl = sbstr("template");
+               if (StrLength(Tmpl) > 0) 
+                       read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
+               else 
+                       read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
+               http_transmit_thing(ChrPtr(Mime), 0);
+               break;
+       case eDELETE:
+               CmdBuf = NewStrBuf ();
+               if (WCC->wc_is_trash) { /** Delete from Trash is a real delete */
+                       serv_printf("DELE %ld", msgnum);        
+               }
+               else {                  /** Otherwise move it to Trash */
+                       serv_printf("MOVE %ld|_TRASH_|0", msgnum);
+               }
+               StrBuf_ServGetln(CmdBuf);
+               FlushStrBuf(WCC->ImportantMsg);
+               StrBufAppendBuf(WCC->ImportantMsg, CmdBuf, 4);
+               GetServerStatus(CmdBuf, &CitStatus);
+               HttpStatus(CitStatus);
+               break;
+       case eCOPY:
+               CopyMessage = 1;
+       case eMOVE:
+               if (GetHash(WCC->Hdr->HTTPHeaders, HKEY("DESTINATION"), &vLine) &&
+                   (vLine!=NULL)) {
+                       Destination = (StrBuf*) vLine;
+                       serv_printf("MOVE %ld|%s|%d", msgnum, ChrPtr(Destination), CopyMessage);
+                       StrBuf_ServGetln(CmdBuf);
+                       FlushStrBuf(WCC->ImportantMsg);
+                       StrBufAppendBuf(WCC->ImportantMsg, CmdBuf, 4);
+                       GetServerStatus(CmdBuf, &CitStatus);
+                       HttpStatus(CitStatus);
+               }
+               else
+                       HttpStatus(500);
+               break;
+       default:
+               break;
+
+       }
+}
 /*
  * Unadorned HTML output of an individual message, suitable
  * for placing in a hidden iframe, for printing, or whatever
@@ -287,15 +420,40 @@ int read_message(StrBuf *Target, const char *tmpl, long tmpllen, long msgnum, co
  * msgnum_as_string == Message number, as a string instead of as a long int
  */
 void embed_message(void) {
+       const StrBuf *Mime;
        long msgnum = 0L;
        wcsession *WCC = WC;
-       const StrBuf *Tmpl = sbstr("template");
+       const StrBuf *Tmpl;
+       StrBuf *CmdBuf = NULL;
 
-       msgnum = StrTol(WCC->UrlFragment2);
-       if (StrLength(Tmpl) > 0) 
-               read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL);
-       else 
-               read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL);
+       msgnum = StrBufExtract_long(WCC->Hdr->ReqLine, 2, '/');
+       switch (WCC->Hdr->eReqType)
+       {
+       case eGET:
+       case ePOST:
+               Tmpl = sbstr("template");
+               if (StrLength(Tmpl) > 0) 
+                       read_message(WCC->WBuf, SKEY(Tmpl), msgnum, NULL, &Mime);
+               else 
+                       read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
+               http_transmit_thing(ChrPtr(Mime), 0);
+               break;
+       case eDELETE:
+               CmdBuf = NewStrBuf ();
+               if (WCC->wc_is_trash) { /** Delete from Trash is a real delete */
+                       serv_printf("DELE %ld", msgnum);        
+               }
+               else {                  /** Otherwise move it to Trash */
+                       serv_printf("MOVE %ld|_TRASH_|0", msgnum);
+               }
+               StrBuf_ServGetln(CmdBuf);
+               FlushStrBuf(WCC->ImportantMsg);
+               StrBufAppendBuf(WCC->ImportantMsg, CmdBuf, 4);
+               break;
+       default:
+               break;
+
+       }
 }
 
 
@@ -306,8 +464,9 @@ void embed_message(void) {
  */
 void print_message(void) {
        long msgnum = 0L;
+       const StrBuf *Mime;
 
-       msgnum = StrTol(WC->UrlFragment2);
+       msgnum = StrBufExtract_long(WC->Hdr->ReqLine, 2, '/');
        output_headers(0, 0, 0, 0, 0, 0);
 
        hprintf("Content-type: text/html\r\n"
@@ -316,7 +475,7 @@ void print_message(void) {
 
        begin_burst();
 
-       read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL);
+       read_message(WC->WBuf, HKEY("view_message_print"), msgnum, NULL, &Mime);
 
        wDumpContent(0);
 }
@@ -326,14 +485,17 @@ void print_message(void) {
  *
  * @param msg_num_as_string Message number as a string instead of as a long int 
  */
-void mobile_message_view(void) {
-  long msgnum = 0L;
-  msgnum = StrTol(WC->UrlFragment2);
-  output_headers(1, 0, 0, 0, 0, 1);
-  begin_burst();
-  do_template("msgcontrols", NULL);
-  read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL);
-  wDumpContent(0);
+void mobile_message_view(void) 
+{
+       long msgnum = 0L;
+       const StrBuf *Mime;
+  
+       msgnum = StrBufExtract_long(WC->Hdr->ReqLine, 2, '/');
+       output_headers(1, 0, 0, 0, 0, 1);
+       begin_burst();
+       do_template("msgcontrols", NULL);
+       read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
+       wDumpContent(0);
 }
 
 /**
@@ -345,7 +507,7 @@ void display_headers(void) {
        long msgnum = 0L;
        char buf[1024];
 
-       msgnum = StrTol(WC->UrlFragment2);
+       msgnum = StrBufExtract_long(WC->Hdr->ReqLine, 2, '/');
        output_headers(0, 0, 0, 0, 0, 0);
 
        hprintf("Content-type: text/plain\r\n"
@@ -693,6 +855,7 @@ void readloop(long oper)
        int defaultsortorder = 0;
        WCTemplputParams SubTP;
        char *ab_name;
+       const StrBuf *Mime;
 
        if (havebstr("is_summary") && (1 == (ibstr("is_summary"))))
                WCC->wc_view = VIEW_MAILBOX;
@@ -926,7 +1089,7 @@ void readloop(long oper)
                        /** if we do a split bbview in the future, begin messages div here */
                        
                        for (a=0; a<num_displayed; ++a) {
-                               read_message(WCC->WBuf, HKEY("view_message"), displayed_msgs[a], NULL);
+                               read_message(WCC->WBuf, HKEY("view_message"), displayed_msgs[a], NULL, &Mime);
                        }
                        
                        /** if we do a split bbview in the future, end messages div here */
@@ -960,7 +1123,7 @@ DONE:
                break;
        case VIEW_ADDRESSBOOK:
                if (is_singlecard)
-                       read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), NULL);
+                       read_message(WC->WBuf, HKEY("view_message"), lbstr("startmsg"), NULL, &Mime);
                else
                        do_addrbook_view(addrbook, num_ab);     /** Render the address book */
                break;
@@ -1568,14 +1731,18 @@ void postpart(StrBuf *partnum, StrBuf *filename, int force_download)
  */
 void mimepart(int force_download)
 {
+       long msgnum, att;
        wcsession *WCC = WC;
        StrBuf *Buf;
        off_t bytes;
        StrBuf *ContentType = NewStrBufPlain(HKEY("application/octet-stream"));
        const char *CT;
 
+       msgnum = StrBufExtract_long(WCC->Hdr->ReqLine, 2, '/');
+       att = StrBufExtract_long(WCC->Hdr->ReqLine, 3, '/');
+
        Buf = NewStrBuf();
-       serv_printf("OPNA %s|%s", ChrPtr(WCC->UrlFragment2), ChrPtr(WCC->UrlFragment3));
+       serv_printf("OPNA %ld|%ld", msgnum, att);
        StrBuf_ServGetln(Buf);
        if (GetServerStatus(Buf, NULL) == 2) {
                StrBufCutLeft(Buf, 4);
@@ -1591,7 +1758,8 @@ void mimepart(int force_download)
 
                if (!force_download) {
                        if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
-                               CT = GuessMimeByFilename(SKEY(WCC->UrlFragment4));
+                               StrBufExtract_token(Buf, WCC->Hdr->ReqLine, 4, '/');
+                               CT = GuessMimeByFilename(SKEY(Buf));
                        }
                        if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
                                CT = GuessMimeType(SKEY(WCC->WBuf));
@@ -1674,15 +1842,29 @@ void download_mimepart(void) {
 }
 
 void view_postpart(void) {
-       postpart(WC->UrlFragment2,
-                WC->UrlFragment3,
-                0);
+       StrBuf *filename = NewStrBuf();
+       StrBuf *partnum = NewStrBuf();
+
+       StrBufExtract_token(partnum, WC->Hdr->ReqLine, 2, '/');
+       StrBufExtract_token(filename, WC->Hdr->ReqLine, 3, '/');
+
+       postpart(partnum, filename, 0);
+
+       FreeStrBuf(&filename);
+       FreeStrBuf(&partnum);
 }
 
 void download_postpart(void) {
-       postpart(WC->UrlFragment2,
-                WC->UrlFragment3,
-                1);
+       StrBuf *filename = NewStrBuf();
+       StrBuf *partnum = NewStrBuf();
+
+       StrBufExtract_token(partnum, WC->Hdr->ReqLine, 2, '/');
+       StrBufExtract_token(filename, WC->Hdr->ReqLine, 3, '/');
+
+       postpart(partnum, filename, 1);
+
+       FreeStrBuf(&filename);
+       FreeStrBuf(&partnum);
 }
 
 void h_readnew(void) { readloop(readnew);}
@@ -1752,7 +1934,8 @@ InitModule_MSG
        WebcitAddUrlHandler(HKEY("move_msg"), move_msg, 0);
        WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
        WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
-       WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL|AJAX);
+       WebcitAddUrlHandler(HKEY("msg"), embed_message, NEED_URL);
+       WebcitAddUrlHandler(HKEY("message"), handle_one_message, NEED_URL|XHTTP_COMMANDS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
        WebcitAddUrlHandler(HKEY("printmsg"), print_message, NEED_URL);
        WebcitAddUrlHandler(HKEY("mobilemsg"), mobile_message_view, NEED_URL);
        WebcitAddUrlHandler(HKEY("msgheaders"), display_headers, NEED_URL);