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