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