* add more module handlers:
[citadel.git] / webcit / auth.c
index 77dc5c4752bc3224074d52e72356c5ec09a01574..7cb7bc83cf7626322c8fd0ecccaa20a8b2e6da03 100644 (file)
@@ -51,43 +51,10 @@ void display_login(void)
  */
 void display_openid_login(char *mesg)
 {
-       char buf[SIZ];
-
-       output_headers(1, 1, 2, 0, 0, 0);
-       wprintf("<div id=\"login_screen\">\n");
-
-       if ((mesg != NULL) && (!IsEmptyStr(mesg))) {
-               stresc(buf, SIZ,  mesg, 0, 0);
-               svprintf(HKEY("MESG"), WCS_STRING, "%s", buf);
-       }
-
-       svprintf(HKEY("LOGIN_INSTRUCTIONS"), WCS_STRING,
-               _("<ul>"
-               "<li>Enter your OpenID URL and click &quot;Login&quot;."
-               "<li><a href=\"http://www.citadel.org/doku.php/documentation:openid\">"
-               "Click here to learn what OpenID is and how Citadel is using it.</a>"
-               "<li>Please log off properly when finished. "
-               "<li>You must use a browser that supports <i>frames</i> and "
-               "<i>cookies</i>. "
-               "<li>Also keep in mind that if your browser is "
-               "configured to block pop-up windows, you will not be able "
-               "to receive any instant messages.<br />"
-               "</ul>")
-       );
-
-       svput("HELLO", WCS_SERVCMD, "MESG hello");
-       
-       svprintf(HKEY("OFFER_CONVENTIONAL_LOGIN"), WCS_STRING,
-               "<div align=center>"
-               "<a href=\"display_login\">"
-               "%s</a>"
-               "</div>"
-               ,
-               "Log in using a user name and password"
-       );
-
-       do_template("openid_login", NULL);
-       wDumpContent(2);
+  begin_burst();
+  output_headers(1, 0, 0, 0, 1, 0);
+  do_template("openid_login", NULL);
+  end_burst();
 }
 
 
@@ -124,7 +91,7 @@ void display_openid_name_request(const StrBuf *claimed_id, const StrBuf *usernam
        svput("EXIT_BUTTON", WCS_STRING, _("Exit"));
 
        svprintf(HKEY("BOXTITLE"), WCS_STRING, _("%s - powered by <a href=\"http://www.citadel.org\">Citadel</a>"),
-                ChrPtr(serv_info.serv_humannode));
+                ChrPtr(WC->serv_info->serv_humannode));
 
        do_template("openid_manual_create", NULL);
        wDumpContent(2);
@@ -147,10 +114,11 @@ void display_openid_name_request(const StrBuf *claimed_id, const StrBuf *usernam
 void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_response)
 {
        wcsession *WCC = WC;
-       char buf[SIZ];
+       StrBuf *Buf;
        StrBuf *FloorDiv;
+       StrBuf *Language = NULL;
 
-       WC->logged_in = 1;
+       WCC->logged_in = 1;
 
        if (WCC->wc_fullname == NULL)
                WCC->wc_fullname = NewStrBufPlain(NULL, StrLength(serv_response));
@@ -178,19 +146,32 @@ void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_respo
 
        load_preferences();
 
+       Buf = NewStrBuf();
        serv_puts("CHEK");
-       serv_getln(buf, sizeof buf);
-       if (buf[0] == '2') {
-               WC->new_mail = extract_int(&buf[4], 0);
-               WC->need_regi = extract_int(&buf[4], 1);
-               WC->need_vali = extract_int(&buf[4], 2);
-               extract_token(WC->cs_inet_email, &buf[4], 3, '|', sizeof WC->cs_inet_email);
+       StrBuf_ServGetln(Buf);
+       if (GetServerStatus(Buf, NULL) == 2) {
+               const char *pch;
+
+               pch = ChrPtr(Buf) + 4;
+               WCC->new_mail  = StrBufExtractNext_long(Buf, &pch, '|');
+               WCC->need_regi = StrBufExtractNext_long(Buf, &pch, '|');
+               WCC->need_vali = StrBufExtractNext_long(Buf, &pch, '|');
+               if (WCC->cs_inet_email == NULL)
+                       WCC->cs_inet_email  = NewStrBuf();
+               StrBufExtract_NextToken(WCC->cs_inet_email, Buf, &pch, '|');
+       }
+       if (havebstr("language"))
+               set_preference("language", NewStrBufDup(SBSTR("language")), 1);
+       else {
+               get_preference("language", &Language);
+               if (Language != NULL) {
+                       set_selected_language(ChrPtr(Language));
+                       go_selected_language();         /* set locale */
+               }
        }
-
-       get_pref_long("current_iconbar", &WC->current_iconbar, current_iconbar_menu);
-
        get_preference("floordiv_expanded", &FloorDiv);
-       WC->floordiv_expanded = FloorDiv;
+       WCC->floordiv_expanded = FloorDiv;
+       FreeStrBuf(&Buf);
 }
 
 
@@ -266,7 +247,6 @@ void do_login(void)
                }
        }
        if (WCC->logged_in) {
-               set_preference("language", NewStrBufPlain(bstr("language"), -1), 1);
                if (WCC->need_regi) {
                        display_reg(1);
                } else if (WCC->need_vali) {
@@ -334,6 +314,7 @@ void openid_manual_create(void)
  */
 void do_openid_login(void)
 {
+       wcsession *WCC = WC;
        char buf[4096];
 
        if (havebstr("language")) {
@@ -349,8 +330,8 @@ void do_openid_login(void)
                snprintf(buf, sizeof buf,
                        "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
                        bstr("openid_url"),
-                       (is_https ? "https" : "http"), WC->http_host,
-                       (is_https ? "https" : "http"), WC->http_host
+                        (is_https ? "https" : "http"), ChrPtr(WCC->http_host),
+                        (is_https ? "https" : "http"), ChrPtr(WCC->http_host)
                );
 
                serv_puts(buf);
@@ -409,9 +390,7 @@ void finalize_openid_login(void)
                                serv_puts("000");
 
                                linecount = 0;
-                               while (StrBuf_ServGetln(Buf), 
-                                      (StrLength(Buf)==3) && 
-                                      !strcmp(ChrPtr(Buf), "000")) 
+                               while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) 
                                {
                                        if (linecount == 0) result = NewStrBufDup(Buf);
                                        if (!strcasecmp(ChrPtr(result), "authenticate")) {
@@ -554,10 +533,6 @@ void do_welcome(void)
  */
 void end_webcit_session(void) {
        
-       if (WC->logged_in) {
-               set_pref_long("current_iconbar", WC->current_iconbar, 0);
-       }
-
        serv_puts("QUIT");
        WC->killthis = 1;
        /* close() of citadel socket will be done by do_housekeeping() */
@@ -568,13 +543,18 @@ void end_webcit_session(void) {
  */
 void do_logout(void)
 {
+       wcsession *WCC = WC;
        char buf[SIZ];
 
-       FlushStrBuf(WC->wc_username);
-       FlushStrBuf(WC->wc_password);
-       FlushStrBuf(WC->wc_roomname);
-       FlushStrBuf(WC->wc_fullname);
+       FlushStrBuf(WCC->wc_username);
+       FlushStrBuf(WCC->wc_password);
+       FlushStrBuf(WCC->wc_roomname);
+       FlushStrBuf(WCC->wc_fullname);
 
+       /* FIXME: this is to suppress the iconbar displaying, because we aren't
+          actually logged out yet */
+       WCC->logged_in = 0;
+       
        /** Calling output_headers() this way causes the cookies to be un-set */
        output_headers(1, 1, 0, 1, 0, 0);
 
@@ -586,7 +566,7 @@ void do_logout(void)
        serv_puts("MESG goodbye");
        serv_getln(buf, sizeof buf);
 
-       if (WC->serv_sock >= 0) {
+       if (WCC->serv_sock >= 0) {
                if (buf[0] == '1') {
                        fmout("CENTER");
                } else {
@@ -641,7 +621,7 @@ void validate(void)
                        serv_printf("VALI %s|%s", buf, bstr("axlevel"));
                        serv_getln(buf, sizeof buf);
                        if (buf[0] != '2') {
-                               wprintf("<b>%s</b><br />\n", &buf[4]);
+                               wprintf("<b>%s</b><br>\n", &buf[4]);
                        }
                }
        }
@@ -652,19 +632,19 @@ void validate(void)
        if (buf[0] == '2') {
                wprintf("<b>");
                wprintf(_("No users require validation at this time."));
-               wprintf("</b><br />\n");
+               wprintf("</b><br>\n");
                wDumpContent(1);
                return;
        }
        if (buf[0] != '3') {
-               wprintf("<b>%s</b><br />\n", &buf[4]);
+               wprintf("<b>%s</b><br>\n", &buf[4]);
                wDumpContent(1);
                return;
        }
 
        wprintf("<div class=\"fix_scrollbar_bug\">"
                "<table class=\"auth_validate\"><tr><td>\n");
-       wprintf("<center>");
+       wprintf("<div id=\"validate\">");
 
        safestrncpy(user, &buf[4], sizeof user);
        serv_printf("GREG %s", user);
@@ -675,7 +655,7 @@ void validate(void)
                        serv_getln(buf, sizeof buf);
                        ++a;
                        if (a == 1)
-                               wprintf("#%s<br /><H1>%s</H1>",
+                               wprintf("#%s<br><H1>%s</H1>",
                                        buf, &cmd[4]);
                        if (a == 2) {
                                char *pch;
@@ -712,20 +692,20 @@ void validate(void)
                                        pch = _("strong");
                                }
 
-                               wprintf("PW: %s<br />\n", pch);
+                               wprintf("PW: %s<br>\n", pch);
                        }
                        if (a == 3)
-                               wprintf("%s<br />\n", buf);
+                               wprintf("%s<br>\n", buf);
                        if (a == 4)
-                               wprintf("%s<br />\n", buf);
+                               wprintf("%s<br>\n", buf);
                        if (a == 5)
                                wprintf("%s, ", buf);
                        if (a == 6)
                                wprintf("%s ", buf);
                        if (a == 7)
-                               wprintf("%s<br />\n", buf);
+                               wprintf("%s<br>\n", buf);
                        if (a == 8)
-                               wprintf("%s<br />\n", buf);
+                               wprintf("%s<br>\n", buf);
                        if (a == 9)
                                wprintf(_("Current access level: %d (%s)\n"),
                                        atoi(buf), axdefs[atoi(buf)]);
@@ -745,7 +725,7 @@ void validate(void)
        }
        wprintf("<br />\n");
 
-       wprintf("</CENTER>\n");
+       wprintf("</div>\n");
        wprintf("</td></tr></table></div>\n");
        wDumpContent(1);
 }
@@ -760,15 +740,21 @@ void validate(void)
  */
 void display_reg(int during_login)
 {
+       StrBuf *Buf;
+       message_summary *VCMsg;
+       wc_mime_attachment *VCAtt;
        long vcard_msgnum;
 
-       if (goto_config_room() != 0) {
+       Buf = NewStrBuf();
+       if (goto_config_room(Buf) != 0) {
                if (during_login) do_welcome();
                else display_main_menu();
+               FreeStrBuf(&Buf);
                return;
        }
 
-       vcard_msgnum = locate_user_vcard_in_this_room();
+       FreeStrBuf(&Buf);
+       vcard_msgnum = locate_user_vcard_in_this_room(&VCMsg, &VCAtt);
        if (vcard_msgnum < 0L) {
                if (during_login) do_welcome();
                else display_main_menu();
@@ -776,10 +762,10 @@ void display_reg(int during_login)
        }
 
        if (during_login) {
-               do_edit_vcard(vcard_msgnum, "1", "do_welcome", USERCONFIGROOM);
+               do_edit_vcard(vcard_msgnum, "1", VCMsg, VCAtt, "do_welcome", USERCONFIGROOM);
        }
        else {
-               do_edit_vcard(vcard_msgnum, "1", "display_main_menu", USERCONFIGROOM);
+               do_edit_vcard(vcard_msgnum, "1", VCMsg, VCAtt, "display_main_menu", USERCONFIGROOM);
        }
 
 }
@@ -799,7 +785,7 @@ void display_changepw(void)
 
        Buf = NewStrBufPlain(_("Change your password"), -1);
        memset(&SubTP, 0, sizeof(WCTemplputParams));
-       SubTP.ContextType = CTX_STRBUF;
+       SubTP.Filter.ContextType = CTX_STRBUF;
        SubTP.Context = Buf;
        DoTemplate(HKEY("beginbox"), NULL, &SubTP);
 
@@ -904,6 +890,9 @@ int ConditionalRoomAide(StrBuf *Target, WCTemplputParams *TP)
        return (WC->is_room_aide == 0);
 }
 
+int ConditionalIsLoggedIn(StrBuf *Target, WCTemplputParams *TP) {
+  return (WC->logged_in == 0);
+}
 int ConditionalRoomAcessDelete(StrBuf *Target, WCTemplputParams *TP)
 {
        wcsession *WCC = WC;
@@ -936,6 +925,21 @@ InitModule_AUTH
        RegisterConditional(HKEY("COND:AIDE"), 2, ConditionalAide, CTX_NONE);
        RegisterConditional(HKEY("COND:ROOMAIDE"), 2, ConditionalRoomAide, CTX_NONE);
        RegisterConditional(HKEY("COND:ACCESS:DELETE"), 2, ConditionalRoomAcessDelete, CTX_NONE);
+       RegisterConditional(HKEY("COND:LOGGEDIN"), 2, ConditionalIsLoggedIn, CTX_NONE);
 
        return ;
 }
+
+
+void 
+SessionDestroyModule_AUTH
+(wcsession *sess)
+{
+       FreeStrBuf(&sess->wc_username);
+       FreeStrBuf(&sess->wc_fullname);
+       FreeStrBuf(&sess->wc_password);
+       FreeStrBuf(&sess->wc_roomname);
+       FreeStrBuf(&sess->httpauth_user);
+       FreeStrBuf(&sess->httpauth_pass);
+       FreeStrBuf(&sess->cs_inet_email);
+}