1 // This module implements server commands related to the display and
2 // manipulation of user profiles (also known as "bio" files).
4 // Copyright (c) 1987-2022 by the citadel.org team
6 // This program is open source software. Use, duplication, or disclosure
7 // is subject to the terms of the GNU General Public License, version 3.
8 // The program is distributed without any warranty, expressed or implied.
10 #include <sys/types.h>
14 #include "../../ctdl_module.h"
15 #include "../../config.h"
18 // Command to enter user bio (profile) in plain text.
19 // This is deprecated , or at least it will be when its replacement is written :)
20 // I want commands to get/set bio in full MIME wonderfulness.
21 void cmd_ebio(char *cmdbuf) {
26 if (!(CC->logged_in)) {
27 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
31 StrBuf *NewProfile = NewStrBufPlain("Content-type: text/plain; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
33 cprintf("%d Transmit user profile in plain text now.\n", SEND_LISTING);
34 while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
35 StrBufAppendBufPlain(NewProfile, buf, -1, 0);
36 StrBufAppendBufPlain(NewProfile, HKEY("\n"), 0);
39 // we have read the new profile from the user , now save it
40 long old_msgnum = CC->user.msgnum_bio;
41 char userconfigroomname[ROOMNAMELEN];
42 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &CC->user, USERCONFIGROOM);
43 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, ChrPtr(NewProfile), FMT_RFC822, "Profile submitted with EBIO command");
44 FreeStrBuf(&NewProfile);
45 CtdlGetUserLock(&CC->user, CC->curr_user);
46 CC->user.msgnum_bio = new_msgnum;
47 CtdlPutUserLock(&CC->user);
49 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
50 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
55 // Command to read user bio (profile) in plain text.
56 // This is deprecated , or at least it will be when its replacement is written :)
57 // I want commands to get/set bio in full MIME wonderfulness.
58 void cmd_rbio(char *cmdbuf) {
59 struct ctdluser ruser;
62 extract_token(buf, cmdbuf, 0, '|', sizeof buf);
63 if (CtdlGetUser(&ruser, buf) != 0) {
64 cprintf("%d No such user.\n",ERROR + NO_SUCH_USER);
68 cprintf("%d OK|%s|%ld|%d|%ld|%ld|%ld\n", LISTING_FOLLOWS,
69 ruser.fullname, ruser.usernum, ruser.axlevel,
70 (long)ruser.lastcall, ruser.timescalled, ruser.posted);
72 struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_bio, 1);
74 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
81 // Import function called by import_old_bio_files() for a single user
82 void import_one_bio_file(char *username, long usernum, char *path) {
83 syslog(LOG_DEBUG, "Import legacy bio for %s, usernum=%ld, filename=%s", username, usernum, path);
85 FILE *fp = fopen(path, "r");
88 fseek(fp, 0, SEEK_END);
89 long data_length = ftell(fp);
91 if (data_length >= 1) {
93 char *unencoded_data = malloc(data_length);
95 fread(unencoded_data, data_length, 1, fp);
96 char *encoded_data = malloc((data_length * 2) + 100);
98 sprintf(encoded_data, "Content-type: text/plain; charset=UTF-8\nContent-transfer-encoding: base64\n\n");
99 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS);
101 char userconfigroomname[ROOMNAMELEN];
102 struct ctdluser usbuf;
104 if (CtdlGetUser(&usbuf, username) == 0) { // no need to lock it , we are still initializing
105 long old_msgnum = usbuf.msgnum_bio;
106 CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
107 long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Profile imported from bio");
108 syslog(LOG_DEBUG, "Message %ld is now the profile for %s", new_msgnum, username);
109 usbuf.msgnum_bio = new_msgnum;
111 unlink(path); // delete the old file , it's in the database now
112 if (old_msgnum > 0) {
113 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
114 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
119 free(unencoded_data);
126 // Look for old-format "bio" files and import them into the message base
127 void import_old_bio_files(void) {
129 struct dirent *filedir_entry;
131 struct ctdluser usbuf;
137 syslog(LOG_DEBUG, "Importing old style bio files into the message base");
138 filedir = opendir("bio");
139 if (filedir == NULL) {
142 while ( (filedir_entry = readdir(filedir)) , (filedir_entry != NULL)) {
143 #ifdef _DIRENT_HAVE_D_NAMLEN
144 d_namelen = filedir_entry->d_namlen;
147 d_namelen = strlen(filedir_entry->d_name);
150 #ifdef _DIRENT_HAVE_D_TYPE
151 d_type = filedir_entry->d_type;
160 #define IFTODT(mode) (((mode) & 0170000) >> 12)
161 #define DTTOIF(dirtype) ((dirtype) << 12)
165 if ((d_namelen == 1) &&
166 (filedir_entry->d_name[0] == '.'))
169 if ((d_namelen == 2) &&
170 (filedir_entry->d_name[0] == '.') &&
171 (filedir_entry->d_name[1] == '.'))
174 snprintf(path, PATH_MAX, "bio/%s", filedir_entry->d_name);
175 if (d_type == DT_UNKNOWN) {
176 if (lstat(path, &s) == 0) {
177 d_type = IFTODT(s.st_mode);
186 usernum = atol(filedir_entry->d_name);
187 if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
188 import_one_bio_file(usbuf.fullname, usernum, path);
197 // Initialization function, called from modules_init.c
198 char *ctdl_module_init_bio(void) {
200 import_old_bio_files();
201 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
202 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
204 // return our module name for the log