]> code.citadel.org Git - citadel.git/blob - citadel/server/modules/image/serv_image.c
serv_image.c: style update
[citadel.git] / citadel / server / modules / image / serv_image.c
1 // Copyright (c) 1987-2022 by the citadel.org team
2 //
3 // This program is open source software.  Use, duplication, or disclosure
4 // is subject to the terms of the GNU General Public License, version 3.
5
6 #include "../../ctdl_module.h"
7 #include "../../config.h"
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <dirent.h>
11
12
13 // DownLoad Room Image (see its icon or whatever)
14 // If this command succeeds, it follows the same protocol as the DLAT command.
15 void cmd_dlri(char *cmdbuf) {
16         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
17         if (CC->room.msgnum_pic < 1) {
18                 cprintf("%d No image found.\n", ERROR + FILE_NOT_FOUND);
19                 return;
20         }
21
22         struct CtdlMessage *msg = CtdlFetchMessage(CC->room.msgnum_pic, 1);
23         if (msg != NULL) {
24                 // The call to CtdlOutputPreLoadedMsg() with MT_SPEW_SECTION will cause the DLRI command
25                 // to have the same output format as the DLAT command, because it calls the same code.
26                 // For example: 600 402132|-1||image/gif|
27                 safestrncpy(CC->download_desired_section, "1", sizeof CC->download_desired_section);
28                 CtdlOutputPreLoadedMsg(msg, MT_SPEW_SECTION, HEADERS_NONE, 1, 0, 0);
29                 CM_Free(msg);
30         }
31         else {
32                 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
33                 return;
34         }
35 }
36
37
38 // UpLoad Room Image (avatar or photo or whatever)
39 void cmd_ulri(char *cmdbuf) {
40         long data_length;
41         char mimetype[SIZ];
42
43         if (CtdlAccessCheck(ac_room_aide)) return;
44
45         data_length = extract_long(cmdbuf, 0);
46         extract_token(mimetype, cmdbuf, 1, '|', sizeof mimetype);
47
48         if (data_length < 20) {
49                 cprintf("%d That's an awfully small file.  Try again.\n", ERROR + ILLEGAL_VALUE);
50                 return;
51         }
52
53         if (strncasecmp(mimetype, "image/", 6)) {
54                 cprintf("%d Only image files are permitted.\n", ERROR + ILLEGAL_VALUE);
55                 return;
56         }
57
58         char *unencoded_data = malloc(data_length + 1);
59         if (!unencoded_data) {
60                 cprintf("%d Could not allocate %ld bytes of memory\n", ERROR + INTERNAL_ERROR , data_length);
61                 return;
62         }
63
64         cprintf("%d %ld\n", SEND_BINARY, data_length);
65         client_read(unencoded_data, data_length);
66
67         // We've got the data read from the client, now save it.
68         char *encoded_data = malloc((data_length * 2) + 100);
69         if (encoded_data) {
70                 sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", mimetype);
71                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS);
72                 long new_msgnum = quickie_message("Citadel", NULL, NULL, SYSCONFIGROOM, encoded_data, FMT_RFC822, "Image uploaded by admin user");
73
74                 if (CtdlGetRoomLock(&CC->room, CC->room.QRname) == 0) {
75                         long old_msgnum = CC->room.msgnum_pic;
76                         syslog(LOG_DEBUG, "Message %ld is now the photo for %s", new_msgnum, CC->room.QRname);
77                         CC->room.msgnum_pic = new_msgnum;
78                         CtdlPutRoomLock(&CC->room);
79                         if (old_msgnum > 0) {
80                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, SYSCONFIGROOM);
81                                 CtdlDeleteMessages(SYSCONFIGROOM, &old_msgnum, 1, "");
82                         }
83                 }
84                 free(encoded_data);
85         }
86
87         free(unencoded_data);
88 }
89
90
91 // DownLoad User Image (see their avatar or photo or whatever)
92 // If this command succeeds, it follows the same protocol as the DLAT command.
93 void cmd_dlui(char *cmdbuf) {
94         struct ctdluser ruser;
95         char buf[SIZ];
96
97         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
98         extract_token(buf, cmdbuf, 0, '|', sizeof buf);
99         if (CtdlGetUser(&ruser, buf) != 0) {
100                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
101                 return;
102         }
103         if (ruser.msgnum_pic < 1) {
104                 cprintf("%d No image found.\n", ERROR + FILE_NOT_FOUND);
105                 return;
106         }
107
108         struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_pic, 1);
109         if (msg != NULL) {
110                 // The call to CtdlOutputPreLoadedMsg() with MT_SPEW_SECTION will cause the DLUI command
111                 // to have the same output format as the DLAT command, because it calls the same code.
112                 // For example: 600 402132|-1||image/gif|
113                 safestrncpy(CC->download_desired_section, "1", sizeof CC->download_desired_section);
114                 CtdlOutputPreLoadedMsg(msg, MT_SPEW_SECTION, HEADERS_NONE, 1, 0, 0);
115                 CM_Free(msg);
116         }
117         else {
118                 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
119                 return;
120         }
121 }
122
123
124 // UpLoad User Image (avatar or photo or whatever)
125 void cmd_ului(char *cmdbuf) {
126         long data_length;
127         char mimetype[SIZ];
128         char username[USERNAME_SIZE];
129         char userconfigroomname[ROOMNAMELEN];
130
131         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
132
133         if (num_parms(cmdbuf) < 2) {
134                 cprintf("%d Usage error\n", ERROR + ILLEGAL_VALUE);
135                 return;
136         }
137
138         data_length = extract_long(cmdbuf, 0);
139         extract_token(mimetype, cmdbuf, 1, '|', sizeof mimetype);
140         extract_token(username, cmdbuf, 2, '|', sizeof username);
141
142         if (data_length < 20) {
143                 cprintf("%d That's an awfully small file.  Try again.\n", ERROR + ILLEGAL_VALUE);
144                 return;
145         }
146
147         if (strncasecmp(mimetype, "image/", 6)) {
148                 cprintf("%d Only image files are permitted.\n", ERROR + ILLEGAL_VALUE);
149                 return;
150         }
151
152         if (IsEmptyStr(username)) {
153                 safestrncpy(username, CC->curr_user, sizeof username);
154         }
155
156         // Normal users can only change their own photo
157         if ( (strcasecmp(username, CC->curr_user)) && (CC->user.axlevel < AxAideU) && (!CC->internal_pgm) ) {
158                 cprintf("%d Higher access required to change another user's photo.\n", ERROR + HIGHER_ACCESS_REQUIRED);
159         }
160
161         // Check to make sure the user exists
162         struct ctdluser usbuf;
163         if (CtdlGetUser(&usbuf, username) != 0) {               // check for existing user, don't lock it yet
164                 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER , username);
165                 return;
166         }
167         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
168
169         char *unencoded_data = malloc(data_length + 1);
170         if (!unencoded_data) {
171                 cprintf("%d Could not allocate %ld bytes of memory\n", ERROR + INTERNAL_ERROR , data_length);
172                 return;
173         }
174
175         cprintf("%d %ld\n", SEND_BINARY, data_length);
176         client_read(unencoded_data, data_length);
177
178         // We've got the data read from the client, now save it.
179         char *encoded_data = malloc((data_length * 2) + 100);
180         if (encoded_data) {
181                 sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", mimetype);
182                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS);
183                 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo uploaded by user");
184
185                 if (CtdlGetUserLock(&usbuf, username) == 0) {   // lock it this time
186                         long old_msgnum = usbuf.msgnum_pic;
187                         syslog(LOG_DEBUG, "Message %ld is now the photo for %s", new_msgnum, username);
188                         usbuf.msgnum_pic = new_msgnum;
189                         CtdlPutUserLock(&usbuf);
190                         if (old_msgnum > 0) {
191                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
192                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
193                         }
194                 }
195
196                 free(encoded_data);
197         }
198
199         free(unencoded_data);
200 }
201
202
203 // Import function called by import_old_userpic_files() for a single user
204 void import_one_userpic_file(char *username, long usernum, char *path) {
205         syslog(LOG_DEBUG, "Import legacy userpic for %s, usernum=%ld, filename=%s", username, usernum, path);
206
207         FILE *fp = fopen(path, "r");
208         if (!fp) return;
209
210         fseek(fp, 0, SEEK_END);
211         long data_length = ftell(fp);
212
213         if (data_length >= 1) {
214                 rewind(fp);
215                 char *unencoded_data = malloc(data_length);
216                 if (unencoded_data) {
217                         fread(unencoded_data, data_length, 1, fp);
218                         char *encoded_data = malloc((data_length * 2) + 100);
219                         if (encoded_data) {
220                                 sprintf(encoded_data, "Content-type: %s\nContent-transfer-encoding: base64\n\n", GuessMimeByFilename(path, strlen(path)));
221                                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS);
222
223                                 char userconfigroomname[ROOMNAMELEN];
224                                 struct ctdluser usbuf;
225
226                                 if (CtdlGetUser(&usbuf, username) == 0) {       // no need to lock it , we are still initializing
227                                         long old_msgnum = usbuf.msgnum_pic;
228                                         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
229                                         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo imported from file");
230                                         syslog(LOG_DEBUG, "Message %ld is now the photo for %s", new_msgnum, username);
231                                         usbuf.msgnum_pic = new_msgnum;
232                                         CtdlPutUser(&usbuf);
233                                         unlink(path);                           // delete the old file , it's in the database now
234                                         if (old_msgnum > 0) {
235                                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
236                                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
237                                         }
238                                 }
239                                 free(encoded_data);
240                         }
241                         free(unencoded_data);
242                 }
243         }
244         fclose(fp);
245 }
246
247
248 // Look for old-format "userpic" files and import them into the message base
249 void import_old_userpic_files(void) {
250         DIR *filedir = NULL;
251         struct dirent *filedir_entry;
252         size_t d_namelen;
253         struct ctdluser usbuf;
254         long usernum = 0;
255         int d_type = 0;
256         struct stat s;
257         char path[PATH_MAX];
258
259
260         syslog(LOG_DEBUG, "Importing old style userpic files into the message base");
261         filedir = opendir (ctdl_usrpic_dir);
262         if (filedir == NULL) {
263                 return;
264         }
265         while ( (filedir_entry = readdir(filedir)) , (filedir_entry != NULL))
266         {
267 #ifdef _DIRENT_HAVE_D_NAMLEN
268                 d_namelen = filedir_entry->d_namlen;
269
270 #else
271                 d_namelen = strlen(filedir_entry->d_name);
272 #endif
273
274 #ifdef _DIRENT_HAVE_D_TYPE
275                 d_type = filedir_entry->d_type;
276 #else
277
278 #ifndef DT_UNKNOWN
279 #define DT_UNKNOWN     0
280 #define DT_DIR         4
281 #define DT_REG         8
282 #define DT_LNK         10
283
284 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
285 #define DTTOIF(dirtype)        ((dirtype) << 12)
286 #endif
287                 d_type = DT_UNKNOWN;
288 #endif
289                 if ((d_namelen == 1) && 
290                     (filedir_entry->d_name[0] == '.'))
291                         continue;
292
293                 if ((d_namelen == 2) && 
294                     (filedir_entry->d_name[0] == '.') &&
295                     (filedir_entry->d_name[1] == '.'))
296                         continue;
297
298                 snprintf(path, PATH_MAX, "%s/%s", ctdl_usrpic_dir, filedir_entry->d_name);
299                 if (d_type == DT_UNKNOWN) {
300                         if (lstat(path, &s) == 0) {
301                                 d_type = IFTODT(s.st_mode);
302                         }
303                 }
304                 switch (d_type) {
305                         case DT_DIR:
306                                 break;
307                         case DT_LNK:
308                         case DT_REG:
309                                 usernum = atol(filedir_entry->d_name);
310                                 if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
311                                         import_one_userpic_file(usbuf.fullname, usernum, path);
312                                 }
313                 }
314         }
315         closedir(filedir);
316         rmdir(ctdl_usrpic_dir);
317 }
318
319
320 // Initialization function, called from modules_init.c
321 char *ctdl_module_init_image(void) {
322         if (!threading) {
323                 import_old_userpic_files();
324                 CtdlRegisterProtoHook(cmd_dlri, "DLRI", "DownLoad Room Image");
325                 CtdlRegisterProtoHook(cmd_ulri, "ULRI", "UpLoad Room Image");
326                 CtdlRegisterProtoHook(cmd_dlui, "DLUI", "DownLoad User Image");
327                 CtdlRegisterProtoHook(cmd_ului, "ULUI", "UpLoad User Image");
328         }
329         // return our module name for the log
330         return "image";
331 }