43cc0e98c15cc874aa68dbf9e9814bcdcdbf4d33
[citadel.git] / citadel / modules / bio / serv_bio.c
1 /*
2  * $Id$
3  *
4  * This module implementsserver commands related to the display and
5  * manipulation of user "bio" files.
6  *
7  *
8  * Copyright (c) 1987-2009 by the citadel.org team
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 3 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "ctdl_module.h"
26
27
28
29 /*
30  * enter user bio
31  */
32 void cmd_ebio(char *cmdbuf) {
33         char buf[SIZ];
34         FILE *fp;
35
36         unbuffer_output();
37
38         if (!(CC->logged_in)) {
39                 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
40                 return;
41         }
42
43         snprintf(buf, sizeof buf, "%s%ld",ctdl_bio_dir,CC->user.usernum);
44         fp = fopen(buf,"w");
45         if (fp == NULL) {
46                 cprintf("%d Cannot create file: %s\n", ERROR + INTERNAL_ERROR,
47                                 strerror(errno));
48                 return;
49         }
50         cprintf("%d  \n",SEND_LISTING);
51         while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
52                 if (ftell(fp) < config.c_maxmsglen) {
53                         fprintf(fp,"%s\n",buf);
54                 }
55         }
56         fclose(fp);
57 }
58
59 /*
60  * read user bio
61  */
62 void cmd_rbio(char *cmdbuf)
63 {
64         struct ctdluser ruser;
65         char buf[256];
66         FILE *fp;
67
68         extract_token(buf, cmdbuf, 0, '|', sizeof buf);
69         if (CtdlGetUser(&ruser, buf) != 0) {
70                 cprintf("%d No such user.\n",ERROR + NO_SUCH_USER);
71                 return;
72         }
73         snprintf(buf, sizeof buf, "%s%ld",ctdl_bio_dir,ruser.usernum);
74         
75         cprintf("%d OK|%s|%ld|%d|%ld|%ld|%ld\n", LISTING_FOLLOWS,
76                 ruser.fullname, ruser.usernum, ruser.axlevel,
77                 (long)ruser.lastcall, ruser.timescalled, ruser.posted);
78         fp = fopen(buf,"r");
79         if (fp == NULL)
80                 cprintf("%s has no bio on file.\n", ruser.fullname);
81         else {
82                 while (fgets(buf, sizeof buf, fp) != NULL) cprintf("%s",buf);
83                 fclose(fp);
84         }
85         cprintf("000\n");
86 }
87
88 /*
89  * list of users who have entered bios
90  */
91 void cmd_lbio(char *cmdbuf) {
92         char buf[256];
93         FILE *ls;
94         struct ctdluser usbuf;
95         char listbios[256];
96
97         snprintf(listbios, sizeof(listbios),"cd %s; ls",ctdl_bio_dir);
98         ls = popen(listbios, "r");
99         if (ls == NULL) {
100                 cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
101                 return;
102         }
103
104         cprintf("%d\n", LISTING_FOLLOWS);
105         while (fgets(buf, sizeof buf, ls)!=NULL)
106                 if (CtdlGetUserByNumber(&usbuf,atol(buf))==0)
107                         cprintf("%s\n", usbuf.fullname);
108         pclose(ls);
109         cprintf("000\n");
110 }
111
112
113
114
115 CTDL_MODULE_INIT(bio)
116 {
117         if (!threading)
118         {
119                 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
120                 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
121                 CtdlRegisterProtoHook(cmd_lbio, "LBIO", "List users with bios");
122         }
123         /* return our Subversion id for the Log */
124         return "$Id$";
125 }
126
127