45cd3118d2d5a08f0c04355d294077de5023f48b
[citadel.git] / webcit-ng / server / user_functions.c
1 // User functions
2 //
3 // Copyright (c) 1996-2023 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
7
8 #include "webcit.h"
9
10
11 // Fetch a user photo (avatar)
12 void fetch_user_photo(struct http_transaction *h, struct ctdlsession *c, char *username) {
13         char buf[1024];
14         int content_length = 0;
15         char content_type[1024];
16         char *image = NULL;
17         int actual_length = 0;
18
19         ctdl_printf(c, "DLUI %s", username);
20         ctdl_readline(c, buf, sizeof(buf));
21         if (buf[0] == '6') {
22                 content_length = extract_int(&buf[4], 0);
23                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
24
25                 image = malloc(content_length);
26                 if (image == NULL) {
27                         do_502(h);
28                         return;
29                 }
30                 actual_length = ctdl_read_binary(c, image, content_length);
31
32                 add_response_header(h, strdup("Content-type"), strdup(content_type));
33                 h->response_code = 200;
34                 h->response_string = strdup("OK");
35                 h->response_body_length = actual_length;
36                 h->response_body = image;
37                 return;
38         }
39
40         // The user has no avatar on file.  Redirect to a generic avatar.
41         http_redirect(h, "/ctdl/s/images/generic-user-icon-32px.png");
42
43         // Sending a 404 makes the browser request the same thing over and over again.
44         // do_404(h);
45 }
46
47
48 // Fetch a user bio (profile)
49 void fetch_user_bio(struct http_transaction *h, struct ctdlsession *c, char *username) {
50         do_404(h);      // FIXME finish this
51 }
52
53
54 // Client requested an object related to a user.
55 void object_in_user(struct http_transaction *h, struct ctdlsession *c, char *requested_username) {
56         char object_name[1024];
57
58         extract_token(object_name, h->url, 4, '/', sizeof object_name);
59
60         if (!strcasecmp(object_name, "userpic")) {              // user photo (avatar)
61                 fetch_user_photo(h, c, requested_username);
62                 return;
63         }
64
65         if (!strcasecmp(object_name, "bio")) {                  // user bio (profile)
66                 fetch_user_bio(h, c, requested_username);
67                 return;
68         }
69
70         do_404(h);                                              // unknown object
71         return;
72 }
73
74
75 // Handle REST/DAV requests for the user itself (such as /ctdl/u/username
76 // or /ctdl/i/username/ but *not* specific properties of the user)
77 void the_user_itself(struct http_transaction *h, struct ctdlsession *c, char *username) {
78         do_404(h);
79 }
80
81
82 // Dispatcher for "/ctdl/u" and "/ctdl/u/" for the user list
83 void user_list(struct http_transaction *h, struct ctdlsession *c) {
84         do_404(h);
85 }
86
87
88 // Dispatcher for paths starting with /ctdl/u/
89 void ctdl_u(struct http_transaction *h, struct ctdlsession *c) {
90         char requested_username[128];
91         char buf[1024];
92
93         // All user-related functions require accessing the user in question.
94         extract_token(requested_username, h->url, 3, '/', sizeof requested_username);
95         unescape_input(requested_username);
96
97         if (IsEmptyStr(requested_username)) {                           //      /ctdl/u/
98                 user_list(h, c);
99                 return;
100         }
101
102         // At this point we have extracted the name of the user we're interested in.
103         // FIXME should we validate it?
104
105         if (num_tokens(h->url, '/') == 4) {                             //      /ctdl/u/username
106                 the_user_itself(h, c, requested_username);
107                 return;
108         }
109
110         extract_token(buf, h->url, 4, '/', sizeof buf);
111         if (num_tokens(h->url, '/') == 5) {
112                 if (IsEmptyStr(buf)) {
113                         the_user_itself(h, c, requested_username);      //      /ctdl/u/username/ (same as /ctdl/u/username)
114                 }
115                 else {
116                         object_in_user(h, c, requested_username);       //      /ctdl/u/username/object
117                 }
118                 return;
119         }
120
121         if (num_tokens(h->url, '/') == 6) {
122                 object_in_user(h, c, requested_username);               //      /ctdl/u/username/object/ or possibly /ctdl/u/username/object/component
123                 return;
124         }
125
126         // If we get to this point, the client requested an action we don't know how to perform.
127         do_404(h);
128 }