* extract_token() now expects to be supplied with the size of the
[citadel.git] / citadel / 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 "citadel.h"
25 #include <unistd.h>
26 #include "citadel_ipc.h"
27 #include "tools.h"
28
29 void logoff(int code)
30 {
31         exit(code);
32 }
33
34 void userlist(CtdlIPC *ipc) { 
35         char buf[SIZ];
36         char fl[SIZ];
37         struct tm tmbuf;
38         time_t lc;
39         char *listing = NULL;
40         int r;
41
42         r = CtdlIPCUserListing(ipc, &listing, buf);
43         if (r / 100 != 1) {
44                 printf("%s\n", buf);
45                 return;
46         }
47         printf("       User Name           Num  L  LastCall  Calls Posts\n");
48         printf("------------------------- ----- - ---------- ----- -----\n");
49         while (strlen(listing) > 0) {
50                 extract_token(buf, listing, 0, '\n', sizeof buf);
51                 remove_token(listing, 0, '\n');
52                 extract_token(fl, buf, 0, '|', sizeof fl);
53                 printf("%-25s ",fl);
54                 printf("%5ld %d ", extract_long(buf,2),
55                         extract_int(buf,1));
56                 lc = extract_long(buf,3);
57                 localtime_r(&lc, &tmbuf);
58                 printf("%02d/%02d/%04d ",
59                         (tmbuf.tm_mon+1),
60                         tmbuf.tm_mday,
61                         (tmbuf.tm_year + 1900));
62                 printf("%5ld %5ld\n",
63                         extract_long(buf,4),extract_long(buf,5));
64         }
65         printf("\n");
66 }
67
68
69 int main(int argc, char **argv)
70 {
71         char buf[SIZ];
72         char hostbuf[SIZ], portbuf[SIZ];
73         CtdlIPC *ipc = NULL;
74
75         ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
76         CtdlIPC_chat_recv(ipc, buf);
77         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
78                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
79                 logoff(atoi(buf));
80         }
81
82         userlist(ipc);
83
84         CtdlIPCQuit(ipc);
85         exit(0);
86 }
87
88
89 #ifndef HAVE_STRERROR
90 /*
91  * replacement strerror() for systems that don't have it
92  */
93 char *strerror(int e)
94 {
95         static char buf[32];
96
97         snprintf(buf, sizeof buf, "errno = %d",e);
98         return(buf);
99 }
100 #endif