remove duplicated declarations
[citadel.git] / citadel / whobbs.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "citadel.h"
6 #include "ipc.h"
7
8 /*
9  * num_parms()  -  discover number of parameters...
10  */
11 int num_parms(char *source)
12 {
13         int a;
14         int count = 1;
15
16         for (a=0; a<strlen(source); ++a) 
17                 if (source[a]=='|') ++count;
18         return(count);
19         }
20
21
22 /*
23  * extract()  -  extract a parameter from a series of "|" separated...
24  */
25 void extract(char *dest, char *source, int parmnum)
26 {
27         char buf[256];
28         int count = 0;
29         int n;
30
31         n = num_parms(source);
32
33         if (parmnum >= n) {
34                 strcpy(dest,"");
35                 return;
36                 }
37         strcpy(buf,source);
38         if ( (parmnum == 0) && (n == 1) ) {
39                 strcpy(dest,buf);
40                 return;
41                 }
42
43         while (count++ < parmnum) do {
44                 strcpy(buf,&buf[1]);
45                 } while( (strlen(buf)>0) && (buf[0]!='|') );
46         if (buf[0]=='|') strcpy(buf,&buf[1]);
47         for (count = 0; count<strlen(buf); ++count)
48                 if (buf[count] == '|') buf[count] = 0;
49         strcpy(dest,buf);
50         }
51
52 /*
53  * extract_int()  -  extract an int parm w/o supplying a buffer
54  */
55 int extract_int(char *source, int parmnum)
56 {
57         char buf[256];
58         
59         extract(buf,source,parmnum);
60         return(atoi(buf));
61         }
62
63
64 void logoff(int code)
65 {
66         exit(code);
67         }
68
69 void main(int argc, char **argv)
70 {
71         char buf[256];
72         char nodetitle[256];
73         int a;
74         int s_pid = 0;
75         int my_pid = 0;
76         char s_user[256];
77         char s_room[256];
78         char s_host[256];
79
80         attach_to_server(argc,argv);
81         serv_gets(buf);
82         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
83                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
84                 logoff(atoi(buf));
85                 }
86         strcpy(nodetitle, "this BBS");
87         serv_puts("INFO");
88         serv_gets(buf);
89         if (buf[0]=='1') {
90                 a = 0;
91                 while (serv_gets(buf), strcmp(buf,"000")) {
92                         if (a==0) my_pid = atoi(buf);
93                         if (a==2) strcpy(nodetitle, buf);
94                         ++a;
95                         }
96                 }
97         printf("            Users currently logged on to %s\n", nodetitle);
98         serv_puts("RWHO");
99         serv_gets(buf);
100         if (buf[0]!='1') {
101                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
102                 logoff(atoi(buf));
103                 }
104
105         printf("Session         User name               Room                  From host\n");
106         printf("------- ------------------------- -------------------- ------------------------\n");
107         while (serv_gets(buf), strcmp(buf,"000")) {
108                 s_pid = extract_int(buf,0);
109                 extract(s_user,buf,1);
110                 extract(s_room,buf,2);
111                 extract(s_host,buf,3);
112                 if (s_pid != my_pid) {
113                         printf("%-7d%c%-25s %-20s %-24s\n",
114                                 s_pid,
115                                 ((s_pid == my_pid) ? '*' : ' '),
116                                 s_user,s_room,s_host);
117                         }
118                 }
119
120         serv_puts("QUIT");
121         serv_gets(buf);
122         exit(0);
123         }
124
125
126 #ifdef NO_STRERROR
127 /*
128  * replacement strerror() for systems that don't have it
129  */
130 char *strerror(int e)
131 {
132         static char buf[32];
133
134         sprintf(buf,"errno = %d",e);
135         return(buf);
136         }
137 #endif