* create function to parse the output of goto into our folder structure
[citadel.git] / webcit / cookie_conversion.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6
7 /*
8  * String to unset the cookie.
9  * Any date "in the past" will work, so I chose my birthday, right down to
10  * the exact minute.  :)
11  */
12 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
13 typedef unsigned char byte;           /* Byte type used by cookie_to_stuff() */
14
15 /*
16  * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
17  */
18 void stuff_to_cookie(int unset_cookies)
19 {
20         wcsession *WCC = WC;
21         char buf[SIZ];
22
23         if (unset_cookies) {
24                 hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
25         }
26         else
27         {
28                 StrBufAppendPrintf(WCC->HBuf, "Set-cookie: webcit=");
29                 snprintf(buf, sizeof(buf), "%d", WCC->wc_session);
30                 StrBufHexescAppend(WCC->HBuf, NULL, buf);
31                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
32                 StrBufHexescAppend(WCC->HBuf, WCC->wc_username, NULL);
33                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
34                 StrBufHexescAppend(WCC->HBuf, WCC->wc_password, NULL);
35                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
36                 StrBufHexescAppend(WCC->HBuf, WCC->CurRoom.name, NULL);
37                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
38                 StrBufHexescAppend(WCC->HBuf, NULL, get_selected_language());
39                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
40
41                 if (server_cookie != NULL) {
42                         StrBufAppendPrintf(WCC->HBuf, 
43                                            ";path=/ \r\n%s\r\n", 
44                                            server_cookie);
45                 }
46                 else {
47                         StrBufAppendBufPlain(WCC->HBuf,
48                                              HKEY("; path=/\r\n"), 0);
49                 }
50         }
51 }
52
53 /*
54  * Extract all that fun stuff out of the cookie.
55  */
56 void cookie_to_stuff(StrBuf *cookie,
57                         int *session,
58                         StrBuf *user,
59                         StrBuf *pass,
60                         StrBuf *room,
61                         StrBuf *language)
62 {
63         if (session != NULL) {
64                 *session = StrBufExtract_int(cookie, 0, '|');
65         }
66         if (user != NULL) {
67                 StrBufExtract_token(user, cookie, 1, '|');
68         }
69         if (pass != NULL) {
70                 StrBufExtract_token(pass, cookie, 2, '|');
71         }
72         if (room != NULL) {
73                 StrBufExtract_token(room, cookie, 3, '|');
74         }
75         if (language != NULL) {
76                 StrBufExtract_token(language, cookie, 4, '|');
77         }
78 }