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