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