a6a3728bbb54b3ee44ad70deeb1f4ebf46fdb10a
[citadel.git] / citadel / modules / image / serv_image.c
1 /*
2  * Copyright (c) 1987-2016 by the citadel.org team
3  *
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.
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 "ctdl_module.h"
16 #include "config.h"
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <dirent.h>
21
22
23
24 /*
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.
27  */
28 void cmd_dlui(char *cmdbuf)
29 {
30         struct ctdluser ruser;
31         char buf[SIZ];
32
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);
37                 return;
38         }
39         if (ruser.msgnum_pic < 1) {
40                 cprintf("%d No image found.\n", ERROR + FILE_NOT_FOUND);
41                 return;
42         }
43
44         struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_pic, 1, 1);
45         if (msg != NULL) {
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);
51                 CM_Free(msg);
52         }
53         else {
54                 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
55                 return;
56         }
57 }
58
59
60 /*
61  * DownLoad User Image (avatar or photo or whatever)
62  */
63 void cmd_ului(char *cmdbuf)
64 {
65         long data_length;
66         char mimetype[SIZ];
67         char username[USERNAME_SIZE];
68
69         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
70
71         if ( (num_parms(cmdbuf) < 2) || (num_parms(cmdbuf) > 3) )
72         {
73                 cprintf("%d Usage error.\n", ERROR + ILLEGAL_VALUE);
74                 return;
75         }
76
77         data_length = extract_long(cmdbuf, 0);
78         extract_token(mimetype, cmdbuf, 1, '|', sizeof mimetype);
79         extract_token(username, cmdbuf, 2, '|', sizeof username);
80
81         if (data_length < 20) {
82                 cprintf("%d That's an awfully small file.  Try again.\n", ERROR + ILLEGAL_VALUE);
83                 return;
84         }
85
86         if (strncasecmp(mimetype, "image/", 6)) {
87                 cprintf("%d Only image files are permitted.\n", ERROR + ILLEGAL_VALUE);
88                 return;
89         }
90
91         if (IsEmptyStr(username)) {
92                 safestrncpy(username, CC->curr_user, sizeof username);
93         }
94
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);
98         }
99
100         cprintf("500 nope not yet, I am %s , modifying %s , data length is %ld\n", CC->curr_user, username, data_length);
101 }
102
103
104 /*
105  * Import function called by import_old_userpic_files() for a single user
106  */
107 void import_one_userpic_file(char *username, long usernum, char *path)
108 {
109         syslog(LOG_DEBUG, "Import legacy userpic for %s, usernum=%ld, filename=%s", username, usernum, path);
110
111         FILE *fp = fopen(path, "r");
112         if (!fp) return;
113
114         fseek(fp, 0, SEEK_END);
115         long data_length = ftell(fp);
116
117         if (data_length >= 1) {
118                 rewind(fp);
119                 char *unencoded_data = malloc(data_length);
120                 if (unencoded_data) {
121                         fread(unencoded_data, data_length, 1, fp);
122                         char *encoded_data = malloc((data_length * 2) + 100);
123                         if (encoded_data) {
124                                 // FIXME try to guess the content-type based on the filename, don't assume GIF
125                                 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
126                                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
127
128                                 char userconfigroomname[ROOMNAMELEN];
129                                 struct ctdluser usbuf;
130
131                                 if (CtdlGetUser(&usbuf, username) == 0) {       // no need to lock it , we are still initializing
132                                         long old_msgnum = usbuf.msgnum_pic;
133                                         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
134                                         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo imported from file");
135                                         syslog(LOG_DEBUG, "Message %ld is now the profile for %s", new_msgnum, username);
136                                         usbuf.msgnum_pic = new_msgnum;
137                                         CtdlPutUser(&usbuf);
138                                         unlink(path);                           // delete the old file , it's in the database now
139                                         if (old_msgnum > 0) {
140                                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
141                                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
142                                         }
143                                 }
144                                 free(encoded_data);
145                         }
146                         free(unencoded_data);
147                 }
148         }
149         fclose(fp);
150 }
151
152
153 /*
154  * Look for old-format "userpic" files and import them into the message base
155  */
156 void import_old_userpic_files(void)
157 {
158         DIR *filedir = NULL;
159         struct dirent *filedir_entry;
160         struct dirent *d;
161         size_t d_namelen;
162         struct ctdluser usbuf;
163         long usernum = 0;
164         int d_type = 0;
165         struct stat s;
166         char path[PATH_MAX];
167
168
169         syslog(LOG_DEBUG, "Importing old style userpic files into the message base");
170         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
171         if (d == NULL) {
172                 return;
173         }
174
175         filedir = opendir (ctdl_usrpic_dir);
176         if (filedir == NULL) {
177                 free(d);
178                 return;
179         }
180         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
181                (filedir_entry != NULL))
182         {
183 #ifdef _DIRENT_HAVE_D_NAMLEN
184                 d_namelen = filedir_entry->d_namlen;
185
186 #else
187                 d_namelen = strlen(filedir_entry->d_name);
188 #endif
189
190 #ifdef _DIRENT_HAVE_D_TYPE
191                 d_type = filedir_entry->d_type;
192 #else
193
194 #ifndef DT_UNKNOWN
195 #define DT_UNKNOWN     0
196 #define DT_DIR         4
197 #define DT_REG         8
198 #define DT_LNK         10
199
200 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
201 #define DTTOIF(dirtype)        ((dirtype) << 12)
202 #endif
203                 d_type = DT_UNKNOWN;
204 #endif
205                 if ((d_namelen == 1) && 
206                     (filedir_entry->d_name[0] == '.'))
207                         continue;
208
209                 if ((d_namelen == 2) && 
210                     (filedir_entry->d_name[0] == '.') &&
211                     (filedir_entry->d_name[1] == '.'))
212                         continue;
213
214                 snprintf(path, PATH_MAX, "%s/%s", ctdl_usrpic_dir, filedir_entry->d_name);
215                 if (d_type == DT_UNKNOWN) {
216                         if (lstat(path, &s) == 0) {
217                                 d_type = IFTODT(s.st_mode);
218                         }
219                 }
220                 switch (d_type)
221                 {
222                 case DT_DIR:
223                         break;
224                 case DT_LNK:
225                 case DT_REG:
226                         usernum = atol(filedir_entry->d_name);
227                         if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
228                                 import_one_userpic_file(usbuf.fullname, usernum, path);
229                         }
230                 }
231         }
232         free(d);
233         closedir(filedir);
234         rmdir(ctdl_usrpic_dir);
235 }
236
237
238
239 CTDL_MODULE_INIT(image)
240 {
241         if (!threading)
242         {
243                 import_old_userpic_files();
244                 CtdlRegisterProtoHook(cmd_dlui, "DLUI", "DownLoad User Image");
245                 CtdlRegisterProtoHook(cmd_ului, "ULUI", "UpLoad User Image");
246         }
247         /* return our module name for the log */
248         return "image";
249 }