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 FreeStrBuf(&NewProfile);
55 CtdlGetUserLock(&CC->user, CC->curr_user);
56 CC->user.msgnum_bio = new_msgnum;
57 CtdlPutUserLock(&CC->user);
59 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
60 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
66 * Command to read user bio (profile) in plain text.
67 * This is deprecated , or at least it will be when its replacement is written :)
68 * I want commands to get/set bio in full MIME wonderfulness.
70 void cmd_rbio(char *cmdbuf)
72 struct ctdluser ruser;
75 extract_token(buf, cmdbuf, 0, '|', sizeof buf);
76 if (CtdlGetUser(&ruser, buf) != 0) {
77 cprintf("%d No such user.\n",ERROR + NO_SUCH_USER);
81 cprintf("%d OK|%s|%ld|%d|%ld|%ld|%ld\n", LISTING_FOLLOWS,
82 ruser.fullname, ruser.usernum, ruser.axlevel,
83 (long)ruser.lastcall, ruser.timescalled, ruser.posted);
85 struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_bio, 1, 1);
87 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
95 * Import function called by import_old_bio_files() for a single user
97 void import_one_bio_file(char *username, long usernum, char *path)
99 syslog(LOG_DEBUG, "Import legacy bio for %s, usernum=%ld, filename=%s", username, usernum, path);
101 FILE *fp = fopen(path, "r");
104 fseek(fp, 0, SEEK_END);
105 long data_length = ftell(fp);
107 if (data_length >= 1) {
109 char *unencoded_data = malloc(data_length);
110 if (unencoded_data) {
111 fread(unencoded_data, data_length, 1, fp);
112 char *encoded_data = malloc((data_length * 2) + 100);
114 sprintf(encoded_data, "Content-type: text/plain; charset=UTF-8\nContent-transfer-encoding: base64\n\n");
115 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, 1);
117 char userconfigroomname[ROOMNAMELEN];
118 struct ctdluser usbuf;
120 if (CtdlGetUser(&usbuf, username) == 0) { // no need to lock it , we are still initializing
121 long old_msgnum = usbuf.msgnum_bio;
122 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
123 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Profile imported from bio");
124 syslog(LOG_DEBUG, "Message %ld is now the profile for %s", new_msgnum, username);
125 usbuf.msgnum_bio = new_msgnum;
127 unlink(path); // delete the old file , it's in the database now
128 if (old_msgnum > 0) {
129 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
130 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
135 free(unencoded_data);
143 * Look for old-format "bio" files and import them into the message base
145 void import_old_bio_files(void)
148 struct dirent *filedir_entry;
151 struct ctdluser usbuf;
158 syslog(LOG_DEBUG, "Importing old style bio files into the message base");
159 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
164 filedir = opendir (ctdl_bio_dir);
165 if (filedir == NULL) {
169 while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
170 (filedir_entry != NULL))
172 #ifdef _DIRENT_HAVE_D_NAMLEN
173 d_namelen = filedir_entry->d_namlen;
176 d_namelen = strlen(filedir_entry->d_name);
179 #ifdef _DIRENT_HAVE_D_TYPE
180 d_type = filedir_entry->d_type;
189 #define IFTODT(mode) (((mode) & 0170000) >> 12)
190 #define DTTOIF(dirtype) ((dirtype) << 12)
194 if ((d_namelen == 1) &&
195 (filedir_entry->d_name[0] == '.'))
198 if ((d_namelen == 2) &&
199 (filedir_entry->d_name[0] == '.') &&
200 (filedir_entry->d_name[1] == '.'))
203 snprintf(path, PATH_MAX, "%s/%s", ctdl_bio_dir, filedir_entry->d_name);
204 if (d_type == DT_UNKNOWN) {
205 if (lstat(path, &s) == 0) {
206 d_type = IFTODT(s.st_mode);
215 usernum = atol(filedir_entry->d_name);
216 if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
217 import_one_bio_file(usbuf.fullname, usernum, path);
228 CTDL_MODULE_INIT(bio)
232 import_old_bio_files();
233 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
234 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
236 /* return our module name for the log */