]>
Commit | Line | Data |
---|---|---|
b7eccf5f P |
1 | /* Some utterly useless dog */ |
2 | ||
3 | #include "../core/schedule.h" | |
4 | #include "../lib/irc_string.h" | |
5 | #include "../localuser/localuserchannel.h" | |
6 | #include "../control/control.h" | |
074a4529 | 7 | #include "../usercount/usercount.h" |
b7eccf5f P |
8 | |
9 | #include <stdlib.h> | |
10 | #include <string.h> | |
11 | #include <stdarg.h> | |
12 | ||
13 | int cmd_serverlist(void *sender, int cargc, char **cargv); | |
14 | void serverlist_hook_newserver(int hook, void *arg); | |
15 | void serverlist_hook_lostserver(int hook, void *arg); | |
16 | int serverlist_versionreply(void *source, int cargc, char **cargv); | |
17 | ||
18 | struct { | |
19 | int used; | |
20 | time_t ts; | |
21 | sstring *version1; | |
22 | sstring *version2; | |
23 | } serverinfo[MAXSERVERS]; | |
24 | ||
25 | void serverlist_doversion(void); | |
26 | ||
27 | ||
28 | void _init(void) { | |
29 | registercontrolhelpcmd("serverlist",NO_OPER,1,&cmd_serverlist,"Usage: serverlist [pattern]\nShows all currently connected servers"); | |
30 | /* hooks for serverlist */ | |
31 | registerhook(HOOK_SERVER_NEWSERVER, &serverlist_hook_newserver); | |
32 | registerhook(HOOK_SERVER_LOSTSERVER, &serverlist_hook_lostserver); | |
33 | int i; | |
34 | ||
35 | for (i = 0; i < MAXSERVERS; i++) { | |
36 | if (serverlist[i].linkstate == LS_LINKED) | |
37 | serverinfo[i].used = 1; | |
38 | else | |
39 | serverinfo[i].used = 0; | |
40 | ||
41 | serverinfo[i].ts = getnettime(); | |
42 | serverinfo[i].version1 = NULL; | |
43 | serverinfo[i].version2 = NULL; | |
44 | } | |
45 | registernumerichandler(351, &serverlist_versionreply, 2); | |
46 | ||
47 | } | |
48 | ||
49 | void _fini(void) { | |
50 | int i; | |
51 | for (i = 0; i < MAXSERVERS; i++) { | |
52 | if (serverinfo[i].used) { | |
53 | freesstring(serverinfo[i].version1); | |
54 | freesstring(serverinfo[i].version2); | |
55 | } | |
56 | } | |
57 | deregisternumerichandler(351, &serverlist_versionreply); | |
58 | ||
59 | deregisterhook(HOOK_SERVER_NEWSERVER, &serverlist_hook_newserver); | |
60 | deregisterhook(HOOK_SERVER_LOSTSERVER, &serverlist_hook_lostserver); | |
61 | ||
62 | deregistercontrolcmd("serverlist",cmd_serverlist); | |
63 | } | |
64 | ||
65 | int cmd_serverlist(void *sender, int cargc, char **cargv) { | |
66 | nick *np = (nick*)sender; | |
67 | int a, i, ucount, acount, scount; | |
68 | ||
074a4529 | 69 | controlreply(np, "%-7s %-30s %5s/%5s/%-5s %-15s %-20s", "Numeric", "Hostname", "EClients", "Clients", "MaxCl", "Connected for", "Version"); |
b7eccf5f P |
70 | |
71 | scount = acount = 0; | |
72 | ||
73 | for (i = 0; i < MAXSERVERS; i++) { | |
74 | if (serverlist[i].linkstate == LS_LINKED && (cargc < 1 || match2strings(cargv[0], serverlist[i].name->content))) { | |
75 | ucount = 0; | |
76 | ||
77 | for (a = 0; a < serverlist[i].maxusernum; a++) | |
78 | if (servernicks[i][a] != NULL) | |
79 | ucount++; | |
80 | ||
81 | acount += ucount; | |
82 | scount++; | |
83 | ||
074a4529 CP |
84 | controlreply(np, "%-7d %-30s %5d/%5d/%-5d %-15s %-20s - %s", i, serverlist[i].name->content, |
85 | servercount[i], ucount, serverlist[i].maxusernum, longtoduration(getnettime() - serverinfo[i].ts, 0), | |
b7eccf5f P |
86 | serverinfo[i].version1 ? serverinfo[i].version1->content : "Unknown", |
87 | serverinfo[i].version2 ? serverinfo[i].version2->content : "Unknown"); | |
88 | } | |
89 | } | |
90 | ||
91 | controlreply(np, "--- End of list. %d users and %d servers on the net.", acount, scount); | |
92 | ||
93 | /* update version info for next time */ | |
94 | serverlist_doversion(); | |
95 | ||
96 | return CMD_OK; | |
97 | } | |
98 | ||
99 | int serverlist_versionreply(void *source, int cargc, char **cargv) { | |
100 | int num; | |
101 | ||
102 | if (cargc < 6) | |
103 | return CMD_OK; | |
104 | ||
105 | num = numerictolong(cargv[0], 2); | |
106 | ||
107 | if (serverinfo[num].used) { | |
108 | freesstring(serverinfo[num].version1); | |
109 | freesstring(serverinfo[num].version2); | |
110 | ||
111 | serverinfo[num].version1 = getsstring(cargv[3], strlen(cargv[3])); | |
112 | serverinfo[num].version2 = getsstring(cargv[5], strlen(cargv[5])); | |
113 | } | |
114 | return CMD_OK; | |
115 | } | |
116 | ||
117 | void serverlist_doversion(void) { | |
118 | int i; | |
119 | char *num1, *numeric; | |
120 | ||
121 | if (mynick == NULL) | |
122 | return; | |
123 | ||
124 | for (i = 0; i < MAXSERVERS; i++) { | |
125 | if (serverlist[i].linkstate == LS_LINKED && serverinfo[i].version1 == NULL) { | |
126 | numeric = longtonumeric(mynick->numeric,5); | |
127 | num1 = (char*)malloc(strlen(numeric) + 1); /* bleh.. longtonumeric() is using static variables */ | |
128 | strcpy(num1, numeric); | |
129 | ||
130 | irc_send("%s V :%s", num1, longtonumeric(i,2)); | |
131 | free(num1); | |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | void serverlist_hook_newserver(int hook, void *arg) { | |
137 | char *num1, *numeric; | |
074a4529 | 138 | long num = (long)arg; |
b7eccf5f P |
139 | |
140 | if (mynick == NULL) | |
141 | return; | |
142 | ||
143 | serverinfo[num].used = 1; | |
144 | serverinfo[num].ts = getnettime(); | |
145 | serverinfo[num].version1 = NULL; | |
146 | serverinfo[num].version2 = NULL; | |
147 | ||
148 | numeric = longtonumeric(mynick->numeric,5); | |
149 | num1 = (char*)malloc(strlen(numeric) + 1); /* bleh.. longtonumeric() is using static variables */ | |
150 | strcpy(num1, numeric); | |
151 | ||
152 | irc_send("%s V :%s", num1, longtonumeric(num,2)); | |
153 | ||
154 | free(num1); | |
155 | } | |
156 | ||
157 | void serverlist_hook_lostserver(int hook, void *arg) { | |
074a4529 | 158 | long num = (long)arg; |
b7eccf5f P |
159 | |
160 | serverinfo[num].used = 0; | |
161 | freesstring(serverinfo[num].version1); | |
162 | freesstring(serverinfo[num].version2); | |
163 | } | |
164 |