]> jfr.im git - irc/quakenet/newserv.git/blob - localuser/localuserstats.c
TRUSTS: require sqlite
[irc/quakenet/newserv.git] / localuser / localuserstats.c
1 /* Functions for retrieving stats from the network to local users */
2
3 #include "localuser.h"
4 #include "../nick/nick.h"
5 #include "../irc/irc.h"
6 #include "../core/error.h"
7 #include "../lib/version.h"
8 #include "../lib/stringbuf.h"
9
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <assert.h>
13 #include <string.h>
14
15 MODULE_VERSION("")
16
17 int handleserverstats(void *source, int cargc, char **cargv);
18 int handleserverstatsend(void *source, int cargc, char **cargv);
19
20 #define RPL_STATSLINKINFO 211
21 #define RPL_STATSCOMMANDS 212
22 #define RPL_STATSCLINE 213
23 #define RPL_STATSNLINE 214 /* unused */
24 #define RPL_STATSILINE 215
25 #define RPL_STATSKLINE 216
26 #define RPL_STATSPLINE 217 /* Undernet extension */
27 #define RPL_STATSYLINE 218
28 #define RPL_STATSJLINE 222 /* Undernet extension */
29 #define RPL_STATSALINE 226 /* Hybrid, Undernet */
30 #define RPL_STATSQLINE 228 /* Undernet extension */
31 #define RPL_STATSVERBOSE 236 /* Undernet verbose server list */
32 #define RPL_STATSENGINE 237 /* Undernet engine name */
33 #define RPL_STATSFLINE 238 /* Undernet feature lines */
34 #define RPL_STATSLLINE 241 /* Undernet dynamicly loaded modules */
35 #define RPL_STATSUPTIME 242
36 #define RPL_STATSOLINE 243
37 #define RPL_STATSHLINE 244
38 #define RPL_STATSTLINE 246 /* Undernet extension */
39 #define RPL_STATSGLINE 247 /* Undernet extension */
40 #define RPL_STATSULINE 248 /* Undernet extension */
41 #define RPL_STATSDEBUG 249 /* Extension to RFC1459 */
42 #define RPL_STATSCONN 250 /* Undernet extension */
43 #define RPL_STATSDLINE 275 /* Undernet extension */
44 #define RPL_STATSRLINE 276 /* Undernet extension */
45 #define RPL_STATSSLINE 398 /* QuakeNet extension -froo */
46
47 #define RPL_ENDOFSTATS 219
48
49 const int numerics[] = { RPL_STATSLINKINFO, RPL_STATSCOMMANDS, RPL_STATSCLINE, RPL_STATSNLINE, RPL_STATSILINE, RPL_STATSKLINE,
50 RPL_STATSPLINE, RPL_STATSYLINE, RPL_STATSJLINE, RPL_STATSALINE, RPL_STATSQLINE, RPL_STATSVERBOSE,
51 RPL_STATSENGINE, RPL_STATSFLINE, RPL_STATSLLINE, RPL_STATSUPTIME, RPL_STATSOLINE, RPL_STATSHLINE,
52 RPL_STATSTLINE, RPL_STATSGLINE, RPL_STATSULINE, RPL_STATSDEBUG, RPL_STATSCONN, RPL_STATSDLINE, RPL_STATSRLINE,
53 RPL_STATSSLINE, 0 };
54
55 void _init() {
56 const int *i;
57 registernumerichandler(RPL_ENDOFSTATS,&handleserverstats,4);
58
59 for(i=&numerics[0];*i;i++)
60 registernumerichandler(*i,&handleserverstats,4);
61 }
62
63 void _fini() {
64 const int *i;
65 deregisternumerichandler(RPL_ENDOFSTATS,&handleserverstats);
66
67 for(i=&numerics[0];*i;i++)
68 deregisternumerichandler(*i,&handleserverstats);
69 }
70
71 /* stats look something like:
72 * XX 242 XXyyy :data
73 */
74
75 int handleserverstats(void *source, int cargc, char **cargv) {
76 void *nargs[3];
77 nick *target;
78 char outbuffer[BUFSIZE * 2 + 5];
79 long numeric = (long)source, i;
80 StringBuf buf;
81
82 if (cargc<4) {
83 return CMD_OK;
84 }
85
86 if (!(target=getnickbynumeric(numerictolong(cargv[2],5)))) {
87 Error("localuserchannel",ERR_WARNING,"Got stats for unknown local user %s.",cargv[2]);
88 return CMD_OK;
89 }
90
91 if (homeserver(target->numeric) != mylongnum) {
92 Error("localuserchannel",ERR_WARNING,"Got stats for non-local user %s.",target->nick);
93 return CMD_OK;
94 }
95
96 sbinit(&buf, outbuffer, sizeof(outbuffer));
97
98 /* bloody inefficient */
99 for(i=3;i<cargc;i++) {
100 if(i != 3)
101 sbaddchar(&buf, ' ');
102 if(i == cargc - 1)
103 sbaddchar(&buf, ':');
104
105 sbaddstr(&buf, cargv[i]);
106 }
107 sbterminate(&buf);
108
109 if(numeric != RPL_ENDOFSTATS) {
110 nargs[0]=(void *)cargv[0];
111 nargs[1]=(void *)numeric;
112 nargs[2]=(void *)outbuffer;
113
114 if (umhandlers[target->numeric&MAXLOCALUSER]) {
115 (umhandlers[target->numeric&MAXLOCALUSER])(target, LU_STATS, nargs);
116 }
117 } else {
118 if (umhandlers[target->numeric&MAXLOCALUSER]) {
119 (umhandlers[target->numeric&MAXLOCALUSER])(target, LU_STATS_END, (void *)outbuffer);
120 }
121 }
122
123 return CMD_OK;
124 }
125