rename wprintf to wc_printf; wchar.h also has a wprintf
[citadel.git] / webcit / openid.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7
8 /*
9  * Display the OpenIDs associated with an account
10  */
11 void display_openids(void)
12 {
13         wcsession *WCC = WC;
14         char buf[1024];
15         int bg = 0;
16
17         output_headers(1, 1, 1, 0, 0, 0);
18
19         wc_printf("<div class=\"fix_scrollbar_bug\">");
20
21         svput("BOXTITLE", WCS_STRING, _("Manage Account/OpenID Associations"));
22         do_template("beginboxx", NULL);
23
24         if (WCC->serv_info->serv_supports_openid) {
25
26                 wc_printf("<table class=\"altern\">");
27         
28                 serv_puts("OIDL");
29                 serv_getln(buf, sizeof buf);
30                 if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
31                         bg = 1 - bg;
32                         wc_printf("<tr class=\"%s\">", (bg ? "even" : "odd"));
33                         wc_printf("<td><img src=\"static/openid-small.gif\"></td><td>");
34                         escputs(buf);
35                         wc_printf("</td><td>");
36                         wc_printf("<a href=\"openid_detach?id_to_detach=");
37                         urlescputs(buf);
38                         wc_printf("\" onClick=\"return confirm('%s');\">",
39                                 _("Do you really want to delete this OpenID?"));
40                         wc_printf("%s</a>", _("(delete)"));
41                         wc_printf("</td></tr>\n");
42                 }
43         
44                 wc_printf("</table><br />\n");
45         
46                 wc_printf("<form method=\"POST\" action=\"openid_attach\">\n");
47                 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WCC->nonce);
48                 wc_printf(_("Add an OpenID: "));
49                 wc_printf("<input type=\"text\" name=\"openid_url\" class=\"openid_urlarea\" size=\"40\">\n");
50                 wc_printf("<input type=\"submit\" name=\"attach_button\" value=\"%s\">"
51                         "</form></center>\n", _("Attach"));
52         }
53
54         else {
55                 wc_printf(_("%s does not permit authentication via OpenID."), ChrPtr(WCC->serv_info->serv_humannode));
56         }
57
58         do_template("endbox", NULL);
59         wc_printf("</div>");
60         wDumpContent(2);
61 }
62
63
64 /*
65  * Attempt to attach an OpenID to an existing, logged-in account
66  */
67 void openid_attach(void) {
68         char buf[4096];
69
70         if (havebstr("attach_button")) {
71                 wcsession *WCC = WC;
72
73                 lprintf(CTDL_DEBUG, "Attempting to attach %s\n", bstr("openid_url"));
74
75                 snprintf(buf, sizeof buf,
76                          "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
77                          bstr("openid_url"),
78                          (is_https ? "https" : "http"), ChrPtr(WCC->Hdr->HR.http_host),
79                          (is_https ? "https" : "http"), ChrPtr(WCC->Hdr->HR.http_host)
80                 );
81
82                 serv_puts(buf);
83                 serv_getln(buf, sizeof buf);
84                 if (buf[0] == '2') {
85                         lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
86                         http_redirect(&buf[4]);
87                         return;
88                 }
89                 else {
90                         lprintf(CTDL_DEBUG, "OpenID attach failed: %s\n", &buf[4]);
91                 }
92         }
93
94         /* If we get to this point then something failed. */
95         display_openids();
96 }
97
98
99 /*
100  * Detach an OpenID from the currently logged-in account
101  */
102 void openid_detach(void) {
103         char buf[1024];
104
105         if (havebstr("id_to_detach")) {
106                 serv_printf("OIDD %s", bstr("id_to_detach"));
107                 serv_getln(buf, sizeof buf);
108                 if (buf[0] != '2') {
109                         strcpy(WC->ImportantMessage, &buf[4]);
110                 }
111         }
112
113         display_openids();
114 }
115
116 void 
117 InitModule_OPENID
118 (void)
119 {
120         WebcitAddUrlHandler(HKEY("display_openids"), "", 0, display_openids, 0);
121         WebcitAddUrlHandler(HKEY("openid_attach"), "", 0, openid_attach, 0);
122         WebcitAddUrlHandler(HKEY("openid_detach"), "", 0, openid_detach, 0);
123 }