* shrinked the rest of these shutdown stuff into the module-handler.
[citadel.git] / webcit / paging.c
index ddc2fbbeaf1087422b7dbb255e2d15ce4f9bba01..d44c816cb2327d4101acc869afb87b93c8583985 100644 (file)
@@ -1,7 +1,5 @@
 /*
  * $Id$
- *
- * Functions which implement the chat and paging facilities.
  */
 
 #include "webcit.h"
@@ -16,23 +14,24 @@ void display_page(void)
        strcpy(recp, bstr("recp"));
 
         output_headers(1, 1, 2, 0, 0, 0);
-        wprintf("<div id=\"banner\">\n"
-                "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
-                "<SPAN CLASS=\"titlebar\">");
+        wprintf("<div id=\"banner\">\n");
+        wprintf("<h1>");
        wprintf(_("Send instant message"));
-       wprintf("</SPAN>"
-                "</TD></TR></TABLE>\n"
-                "</div>\n<div id=\"content\">\n"
-        );
-                                                                                                                             
+       wprintf("</h1>");
+        wprintf("</div>\n");
+
+       wprintf("<div id=\"content\" class=\"service\">\n");
+
         wprintf("<div class=\"fix_scrollbar_bug\">"
-               "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
+               "<table class=\"paging_background\"><tr><td>\n");
 
        wprintf(_("Send an instant message to: "));
        escputs(recp);
        wprintf("<br>\n");
 
        wprintf("<FORM METHOD=\"POST\" action=\"page_user\">\n");
+       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
+       wprintf("<input type=\"hidden\" name=\"template\" value=\"who\">\n");
 
        wprintf("<TABLE border=0 width=100%%><TR><TD>\n");
 
@@ -40,10 +39,6 @@ void display_page(void)
        escputs(recp);
        wprintf("\">\n");
 
-       wprintf("<INPUT TYPE=\"hidden\" NAME=\"closewin\" VALUE=\"");
-       escputs(bstr("closewin"));
-       wprintf("\">\n");
-
        wprintf(_("Enter message text:"));
        wprintf("<br />");
 
@@ -60,76 +55,66 @@ void display_page(void)
        wDumpContent(1);
 }
 
-/*
- * page another user
+/**
+ * \brief page another user
  */
 void page_user(void)
 {
-       char recp[SIZ];
-       char buf[SIZ];
-       char closewin[SIZ];
+       char recp[256];
+       char buf[256];
 
-        output_headers(1, 1, 2, 0, 0, 0);
-        wprintf("<div id=\"banner\">\n"
-                "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
-                "<SPAN CLASS=\"titlebar\">");
-       wprintf(_("Add or edit an event"));
-       wprintf("</SPAN>"
-                "</TD></TR></TABLE>\n"
-                "</div>\n<div id=\"content\">\n"
-        );
-                                                                                                                             
-       strcpy(recp, bstr("recp"));
-       strcpy(closewin, bstr("closewin"));
+       safestrncpy(recp, bstr("recp"), sizeof recp);
 
-       if (strlen(bstr("send_button")) == 0) {
-               wprintf("<EM>");
-               wprintf(_("Message was not sent."));
-               wprintf("</EM><br />\n");
+       if (!havebstr("send_button")) {
+               safestrncpy(WC->ImportantMessage,
+                       _("Message was not sent."),
+                       sizeof WC->ImportantMessage
+               );
        } else {
                serv_printf("SEXP %s|-", recp);
                serv_getln(buf, sizeof buf);
 
                if (buf[0] == '4') {
-                       text_to_server(bstr("msgtext"), 0);
+                       text_to_server(bstr("msgtext"));
                        serv_puts("000");
-                       wprintf("<EM>");
-                       wprintf(_("Message has been sent to "));
-                       escputs(recp);
-                       wprintf(".</EM><br />\n");
+                       stresc(buf, 256, recp, 0, 0);
+                       snprintf(WC->ImportantMessage,
+                               sizeof WC->ImportantMessage,
+                               "%s%s.",
+                               _("Message has been sent to "),
+                               buf
+                       );
                }
                else {
-                       wprintf("<EM>%s</EM><br />\n", &buf[4]);
+                       safestrncpy(WC->ImportantMessage, &buf[4], sizeof WC->ImportantMessage);
                }
        }
-       
-       if (!strcasecmp(closewin, "yes")) {
-               wprintf("<CENTER><a href=\"javascript:window.close();\">");
-               wprintf(_("[ close window ]"));
-               wprintf("</A></CENTER>\n");
-       }
 
-       wDumpContent(1);
+       url_do_template();
 }
 
 
 
-/*
- * multiuser chat
+/**
+ * \brief multiuser chat
  */
 void do_chat(void)
 {
        char buf[SIZ];
 
-       /* First, check to make sure we're still allowed in this room. */
-       serv_printf("GOTO %s", WC->wc_roomname);
+       /** First, check to make sure we're still allowed in this room. */
+       serv_printf("GOTO %s", ChrPtr(WC->wc_roomname));
        serv_getln(buf, sizeof buf);
        if (buf[0] != '2') {
-               smart_goto("_BASEROOM_");
+               StrBuf *Buf;
+               Buf = NewStrBufPlain(HKEY("_BASEROOM_"));
+               smart_goto(Buf);
+               FreeStrBuf(&Buf);
                return;
        }
 
-       /* If the chat socket is still open from a previous chat,
+       /**
+        * If the chat socket is still open from a previous chat,
         * close it -- because it might be stale or in the wrong room.
         */
        if (WC->chat_sock < 0) {
@@ -137,60 +122,82 @@ void do_chat(void)
                WC->chat_sock = (-1);
        }
 
-       /* WebCit Chat works by having transmit, receive, and refresh
+       /**
+        * WebCit Chat works by having transmit, receive, and refresh
         * frames.  Load the frameset.  (This isn't AJAX but the headers
         * output by begin_ajax_response() happen to be the ones we need.)
         */
        begin_ajax_response();
-       do_template("chatframeset");
+       do_template("chatframeset", NULL);
        end_ajax_response();
        return;
 }
 
 
-/*
+/**
+ * \brief display page popup
  * If there are instant messages waiting, and we notice that we haven't checked them in
  * a while, it probably means that we need to open the instant messenger window.
  */
 void page_popup(void)
 {
+       int len;
        char buf[SIZ];
 
-       /* First, do the check as part of our page load. */
+       /** JavaScript function to alert the user that popups are probably blocked */
+       wprintf("<script type=\"text/javascript\">      "
+               "function PopUpFailed() {       "
+               " alert(\"%s\");        "
+               "}      "
+               "</script>\n",
+               _("You have one or more instant messages waiting, but the Citadel Instant Messenger "
+                 "window failed to open.  This is probably because you have a popup blocker "
+                 "installed.  Please configure your popup blocker to allow popups from this site "
+                 "if you wish to receive instant messages.")
+       );
+
+       /** First, do the check as part of our page load. */
        serv_puts("NOOP");
-       serv_getln(buf, sizeof buf);
-       if (buf[3] == '*') {
+       len = serv_getln(buf, sizeof buf);
+       if ((len >= 3) && (buf[3] == '*')) {
                if ((time(NULL) - WC->last_pager_check) > 60) {
                        wprintf("<script type=\"text/javascript\">"
-                               " window.open('static/instant_messenger.html', 'CTDL_MESSENGER', "
-                               " 'width=700,height=400');"
+                               " var oWin = window.open('static/instant_messenger.html', "
+                               " 'CTDL_MESSENGER', 'width=700,height=400');    "
+                               " if (oWin==null || typeof(oWin)==\"undefined\") {      "
+                               "  PopUpFailed();       "
+                               " }     "
                                "</script>"
                        );      
                }
        }
 
-       /* Then schedule it to happen again a minute from now if the user is idle. */
-       wprintf("<script type=\"text/javascript\">      \n"
-               " function HandleSslp(sslg_xmlresponse) {       \n"
-               "  sslg_response = sslg_xmlresponse.responseText.substr(0, 1);  \n"
-               "  if (sslg_response == 'Y') {  \n"
-               "   window.open('static/instant_messenger.html', 'CTDL_MESSENGER',      \n"
-               "    'width=700,height=400');   \n"
-               "   }   \n"
-               " }     \n"
-               " function CheckPager() {       \n"
-               "  new Ajax.Request('sslg', { method: 'get', parameters: Math.random(), \n"
-               "   onSuccess: HandleSslp } );  \n"
-               " }     \n"
-               " new PeriodicalExecuter(CheckPager, 30);       \n"
-               "</script>      \n"
+       /** Then schedule it to happen again a minute from now if the user is idle. */
+       wprintf("<script type=\"text/javascript\">      "
+               " function HandleSslp(sslg_xmlresponse) {       "
+               "  sslg_response = sslg_xmlresponse.responseText.substr(0, 1);  "
+               "  if (sslg_response == 'Y') {  "
+               "   var oWin = window.open('static/instant_messenger.html', 'CTDL_MESSENGER',   "
+               "    'width=700,height=400');   "
+               "   if (oWin==null || typeof(oWin)==\"undefined\") {    "
+               "    PopUpFailed();     "
+               "   }   "
+               "  }    "
+               " }     "
+               " function CheckPager() {       "
+               "  new Ajax.Request('sslg', { method: 'get', parameters: CtdlRandomString(),    "
+               "   onSuccess: HandleSslp } );  "
+               " }     "
+               " new PeriodicalExecuter(CheckPager, 30);       "
+               "</script>      "
        );
 }
 
 
 
-/*
- * Support function for chat -- make sure the chat socket is connected
+/**
+ * \brief Support function for chat
+ * make sure the chat socket is connected
  * and in chat mode.
  */
 int setup_chat_socket(void) {
@@ -201,12 +208,12 @@ int setup_chat_socket(void) {
        if (WC->chat_sock < 0) {
 
                if (!strcasecmp(ctdlhost, "uds")) {
-                       /* unix domain socket */
+                       /** unix domain socket */
                        sprintf(buf, "%s/citadel.socket", ctdlport);
                        WC->chat_sock = uds_connectsock(buf);
                }
                else {
-                       /* tcp socket */
+                       /** tcp socket */
                        WC->chat_sock = tcp_connectsock(ctdlhost, ctdlport);
                }
 
@@ -214,20 +221,20 @@ int setup_chat_socket(void) {
                        return(errno);
                }
 
-               /* Temporarily swap the serv and chat sockets during chat talk */
+               /** Temporarily swap the serv and chat sockets during chat talk */
                i = WC->serv_sock;
                WC->serv_sock = WC->chat_sock;
                WC->chat_sock = i;
 
                serv_getln(buf, sizeof buf);
                if (buf[0] == '2') {
-                       serv_printf("USER %s", WC->wc_username);
+                       serv_printf("USER %s", ChrPtr(WC->wc_username));
                        serv_getln(buf, sizeof buf);
                        if (buf[0] == '3') {
-                               serv_printf("PASS %s", WC->wc_password);
+                               serv_printf("PASS %s", ChrPtr(WC->wc_password));
                                serv_getln(buf, sizeof buf);
                                if (buf[0] == '2') {
-                                       serv_printf("GOTO %s", WC->wc_roomname);
+                                       serv_printf("GOTO %s", ChrPtr(WC->wc_roomname));
                                        serv_getln(buf, sizeof buf);
                                        if (buf[0] == '2') {
                                                serv_puts("CHAT");
@@ -240,7 +247,7 @@ int setup_chat_socket(void) {
                        }
                }
 
-               /* Unswap the sockets. */
+               /** Unswap the sockets. */
                i = WC->serv_sock;
                WC->serv_sock = WC->chat_sock;
                WC->chat_sock = i;
@@ -253,8 +260,9 @@ int setup_chat_socket(void) {
 
 
 
-/*
- * Receiving side of the chat window.  This is implemented in a
+/**
+ * \brief Receiving side of the chat window.  
+ * This is implemented in a
  * tiny hidden IFRAME that just does JavaScript writes to
  * other frames whenever it refreshes and finds new data.
  */
@@ -270,8 +278,7 @@ void chat_recv(void) {
 
        output_headers(0, 0, 0, 0, 0, 0);
 
-       wprintf("Content-type: text/html; charset=utf-8\n");
-       wprintf("\n");
+       hprintf("Content-type: text/html; charset=utf-8\r\n");
        wprintf("<html>\n"
                "<head>\n"
                "<meta http-equiv=\"refresh\" content=\"3\" />\n"
@@ -287,7 +294,7 @@ void chat_recv(void) {
                return;
        }
 
-       /*
+       /**
         * See if there is any chat data waiting.
         */
        output_data = strdup("");
@@ -296,10 +303,10 @@ void chat_recv(void) {
                pf.fd = WC->chat_sock;
                pf.events = POLLIN;
                pf.revents = 0;
-               if (poll(&pf, 1, 1) > 0) if (pf.revents & POLLIN) {
+               if ((poll(&pf, 1, 1) > 0) && (pf.revents & POLLIN)) {
                        ++got_data;
 
-                       /* Temporarily swap the serv and chat sockets during chat talk */
+                       /** Temporarily swap the serv and chat sockets during chat talk */
                        i = WC->serv_sock;
                        WC->serv_sock = WC->chat_sock;
                        WC->chat_sock = i;
@@ -312,12 +319,12 @@ void chat_recv(void) {
                                end_chat_now = 1;
                        }
                        
-                       /* Unswap the sockets. */
+                       /** Unswap the sockets. */
                        i = WC->serv_sock;
                        WC->serv_sock = WC->chat_sock;
                        WC->chat_sock = i;
 
-                       /* Append our output data */
+                       /** Append our output data */
                        output_data = realloc(output_data, strlen(output_data) + strlen(buf) + 4);
                        strcat(output_data, buf);
                        strcat(output_data, "\n");
@@ -331,13 +338,14 @@ void chat_recv(void) {
                wprintf("<img src=\"static/blank.gif\" onLoad=\"parent.window.close();\">\n");
        }
 
-       if (strlen(output_data) > 0) {
-
-               if (output_data[strlen(output_data)-1] == '\n') {
-                       output_data[strlen(output_data)-1] = 0;
+       if (!IsEmptyStr(output_data)) {
+               int len;
+               len = strlen(output_data);
+               if (output_data[len-1] == '\n') {
+                       output_data[len-1] = 0;
                }
 
-               /* Output our fun to the other frame. */
+               /** Output our fun to the other frame. */
                wprintf("<img src=\"static/blank.gif\" WIDTH=1 HEIGHT=1\n"
                        "onLoad=\" \n"
                );
@@ -373,7 +381,7 @@ void chat_recv(void) {
                                if (strcasecmp(cl_user, WC->last_chat_user)) {
                                        wprintf("<B>");
        
-                                       if (!strcasecmp(cl_user, WC->wc_fullname)) {
+                                       if (!strcasecmp(cl_user, ChrPtr(WC->wc_fullname))) {
                                                wprintf("<FONT COLOR=&quot;#FF0000&quot;>");
                                        }
                                        else {
@@ -408,8 +416,8 @@ void chat_recv(void) {
 }
 
 
-/*
- * sending side of the chat window
+/**
+ * \brief sending side of the chat window
  */
 void chat_send(void) {
        int i;
@@ -417,28 +425,27 @@ void chat_send(void) {
        char buf[SIZ];
 
        output_headers(0, 0, 0, 0, 0, 0);
-       wprintf("Content-type: text/html; charset=utf-8\n");
-       wprintf("\n");
+       hprintf("Content-type: text/html; charset=utf-8\r\n");
        wprintf("<HTML>"
                "<BODY onLoad=\"document.chatsendform.send_this.focus();\" >"
        );
 
-       if (bstr("send_this") != NULL) {
+       if (havebstr("send_this")) {
                strcpy(send_this, bstr("send_this"));
        }
        else {
                strcpy(send_this, "");
        }
 
-       if (strlen(bstr("help_button")) > 0) {
+       if (havebstr("help_button")) {
                strcpy(send_this, "/help");
        }
 
-       if (strlen(bstr("list_button")) > 0) {
+       if (havebstr("list_button")) {
                strcpy(send_this, "/who");
        }
 
-       if (strlen(bstr("exit_button")) > 0) {
+       if (havebstr("exit_button")) {
                strcpy(send_this, "/quit");
        }
 
@@ -449,12 +456,12 @@ void chat_send(void) {
                return;
        }
 
-       /* Temporarily swap the serv and chat sockets during chat talk */
+       /** Temporarily swap the serv and chat sockets during chat talk */
        i = WC->serv_sock;
        WC->serv_sock = WC->chat_sock;
        WC->chat_sock = i;
 
-       while (strlen(send_this) > 0) {
+       while (!IsEmptyStr(send_this)) {
                if (strlen(send_this) < 67) {
                        serv_puts(send_this);
                        strcpy(send_this, "");
@@ -470,12 +477,13 @@ void chat_send(void) {
                }
        }
 
-       /* Unswap the sockets. */
+       /** Unswap the sockets. */
        i = WC->serv_sock;
        WC->serv_sock = WC->chat_sock;
        WC->chat_sock = i;
 
        wprintf("<FORM METHOD=\"POST\" action=\"chat_send\" NAME=\"chatsendform\">\n");
+       wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
        wprintf("<INPUT TYPE=\"text\" SIZE=\"80\" MAXLENGTH=\"%d\" "
                "NAME=\"send_this\">\n", SIZ-10);
        wprintf("<br />");
@@ -489,3 +497,42 @@ void chat_send(void) {
        wDumpContent(0);
 }
 
+
+void ajax_send_instant_message(void) {
+       char recp[256];
+       char buf[256];
+
+       safestrncpy(recp, bstr("recp"), sizeof recp);
+
+       serv_printf("SEXP %s|-", recp);
+       serv_getln(buf, sizeof buf);
+
+       if (buf[0] == '4') {
+               text_to_server(bstr("msg"));
+               serv_puts("000");
+       }
+
+       escputs(buf);   /* doesn't really matter what we return - the client ignores it */
+}
+
+
+void 
+InitModule_PAGING
+(void)
+{
+       WebcitAddUrlHandler(HKEY("display_page"), display_page, 0);
+       WebcitAddUrlHandler(HKEY("page_user"), page_user, 0);
+       WebcitAddUrlHandler(HKEY("chat"), do_chat, 0);
+       WebcitAddUrlHandler(HKEY("chat_recv"), chat_recv, 0);
+       WebcitAddUrlHandler(HKEY("chat_send"), chat_send, 0);
+       WebcitAddUrlHandler(HKEY("ajax_send_instant_message"), ajax_send_instant_message, AJAX);
+}
+
+
+void 
+SessionDestroyModule_CHAT
+(wcsession *sess)
+{
+       if (sess->chat_sock > 0)
+               close(sess->chat_sock);
+}