]> jfr.im git - irc/quakenet/newserv.git/blob - trusts2/trusts.c
bb9b1dd9d6ad1ec7aed7c595eba1fe53e4c46fc3
[irc/quakenet/newserv.git] / trusts2 / trusts.c
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;
8 int tgb_ext;
9 int tgn_ext;
10
11 unsigned long trusts_lasttrustgroupid;
12 unsigned long trusts_lasttrusthostid;
13 unsigned long trusts_lasttrustblockid;
14 int trusts_loaded = 0;
15 int removeusers = 0;
16
17 static void trusts_status(int hooknum, void *arg);
18 void trustsfinishinit(int hooknum, void *arg);
19
20 void _init(void) {
21 trusts_hash_init();
22
23 tgh_ext = registernodeext("trusthost");
24 if ( !tgh_ext ) {
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)");
32 return;
33 }
34
35 tgn_ext = registernickext("trustnick");
36 if ( !tgn_ext ) {
37 Error("trusts", ERR_FATAL, "Could not register a required nick extension (trustnick)");
38 return;
39 }
40
41 registerhook(HOOK_TRUSTS_DBLOADED, trustsfinishinit);
42
43 if ( !trusts_load_db()) {
44 return;
45 }
46
47 if (trusts_loaded)
48 trustsfinishinit(HOOK_TRUSTS_DBLOADED, NULL);
49 }
50
51 void trustsfinishinit(int hooknum, void *arg) {
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;
61
62 int i;
63
64 deregisterhook(HOOK_TRUSTS_DBLOADED, trustsfinishinit);
65
66 if ( trusts_loaded ) {
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
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);
95
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
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 }
162 }
163
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;
171 }