2 * This module implementsserver commands related to the display and
3 * manipulation of user "bio" files.
5 * Copyright (c) 1987-2016 by the citadel.org team
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.
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.
18 #include "ctdl_module.h"
21 #include <sys/types.h>
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.
31 void cmd_ebio(char *cmdbuf) {
36 if (!(CC->logged_in)) {
37 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
41 StrBuf *NewProfile = NewStrBufPlain("Content-type: text/plain; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
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);
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);
58 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
59 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
62 FreeStrBuf(&NewProfile);
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.
71 void cmd_rbio(char *cmdbuf)
73 struct ctdluser ruser;
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);
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);
86 struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_bio, 1, 1);
88 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
96 * Import function called by import_old_bio_files() for a single user
98 void import_one_bio_file(char *username, long usernum, char *path)
100 syslog(LOG_DEBUG, "Import legacy bio for %s, usernum=%ld, filename=%s", username, usernum, path);
102 FILE *fp = fopen(path, "r");
105 fseek(fp, 0, SEEK_END);
106 long data_length = ftell(fp);
108 if (data_length >= 1) {
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);
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);
118 char userconfigroomname[ROOMNAMELEN];
119 struct ctdluser usbuf;
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;
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, "");
136 free(unencoded_data);
144 * Look for old-format "bio" files and import them into the message base
146 void import_old_bio_files(void)
149 struct dirent *filedir_entry;
152 struct ctdluser usbuf;
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);
165 filedir = opendir (ctdl_bio_dir);
166 if (filedir == NULL) {
170 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
171 (filedir_entry != NULL))
173 #ifdef _DIRENT_HAVE_D_NAMLEN
174 d_namelen = filedir_entry->d_namlen;
177 d_namelen = strlen(filedir_entry->d_name);
180 #ifdef _DIRENT_HAVE_D_TYPE
181 d_type = filedir_entry->d_type;
190 #define IFTODT(mode) (((mode) & 0170000) >> 12)
191 #define DTTOIF(dirtype) ((dirtype) << 12)
195 if ((d_namelen == 1) &&
196 (filedir_entry->d_name[0] == '.'))
199 if ((d_namelen == 2) &&
200 (filedir_entry->d_name[0] == '.') &&
201 (filedir_entry->d_name[1] == '.'))
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);
216 usernum = atol(filedir_entry->d_name);
217 if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
218 import_one_bio_file(usbuf.fullname, usernum, path);
229 CTDL_MODULE_INIT(bio)
233 import_old_bio_files();
234 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
235 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
237 /* return our module name for the log */