]>
Commit | Line | Data |
---|---|---|
2d4ba67d | 1 | #include <stdio.h> |
2129448c | 2 | #include <string.h> |
c4610da5 | 3 | #include "../lib/version.h" |
1bbe1ac3 CP |
4 | #include "../core/hooks.h" |
5 | #include "../core/error.h" | |
45989eab | 6 | #include "../core/nsmalloc.h" |
82a316e7 | 7 | #include "../server/server.h" |
1bbe1ac3 | 8 | #include "trusts.h" |
2b6e02e2 | 9 | |
c4610da5 GB |
10 | MODULE_VERSION(""); |
11 | ||
1bbe1ac3 CP |
12 | void trusts_registerevents(void); |
13 | void trusts_deregisterevents(void); | |
14 | ||
2d4ba67d | 15 | static void statusfn(int, void *); |
1d08cad3 | 16 | static void whoisfn(int, void *); |
2b6e02e2 | 17 | |
2129448c | 18 | static sstring *tgextnames[MAXTGEXTS]; |
1bbe1ac3 CP |
19 | |
20 | int trusts_thext, trusts_nextuserext; | |
b9c52ee0 | 21 | int trustsdbloaded; |
1bbe1ac3 | 22 | |
2b6e02e2 | 23 | void _init(void) { |
1bbe1ac3 CP |
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 | ||
2d4ba67d | 37 | registerhook(HOOK_CORE_STATSREQUEST, statusfn); |
1d08cad3 | 38 | registerhook(HOOK_CONTROL_WHOISREQUEST, &whoisfn); |
1bbe1ac3 | 39 | trusts_registerevents(); |
2b6e02e2 CP |
40 | } |
41 | ||
83bccee3 | 42 | void _fini(void) { |
1bbe1ac3 CP |
43 | if(trusts_thext != -1) { |
44 | releasenickext(trusts_thext); | |
45 | releasenickext(trusts_nextuserext); | |
46 | } | |
47 | ||
b9c52ee0 | 48 | deregisterhook(HOOK_CORE_STATSREQUEST, statusfn); |
1d08cad3 | 49 | deregisterhook(HOOK_CONTROL_WHOISREQUEST, &whoisfn); |
b9c52ee0 | 50 | trusts_deregisterevents(); |
2d4ba67d | 51 | |
45989eab | 52 | nscheckfreeall(POOL_TRUSTS); |
45e8ce62 CP |
53 | } |
54 | ||
1d08cad3 P |
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); | |
77795ca2 GB |
70 | |
71 | if (th->maxpernode > 0) { | |
72 | snprintf(message, sizeof(message), "Node : %s", CIDRtostr(np->p_ipaddr, th->nodebits)); | |
1d08cad3 P |
73 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); |
74 | ||
75 | patricia_node_t *node; | |
76 | int usercount = 0; | |
77 | ||
78 | node = refnode(iptree, &(np->p_ipaddr), th->nodebits); | |
79 | usercount = node->usercount; | |
80 | derefnode(iptree, node); | |
81 | ||
77795ca2 | 82 | snprintf(message, sizeof(message), "Node : Usage: %d/%d", usercount, th->maxpernode); |
1d08cad3 | 83 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); |
77795ca2 GB |
84 | } |
85 | ||
86 | if (th->group->trustedfor > 0) { | |
1d08cad3 P |
87 | snprintf(message, sizeof(message), "Trusthost : %s", CIDRtostr(th->ip, th->bits)); |
88 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
77795ca2 GB |
89 | |
90 | snprintf(message, sizeof(message), "Trustgroup : Usage: %d/%d", th->group->count, th->group->trustedfor); | |
91 | triggerhook(HOOK_CONTROL_WHOISREPLY, message); | |
1d08cad3 P |
92 | } |
93 | } | |
94 | ||
2d4ba67d CP |
95 | static void statusfn(int hooknum, void *arg) { |
96 | if((long)arg > 10) { | |
97 | char message[100]; | |
bf5b66e5 | 98 | int groupcount = 0, hostcount = 0, usercount = 0; |
2d4ba67d CP |
99 | trustgroup *tg; |
100 | trusthost *th; | |
101 | ||
102 | for(tg=tglist;tg;tg=tg->next) { | |
bf5b66e5 | 103 | usercount+=tg->count; |
2d4ba67d CP |
104 | groupcount++; |
105 | for(th=tg->hosts;th;th=th->next) | |
106 | hostcount++; | |
107 | } | |
108 | ||
bf5b66e5 | 109 | snprintf(message, sizeof(message), "Trusts :%7d groups, %7d hosts, %7d users", groupcount, hostcount, usercount); |
2d4ba67d CP |
110 | triggerhook(HOOK_CORE_STATSREPLY, message); |
111 | } | |
2b6e02e2 | 112 | } |
2129448c CP |
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<MAXNICKEXTS;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 | } | |
82a316e7 CP |
152 | |
153 | int trusts_fullyonline(void) { | |
154 | if(myhub == -1) | |
155 | return 0; | |
156 | ||
157 | return serverlist[myhub].linkstate == LS_LINKED; | |
158 | } | |
159 |