afebe1242b4f5665f3c1b247d179c435cb9a2162
[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 "sysdep.h"
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <pwd.h>
32 #include <errno.h>
33 #include <sys/types.h>
34
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 #  include <sys/time.h>
41 # else
42 #  include <time.h>
43 # endif
44 #endif
45
46 #include <sys/wait.h>
47 #include <string.h>
48 #include <limits.h>
49 #include <libcitadel.h>
50 #include "citadel.h"
51 #include "server.h"
52 #include "citserver.h"
53 #include "support.h"
54 #include "config.h"
55 #include "control.h"
56 #include "user_ops.h"
57 #include "policy.h"
58 #include "database.h"
59 #include "msgbase.h"
60 #include "citadel_dirs.h"
61
62 #include "ctdl_module.h"
63
64 /*
65  * enter user bio
66  */
67 void cmd_ebio(char *cmdbuf) {
68         char buf[SIZ];
69         FILE *fp;
70
71         unbuffer_output();
72
73         if (!(CC->logged_in)) {
74                 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
75                 return;
76         }
77
78         snprintf(buf, sizeof buf, "%s%ld",ctdl_bio_dir,CC->user.usernum);
79         fp = fopen(buf,"w");
80         if (fp == NULL) {
81                 cprintf("%d Cannot create file: %s\n", ERROR + INTERNAL_ERROR,
82                                 strerror(errno));
83                 return;
84         }
85         cprintf("%d  \n",SEND_LISTING);
86         while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
87                 if (ftell(fp) < config.c_maxmsglen) {
88                         fprintf(fp,"%s\n",buf);
89                 }
90         }
91         fclose(fp);
92 }
93
94 /*
95  * read user bio
96  */
97 void cmd_rbio(char *cmdbuf)
98 {
99         struct ctdluser ruser;
100         char buf[256];
101         FILE *fp;
102
103         extract_token(buf, cmdbuf, 0, '|', sizeof buf);
104         if (getuser(&ruser, buf) != 0) {
105                 cprintf("%d No such user.\n",ERROR + NO_SUCH_USER);
106                 return;
107         }
108         snprintf(buf, sizeof buf, "%s%ld",ctdl_bio_dir,ruser.usernum);
109         
110         cprintf("%d OK|%s|%ld|%d|%ld|%ld|%ld\n", LISTING_FOLLOWS,
111                 ruser.fullname, ruser.usernum, ruser.axlevel,
112                 (long)ruser.lastcall, ruser.timescalled, ruser.posted);
113         fp = fopen(buf,"r");
114         if (fp == NULL)
115                 cprintf("%s has no bio on file.\n", ruser.fullname);
116         else {
117                 while (fgets(buf, sizeof buf, fp) != NULL) cprintf("%s",buf);
118                 fclose(fp);
119         }
120         cprintf("000\n");
121 }
122
123 /*
124  * list of users who have entered bios
125  */
126 void cmd_lbio(char *cmdbuf) {
127         char buf[256];
128         FILE *ls;
129         struct ctdluser usbuf;
130         char listbios[256];
131
132         snprintf(listbios, sizeof(listbios),"cd %s; ls",ctdl_bio_dir);
133         ls = popen(listbios, "r");
134         if (ls == NULL) {
135                 cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
136                 return;
137         }
138
139         cprintf("%d\n", LISTING_FOLLOWS);
140         while (fgets(buf, sizeof buf, ls)!=NULL)
141                 if (getuserbynumber(&usbuf,atol(buf))==0)
142                         cprintf("%s\n", usbuf.fullname);
143         pclose(ls);
144         cprintf("000\n");
145 }
146
147
148
149
150 CTDL_MODULE_INIT(bio)
151 {
152         if (!threading)
153         {
154                 CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
155                 CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
156                 CtdlRegisterProtoHook(cmd_lbio, "LBIO", "List users with bios");
157         }
158         /* return our Subversion id for the Log */
159         return "$Id$";
160 }
161
162