Roomediting: add permission check to to some dialogs
[citadel.git] / webcit / roomops.c
index 4799d1c6d534bc08def015d78a6eb20585c7b72d..b6fdaa8df8113f5154a3bddd45c2e60050fcb920 100644 (file)
@@ -1,11 +1,26 @@
 /*
  * Lots of different room-related operations.
+ *
+ * Copyright (c) 1996-2011 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.
+ *
+ * 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 "webcit.h"
 #include "webserver.h"
 
-
 ConstStr QRFlagList[] = {
        {HKEY(strof(QR_PERMANENT))},
        {HKEY(strof(QR_INUSE))},
@@ -41,7 +56,7 @@ ConstStr QR2FlagList[] = {
        {HKEY("")}
 };
 
-void DBG_QR(long QR)
+void _DBG_QR(long QR)
 {
        int i = 1;
        int j=0;
@@ -64,7 +79,7 @@ void DBG_QR(long QR)
 
 
 
-void DBG_QR2(long QR2)
+void _DBG_QR2(long QR2)
 {
        int i = 1;
        int j=0;
@@ -98,6 +113,11 @@ void DBG_QR2(long QR2)
 /*******************************************************************************
  ***************************** Goto Commands ***********************************
  ******************************************************************************/
+
+void dotskip(void) {
+       smart_goto(sbstr("room"));
+}
+
 void dotgoto(void) {
        if (!havebstr("room")) {
                readloop(readnew, eUseDefault);
@@ -166,18 +186,30 @@ long gotoroom(const StrBuf *gname)
        StrBuf *Buf;
        static long ls = (-1L);
        long err = 0;
+       int room_name_supplied = 0;
+       int is_baseroom = 0;
 
        /* store ungoto information */
-       if (StrLength(gname) > 0)
+       if (StrLength(gname) > 0) {
+               room_name_supplied = 1;
+       }
+       if (room_name_supplied) {
                strcpy(WCC->ugname, ChrPtr(WCC->CurRoom.name));
+               if (!strcasecmp(ChrPtr(gname), "_BASEROOM_")) {
+                       is_baseroom = 1;
+               }
+       }
        WCC->uglsn = ls;
        Buf = NewStrBuf();
 
        /* move to the new room */
-       if (StrLength(gname) > 0)
+       if (room_name_supplied) {
                serv_printf("GOTO %s", ChrPtr(gname));
-       else /* or just refresh the current state... */
+       }
+       else {
+               /* or just refresh the current state... */
                serv_printf("GOTO 00000000000000000000");
+       }
        StrBuf_ServGetln(Buf);
        if  (GetServerStatus(Buf, &err) != 2) {
                serv_puts("GOTO _BASEROOM_");
@@ -195,11 +227,11 @@ long gotoroom(const StrBuf *gname)
        FlushFolder(&WCC->CurRoom);
        ParseGoto(&WCC->CurRoom, Buf);
 
-       if (StrLength(gname) > 0)
-       {
+       if (room_name_supplied) {
                remove_march(WCC->CurRoom.name);
-               if (!strcasecmp(ChrPtr(gname), "_BASEROOM_"))
+               if (is_baseroom) {
                        remove_march(gname);
+               }
        }
        FreeStrBuf(&Buf);
 
@@ -240,7 +272,7 @@ void ParseGoto(folder *room, StrBuf *Line)
        memset(room, 0, sizeof(folder));
        room->name = pBuf;
 
-       StrBufExtract_NextToken(room->name, Line, &Pos, '|'); // WC->CurRoom->name
+       StrBufExtract_NextToken(room->name, Line, &Pos, '|');
 
        room->nNewMessages = StrBufExtractNext_long(Line, &Pos, '|'); 
        if (room->nNewMessages > 0)
@@ -250,14 +282,14 @@ void ParseGoto(folder *room, StrBuf *Line)
 
        room->ShowInfo =  StrBufExtractNext_long(Line, &Pos, '|');
        
-       room->QRFlags = StrBufExtractNext_long(Line, &Pos, '|'); //CurRoom->QRFlags
+       room->QRFlags = StrBufExtractNext_long(Line, &Pos, '|');
 
        DBG_QR(room->QRFlags);
 
        room->HighestRead = StrBufExtractNext_long(Line, &Pos, '|');
        room->LastMessageRead = StrBufExtractNext_long(Line, &Pos, '|');
 
-       room->is_inbox = StrBufExtractNext_long(Line, &Pos, '|'); // is_mailbox
+       room->is_inbox = StrBufExtractNext_long(Line, &Pos, '|');
 
        flag = StrBufExtractNext_long(Line, &Pos, '|');
        if (WCC->is_aide || flag) {
@@ -266,17 +298,17 @@ void ParseGoto(folder *room, StrBuf *Line)
 
        room->UsersNewMAilboxMessages = StrBufExtractNext_long(Line, &Pos, '|');
 
-       room->floorid = StrBufExtractNext_int(Line, &Pos, '|'); // wc_floor
+       room->floorid = StrBufExtractNext_int(Line, &Pos, '|');
 
-       room->view = StrBufExtractNext_long(Line, &Pos, '|'); // CurRoom->view
+       room->view = StrBufExtractNext_long(Line, &Pos, '|');
 
-       room->defview = StrBufExtractNext_long(Line, &Pos, '|'); // CurRoom->defview
+       room->defview = StrBufExtractNext_long(Line, &Pos, '|');
 
        flag = StrBufExtractNext_long(Line, &Pos, '|');
        if (flag)
-               room->RAFlags |= UA_ISTRASH; // wc_is_trash
+               room->RAFlags |= UA_ISTRASH;
 
-       room->QRFlags2 = StrBufExtractNext_long(Line, &Pos, '|'); // CurRoom->QRFlags2
+       room->QRFlags2 = StrBufExtractNext_long(Line, &Pos, '|');
        DBG_QR2(room->QRFlags2);
 
        /* find out, whether we are in a sub-room */
@@ -317,21 +349,17 @@ void ParseGoto(folder *room, StrBuf *Line)
        room->Floor = (const Floor*) vFloor;
 }
 
-/**
- * \brief Delete the current room
+/*
+ * Delete the current room
  */
 void delete_room(void)
 {
-       char buf[SIZ];
-
+       StrBuf *Line;
        
        serv_puts("KILL 1");
-       serv_getln(buf, sizeof buf);
-
-       if (buf[0] != '2') {
-               strcpy(WC->ImportantMessage, &buf[4]);
+       StrBuf_ServGetln(Line);
+       if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
                display_main_menu();
-               return;
        } else {
                StrBuf *Buf;
                
@@ -340,10 +368,11 @@ void delete_room(void)
                smart_goto(Buf);
                FreeStrBuf(&Buf);
        }
+       FreeStrBuf(&Line);
 }
 
-/**
- * \brief zap a room
+/*
+ * zap a room
  */
 void zap(void)
 {
@@ -452,7 +481,7 @@ int SaveRoomAide(folder *Room)
 }
 
 
-int GetCurrentRoomFlags(folder *Room)
+int GetCurrentRoomFlags(folder *Room, int CareForStatusMessage)
 {
        StrBuf *Buf;
 
@@ -463,8 +492,10 @@ int GetCurrentRoomFlags(folder *Room)
                FlushStrBuf(Room->XAPass);
                FlushStrBuf(Room->Directory);
                StrBufCutLeft(Buf, 4);
-               AppendImportantMessage (SKEY(Buf));
+               if (CareForStatusMessage)
+                       AppendImportantMessage (SKEY(Buf));
                FreeStrBuf(&Buf);
+               Room->XALoaded = 2;
                return 0;
        } else {
                const char *Pos;
@@ -531,10 +562,10 @@ void LoadRoomXA (void)
 {
        wcsession *WCC = WC;
                
-       if (WCC->CurRoom.XALoaded)
+       if (WCC->CurRoom.XALoaded > 0)
                return;
 
-       GetCurrentRoomFlags(&WCC->CurRoom);
+       GetCurrentRoomFlags(&WCC->CurRoom, 0);
 }
 
 
@@ -629,7 +660,7 @@ void LoadXRoomXCountFiles(void)
 void toggle_self_service(void) {
        wcsession *WCC = WC;
 
-       if (GetCurrentRoomFlags (&WCC->CurRoom) == 0)
+       if (GetCurrentRoomFlags (&WCC->CurRoom, 1) == 0)
                return;
 
        if (yesbstr("QR2_SelfList")) 
@@ -653,7 +684,7 @@ void toggle_self_service(void) {
 
        SetCurrentRoomFlags (&WCC->CurRoom);
        
-       http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+       http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
 }
 
 
@@ -674,13 +705,14 @@ void editroom(void)
        int succ1, succ2;
 
        if (!havebstr("ok_button")) {
-               strcpy(WC->ImportantMessage,
-                      _("Cancelled.  Changes were not saved."));
-               http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+               AppendImportantMessage(_("Cancelled.  Changes were not saved."), -1);
+               http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
                return;
        }
-       if (GetCurrentRoomFlags (&WCC->CurRoom) == 0)
+       if (GetCurrentRoomFlags (&WCC->CurRoom, 1) == 0) {
+               http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
                return;
+       }
 
        LoadRoomAide();
 
@@ -804,7 +836,7 @@ void editroom(void)
        
        if (succ1 + succ2 == 0)
                AppendImportantMessage (_("Your changes have been saved."), -1);
-       http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+       http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
        return;
 }
 
@@ -821,7 +853,7 @@ void do_invt_kick(void)
        wcsession *WCC = WC;
 
 
-       if (GetCurrentRoomFlags(&WCC->CurRoom) == 1)
+       if (GetCurrentRoomFlags(&WCC->CurRoom, 1) == 1)
        {
                const char *Pos;
                UserNames = sbstr("username");
@@ -839,7 +871,8 @@ void do_invt_kick(void)
                        if ((StrLength(User) > 0) && (Kick))
                        {
                                serv_printf("KICK %s", ChrPtr(User));
-                               StrBuf_ServGetln(Buf);
+                               if (StrBuf_ServGetln(Buf) < 0)
+                                       break;
                                if (GetServerStatus(Buf, NULL) != 2) {
                                        StrBufCutLeft(Buf, 4);
                                        AppendImportantMessage(SKEY(Buf));
@@ -855,7 +888,8 @@ void do_invt_kick(void)
                        else if ((StrLength(User) > 0) && (Invite))
                        {
                                serv_printf("INVT %s", ChrPtr(User));
-                               StrBuf_ServGetln(Buf);
+                               if (StrBuf_ServGetln(Buf) < 0)
+                                       break;
                                if (GetServerStatus(Buf, NULL) != 2) {
                                        StrBufCutLeft(Buf, 4);
                                        AppendImportantMessage(SKEY(Buf));
@@ -871,7 +905,7 @@ void do_invt_kick(void)
                 }
         }
 
-       http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+       http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
 }
 
 
@@ -880,7 +914,7 @@ void do_invt_kick(void)
  */
 void entroom(void)
 {
-       char buf[SIZ];
+       StrBuf *Line;
        const StrBuf *er_name;
        const StrBuf *er_type;
        const StrBuf *er_password;
@@ -890,8 +924,7 @@ void entroom(void)
        wcsession *WCC = WC;
 
        if (!havebstr("ok_button")) {
-               strcpy(WC->ImportantMessage,
-                      _("Cancelled.  No new room was created."));
+               AppendImportantMessage(_("Cancelled.  No new room was created."), -1);
                display_main_menu();
                return;
        }
@@ -919,9 +952,10 @@ void entroom(void)
                    0, 
                    er_view);
 
-       serv_getln(buf, sizeof buf);
-       if (buf[0] != '2') {
-               strcpy(WCC->ImportantMessage, &buf[4]);
+       Line = NewStrBuf();
+       StrBuf_ServGetln(Line);
+       if (GetServerStatusMsg(Line, NULL, 1, 2) != 2) {
+               FreeStrBuf(&Line);
                display_main_menu();
                return;
        }
@@ -929,15 +963,16 @@ void entroom(void)
        gotoroom(er_name);
 
        serv_printf("VIEW %d", er_view);
-       serv_getln(buf, sizeof buf);
+       StrBuf_ServGetln(Line);
+       FreeStrBuf(&Line); /* TODO: should we care about errors? */
        WCC->CurRoom.view = er_view;
 
        if ( (WCC != NULL) && ( (WCC->CurRoom.RAFlags & UA_ADMINALLOWED) != 0) )  {
-               http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+               http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
        } else {
                smart_goto(WCC->CurRoom.name);
        }
-
+       FreeStrBuf(&Line);
 }
 
 
@@ -960,40 +995,38 @@ void change_view(void) {
 
 
 
-/**
- * \brief Set the message expire policy for this room and/or floor
+/*
+ * Set the message expire policy for this room and/or floor
  */
 void set_room_policy(void) {
-       char buf[SIZ];
+       StrBuf *Line;
 
        if (!havebstr("ok_button")) {
-               strcpy(WC->ImportantMessage,
-                      _("Cancelled.  Changes were not saved."));
-               http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+               AppendImportantMessage(_("Cancelled.  Changes were not saved."), -1);
+               http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
                return;
        }
+       Line = NewStrBuf();
 
        serv_printf("SPEX roompolicy|%d|%d", ibstr("roompolicy"), ibstr("roomvalue"));
-       serv_getln(buf, sizeof buf);
-       strcpy(WC->ImportantMessage, &buf[4]);
-
+       StrBuf_ServGetln(Line);
+       GetServerStatusMsg(Line, NULL, 1, 0);
        if (WC->axlevel >= 6) {
-               strcat(WC->ImportantMessage, "<br>\n");
                serv_printf("SPEX floorpolicy|%d|%d", ibstr("floorpolicy"), ibstr("floorvalue"));
-               serv_getln(buf, sizeof buf);
-               strcat(WC->ImportantMessage, &buf[4]);
+               StrBuf_ServGetln(Line);
+               GetServerStatusMsg(Line, NULL, 1, 0);
        }
+       FreeStrBuf(&Line);
        ReloadCurrentRoom();
-       http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+       http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
 }
 
 
 
-/**
- * \brief Perform changes to a room's network configuration
+/*
+ * Perform changes to a room's network configuration
  */
 void netedit(void) {
-       FILE *fp;
        char buf[SIZ];
        char line[SIZ];
        char cmpa0[SIZ];
@@ -1002,6 +1035,7 @@ void netedit(void) {
        char cmpb1[SIZ];
        int i, num_addrs;
        StrBuf *Line;
+       StrBuf *TmpBuf;
        int Done;
 
        /*/ TODO: do line dynamic! */
@@ -1024,25 +1058,18 @@ void netedit(void) {
                strcat(line, bstr("suffix"));
        }
        else {
-               http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
-               return;
-       }
-
-
-       fp = tmpfile();
-       if (fp == NULL) {
-               http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+               http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
                return;
        }
 
        Line = NewStrBuf();
+       TmpBuf = NewStrBuf();
        serv_puts("GNET");
        StrBuf_ServGetln(Line);
        if  (GetServerStatus(Line, NULL) != 1) {
-               fclose(fp);
                AppendImportantMessage(SRV_STATUS_MSG(Line));   
                FreeStrBuf(&Line);
-               http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+               http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
                return;
        }
 
@@ -1063,26 +1090,24 @@ void netedit(void) {
                        if ( (strcasecmp(cmpa0, cmpb0)) 
                             || (strcasecmp(cmpa1, cmpb1)) ) {
                                StrBufAppendBufPlain(Line, HKEY("\n"), 0);
-                               fwrite(SKEY(Line), 1, fp);
+                               StrBufAppendBuf(TmpBuf, Line, 0);
                        }
                }
        }
 
-       rewind(fp);
        serv_puts("SNET");
        StrBuf_ServGetln(Line);
        if  (GetServerStatus(Line, NULL) != 4) {
-               fclose(fp);
+
                AppendImportantMessage(SRV_STATUS_MSG(Line));   
-               http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+               http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
                FreeStrBuf(&Line);
+               FreeStrBuf(&TmpBuf);
                return;
        }
 
-       while (fgets(buf, sizeof buf, fp) != NULL) {
-               buf[strlen(buf)-1] = 0;
-               serv_puts(buf);
-       }
+       serv_putbuf(TmpBuf);
+       FreeStrBuf(&TmpBuf);
 
        if (havebstr("add_button")) {
                num_addrs = num_tokens(bstr("line"), ',');
@@ -1104,38 +1129,21 @@ void netedit(void) {
        }
 
        serv_puts("000");
-       fclose(fp);
        FlushIgnetCfgs(&WC->CurRoom);
        FreeStrBuf(&Line);
 
-       http_transmit_thing(ChrPtr(do_template("room_edit", NULL)), 0);
+       http_transmit_thing(ChrPtr(do_template("room_edit")), 0);
 }
 
-/**
- * \brief Do either a known rooms list or a folders list, depending on the
- * user's preference
+/*
+ * Known rooms list (box style)
  */
 void knrooms(void)
 {
-       StrBuf *ListView = NULL;
-
-       /** Determine whether the user is trying to change views */
-       if (havebstr("view")) {
-               ListView = NewStrBufDup(SBSTR("view"));
-               set_preference("roomlistview", ListView, 1);
-       }
-       /** Sanitize the input so its safe */
-       if ((get_preference("roomlistview", &ListView) == 0)||
-           (
-                   (strcasecmp(ChrPtr(ListView), "folders") != 0) &&
-                   (strcasecmp(ChrPtr(ListView), "rooms") != 0)
-                   )
-               )
-       {
-               ListView = NewStrBufPlain(HKEY("rooms"));
-               set_preference("roomlistview", ListView, 0);
-       }
-       url_do_template();
+       DeleteHash(&WC->Rooms);
+       output_headers(1, 1, 1, 0, 0, 0); 
+       do_template("knrooms");
+       wDumpContent(1);
 }
 
 
@@ -1184,7 +1192,7 @@ void delete_floor(void) {
        AppendImportantMessage (SKEY(Buf));
 
        FlushRoomlist();
-       http_transmit_thing(ChrPtr(do_template("floors", NULL)), 0);
+       http_transmit_thing(ChrPtr(do_template("floors")), 0);
        FreeStrBuf(&Buf);
 }
 
@@ -1207,7 +1215,7 @@ void create_floor(void) {
        }
        AppendImportantMessage (SKEY(Buf));
        FlushRoomlist();
-       http_transmit_thing(ChrPtr(do_template("floors", NULL)), 0);
+       http_transmit_thing(ChrPtr(do_template("floors")), 0);
        FreeStrBuf(&Buf);
 }
 
@@ -1227,7 +1235,7 @@ void rename_floor(void) {
        StrBufCutLeft(Buf, 4);
        AppendImportantMessage (SKEY(Buf));
 
-       http_transmit_thing(ChrPtr(do_template("floors", NULL)), 0);
+       http_transmit_thing(ChrPtr(do_template("floors")), 0);
        FreeStrBuf(&Buf);
 }
 
@@ -1291,7 +1299,7 @@ InitModule_ROOMOPS
        WebcitAddUrlHandler(HKEY("rename_floor"), "", 0, rename_floor, 0);
        WebcitAddUrlHandler(HKEY("create_floor"), "", 0, create_floor, 0);
 
-       WebcitAddUrlHandler(HKEY("knrooms"), "", 0, knrooms, 0);
+       WebcitAddUrlHandler(HKEY("knrooms"), "", 0, knrooms, ANONYMOUS);
        WebcitAddUrlHandler(HKEY("dotgoto"), "", 0, dotgoto, NEED_URL);
        WebcitAddUrlHandler(HKEY("dotskip"), "", 0, dotskip, NEED_URL);
 
@@ -1400,5 +1408,3 @@ SessionDestroyModule_ROOMOPS
        _FlushRoomList (sess);
 }
 
-
-/*@}*/