1c7e72db1c3169e5614d9416968d1d10b95d962a
[citadel.git] / webcit / cookie_conversion.c
1 /*
2  * Copyright (c) 1996-2012 by the citadel.org team
3  *
4  * This program is open source software.  You can redistribute it and/or
5  * modify it under the terms of the GNU General Public License, version 3.
6  * 
7  * 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * 
15  * 
16  * 
17  */
18
19 #include "webcit.h"
20
21 /*
22  * String to unset the cookie.
23  * Any date "in the past" will work, so I chose my birthday, right down to
24  * the exact minute.  :)
25  */
26 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
27 typedef unsigned char byte;           /* Byte type used by cookie_to_stuff() */
28 extern const char *get_selected_language(void);
29
30 /*
31  * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
32  */
33 void stuff_to_cookie(int unset_cookies)
34 {
35         wcsession *WCC = WC;
36         char buf[SIZ];
37
38         if (unset_cookies) {
39                 hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
40         }
41         else
42         {
43                 StrBufAppendPrintf(WCC->HBuf, "Set-cookie: webcit=");
44                 snprintf(buf, sizeof(buf), "%d", WCC->wc_session);
45                 StrBufHexescAppend(WCC->HBuf, NULL, buf);
46                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
47                 StrBufHexescAppend(WCC->HBuf, WCC->wc_username, NULL);
48                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
49                 StrBufHexescAppend(WCC->HBuf, WCC->wc_password, NULL);
50                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
51                 StrBufHexescAppend(WCC->HBuf, WCC->CurRoom.name, NULL);
52                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
53                 StrBufHexescAppend(WCC->HBuf, NULL, get_selected_language());
54                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
55
56                 if (server_cookie != NULL) {
57                         StrBufAppendPrintf(WCC->HBuf, 
58                                            ";path=/ \r\n%s\r\n", 
59                                            server_cookie);
60                 }
61                 else {
62                         StrBufAppendBufPlain(WCC->HBuf,
63                                              HKEY("; path=/\r\n"), 0);
64                 }
65         }
66 }
67
68 /*
69  * Extract all that fun stuff out of the cookie.
70  */
71 void cookie_to_stuff(StrBuf *cookie,
72                         int *session,
73                         StrBuf *user,
74                         StrBuf *pass,
75                         StrBuf *room,
76                         StrBuf *language)
77 {
78         if (session != NULL) {
79                 *session = StrBufExtract_int(cookie, 0, '|');
80         }
81         if (user != NULL) {
82                 StrBufExtract_token(user, cookie, 1, '|');
83         }
84         if (pass != NULL) {
85                 StrBufExtract_token(pass, cookie, 2, '|');
86         }
87         if (room != NULL) {
88                 StrBufExtract_token(room, cookie, 3, '|');
89         }
90         if (language != NULL) {
91                 StrBufExtract_token(language, cookie, 4, '|');
92         }
93 }