]> jfr.im git - irc/quakenet/newserv.git/blame - trusts2/trusts.c
trust search ast
[irc/quakenet/newserv.git] / trusts2 / trusts.c
CommitLineData
e2527cba
P
1#include "trusts.h"
2#include <stdlib.h>
3#include <string.h>
4#include <stdarg.h>
5#include "../core/nsmalloc.h"
6
7int tgh_ext;
ef9fab2e
P
8int tgb_ext;
9
9a8ffb84
P
10unsigned long trusts_lasttrustgroupid;
11unsigned long trusts_lasttrusthostid;
12unsigned long trusts_lasttrustblockid;
13int trusts_loaded;
14int removeusers = 0;
e2527cba
P
15
16static void trusts_status(int hooknum, void *arg);
9a8ffb84 17void trustsfinishinit(int hooknum, void *arg);
e2527cba
P
18
19void _init(void) {
20 trusts_hash_init();
21
22 tgh_ext = registernodeext("trusthost");
23 if ( !tgh_ext ) {
ef9fab2e
P
24 Error("trusts", ERR_FATAL, "Could not register a required node extension (trusthost)");
25 return;
26 }
27
28 tgb_ext = registernodeext("trustblock");
29 if ( !tgb_ext ) {
30 Error("trusts", ERR_FATAL, "Could not register a required node extension (trustblock)");
e2527cba
P
31 return;
32 }
33
34 if ( !trusts_load_db()) {
35 return;
36 }
e2527cba 37
9a8ffb84
P
38 registerhook(HOOK_TRUSTS_DBLOADED, trustsfinishinit);
39
40 if (trusts_loaded)
41 trustsfinishinit(HOOK_TRUSTS_DBLOADED, NULL);
42}
43
44void trustsfinishinit(int hooknum, void *arg) {
45 Error("trusts",ERR_INFO,"Database loaded, finishing initialisation.");
46
47 deregisterhook(HOOK_TRUSTS_DBLOADED, trustsfinishinit);
e2527cba
P
48
49 registerhook(HOOK_NICK_NEWNICK, &trusts_hook_newuser);
50 registerhook(HOOK_NICK_LOSTNICK, &trusts_hook_lostuser);
51
52 registerhook(HOOK_CORE_STATSREQUEST, trusts_status);
53}
54
55void _fini(void) {
56 trusthost_t *thptr;
57 trustgroupidentcount_t *t;
ef9fab2e 58
e2527cba
P
59 int i;
60 for ( i = 0; i < TRUSTS_HASH_HOSTSIZE ; i++ ) {
61 for ( thptr = trusthostidtable[i]; thptr; thptr = thptr-> nextbyid ) {
62 derefnode(iptree,thptr->node);
63 }
64 }
ef9fab2e
P
65 if (tgh_ext)
66 releasenodeext(tgh_ext);
67 if (tgb_ext)
68 releasenodeext(tgb_ext);
e2527cba
P
69
70 if ( trusts_loaded ) {
e2527cba
P
71 deregisterhook(HOOK_NICK_NEWNICK, &trusts_hook_newuser);
72 deregisterhook(HOOK_NICK_LOSTNICK, &trusts_hook_lostuser);
73
74 deregisterhook(HOOK_CORE_STATSREQUEST, trusts_status);
75 }
76
77 for ( i = 0; i < TRUSTS_HASH_IDENTSIZE ; i++ ) {
78 for ( t = trustgroupidentcounttable[i]; t; t = t-> next ) {
79 if (t->ident) {
80 freesstring(t->ident);
81 }
82 }
83 }
84
ef9fab2e
P
85 trustblock_freeall();
86
e2527cba
P
87 /* @@@ CLOSE DB */
88
89 trusts_hash_fini();
90
91 nsfreeall(POOL_TRUSTS);
92}
93
94void increment_trust_ipnode(patricia_node_t *node) {
95 patricia_node_t *parent;
96 trusthost_t *tgh = NULL;
97 time_t curtime = getnettime();
98 parent = node;
99 while (parent) {
100 if(parent->exts && parent->exts[tgh_ext]) {
101 /* update the trusted hosts themselves */
102 tgh = (trusthost_t *)parent->exts[tgh_ext];
103 tgh->lastused = curtime;
104 if (tgh->node->usercount > tgh->maxused) { tgh->maxused = tgh->node->usercount; }
105
106 /* update the trustgroup itself */
107 tgh->trustgroup->currenton++;
108 tgh->trustgroup->lastused = curtime;
109 if (tgh->trustgroup->currenton > tgh->trustgroup->maxusage) { tgh->trustgroup->maxusage = tgh->trustgroup->currenton; }
110 }
111 parent = parent->parent;
112 }
113}
114
115void decrement_trust_ipnode(patricia_node_t *node) {
116 patricia_node_t *parent;
117 trusthost_t *tgh = NULL;
118 time_t curtime = getnettime();
119
120 parent = node;
121 while (parent) {
122 if(parent->exts && parent->exts[tgh_ext]) {
123 tgh = (trusthost_t *)parent->exts[tgh_ext];
124 tgh->trustgroup->currenton--;
125 tgh->lastused = curtime;
126
127 tgh->trustgroup->lastused = curtime;
128 }
129 parent = parent->parent;
130 }
131}
132
133void trust_debug(char *format, ...) {
134 char buf[512];
135 va_list va;
136 channel *debugcp = findchannel("#qnet.trusts");
137 if(debugcp) {
138 va_start(va, format);
139 vsnprintf(buf, sizeof(buf), format, va);
140 va_end(va);
141 controlchanmsg(debugcp,buf);
142 }
143}
144
145static 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 }
165
166}