Remove $Id$ tags from most of webcit
[citadel.git] / webcit / cookie_conversion.c
1 /*
2  * Copyright (c) 1996-2010 by the citadel.org team
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
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  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
29 /*
30  * Pack all session info into one easy-to-digest cookie. Healthy and delicious!
31  */
32 void stuff_to_cookie(int unset_cookies)
33 {
34         wcsession *WCC = WC;
35         char buf[SIZ];
36
37         if (unset_cookies) {
38                 hprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
39         }
40         else
41         {
42                 StrBufAppendPrintf(WCC->HBuf, "Set-cookie: webcit=");
43                 snprintf(buf, sizeof(buf), "%d", WCC->wc_session);
44                 StrBufHexescAppend(WCC->HBuf, NULL, buf);
45                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
46                 StrBufHexescAppend(WCC->HBuf, WCC->wc_username, NULL);
47                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
48                 StrBufHexescAppend(WCC->HBuf, WCC->wc_password, NULL);
49                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
50                 StrBufHexescAppend(WCC->HBuf, WCC->CurRoom.name, NULL);
51                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
52                 StrBufHexescAppend(WCC->HBuf, NULL, get_selected_language());
53                 StrBufHexescAppend(WCC->HBuf, NULL, "|");
54
55                 if (server_cookie != NULL) {
56                         StrBufAppendPrintf(WCC->HBuf, 
57                                            ";path=/ \r\n%s\r\n", 
58                                            server_cookie);
59                 }
60                 else {
61                         StrBufAppendBufPlain(WCC->HBuf,
62                                              HKEY("; path=/\r\n"), 0);
63                 }
64         }
65 }
66
67 /*
68  * Extract all that fun stuff out of the cookie.
69  */
70 void cookie_to_stuff(StrBuf *cookie,
71                         int *session,
72                         StrBuf *user,
73                         StrBuf *pass,
74                         StrBuf *room,
75                         StrBuf *language)
76 {
77         if (session != NULL) {
78                 *session = StrBufExtract_int(cookie, 0, '|');
79         }
80         if (user != NULL) {
81                 StrBufExtract_token(user, cookie, 1, '|');
82         }
83         if (pass != NULL) {
84                 StrBufExtract_token(pass, cookie, 2, '|');
85         }
86         if (room != NULL) {
87                 StrBufExtract_token(room, cookie, 3, '|');
88         }
89         if (language != NULL) {
90                 StrBufExtract_token(language, cookie, 4, '|');
91         }
92 }