3f78cea6fb0e03b61aa2c852984de2864838a2d8
[citadel.git] / citadel / utils / userlist.c
1 /*
2  * $Id$
3  *
4  * Command-line user list utility.
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #if TIME_WITH_SYS_TIME
14 # include <sys/time.h>
15 # include <time.h>
16 #else
17 # if HAVE_SYS_TIME_H
18 #  include <sys/time.h>
19 # else
20 #  include <time.h>
21 # endif
22 #endif
23
24 #include <libcitadel.h>
25 #include "citadel.h"
26 #include <unistd.h>
27 #include "citadel_ipc.h"
28 #include "citadel_dirs.h"
29
30 void logoff(int code)
31 {
32         exit(code);
33 }
34
35 void userlist(CtdlIPC *ipc) { 
36         char buf[SIZ];
37         char fl[SIZ];
38         struct tm tmbuf;
39         time_t lc;
40         char *listing = NULL;
41         int r;
42
43         r = CtdlIPCUserListing(ipc, "", &listing, buf);
44         if (r / 100 != 1) {
45                 printf("%s\n", buf);
46                 return;
47         }
48         printf("       User Name           Num  L Last Visit Logins Messages\n");
49         printf("------------------------- ----- - ---------- ------ --------\n");
50         while (strlen(listing) > 0) {
51                 extract_token(buf, listing, 0, '\n', sizeof buf);
52                 remove_token(listing, 0, '\n');
53                 extract_token(fl, buf, 0, '|', sizeof fl);
54                 printf("%-25s ",fl);
55                 printf("%5ld %d ", extract_long(buf,2),
56                         extract_int(buf,1));
57                 lc = extract_long(buf,3);
58                 localtime_r(&lc, &tmbuf);
59                 printf("%02d/%02d/%04d ",
60                         (tmbuf.tm_mon+1),
61                         tmbuf.tm_mday,
62                         (tmbuf.tm_year + 1900));
63                 printf("%6ld %8ld\n",
64                         extract_long(buf,4),extract_long(buf,5));
65         }
66         printf("\n");
67 }
68
69
70 int main(int argc, char **argv)
71 {
72         char buf[SIZ];
73         char hostbuf[SIZ], portbuf[SIZ];
74         CtdlIPC *ipc = NULL;
75         int relh=0;
76         int home=0;
77         char relhome[PATH_MAX]="";
78         char ctdldir[PATH_MAX]=CTDLDIR;
79
80         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
81
82         ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
83         CtdlIPC_chat_recv(ipc, buf);
84         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
85                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
86                 logoff(atoi(buf));
87         }
88
89         userlist(ipc);
90
91         CtdlIPCQuit(ipc);
92         exit(0);
93 }
94
95
96 #ifndef HAVE_STRERROR
97 /*
98  * replacement strerror() for systems that don't have it
99  */
100 char *strerror(int e)
101 {
102         static char buf[32];
103
104         snprintf(buf, sizeof buf, "errno = %d",e);
105         return(buf);
106 }
107 #endif
108
109
110 /*
111  * Stub function
112  */
113 void stty_ctdl(int cmd) {
114 }
115