Back to index

citadel  8.12
serv_bio.c
Go to the documentation of this file.
00001 /*
00002  * This module implementsserver commands related to the display and
00003  * manipulation of user "bio" files.
00004  *
00005  * Copyright (c) 1987-2012 by the citadel.org team
00006  *
00007  * This program is open source software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  */
00017 
00018 #include "ctdl_module.h"
00019 
00020 #include <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <dirent.h>
00023 
00024 
00025 /*
00026  * enter user bio
00027  */
00028 void cmd_ebio(char *cmdbuf) {
00029        char buf[SIZ];
00030        FILE *fp;
00031 
00032        unbuffer_output();
00033 
00034        if (!(CC->logged_in)) {
00035               cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
00036               return;
00037        }
00038 
00039        snprintf(buf, sizeof buf, "%s%ld",ctdl_bio_dir,CC->user.usernum);
00040        fp = fopen(buf,"w");
00041        if (fp == NULL) {
00042               cprintf("%d Cannot create file: %s\n", ERROR + INTERNAL_ERROR,
00043                             strerror(errno));
00044               return;
00045        }
00046        cprintf("%d  \n",SEND_LISTING);
00047        while(client_getln(buf, sizeof buf) >= 0 && strcmp(buf,"000")) {
00048               if (ftell(fp) < config.c_maxmsglen) {
00049                      fprintf(fp,"%s\n",buf);
00050               }
00051        }
00052        fclose(fp);
00053 }
00054 
00055 /*
00056  * read user bio
00057  */
00058 void cmd_rbio(char *cmdbuf)
00059 {
00060        struct ctdluser ruser;
00061        char buf[256];
00062        FILE *fp;
00063 
00064        extract_token(buf, cmdbuf, 0, '|', sizeof buf);
00065        if (CtdlGetUser(&ruser, buf) != 0) {
00066               cprintf("%d No such user.\n",ERROR + NO_SUCH_USER);
00067               return;
00068        }
00069        snprintf(buf, sizeof buf, "%s%ld",ctdl_bio_dir,ruser.usernum);
00070        
00071        cprintf("%d OK|%s|%ld|%d|%ld|%ld|%ld\n", LISTING_FOLLOWS,
00072               ruser.fullname, ruser.usernum, ruser.axlevel,
00073               (long)ruser.lastcall, ruser.timescalled, ruser.posted);
00074        fp = fopen(buf,"r");
00075        if (fp == NULL)
00076               cprintf("%s has no bio on file.\n", ruser.fullname);
00077        else {
00078               while (fgets(buf, sizeof buf, fp) != NULL) cprintf("%s",buf);
00079               fclose(fp);
00080        }
00081        cprintf("000\n");
00082 }
00083 
00084 /*
00085  * list of users who have entered bios
00086  */
00087 void cmd_lbio(char *cmdbuf)
00088 {
00089        DIR *filedir = NULL;
00090        struct dirent *filedir_entry;
00091        struct dirent *d;
00092        int dont_resolve_uids;
00093        size_t d_namelen;
00094        struct ctdluser usbuf;
00095 
00096        d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 2);
00097        if (d == NULL) {
00098               cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
00099               return;
00100        }
00101 
00102        filedir = opendir (ctdl_bio_dir);
00103        if (filedir == NULL) {
00104               free(d);
00105               cprintf("%d Cannot open listing.\n", ERROR + FILE_NOT_FOUND);
00106               return;
00107        }
00108        dont_resolve_uids = *cmdbuf == '1';
00109         cprintf("%d\n", LISTING_FOLLOWS);
00110        while ((readdir_r(filedir, d, &filedir_entry) == 0) &&
00111               (filedir_entry != NULL))
00112        {
00113 #ifdef _DIRENT_HAVE_D_NAMELEN
00114               d_namelen = filedir_entry->d_namelen;
00115 #else
00116               d_namelen = strlen(filedir_entry->d_name);
00117 #endif
00118               if (((d_namelen == 1) && (filedir_entry->d_name[0] == '.')) || 
00119                   ((d_namelen == 2) && (filedir_entry->d_name[0] == '.') && (filedir_entry->d_name[1] == '.')))
00120                      continue;
00121                   
00122               if (dont_resolve_uids) {
00123                      filedir_entry->d_name[d_namelen++] = '\n';
00124                      filedir_entry->d_name[d_namelen] = '\0';
00125                      client_write(filedir_entry->d_name, d_namelen);
00126               }
00127               else if (CtdlGetUserByNumber(&usbuf,atol(filedir_entry->d_name))==0)
00128                      cprintf("%s\n", usbuf.fullname);
00129        }
00130        free(d);
00131        closedir(filedir);
00132        cprintf("000\n");
00133 
00134 }
00135 
00136 
00137 
00138 CTDL_MODULE_INIT(bio)
00139 {
00140        if (!threading)
00141        {
00142                CtdlRegisterProtoHook(cmd_ebio, "EBIO", "Enter your bio");
00143               CtdlRegisterProtoHook(cmd_rbio, "RBIO", "Read a user's bio");
00144                CtdlRegisterProtoHook(cmd_lbio, "LBIO", "List users with bios");
00145        }
00146        /* return our module name for the log */
00147         return "bio";
00148 }
00149 
00150