Fixed a problem with wiki history display being broken because carriage returns are...
[citadel.git] / citadel / modules / wiki / serv_wiki.c
index 62b2132d521d217c38037cc450a768e9c2d9bc11..07e5eed41e231aca188764b375681559b62d8692 100644 (file)
@@ -1,21 +1,15 @@
 /*
  * Server-side module for Wiki rooms.  This handles things like version control. 
  * 
- * Copyright (c) 2009-2011 by the citadel.org team
+ * Copyright (c) 2009-2012 by the citadel.org team
  *
  * This program is open source software.  You can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 3 of the
- * License, or (at your option) any later version.
+ * modify it under the terms of the GNU General Public License, version 3.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
 #include "sysdep.h"
@@ -168,7 +162,7 @@ int wiki_upload_beforesave(struct CtdlMessage *msg) {
        fclose(fp);
 
        snprintf(diff_cmd, sizeof diff_cmd,
-               "diff -u %s %s >%s",
+               DIFF " -u %s %s >%s",
                diff_new_filename,
                ((old_msg != NULL) ? diff_old_filename : "/dev/null"),
                diff_out_filename
@@ -193,7 +187,7 @@ int wiki_upload_beforesave(struct CtdlMessage *msg) {
                fclose(fp);
        }
 
-       syslog(LOG_DEBUG, "diff length is %d bytes", diffbuf_len);
+       syslog(LOG_DEBUG, "diff length is "SIZE_T_FMT" bytes", diffbuf_len);
 
        unlink(diff_old_filename);
        unlink(diff_new_filename);
@@ -281,11 +275,8 @@ int wiki_upload_beforesave(struct CtdlMessage *msg) {
                }
        } while ( (IsEmptyStr(boundary)) && (*ptr != 0) );
 
-
-       /****************** STACK SMASH IS SOMEWHERE BELOW THIS LINE **************/
-
-
-       /* Now look for the first boundary.  That is where we need to insert our fun.
+       /*
+        * Now look for the first boundary.  That is where we need to insert our fun.
         */
        if (!IsEmptyStr(boundary)) {
                snprintf(prefixed_boundary, sizeof prefixed_boundary, "--%s", boundary);
@@ -295,16 +286,15 @@ int wiki_upload_beforesave(struct CtdlMessage *msg) {
                ptr = bmstrcasestr(history_msg->cm_fields['M'], prefixed_boundary);
                if (ptr != NULL) {
                        char *the_rest_of_it = strdup(ptr);
-                       char uuid[32];
+                       char uuid[64];
                        char memo[512];
-                       char encoded_memo[768];
+                       char encoded_memo[1024];
                        generate_uuid(uuid);
                        snprintf(memo, sizeof memo, "%s|%ld|%s|%s", 
                                uuid,
                                time(NULL),
                                CCC->user.fullname,
                                config.c_nodename
-                               /* no longer logging CCC->cs_inet_email */
                        );
                        CtdlEncodeBase64(encoded_memo, memo, strlen(memo), 0);
                        sprintf(ptr, "--%s\n"
@@ -324,10 +314,9 @@ int wiki_upload_beforesave(struct CtdlMessage *msg) {
                }
 
                history_msg->cm_fields['T'] = realloc(history_msg->cm_fields['T'], 32);
-               if (history_msg->cm_fields['T'] == NULL) {
-                       syslog(LOG_EMERG, "*** REALLOC FAILED *** %s", strerror(errno));
+               if (history_msg->cm_fields['T'] != NULL) {
+                       snprintf(history_msg->cm_fields['T'], 32, "%ld", time(NULL));
                }
-               snprintf(history_msg->cm_fields['T'], 32, "%ld", time(NULL));
        
                CtdlSubmitMsg(history_msg, NULL, "", 0);
        }
@@ -335,10 +324,6 @@ int wiki_upload_beforesave(struct CtdlMessage *msg) {
                syslog(LOG_ALERT, "Empty boundary string in history message.  No history!\n");
        }
 
-
-       /****************** STACK SMASH IS SOMEWHERE BELOW THIS LINE **************/
-
-
        free(diffbuf);
        free(history_msg);
        return(0);
@@ -440,10 +425,10 @@ void wiki_rev_callback(char *name, char *filename, char *partnum, char *disp,
 
        CtdlDecodeBase64(memo, filename, strlen(filename));
        extract_token(this_rev, memo, 0, '|', sizeof this_rev);
-       syslog(LOG_DEBUG, "callback found rev: %s\n", this_rev);
+       striplt(this_rev);
 
        /* Perform the patch */
-       fp = popen("patch -f -s -p0 -r /dev/null >/dev/null 2>/dev/null", "w");
+       fp = popen(PATCH " -f -s -p0 -r /dev/null >/dev/null 2>/dev/null", "w");
        if (fp) {
                /* Replace the filenames in the patch with the tempfilename we're actually tweaking */
                fprintf(fp, "--- %s\n", hecbd->tempfilename);
@@ -575,6 +560,7 @@ void wiki_rev(char *pagename, char *rev, char *operation)
        memset(&hecbd, 0, sizeof(struct HistoryEraserCallBackData));
        hecbd.tempfilename = temp;
        hecbd.stop_when = rev;
+       striplt(hecbd.stop_when);
 
        mime_parser(msg->cm_fields['M'], NULL, *wiki_rev_callback, NULL, NULL, (void *)&hecbd, 0);
        CtdlFreeMessage(msg);
@@ -612,6 +598,24 @@ void wiki_rev(char *pagename, char *rev, char *operation)
                        msg->cm_fields['A'] = strdup("Citadel");
                        CtdlCreateRoom(wwm, 5, "", 0, 1, 1, VIEW_BBS);  /* Not an error if already exists */
                        msgnum = CtdlSubmitMsg(msg, NULL, wwm, 0);      /* Store the revision here */
+
+                       /*
+                        * WARNING: VILE SLEAZY HACK
+                        * This will avoid the 'message xxx is not in this room' security error,
+                        * but only if the client fetches the message we just generated immediately
+                        * without first trying to perform other fetch operations.
+                        */
+                       if (CC->cached_msglist != NULL) {
+                               free(CC->cached_msglist);
+                               CC->cached_msglist = NULL;
+                               CC->cached_num_msgs = 0;
+                       }
+                       CC->cached_msglist = malloc(sizeof(long));
+                       if (CC->cached_msglist != NULL) {
+                               CC->cached_num_msgs = 1;
+                               CC->cached_msglist[0] = msgnum;
+                       }
+
                }
                else if (!strcasecmp(operation, "revert")) {
                        snprintf(timestamp, sizeof timestamp, "%ld", time(NULL));
@@ -688,6 +692,6 @@ CTDL_MODULE_INIT(wiki)
                CtdlRegisterProtoHook(cmd_wiki, "WIKI", "Commands related to Wiki management");
        }
 
-       /* return our Subversion id for the Log */
+       /* return our module name for the log */
        return "wiki";
 }