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