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