2 * Copyright (c) 1987-2016 by the citadel.org team
4 * This program is open source 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.
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.
15 #include "ctdl_module.h"
18 #include <sys/types.h>
25 * DownLoad User Image (see their avatar or photo or whatever)
26 * If this command succeeds, it follows the same protocol as the DLAT command.
28 void cmd_dlui(char *cmdbuf)
30 struct ctdluser ruser;
33 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
34 extract_token(buf, cmdbuf, 0, '|', sizeof buf);
35 if (CtdlGetUser(&ruser, buf) != 0) {
36 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
39 if (ruser.msgnum_pic < 1) {
40 cprintf("%d No image found.\n", ERROR + FILE_NOT_FOUND);
44 struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_pic, 1, 1);
46 // The call to CtdlOutputPreLoadedMsg() with MT_SPEW_SECTION will cause the DLUI command
47 // to have the same output format as the DLAT command, because it calls the same code.
48 // For example: 600 402132|-1||image/gif|
49 safestrncpy(CC->download_desired_section, "1", sizeof CC->download_desired_section);
50 CtdlOutputPreLoadedMsg(msg, MT_SPEW_SECTION, HEADERS_NONE, 1, 0, 0);
54 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
61 * DownLoad User Image (avatar or photo or whatever)
63 void cmd_ului(char *cmdbuf)
67 char username[USERNAME_SIZE];
69 if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
71 if (num_parms(cmdbuf) < 2)
73 cprintf("%d Usage error\n", ERROR + ILLEGAL_VALUE);
77 data_length = extract_long(cmdbuf, 0);
78 extract_token(mimetype, cmdbuf, 1, '|', sizeof mimetype);
79 extract_token(username, cmdbuf, 2, '|', sizeof username);
81 if (data_length < 20) {
82 cprintf("%d That's an awfully small file. Try again.\n", ERROR + ILLEGAL_VALUE);
86 if (strncasecmp(mimetype, "image/", 6)) {
87 cprintf("%d Only image files are permitted.\n", ERROR + ILLEGAL_VALUE);
91 if (IsEmptyStr(username)) {
92 safestrncpy(username, CC->curr_user, sizeof username);
95 // Normal users can only change their own photo
96 if ( (strcasecmp(username, CC->curr_user)) && (CC->user.axlevel < AxAideU) && (!CC->internal_pgm) ) {
97 cprintf("%d Higher access required to change another user's photo.\n", ERROR + HIGHER_ACCESS_REQUIRED);
100 // Check to make sure the user exists
102 struct ctdluser usbuf;
103 if (CtdlGetUser(&usbuf, username) != 0) { // check for existing user, don't lock it yet
104 cprintf("%d %s not found.\n", ERROR + NO_SUCH_USER , username);
108 char *unencoded_data = malloc(data_length + 1);
109 if (!unencoded_data) {
110 cprintf("%d Could not allocate %ld bytes of memory\n", ERROR + INTERNAL_ERROR , data_length);
114 cprintf("%d %ld\n", SEND_BINARY, data_length);
115 client_read(unencoded_data, data_length);
117 // We've got the data read from the client, now save it.
120 free(unencoded_data);
125 * Import function called by import_old_userpic_files() for a single user
127 void import_one_userpic_file(char *username, long usernum, char *path)
129 syslog(LOG_DEBUG, "Import legacy userpic for %s, usernum=%ld, filename=%s", username, usernum, path);
131 FILE *fp = fopen(path, "r");
134 fseek(fp, 0, SEEK_END);
135 long data_length = ftell(fp);
137 if (data_length >= 1) {
139 char *unencoded_data = malloc(data_length);
140 if (unencoded_data) {
141 fread(unencoded_data, data_length, 1, fp);
142 char *encoded_data = malloc((data_length * 2) + 100);
144 // FIXME try to guess the content-type based on the filename, don't assume GIF
145 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
146 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
148 char userconfigroomname[ROOMNAMELEN];
149 struct ctdluser usbuf;
151 if (CtdlGetUser(&usbuf, username) == 0) { // no need to lock it , we are still initializing
152 long old_msgnum = usbuf.msgnum_pic;
153 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
154 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo imported from file");
155 syslog(LOG_DEBUG, "Message %ld is now the profile for %s", new_msgnum, username);
156 usbuf.msgnum_pic = new_msgnum;
158 unlink(path); // delete the old file , it's in the database now
159 if (old_msgnum > 0) {
160 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
161 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
166 free(unencoded_data);
174 * Look for old-format "userpic" files and import them into the message base
176 void import_old_userpic_files(void)
179 struct dirent *filedir_entry;
182 struct ctdluser usbuf;
189 syslog(LOG_DEBUG, "Importing old style userpic files into the message base");
190 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
195 filedir = opendir (ctdl_usrpic_dir);
196 if (filedir == NULL) {
200 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
201 (filedir_entry != NULL))
203 #ifdef _DIRENT_HAVE_D_NAMLEN
204 d_namelen = filedir_entry->d_namlen;
207 d_namelen = strlen(filedir_entry->d_name);
210 #ifdef _DIRENT_HAVE_D_TYPE
211 d_type = filedir_entry->d_type;
220 #define IFTODT(mode) (((mode) & 0170000) >> 12)
221 #define DTTOIF(dirtype) ((dirtype) << 12)
225 if ((d_namelen == 1) &&
226 (filedir_entry->d_name[0] == '.'))
229 if ((d_namelen == 2) &&
230 (filedir_entry->d_name[0] == '.') &&
231 (filedir_entry->d_name[1] == '.'))
234 snprintf(path, PATH_MAX, "%s/%s", ctdl_usrpic_dir, filedir_entry->d_name);
235 if (d_type == DT_UNKNOWN) {
236 if (lstat(path, &s) == 0) {
237 d_type = IFTODT(s.st_mode);
246 usernum = atol(filedir_entry->d_name);
247 if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
248 import_one_userpic_file(usbuf.fullname, usernum, path);
254 rmdir(ctdl_usrpic_dir);
259 CTDL_MODULE_INIT(image)
263 import_old_userpic_files();
264 CtdlRegisterProtoHook(cmd_dlui, "DLUI", "DownLoad User Image");
265 CtdlRegisterProtoHook(cmd_ului, "ULUI", "UpLoad User Image");
267 /* return our module name for the log */