]>
Commit | Line | Data |
---|---|---|
1 | #include <stdio.h> | |
2 | #include <string.h> | |
3 | #include "../lib/version.h" | |
4 | #include "../core/hooks.h" | |
5 | #include "../core/error.h" | |
6 | #include "../core/nsmalloc.h" | |
7 | #include "../server/server.h" | |
8 | #include "trusts.h" | |
9 | ||
10 | MODULE_VERSION(""); | |
11 | ||
12 | void trusts_registerevents(void); | |
13 | void trusts_deregisterevents(void); | |
14 | ||
15 | static void statusfn(int, void *); | |
16 | static void whoisfn(int, void *); | |
17 | ||
18 | static sstring *tgextnames[MAXTGEXTS]; | |
19 | ||
20 | int trusts_thext, trusts_nextuserext; | |
21 | int trustsdbloaded; | |
22 | ||
23 | void _init(void) { | |
24 | trusts_thext = registernickext("trustth"); | |
25 | if(trusts_thext == -1) { | |
26 | Error("trusts", ERR_ERROR, "Unable to register first nick extension."); | |
27 | return; | |
28 | } | |
29 | ||
30 | trusts_nextuserext = registernickext("trustnext"); | |
31 | if(trusts_thext == -1) { | |
32 | releasenickext(trusts_thext); | |
33 | Error("trusts", ERR_ERROR, "Unable to register second nick extension."); | |
34 | return; | |
35 | } | |
36 | ||
37 | registerhook(HOOK_CORE_STATSREQUEST, statusfn); | |
38 | registerhook(HOOK_CONTROL_WHOISREQUEST, &whoisfn); | |
39 | trusts_registerevents(); | |
40 | } | |
41 | ||
42 | void _fini(void) { | |
43 | if(trusts_thext != -1) { | |
44 | releasenickext(trusts_thext); | |
45 | releasenickext(trusts_nextuserext); | |
46 | } | |
47 | ||
48 | deregisterhook(HOOK_CORE_STATSREQUEST, statusfn); | |
49 | deregisterhook(HOOK_CONTROL_WHOISREQUEST, &whoisfn); | |
50 | trusts_deregisterevents(); | |
51 | ||
52 | nscheckfreeall(POOL_TRUSTS); | |
53 | } | |
54 | ||
55 | static void whoisfn(int hooknum, void *arg) { | |
56 | trusthost *th; | |
57 | char message[512]; | |
58 | nick *np = (nick *)arg; | |
59 | ||
60 | if(!np) | |
61 | return; | |
62 | ||
63 | th = gettrusthost(np); | |
64 | ||
65 | if(!th) | |
66 | return; | |
67 | ||
68 | snprintf(message, sizeof(message), "Trustgroup: %s (#%d)", th->group->name->content, th->group->id); | |
69 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
70 | ||
71 | if (th->maxpernode > 0) { | |
72 | snprintf(message, sizeof(message), "Node : %s", CIDRtostr(np->ipaddress, th->nodebits)); | |
73 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
74 | ||
75 | patricia_node_t *node; | |
76 | int usercount = 0; | |
77 | ||
78 | node = refnode(iptree, &(np->ipaddress), th->nodebits); | |
79 | usercount = node->usercount; | |
80 | derefnode(iptree, node); | |
81 | ||
82 | snprintf(message, sizeof(message), "Node : Usage: %d/%d", usercount, th->maxpernode); | |
83 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
84 | } | |
85 | ||
86 | if (th->group->trustedfor > 0) { | |
87 | snprintf(message, sizeof(message), "Trusthost : %s", CIDRtostr(th->ip, th->bits)); | |
88 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
89 | ||
90 | snprintf(message, sizeof(message), "Trustgroup : Usage: %d/%d", th->group->count, th->group->trustedfor); | |
91 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
92 | } | |
93 | } | |
94 | ||
95 | static void statusfn(int hooknum, void *arg) { | |
96 | if((long)arg > 10) { | |
97 | char message[100]; | |
98 | int groupcount = 0, hostcount = 0, usercount = 0; | |
99 | trustgroup *tg; | |
100 | trusthost *th; | |
101 | ||
102 | for(tg=tglist;tg;tg=tg->next) { | |
103 | usercount+=tg->count; | |
104 | groupcount++; | |
105 | for(th=tg->hosts;th;th=th->next) | |
106 | hostcount++; | |
107 | } | |
108 | ||
109 | snprintf(message, sizeof(message), "Trusts :%7d groups, %7d hosts, %7d users", groupcount, hostcount, usercount); | |
110 | triggerhook(HOOK_CORE_STATSREPLY, message); | |
111 | } | |
112 | } | |
113 | ||
114 | int findtgext(const char *name) { | |
115 | int i; | |
116 | ||
117 | for(i=0;i<MAXTGEXTS;i++) | |
118 | if(tgextnames[i] && !strcmp(name, tgextnames[i]->content)) | |
119 | return i; | |
120 | ||
121 | return -1; | |
122 | } | |
123 | ||
124 | int registertgext(const char *name) { | |
125 | int i; | |
126 | ||
127 | if(findtgext(name) != -1) { | |
128 | Error("trusts", ERR_WARNING, "Tried to register duplicate trust group extension: %s.", name); | |
129 | return -1; | |
130 | } | |
131 | ||
132 | for(i=0;i<MAXTGEXTS;i++) { | |
133 | if(!tgextnames[i]) { | |
134 | tgextnames[i] = getsstring(name, 100); | |
135 | return i; | |
136 | } | |
137 | } | |
138 | ||
139 | Error("trusts", ERR_WARNING, "Tried to register too many trust group extensions: %s.", name); | |
140 | return -1; | |
141 | } | |
142 | ||
143 | void releasetgext(int index) { | |
144 | trustgroup *tg; | |
145 | ||
146 | freesstring(tgextnames[index]); | |
147 | tgextnames[index] = NULL; | |
148 | ||
149 | for(tg=tglist;tg;tg=tg->next) | |
150 | tg->exts[index] = NULL; | |
151 | } | |
152 | ||
153 | int trusts_fullyonline(void) { | |
154 | if(myhub == -1) | |
155 | return 0; | |
156 | ||
157 | return serverlist[myhub].linkstate == LS_LINKED; | |
158 | } | |
159 |