Mailing list header changes (fuck you Google)
[citadel.git] / webcit-ng / user_functions.c
1 /*
2  * User functions
3  *
4  * Copyright (c) 1996-2018 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
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 #include "webcit.h"
16
17
18 /*
19  * Fetch a user photo (avatar)
20  */
21 void fetch_user_photo(struct http_transaction *h, struct ctdlsession *c, char *username)
22 {
23         char buf[1024];
24         int content_length = 0;
25         char content_type[1024];
26         char *image = NULL;
27         int actual_length = 0;
28
29         ctdl_printf(c, "DLUI %s", username);
30         ctdl_readline(c, buf, sizeof(buf));
31         if (buf[0] == '6') {
32                 content_length = extract_int(&buf[4], 0);
33                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
34
35                 image = malloc(content_length);
36                 if (image == NULL) {
37                         do_502(h);
38                         return;
39                 }
40                 actual_length = ctdl_read_binary(c, image, content_length);
41
42                 add_response_header(h, strdup("Content-type"), strdup(content_type));
43                 h->response_code = 200;
44                 h->response_string = strdup("OK");
45                 h->response_body_length = actual_length;
46                 h->response_body = image;
47                 return;
48         }
49
50         do_404(h);
51 }
52
53
54 /*
55  * Fetch a user bio (profile)
56  */
57 void fetch_user_bio(struct http_transaction *h, struct ctdlsession *c, char *username)
58 {
59         do_404(h);      // FIXME finish this
60 }
61
62
63 /*
64  * Client requested an object related to a user.
65  */
66 void object_in_user(struct http_transaction *h, struct ctdlsession *c, char *requested_username)
67 {
68         char object_name[1024];
69
70         extract_token(object_name, h->uri, 4, '/', sizeof object_name);
71
72         if (!strcasecmp(object_name, "userpic")) {              // user photo (avatar)
73                 fetch_user_photo(h, c, requested_username);
74                 return;
75         }
76
77         if (!strcasecmp(object_name, "bio")) {                  // user bio (profile)
78                 fetch_user_bio(h, c, requested_username);
79                 return;
80         }
81
82         do_404(h);                                      // unknown object
83         return;
84 }
85
86
87 /*
88  * Handle REST/DAV requests for the user itself (such as /ctdl/u/username
89  * or /ctdl/i/username/ but *not* specific properties of the user)
90  */
91 void the_user_itself(struct http_transaction *h, struct ctdlsession *c, char *username)
92 {
93         do_404(h);
94 }
95
96
97 /*
98  * Dispatcher for "/ctdl/u" and "/ctdl/u/" for the user list
99  */
100 void user_list(struct http_transaction *h, struct ctdlsession *c)
101 {
102         do_404(h);
103 }
104
105
106 /*
107  * Dispatcher for paths starting with /ctdl/u/
108  */
109 void ctdl_u(struct http_transaction *h, struct ctdlsession *c)
110 {
111         char requested_username[128];
112         char buf[1024];
113
114         // All user-related functions require accessing the user in question.
115         extract_token(requested_username, h->uri, 3, '/', sizeof requested_username);
116         unescape_input(requested_username);
117
118         if (IsEmptyStr(requested_username)) {                           //      /ctdl/u/
119                 user_list(h, c);
120                 return;
121         }
122
123         // At this point we have extracted the name of the user we're interested in.
124         // FIXME should we validate it?
125
126         if (num_tokens(h->uri, '/') == 4) {                             //      /ctdl/u/username
127                 the_user_itself(h, c, requested_username);
128                 return;
129         }
130
131         extract_token(buf, h->uri, 4, '/', sizeof buf);
132         if (num_tokens(h->uri, '/') == 5) {
133                 if (IsEmptyStr(buf)) {
134                         the_user_itself(h, c, requested_username);      //      /ctdl/u/username/       ( same as /ctdl/u/username )
135                 } else {
136                         object_in_user(h, c, requested_username);       //      /ctdl/u/username/object
137                 }
138                 return;
139         }
140
141         if (num_tokens(h->uri, '/') == 6) {
142                 object_in_user(h, c, requested_username);               //      /ctdl/u/username/object/ or possibly /ctdl/u/username/object/component
143                 return;
144         }
145
146         // If we get to this point, the client requested an action we don't know how to perform.
147         do_404(h);
148 }