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