6ccce90c1eddb0ebbb3b46d4a568cd664be3a055
[citadel.git] / citadel / server / modules / bio / serv_bio.c
1 // This module implements server commands related to the display and
2 // manipulation of user profiles (also known as "bio" files).
3 //
4 // Copyright (c) 1987-2022 by the citadel.org team
5 //
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.
9
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <dirent.h>
13
14 #include "../../ctdl_module.h"
15 #include "../../config.h"
16
17
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) {
22         char buf[SIZ];
23
24         unbuffer_output();
25
26         if (!(CC->logged_in)) {
27                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
28                 return;
29         }
30
31         StrBuf *NewProfile = NewStrBufPlain("Content-type: text/plain; charset=UTF-8\nContent-transfer-encoding: 8bit\n\n", -1);
32
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);
37         }
38
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);
48         if (old_msgnum > 0) {
49                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
50                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
51         }
52 }
53
54
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;
60         char buf[SIZ];
61
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);
65                 return;
66         }
67
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);
71
72         struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_bio, 1);
73         if (msg != NULL) {
74                 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
75                 CM_Free(msg);
76         }
77         cprintf("000\n");
78 }
79
80
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);
84
85         FILE *fp = fopen(path, "r");
86         if (!fp) return;
87
88         fseek(fp, 0, SEEK_END);
89         long data_length = ftell(fp);
90
91         if (data_length >= 1) {
92                 rewind(fp);
93                 char *unencoded_data = malloc(data_length);
94                 if (unencoded_data) {
95                         fread(unencoded_data, data_length, 1, fp);
96                         char *encoded_data = malloc((data_length * 2) + 100);
97                         if (encoded_data) {
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, 1);
100
101                                 char userconfigroomname[ROOMNAMELEN];
102                                 struct ctdluser usbuf;
103
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;
110                                         CtdlPutUser(&usbuf);
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, "");
115                                         }
116                                 }
117                                 free(encoded_data);
118                         }
119                         free(unencoded_data);
120                 }
121         }
122         fclose(fp);
123 }
124
125
126 // Look for old-format "bio" files and import them into the message base
127 void import_old_bio_files(void) {
128         DIR *filedir = NULL;
129         struct dirent *filedir_entry;
130         size_t d_namelen;
131         struct ctdluser usbuf;
132         long usernum = 0;
133         int d_type = 0;
134         struct stat s;
135         char path[PATH_MAX];
136
137         syslog(LOG_DEBUG, "Importing old style bio files into the message base");
138         filedir = opendir("bio");
139         if (filedir == NULL) {
140                 return;
141         }
142         while ( (filedir_entry = readdir(filedir)) , (filedir_entry != NULL)) {
143 #ifdef _DIRENT_HAVE_D_NAMLEN
144                 d_namelen = filedir_entry->d_namlen;
145
146 #else
147                 d_namelen = strlen(filedir_entry->d_name);
148 #endif
149
150 #ifdef _DIRENT_HAVE_D_TYPE
151                 d_type = filedir_entry->d_type;
152 #else
153
154 #ifndef DT_UNKNOWN
155 #define DT_UNKNOWN     0
156 #define DT_DIR         4
157 #define DT_REG         8
158 #define DT_LNK         10
159
160 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
161 #define DTTOIF(dirtype)        ((dirtype) << 12)
162 #endif
163                 d_type = DT_UNKNOWN;
164 #endif
165                 if ((d_namelen == 1) && 
166                     (filedir_entry->d_name[0] == '.'))
167                         continue;
168
169                 if ((d_namelen == 2) && 
170                     (filedir_entry->d_name[0] == '.') &&
171                     (filedir_entry->d_name[1] == '.'))
172                         continue;
173
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);
178                         }
179                 }
180                 switch (d_type)
181                 {
182                 case DT_DIR:
183                         break;
184                 case DT_LNK:
185                 case DT_REG:
186                         usernum = atol(filedir_entry->d_name);
187                         if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
188                                 import_one_bio_file(usbuf.fullname, usernum, path);
189                         }
190                 }
191         }
192         closedir(filedir);
193         rmdir("bio");
194 }
195
196
197 // Initialization function, called from modules_init.c
198 char *ctdl_module_init_bio(void) {
199         if (!threading) {
200                 import_old_bio_files();
201                 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
202                 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
203         }
204         // return our module name for the log
205         return "bio";
206 }