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