* move stuff from Hdr into substruct that may be easily wiped by memset 0'ing them
[citadel.git] / webcit / messages.c
index 6b683a1e65b6332314f2c785d00b6d5272fcdd36..a3bca061a13ec2d54410d6b39015cfcdb7a03425 100644 (file)
@@ -277,9 +277,151 @@ 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;
+       const char *pMsg;
+
 
+       pMsg = strchr(ChrPtr(WCC->Hdr->HR.ReqLine), '/');
+       if (pMsg == NULL) {
+               HttpStatus(CitStatus);
+               return;
+       }
 
+       msgnum = atol(pMsg + 1);
+       StrBufCutAt(WCC->Hdr->HR.ReqLine, 0, pMsg);
+       gotoroom(WCC->Hdr->HR.ReqLine);
+       switch (WCC->Hdr->HR.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
@@ -293,8 +435,8 @@ void embed_message(void) {
        const StrBuf *Tmpl;
        StrBuf *CmdBuf = NULL;
 
-       msgnum = StrTol(WCC->UrlFragment2);
-       switch (WCC->eReqType)
+       msgnum = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 0, '/');
+       switch (WCC->Hdr->HR.eReqType)
        {
        case eGET:
        case ePOST:
@@ -333,7 +475,7 @@ void print_message(void) {
        long msgnum = 0L;
        const StrBuf *Mime;
 
-       msgnum = StrTol(WC->UrlFragment2);
+       msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
        output_headers(0, 0, 0, 0, 0, 0);
 
        hprintf("Content-type: text/html\r\n"
@@ -357,7 +499,7 @@ void mobile_message_view(void)
        long msgnum = 0L;
        const StrBuf *Mime;
   
-       msgnum = StrTol(WC->UrlFragment2);
+       msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
        output_headers(1, 0, 0, 0, 0, 1);
        begin_burst();
        do_template("msgcontrols", NULL);
@@ -374,7 +516,7 @@ void display_headers(void) {
        long msgnum = 0L;
        char buf[1024];
 
-       msgnum = StrTol(WC->UrlFragment2);
+       msgnum = StrBufExtract_long(WC->Hdr->HR.ReqLine, 0, '/');
        output_headers(0, 0, 0, 0, 0, 0);
 
        hprintf("Content-type: text/plain\r\n"
@@ -1598,14 +1740,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->HR.ReqLine, 0, '/');
+       att = StrBufExtract_long(WCC->Hdr->HR.ReqLine, 1, '/');
+
        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);
@@ -1621,7 +1767,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->HR.ReqLine, 2, '/');
+                               CT = GuessMimeByFilename(SKEY(Buf));
                        }
                        if (!strcasecmp(ChrPtr(ContentType), "application/octet-stream")) {
                                CT = GuessMimeType(SKEY(WCC->WBuf));
@@ -1704,15 +1851,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->HR.ReqLine, 0, '/');
+       StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
+
+       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->HR.ReqLine, 0, '/');
+       StrBufExtract_token(filename, WC->Hdr->HR.ReqLine, 1, '/');
+
+       postpart(partnum, filename, 1);
+
+       FreeStrBuf(&filename);
+       FreeStrBuf(&partnum);
 }
 
 void h_readnew(void) { readloop(readnew);}
@@ -1783,6 +1944,7 @@ InitModule_MSG
        WebcitAddUrlHandler(HKEY("delete_msg"), delete_msg, 0);
        WebcitAddUrlHandler(HKEY("confirm_move_msg"), confirm_move_msg, 0);
        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);