1 // Copyright (c) 1987-2022 by the citadel.org team
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.
6 #include "../../ctdl_module.h"
7 #include "../../config.h"
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);
22 struct CtdlMessage *msg = CtdlFetchMessage(CC->room.msgnum_pic, 1);
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);
32 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
38 // UpLoad Room Image (avatar or photo or whatever)
39 void cmd_ulri(char *cmdbuf) {
43 if (CtdlAccessCheck(ac_room_aide)) return;
45 data_length = extract_long(cmdbuf, 0);
46 extract_token(mimetype, cmdbuf, 1, '|', sizeof mimetype);
48 if (data_length < 20) {
49 cprintf("%d That's an awfully small file. Try again.\n", ERROR + ILLEGAL_VALUE);
53 if (strncasecmp(mimetype, "image/", 6)) {
54 cprintf("%d Only image files are permitted.\n", ERROR + ILLEGAL_VALUE);
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);
64 cprintf("%d %ld\n", SEND_BINARY, data_length);
65 client_read(unencoded_data, data_length);
67 // We've got the data read from the client, now save it.
68 char *encoded_data = malloc((data_length * 2) + 100);
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");
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);
80 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, SYSCONFIGROOM);
81 CtdlDeleteMessages(SYSCONFIGROOM, &old_msgnum, 1, "");
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;
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);
103 if (ruser.msgnum_pic < 1) {
104 cprintf("%d No image found.\n", ERROR + FILE_NOT_FOUND);
108 struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_pic, 1);
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);
118 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
124 // UpLoad User Image (avatar or photo or whatever)
125 void cmd_ului(char *cmdbuf) {
128 char username[USERNAME_SIZE];
129 char userconfigroomname[ROOMNAMELEN];
131 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
133 if (num_parms(cmdbuf) < 2) {
134 cprintf("%d Usage error\n", ERROR + ILLEGAL_VALUE);
138 data_length = extract_long(cmdbuf, 0);
139 extract_token(mimetype, cmdbuf, 1, '|', sizeof mimetype);
140 extract_token(username, cmdbuf, 2, '|', sizeof username);
142 if (data_length < 20) {
143 cprintf("%d That's an awfully small file. Try again.\n", ERROR + ILLEGAL_VALUE);
147 if (strncasecmp(mimetype, "image/", 6)) {
148 cprintf("%d Only image files are permitted.\n", ERROR + ILLEGAL_VALUE);
152 if (IsEmptyStr(username)) {
153 safestrncpy(username, CC->curr_user, sizeof username);
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);
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);
167 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
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);
175 cprintf("%d %ld\n", SEND_BINARY, data_length);
176 client_read(unencoded_data, data_length);
178 // We've got the data read from the client, now save it.
179 char *encoded_data = malloc((data_length * 2) + 100);
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");
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, "");
199 free(unencoded_data);
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);
207 FILE *fp = fopen(path, "r");
210 fseek(fp, 0, SEEK_END);
211 long data_length = ftell(fp);
213 if (data_length >= 1) {
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);
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);
223 char userconfigroomname[ROOMNAMELEN];
224 struct ctdluser usbuf;
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;
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, "");
241 free(unencoded_data);
248 // Look for old-format "userpic" files and import them into the message base
249 void import_old_userpic_files(void) {
251 struct dirent *filedir_entry;
253 struct ctdluser usbuf;
260 syslog(LOG_DEBUG, "Importing old style userpic files into the message base");
261 filedir = opendir (ctdl_usrpic_dir);
262 if (filedir == NULL) {
265 while ( (filedir_entry = readdir(filedir)) , (filedir_entry != NULL))
267 #ifdef _DIRENT_HAVE_D_NAMLEN
268 d_namelen = filedir_entry->d_namlen;
271 d_namelen = strlen(filedir_entry->d_name);
274 #ifdef _DIRENT_HAVE_D_TYPE
275 d_type = filedir_entry->d_type;
284 #define IFTODT(mode) (((mode) & 0170000) >> 12)
285 #define DTTOIF(dirtype) ((dirtype) << 12)
289 if ((d_namelen == 1) &&
290 (filedir_entry->d_name[0] == '.'))
293 if ((d_namelen == 2) &&
294 (filedir_entry->d_name[0] == '.') &&
295 (filedir_entry->d_name[1] == '.'))
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);
309 usernum = atol(filedir_entry->d_name);
310 if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
311 import_one_userpic_file(usbuf.fullname, usernum, path);
316 rmdir(ctdl_usrpic_dir);
320 // Initialization function, called from modules_init.c
321 char *ctdl_module_init_image(void) {
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");
329 // return our module name for the log