56e0d482488f23c28f8e5d3467cbb49f33de952f
[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
96         d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
97         if (d == NULL) {
98                 cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
99                 return;
100         }
101
102         filedir = opendir (ctdl_bio_dir);
103         if (filedir == NULL) {
104                 free(d);
105                 cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
106                 return;
107         }
108         dont_resolve_uids = *cmdbuf == '1';
109         cprintf("%d\n", LISTING_FOLLOWS);
110         while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
111                (filedir_entry != NULL))
112         {
113 #ifdef _DIRENT_HAVE_D_NAMELEN
114                 d_namelen = filedir_entry->d_namelen;
115 #else
116                 d_namelen = strlen(filedir_entry->d_name);
117 #endif
118                 if (((d_namelen == 1) && (filedir_entry->d_name[0] == '.')) || 
119                     ((d_namelen == 2) && (filedir_entry->d_name[0] == '.') && (filedir_entry->d_name[1] == '.')))
120                         continue;
121                     
122                 if (dont_resolve_uids) {
123                         filedir_entry->d_name[d_namelen++] = '\n';
124                         filedir_entry->d_name[d_namelen] = '\0';
125                         client_write(filedir_entry->d_name, d_namelen);
126                 }
127                 else if (CtdlGetUserByNumber(&usbuf,atol(filedir_entry->d_name))==0)
128                         cprintf("%s\n", usbuf.fullname);
129         }
130         free(d);
131         closedir(filedir);
132         cprintf("000\n");
133
134 }
135
136
137
138 CTDL_MODULE_INIT(bio)
139 {
140         if (!threading)
141         {
142                 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
143                 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
144                 CtdlRegisterProtoHook(cmd_lbio, "LBIO", "List users with bios");
145         }
146         /* return our module name for the log */
147         return "bio";
148 }
149
150