]>
Commit | Line | Data |
---|---|---|
e2527cba P |
1 | #include "trusts.h" |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | #include <stdarg.h> | |
5 | #include "../core/nsmalloc.h" | |
78530c9c P |
6 | #include "../lib/version.h" |
7 | ||
8 | MODULE_VERSION(""); | |
e2527cba | 9 | |
1ce4eeea P |
10 | int tgh_ext = -1; |
11 | int tgb_ext = -1; | |
12 | int tgn_ext = -1; | |
ef9fab2e | 13 | |
9a8ffb84 P |
14 | unsigned long trusts_lasttrustgroupid; |
15 | unsigned long trusts_lasttrusthostid; | |
16 | unsigned long trusts_lasttrustblockid; | |
4f2ce270 | 17 | int trusts_loaded = 0; |
9a8ffb84 | 18 | int removeusers = 0; |
e2527cba P |
19 | |
20 | static void trusts_status(int hooknum, void *arg); | |
9a8ffb84 | 21 | void trustsfinishinit(int hooknum, void *arg); |
e2527cba P |
22 | |
23 | void _init(void) { | |
24 | trusts_hash_init(); | |
25 | ||
26 | tgh_ext = registernodeext("trusthost"); | |
1ce4eeea | 27 | if ( tgh_ext == -1 ) { |
ef9fab2e P |
28 | Error("trusts", ERR_FATAL, "Could not register a required node extension (trusthost)"); |
29 | return; | |
30 | } | |
31 | ||
32 | tgb_ext = registernodeext("trustblock"); | |
1ce4eeea | 33 | if ( tgb_ext == -1 ) { |
ef9fab2e | 34 | Error("trusts", ERR_FATAL, "Could not register a required node extension (trustblock)"); |
e2527cba P |
35 | return; |
36 | } | |
37 | ||
4f2ce270 | 38 | tgn_ext = registernickext("trustnick"); |
1ce4eeea | 39 | if ( tgn_ext == -1 ) { |
4f2ce270 | 40 | Error("trusts", ERR_FATAL, "Could not register a required nick extension (trustnick)"); |
e2527cba P |
41 | return; |
42 | } | |
4f2ce270 | 43 | |
9a8ffb84 P |
44 | registerhook(HOOK_TRUSTS_DBLOADED, trustsfinishinit); |
45 | ||
4f2ce270 P |
46 | if ( !trusts_load_db()) { |
47 | return; | |
48 | } | |
49 | ||
9a8ffb84 P |
50 | if (trusts_loaded) |
51 | trustsfinishinit(HOOK_TRUSTS_DBLOADED, NULL); | |
52 | } | |
53 | ||
54 | void trustsfinishinit(int hooknum, void *arg) { | |
e2527cba P |
55 | registerhook(HOOK_NICK_NEWNICK, &trusts_hook_newuser); |
56 | registerhook(HOOK_NICK_LOSTNICK, &trusts_hook_lostuser); | |
57 | ||
58 | registerhook(HOOK_CORE_STATSREQUEST, trusts_status); | |
59 | } | |
60 | ||
61 | void _fini(void) { | |
62 | trusthost_t *thptr; | |
63 | trustgroupidentcount_t *t; | |
ef9fab2e | 64 | |
e2527cba | 65 | int i; |
4f2ce270 P |
66 | |
67 | deregisterhook(HOOK_TRUSTS_DBLOADED, trustsfinishinit); | |
e2527cba P |
68 | |
69 | if ( trusts_loaded ) { | |
e2527cba P |
70 | deregisterhook(HOOK_NICK_NEWNICK, &trusts_hook_newuser); |
71 | deregisterhook(HOOK_NICK_LOSTNICK, &trusts_hook_lostuser); | |
72 | ||
73 | deregisterhook(HOOK_CORE_STATSREQUEST, trusts_status); | |
74 | } | |
75 | ||
76 | for ( i = 0; i < TRUSTS_HASH_IDENTSIZE ; i++ ) { | |
77 | for ( t = trustgroupidentcounttable[i]; t; t = t-> next ) { | |
78 | if (t->ident) { | |
79 | freesstring(t->ident); | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
4f2ce270 P |
84 | patricia_node_t *node; |
85 | PATRICIA_WALK_CLEAR(iptree->head,node) { | |
86 | if (node && node->exts[tgb_ext]) { | |
87 | trustblock_free(node->exts[tgb_ext]); | |
88 | node->exts[tgb_ext] = NULL; | |
89 | } | |
90 | } PATRICIA_WALK_CLEAR_END; | |
91 | ||
1ce4eeea | 92 | if (tgh_ext != -1) |
4f2ce270 | 93 | releasenodeext(tgh_ext); |
1ce4eeea | 94 | if (tgb_ext != -1) |
4f2ce270 | 95 | releasenodeext(tgb_ext); |
1ce4eeea | 96 | if (tgn_ext != -1) |
f83d6a33 | 97 | releasenickext(tgn_ext); |
ef9fab2e | 98 | |
e2527cba P |
99 | /* @@@ CLOSE DB */ |
100 | ||
101 | trusts_hash_fini(); | |
102 | ||
103 | nsfreeall(POOL_TRUSTS); | |
104 | } | |
105 | ||
106 | void increment_trust_ipnode(patricia_node_t *node) { | |
107 | patricia_node_t *parent; | |
108 | trusthost_t *tgh = NULL; | |
109 | time_t curtime = getnettime(); | |
110 | parent = node; | |
111 | while (parent) { | |
112 | if(parent->exts && parent->exts[tgh_ext]) { | |
113 | /* update the trusted hosts themselves */ | |
114 | tgh = (trusthost_t *)parent->exts[tgh_ext]; | |
115 | tgh->lastused = curtime; | |
116 | if (tgh->node->usercount > tgh->maxused) { tgh->maxused = tgh->node->usercount; } | |
117 | ||
118 | /* update the trustgroup itself */ | |
119 | tgh->trustgroup->currenton++; | |
120 | tgh->trustgroup->lastused = curtime; | |
121 | if (tgh->trustgroup->currenton > tgh->trustgroup->maxusage) { tgh->trustgroup->maxusage = tgh->trustgroup->currenton; } | |
122 | } | |
123 | parent = parent->parent; | |
124 | } | |
125 | } | |
126 | ||
127 | void decrement_trust_ipnode(patricia_node_t *node) { | |
128 | patricia_node_t *parent; | |
129 | trusthost_t *tgh = NULL; | |
130 | time_t curtime = getnettime(); | |
131 | ||
132 | parent = node; | |
133 | while (parent) { | |
134 | if(parent->exts && parent->exts[tgh_ext]) { | |
135 | tgh = (trusthost_t *)parent->exts[tgh_ext]; | |
136 | tgh->trustgroup->currenton--; | |
137 | tgh->lastused = curtime; | |
138 | ||
139 | tgh->trustgroup->lastused = curtime; | |
140 | } | |
141 | parent = parent->parent; | |
142 | } | |
143 | } | |
144 | ||
e2527cba P |
145 | static void trusts_status(int hooknum, void *arg) { |
146 | if((long)arg > 10) { | |
147 | char message[100]; | |
148 | int tgcount = 0, thcount = 0; | |
149 | trustgroup_t *tg; trusthost_t* thptr; int i; | |
150 | ||
151 | for ( i = 0; i < TRUSTS_HASH_GROUPSIZE ; i++ ) { | |
152 | for ( tg = trustgroupidtable[i]; tg; tg = tg -> nextbyid ) { | |
153 | tgcount++; | |
154 | } | |
155 | } | |
156 | ||
157 | for ( i = 0; i < TRUSTS_HASH_HOSTSIZE ; i++ ) { | |
158 | for ( thptr = trusthostidtable[i]; thptr; thptr = thptr-> nextbyid ) { | |
159 | thcount++; | |
160 | } | |
161 | } | |
162 | snprintf(message, sizeof(message), "Trusts :%7d groups, %7d hosts", tgcount, thcount); | |
163 | triggerhook(HOOK_CORE_STATSREPLY, message); | |
164 | } | |
4f2ce270 | 165 | } |
e2527cba | 166 | |
4f2ce270 P |
167 | int trusts_ignore_np(nick *np) { |
168 | if(SIsService(&serverlist[homeserver(np->numeric)])) { | |
169 | /* ANY user created by a server (nterface,fakeusers,Q) are ignored in relation to trusts */ | |
170 | /* NOTE: we might need to review this if we ever used newserv to handle client/user connections in some way */ | |
171 | return 1; | |
172 | } | |
173 | return 0; | |
e2527cba | 174 | } |