User profiles (bios) are now stored as MIME in the user config room. If a bio/ direc...
[citadel.git] / citadel / modules / bio / serv_bio.c
1 /*
2  * This module implementsserver commands related to the display and
3  * manipulation of user "bio" files.
4  *
5  * Copyright (c) 1987-2016 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include "ctdl_module.h"
19 #include "config.h"
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <dirent.h>
24
25
26 /*
27  * Command to enter user bio (profile) in plain text.
28  * This is deprecated , or at least it will be when its replacement is written  :)
29  * I want commands to get/set bio in full MIME wonderfulness.
30  */
31 void cmd_ebio(char *cmdbuf) {
32         char buf[SIZ];
33
34         unbuffer_output();
35
36         if (!(CC->logged_in)) {
37                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
38                 return;
39         }
40
41         StrBuf *NewProfile = NewStrBufPlain("Content-type: text/plain; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
42
43         cprintf("%d Transmit user profile in plain text now.\n", SEND_LISTING);
44         while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
45                 StrBufAppendBufPlain(NewProfile, buf, -1, 0);
46                 StrBufAppendBufPlain(NewProfile, HKEY("\n"), 0);
47         }
48
49         /* we have read the new profile from the user , now save it */
50         long old_msgnum = CC->user.msgnum_bio;
51         char userconfigroomname[ROOMNAMELEN];
52         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
53         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, ChrPtr(NewProfile), FMT_RFC822, "Profile submitted with EBIO command");
54         CtdlGetUserLock(&CC->user, CC->curr_user);
55         CC->user.msgnum_bio = new_msgnum;
56         CtdlPutUserLock(&CC->user);
57         if (old_msgnum > 0) {
58                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
59                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
60         }
61
62         FreeStrBuf(&NewProfile);
63 }
64
65
66 /*
67  * Command to read user bio (profile) in plain text.
68  * This is deprecated , or at least it will be when its replacement is written  :)
69  * I want commands to get/set bio in full MIME wonderfulness.
70  */
71 void cmd_rbio(char *cmdbuf)
72 {
73         struct ctdluser ruser;
74         char buf[SIZ];
75
76         extract_token(buf, cmdbuf, 0, '|', sizeof buf);
77         if (CtdlGetUser(&ruser, buf) != 0) {
78                 cprintf("%d No such user.\n",ERROR + NO_SUCH_USER);
79                 return;
80         }
81
82         cprintf("%d OK|%s|%ld|%d|%ld|%ld|%ld\n", LISTING_FOLLOWS,
83                 ruser.fullname, ruser.usernum, ruser.axlevel,
84                 (long)ruser.lastcall, ruser.timescalled, ruser.posted);
85
86         struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_bio, 1, 1);
87         if (msg != NULL) {
88                 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
89                 CM_Free(msg);
90         }
91         cprintf("000\n");
92 }
93
94
95 /*
96  * Import function called by import_old_bio_files() for a single user
97  */
98 void import_one_bio_file(char *username, long usernum, char *path)
99 {
100         syslog(LOG_DEBUG, "Import legacy bio for %s, usernum=%ld, filename=%s", username, usernum, path);
101
102         FILE *fp = fopen(path, "r");
103         if (!fp) return;
104
105         fseek(fp, 0, SEEK_END);
106         long data_length = ftell(fp);
107
108         if (data_length >= 1) {
109                 rewind(fp);
110                 char *unencoded_data = malloc(data_length);
111                 if (unencoded_data) {
112                         fread(unencoded_data, data_length, 1, fp);
113                         char *encoded_data = malloc((data_length * 2) + 100);
114                         if (encoded_data) {
115                                 sprintf(encoded_data, "Content-type: text/plain; charset=UTF-8\nContent-transfer-encoding: base64\n\n");
116                                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
117
118                                 char userconfigroomname[ROOMNAMELEN];
119                                 struct ctdluser usbuf;
120
121                                 if (CtdlGetUser(&usbuf, username) == 0) {       // no need to lock it , we are still initializing
122                                         long old_msgnum = usbuf.msgnum_bio;
123                                         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
124                                         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Profile imported from bio");
125                                         syslog(LOG_DEBUG, "Message %ld is now the profile for %s", new_msgnum, username);
126                                         usbuf.msgnum_bio = new_msgnum;
127                                         CtdlPutUser(&usbuf);
128                                         unlink(path);                           // delete the old file , it's in the database now
129                                         if (old_msgnum > 0) {
130                                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
131                                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
132                                         }
133                                 }
134                                 free(encoded_data);
135                         }
136                         free(unencoded_data);
137                 }
138         }
139         fclose(fp);
140 }
141
142
143 /*
144  * Look for old-format "bio" files and import them into the message base
145  */
146 void import_old_bio_files(void)
147 {
148         DIR *filedir = NULL;
149         struct dirent *filedir_entry;
150         struct dirent *d;
151         size_t d_namelen;
152         struct ctdluser usbuf;
153         long usernum = 0;
154         int d_type = 0;
155         struct stat s;
156         char path[PATH_MAX];
157
158
159         syslog(LOG_DEBUG, "Importing old style bio files into the message base");
160         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
161         if (d == NULL) {
162                 return;
163         }
164
165         filedir = opendir (ctdl_bio_dir);
166         if (filedir == NULL) {
167                 free(d);
168                 return;
169         }
170         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
171                (filedir_entry != NULL))
172         {
173 #ifdef _DIRENT_HAVE_D_NAMLEN
174                 d_namelen = filedir_entry->d_namlen;
175
176 #else
177                 d_namelen = strlen(filedir_entry->d_name);
178 #endif
179
180 #ifdef _DIRENT_HAVE_D_TYPE
181                 d_type = filedir_entry->d_type;
182 #else
183
184 #ifndef DT_UNKNOWN
185 #define DT_UNKNOWN     0
186 #define DT_DIR         4
187 #define DT_REG         8
188 #define DT_LNK         10
189
190 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
191 #define DTTOIF(dirtype)        ((dirtype) << 12)
192 #endif
193                 d_type = DT_UNKNOWN;
194 #endif
195                 if ((d_namelen == 1) && 
196                     (filedir_entry->d_name[0] == '.'))
197                         continue;
198
199                 if ((d_namelen == 2) && 
200                     (filedir_entry->d_name[0] == '.') &&
201                     (filedir_entry->d_name[1] == '.'))
202                         continue;
203
204                 snprintf(path, PATH_MAX, "%s/%s", ctdl_bio_dir, filedir_entry->d_name);
205                 if (d_type == DT_UNKNOWN) {
206                         if (lstat(path, &s) == 0) {
207                                 d_type = IFTODT(s.st_mode);
208                         }
209                 }
210                 switch (d_type)
211                 {
212                 case DT_DIR:
213                         break;
214                 case DT_LNK:
215                 case DT_REG:
216                         usernum = atol(filedir_entry->d_name);
217                         if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
218                                 import_one_bio_file(usbuf.fullname, usernum, path);
219                         }
220                 }
221         }
222         free(d);
223         closedir(filedir);
224         rmdir(ctdl_bio_dir);
225 }
226
227
228
229 CTDL_MODULE_INIT(bio)
230 {
231         if (!threading)
232         {
233                 import_old_bio_files();
234                 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
235                 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
236         }
237         /* return our module name for the log */
238         return "bio";
239 }