3f327324f1fedb876306c677e3075c68e6434170
[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         extract_token(buf, cmdbuf, 0, '|', sizeof buf);
34         if (CtdlGetUser(&ruser, buf) != 0) {
35                 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
36                 return;
37         }
38         if (ruser.msgnum_pic < 1) {
39                 cprintf("%d No image found.\n", ERROR + FILE_NOT_FOUND);
40                 return;
41         }
42
43         struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_pic, 1, 1);
44         if (msg != NULL) {
45                 // The call to CtdlOutputPreLoadedMsg() with MT_SPEW_SECTION will cause the DLUI command
46                 // to have the same output format as the DLAT command, because it calls the same code.
47                 // For example: 600 402132|-1||image/gif|
48                 safestrncpy(CC->download_desired_section, "1", sizeof CC->download_desired_section);
49                 CtdlOutputPreLoadedMsg(msg, MT_SPEW_SECTION, HEADERS_NONE, 1, 0, 0);
50                 CM_Free(msg);
51         }
52         else {
53                 cprintf("%d No image found.\n", ERROR + MESSAGE_NOT_FOUND);
54                 return;
55         }
56 }
57
58
59
60
61 /*
62  * Import function called by import_old_userpic_files() for a single user
63  */
64 void import_one_userpic_file(char *username, long usernum, char *path)
65 {
66         syslog(LOG_DEBUG, "Import legacy userpic for %s, usernum=%ld, filename=%s", username, usernum, path);
67
68         FILE *fp = fopen(path, "r");
69         if (!fp) return;
70
71         fseek(fp, 0, SEEK_END);
72         long data_length = ftell(fp);
73
74         if (data_length >= 1) {
75                 rewind(fp);
76                 char *unencoded_data = malloc(data_length);
77                 if (unencoded_data) {
78                         fread(unencoded_data, data_length, 1, fp);
79                         char *encoded_data = malloc((data_length * 2) + 100);
80                         if (encoded_data) {
81                                 // FIXME try to guess the content-type based on the filename, don't assume GIF
82                                 sprintf(encoded_data, "Content-type: image/gif\nContent-transfer-encoding: base64\n\n");
83                                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
84
85                                 char userconfigroomname[ROOMNAMELEN];
86                                 struct ctdluser usbuf;
87
88                                 if (CtdlGetUser(&usbuf, username) == 0) {       // no need to lock it , we are still initializing
89                                         long old_msgnum = usbuf.msgnum_pic;
90                                         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
91                                         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Photo imported from file");
92                                         syslog(LOG_DEBUG, "Message %ld is now the profile for %s", new_msgnum, username);
93                                         usbuf.msgnum_pic = new_msgnum;
94                                         CtdlPutUser(&usbuf);
95                                         unlink(path);                           // delete the old file , it's in the database now
96                                         if (old_msgnum > 0) {
97                                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
98                                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
99                                         }
100                                 }
101                                 free(encoded_data);
102                         }
103                         free(unencoded_data);
104                 }
105         }
106         fclose(fp);
107 }
108
109
110 /*
111  * Look for old-format "userpic" files and import them into the message base
112  */
113 void import_old_userpic_files(void)
114 {
115         DIR *filedir = NULL;
116         struct dirent *filedir_entry;
117         struct dirent *d;
118         size_t d_namelen;
119         struct ctdluser usbuf;
120         long usernum = 0;
121         int d_type = 0;
122         struct stat s;
123         char path[PATH_MAX];
124
125
126         syslog(LOG_DEBUG, "Importing old style userpic files into the message base");
127         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
128         if (d == NULL) {
129                 return;
130         }
131
132         filedir = opendir (ctdl_usrpic_dir);
133         if (filedir == NULL) {
134                 free(d);
135                 return;
136         }
137         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
138                (filedir_entry != NULL))
139         {
140 #ifdef _DIRENT_HAVE_D_NAMLEN
141                 d_namelen = filedir_entry->d_namlen;
142
143 #else
144                 d_namelen = strlen(filedir_entry->d_name);
145 #endif
146
147 #ifdef _DIRENT_HAVE_D_TYPE
148                 d_type = filedir_entry->d_type;
149 #else
150
151 #ifndef DT_UNKNOWN
152 #define DT_UNKNOWN     0
153 #define DT_DIR         4
154 #define DT_REG         8
155 #define DT_LNK         10
156
157 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
158 #define DTTOIF(dirtype)        ((dirtype) << 12)
159 #endif
160                 d_type = DT_UNKNOWN;
161 #endif
162                 if ((d_namelen == 1) && 
163                     (filedir_entry->d_name[0] == '.'))
164                         continue;
165
166                 if ((d_namelen == 2) && 
167                     (filedir_entry->d_name[0] == '.') &&
168                     (filedir_entry->d_name[1] == '.'))
169                         continue;
170
171                 snprintf(path, PATH_MAX, "%s/%s", ctdl_usrpic_dir, filedir_entry->d_name);
172                 if (d_type == DT_UNKNOWN) {
173                         if (lstat(path, &s) == 0) {
174                                 d_type = IFTODT(s.st_mode);
175                         }
176                 }
177                 switch (d_type)
178                 {
179                 case DT_DIR:
180                         break;
181                 case DT_LNK:
182                 case DT_REG:
183                         usernum = atol(filedir_entry->d_name);
184                         if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
185                                 import_one_userpic_file(usbuf.fullname, usernum, path);
186                         }
187                 }
188         }
189         free(d);
190         closedir(filedir);
191         rmdir(ctdl_usrpic_dir);
192 }
193
194
195
196 CTDL_MODULE_INIT(image)
197 {
198         if (!threading)
199         {
200                 import_old_userpic_files();
201                 CtdlRegisterProtoHook(cmd_dlui, "DLUI", "DownLoad User Image");
202         }
203         /* return our module name for the log */
204         return "image";
205 }