041485d933f0afb5767d73399cd3a34e340b6908
[citadel.git] / citadel / modules / bio / serv_bio.c
1 /*
2  * This module implementsserver commands related to the display and
3  * manipulation of user "bio" files.
4  *
5  * Copyright (c) 1987-2012 by the citadel.org team
6  *
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.
11  *
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.
16  */
17
18 #include "ctdl_module.h"
19
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <dirent.h>
23
24
25 /*
26  * enter user bio
27  */
28 void cmd_ebio(char *cmdbuf) {
29         char buf[SIZ];
30         FILE *fp;
31
32         unbuffer_output();
33
34         if (!(CC->logged_in)) {
35                 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
36                 return;
37         }
38
39         snprintf(buf, sizeof buf, "%s%ld",ctdl_bio_dir,CC->user.usernum);
40         fp = fopen(buf,"w");
41         if (fp == NULL) {
42                 cprintf("%d Cannot create file: %s\n", ERROR + INTERNAL_ERROR,
43                                 strerror(errno));
44                 return;
45         }
46         cprintf("%d  \n",SEND_LISTING);
47         while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
48                 if (ftell(fp) < config.c_maxmsglen) {
49                         fprintf(fp,"%s\n",buf);
50                 }
51         }
52         fclose(fp);
53 }
54
55 /*
56  * read user bio
57  */
58 void cmd_rbio(char *cmdbuf)
59 {
60         struct ctdluser ruser;
61         char buf[256];
62         FILE *fp;
63
64         extract_token(buf, cmdbuf, 0, '|', sizeof buf);
65         if (CtdlGetUser(&ruser, buf) != 0) {
66                 cprintf("%d No such user.\n",ERROR + NO_SUCH_USER);
67                 return;
68         }
69         snprintf(buf, sizeof buf, "%s%ld",ctdl_bio_dir,ruser.usernum);
70         
71         cprintf("%d OK|%s|%ld|%d|%ld|%ld|%ld\n", LISTING_FOLLOWS,
72                 ruser.fullname, ruser.usernum, ruser.axlevel,
73                 (long)ruser.lastcall, ruser.timescalled, ruser.posted);
74         fp = fopen(buf,"r");
75         if (fp == NULL)
76                 cprintf("%s has no bio on file.\n", ruser.fullname);
77         else {
78                 while (fgets(buf, sizeof buf, fp) != NULL) cprintf("%s",buf);
79                 fclose(fp);
80         }
81         cprintf("000\n");
82 }
83
84 /*
85  * list of users who have entered bios
86  */
87 void cmd_lbio(char *cmdbuf)
88 {
89         DIR *filedir = NULL;
90         struct dirent *filedir_entry;
91         struct dirent *d;
92         int dont_resolve_uids;
93         size_t d_namelen;
94         struct ctdluser usbuf;
95         int d_type = 0;
96
97
98         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
99         if (d == NULL) {
100                 cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
101                 return;
102         }
103
104         filedir = opendir (ctdl_bio_dir);
105         if (filedir == NULL) {
106                 free(d);
107                 cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
108                 return;
109         }
110         dont_resolve_uids = *cmdbuf == '1';
111         cprintf("%d\n", LISTING_FOLLOWS);
112         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
113                (filedir_entry != NULL))
114         {
115 #ifdef _DIRENT_HAVE_D_NAMLEN
116                 d_namelen = filedir_entry->d_namelen;
117
118 #else
119                 d_namelen = strlen(filedir_entry->d_name);
120 #endif
121
122 #ifdef _DIRENT_HAVE_D_TYPE
123                 d_type = filedir_entry->d_type;
124 #else
125
126 #ifndef DT_UNKNOWN
127 #define DT_UNKNOWN     0
128 #define DT_DIR         4
129 #define DT_REG         8
130 #define DT_LNK         10
131
132 #define IFTODT(mode)   (((mode) & 0170000) >> 12)
133 #define DTTOIF(dirtype)        ((dirtype) << 12)
134 #endif
135                 d_type = DT_UNKNOWN;
136 #endif
137                 if ((d_namelen == 1) && 
138                     (filedir_entry->d_name[0] == '.'))
139                         continue;
140
141                 if ((d_namelen == 2) && 
142                     (filedir_entry->d_name[0] == '.') &&
143                     (filedir_entry->d_name[1] == '.'))
144                         continue;
145
146                 if (d_type == DT_UNKNOWN) {
147                         struct stat s;
148                         char path[PATH_MAX];
149                         snprintf(path, PATH_MAX, "%s/%s", 
150                                  ctdl_bio_dir, filedir_entry->d_name);
151                         if (lstat(path, &s) == 0) {
152                                 d_type = IFTODT(s.st_mode);
153                         }
154                 }
155                 switch (d_type)
156                 {
157                 case DT_DIR:
158                         break;
159                 case DT_LNK:
160                 case DT_REG:
161                         if (dont_resolve_uids) {
162                                 filedir_entry->d_name[d_namelen++] = '\n';
163                                 filedir_entry->d_name[d_namelen] = '\0';
164                                 client_write(filedir_entry->d_name, d_namelen);
165                         }
166                         else if (CtdlGetUserByNumber(&usbuf,atol(filedir_entry->d_name))==0)
167                                 cprintf("%s\n", usbuf.fullname);
168                 }
169         }
170         free(d);
171         closedir(filedir);
172         cprintf("000\n");
173
174 }
175
176
177
178 CTDL_MODULE_INIT(bio)
179 {
180         if (!threading)
181         {
182                 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
183                 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
184                 CtdlRegisterProtoHook(cmd_lbio, "LBIO", "List users with bios");
185         }
186         /* return our module name for the log */
187         return "bio";
188 }
189
190