00615a06ca2ccf22a46c84262e10bd6114fe5fe6
[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         if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
66
67         cprintf("500 FIXME not finished\n");
68 }
69
70
71 /*
72  * Import function called by import_old_userpic_files() for a single user
73  */
74 void import_one_userpic_file(char *username, long usernum, char *path)
75 {
76         syslog(LOG_DEBUG, "Import legacy userpic for %s, usernum=%ld, filename=%s", username, usernum, path);
77
78         FILE *fp = fopen(path, "r");
79         if (!fp) return;
80
81         fseek(fp, 0, SEEK_END);
82         long data_length = ftell(fp);
83
84         if (data_length >= 1) {
85                 rewind(fp);
86                 char *unencoded_data = malloc(data_length);
87                 if (unencoded_data) {
88                         fread(unencoded_data, data_length, 1, fp);
89                         char *encoded_data = malloc((data_length * 2) + 100);
90                         if (encoded_data) {
91                                 // FIXME try to guess the content-type based on the filename, don't assume GIF
92                                 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
93                                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
94
95                                 char userconfigroomname[ROOMNAMELEN];
96                                 struct ctdluser usbuf;
97
98                                 if (CtdlGetUser(&usbuf, username) == 0) {       // no need to lock it , we are still initializing
99                                         long old_msgnum = usbuf.msgnum_pic;
100                                         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
101                                         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo imported from file");
102                                         syslog(LOG_DEBUG, "Message %ld is now the profile for %s", new_msgnum, username);
103                                         usbuf.msgnum_pic = new_msgnum;
104                                         CtdlPutUser(&usbuf);
105                                         unlink(path);                           // delete the old file , it's in the database now
106                                         if (old_msgnum > 0) {
107                                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
108                                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
109                                         }
110                                 }
111                                 free(encoded_data);
112                         }
113                         free(unencoded_data);
114                 }
115         }
116         fclose(fp);
117 }
118
119
120 /*
121  * Look for old-format "userpic" files and import them into the message base
122  */
123 void import_old_userpic_files(void)
124 {
125         DIR *filedir = NULL;
126         struct dirent *filedir_entry;
127         struct dirent *d;
128         size_t d_namelen;
129         struct ctdluser usbuf;
130         long usernum = 0;
131         int d_type = 0;
132         struct stat s;
133         char path[PATH_MAX];
134
135
136         syslog(LOG_DEBUG, "Importing old style userpic files into the message base");
137         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
138         if (d == NULL) {
139                 return;
140         }
141
142         filedir = opendir (ctdl_usrpic_dir);
143         if (filedir == NULL) {
144                 free(d);
145                 return;
146         }
147         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
148                (filedir_entry != NULL))
149         {
150 #ifdef _DIRENT_HAVE_D_NAMLEN
151                 d_namelen = filedir_entry->d_namlen;
152
153 #else
154                 d_namelen = strlen(filedir_entry->d_name);
155 #endif
156
157 #ifdef _DIRENT_HAVE_D_TYPE
158                 d_type = filedir_entry->d_type;
159 #else
160
161 #ifndef DT_UNKNOWN
162 #define DT_UNKNOWN     0
163 #define DT_DIR         4
164 #define DT_REG         8
165 #define DT_LNK         10
166
167 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
168 #define DTTOIF(dirtype)        ((dirtype) << 12)
169 #endif
170                 d_type = DT_UNKNOWN;
171 #endif
172                 if ((d_namelen == 1) && 
173                     (filedir_entry->d_name[0] == '.'))
174                         continue;
175
176                 if ((d_namelen == 2) && 
177                     (filedir_entry->d_name[0] == '.') &&
178                     (filedir_entry->d_name[1] == '.'))
179                         continue;
180
181                 snprintf(path, PATH_MAX, "%s/%s", ctdl_usrpic_dir, filedir_entry->d_name);
182                 if (d_type == DT_UNKNOWN) {
183                         if (lstat(path, &s) == 0) {
184                                 d_type = IFTODT(s.st_mode);
185                         }
186                 }
187                 switch (d_type)
188                 {
189                 case DT_DIR:
190                         break;
191                 case DT_LNK:
192                 case DT_REG:
193                         usernum = atol(filedir_entry->d_name);
194                         if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
195                                 import_one_userpic_file(usbuf.fullname, usernum, path);
196                         }
197                 }
198         }
199         free(d);
200         closedir(filedir);
201         rmdir(ctdl_usrpic_dir);
202 }
203
204
205
206 CTDL_MODULE_INIT(image)
207 {
208         if (!threading)
209         {
210                 import_old_userpic_files();
211                 CtdlRegisterProtoHook(cmd_dlui, "DLUI", "DownLoad User Image");
212                 CtdlRegisterProtoHook(cmd_ului, "ULUI", "UpLoad User Image");
213         }
214         /* return our module name for the log */
215         return "image";
216 }