]> code.citadel.org Git - citadel.git/blob - citadel/server/modules/bio/serv_bio.c
Merge branch 'Webcit_Coredump_StrBufQuotedPrintableEncode' into 'master'
[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|0|0\n", LISTING_FOLLOWS,
69                 ruser.fullname, ruser.usernum, ruser.axlevel, (long)ruser.lastcall);
70
71         struct CtdlMessage *msg = CtdlFetchMessage(ruser.msgnum_bio, 1);
72         if (msg != NULL) {
73                 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
74                 CM_Free(msg);
75         }
76         cprintf("000\n");
77 }
78
79
80 // Import function called by import_old_bio_files() for a single user
81 void import_one_bio_file(char *username, long usernum, char *path) {
82         syslog(LOG_DEBUG, "Import legacy bio for %s, usernum=%ld, filename=%s", username, usernum, path);
83
84         FILE *fp = fopen(path, "r");
85         if (!fp) return;
86
87         fseek(fp, 0, SEEK_END);
88         long data_length = ftell(fp);
89
90         if (data_length >= 1) {
91                 rewind(fp);
92                 char *unencoded_data = malloc(data_length);
93                 if (unencoded_data) {
94                         fread(unencoded_data, data_length, 1, fp);
95                         char *encoded_data = malloc((data_length * 2) + 100);
96                         if (encoded_data) {
97                                 sprintf(encoded_data, "Content-type: text/plain; charset=UTF-8\nContent-transfer-encoding: base64\n\n");
98                                 CtdlEncodeBase64(&encoded_data[strlen(encoded_data)], unencoded_data, data_length, BASE64_YES_LINEBREAKS);
99
100                                 char userconfigroomname[ROOMNAMELEN];
101                                 struct ctdluser usbuf;
102
103                                 if (CtdlGetUser(&usbuf, username) == 0) {       // no need to lock it , we are still initializing
104                                         long old_msgnum = usbuf.msgnum_bio;
105                                         CtdlMailboxName(userconfigroomname, sizeof userconfigroomname, &usbuf, USERCONFIGROOM);
106                                         long new_msgnum = quickie_message("Citadel", NULL, NULL, userconfigroomname, encoded_data, FMT_RFC822, "Profile imported from bio");
107                                         syslog(LOG_DEBUG, "Message %ld is now the profile for %s", new_msgnum, username);
108                                         usbuf.msgnum_bio = new_msgnum;
109                                         CtdlPutUser(&usbuf);
110                                         unlink(path);                           // delete the old file , it's in the database now
111                                         if (old_msgnum > 0) {
112                                                 syslog(LOG_DEBUG, "Deleting old message %ld from %s", old_msgnum, userconfigroomname);
113                                                 CtdlDeleteMessages(userconfigroomname, &old_msgnum, 1, "");
114                                         }
115                                 }
116                                 free(encoded_data);
117                         }
118                         free(unencoded_data);
119                 }
120         }
121         fclose(fp);
122 }
123
124
125 // Look for old-format "bio" files and import them into the message base
126 void import_old_bio_files(void) {
127         DIR *filedir = NULL;
128         struct dirent *filedir_entry;
129         size_t d_namelen;
130         struct ctdluser usbuf;
131         long usernum = 0;
132         int d_type = 0;
133         struct stat s;
134         char path[PATH_MAX];
135
136         syslog(LOG_DEBUG, "Importing old style bio files into the message base");
137         filedir = opendir("bio");
138         if (filedir == NULL) {
139                 return;
140         }
141         while ( (filedir_entry = readdir(filedir)) , (filedir_entry != NULL)) {
142 #ifdef _DIRENT_HAVE_D_NAMLEN
143                 d_namelen = filedir_entry->d_namlen;
144
145 #else
146                 d_namelen = strlen(filedir_entry->d_name);
147 #endif
148
149 #ifdef _DIRENT_HAVE_D_TYPE
150                 d_type = filedir_entry->d_type;
151 #else
152
153 #ifndef DT_UNKNOWN
154 #define DT_UNKNOWN     0
155 #define DT_DIR         4
156 #define DT_REG         8
157 #define DT_LNK         10
158
159 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
160 #define DTTOIF(dirtype)        ((dirtype) << 12)
161 #endif
162                 d_type = DT_UNKNOWN;
163 #endif
164                 if ((d_namelen == 1) && 
165                     (filedir_entry->d_name[0] == '.'))
166                         continue;
167
168                 if ((d_namelen == 2) && 
169                     (filedir_entry->d_name[0] == '.') &&
170                     (filedir_entry->d_name[1] == '.'))
171                         continue;
172
173                 snprintf(path, PATH_MAX, "bio/%s", filedir_entry->d_name);
174                 if (d_type == DT_UNKNOWN) {
175                         if (lstat(path, &s) == 0) {
176                                 d_type = IFTODT(s.st_mode);
177                         }
178                 }
179                 switch (d_type)
180                 {
181                 case DT_DIR:
182                         break;
183                 case DT_LNK:
184                 case DT_REG:
185                         usernum = atol(filedir_entry->d_name);
186                         if (CtdlGetUserByNumber(&usbuf, usernum) == 0) {
187                                 import_one_bio_file(usbuf.fullname, usernum, path);
188                         }
189                 }
190         }
191         closedir(filedir);
192         rmdir("bio");
193 }
194
195
196 // Initialization function, called from modules_init.c
197 char *ctdl_module_init_bio(void) {
198         if (!threading) {
199                 import_old_bio_files();
200                 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
201                 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
202         }
203         // return our module name for the log
204         return "bio";
205 }