]> jfr.im git - irc/quakenet/newserv.git/blob - trusts2/trusts.c
Merge.
[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 #include "../lib/version.h"
7
8 MODULE_VERSION("");
9
10 int tgh_ext = -1;
11 int tgb_ext = -1;
12 int tgn_ext = -1;
13
14 unsigned long trusts_lasttrustgroupid;
15 unsigned long trusts_lasttrusthostid;
16 unsigned long trusts_lasttrustblockid;
17 int trusts_loaded = 0;
18 int removeusers = 0;
19
20 static void trusts_status(int hooknum, void *arg);
21 void trustsfinishinit(int hooknum, void *arg);
22
23 void _init(void) {
24 trusts_hash_init();
25
26 tgh_ext = registernodeext("trusthost");
27 if ( tgh_ext == -1 ) {
28 Error("trusts", ERR_FATAL, "Could not register a required node extension (trusthost)");
29 return;
30 }
31
32 tgb_ext = registernodeext("trustblock");
33 if ( tgb_ext == -1 ) {
34 Error("trusts", ERR_FATAL, "Could not register a required node extension (trustblock)");
35 return;
36 }
37
38 tgn_ext = registernickext("trustnick");
39 if ( tgn_ext == -1 ) {
40 Error("trusts", ERR_FATAL, "Could not register a required nick extension (trustnick)");
41 return;
42 }
43
44 registerhook(HOOK_TRUSTS_DBLOADED, trustsfinishinit);
45
46 if ( !trusts_load_db()) {
47 return;
48 }
49
50 if (trusts_loaded)
51 trustsfinishinit(HOOK_TRUSTS_DBLOADED, NULL);
52 }
53
54 void trustsfinishinit(int hooknum, void *arg) {
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;
64
65 int i;
66
67 deregisterhook(HOOK_TRUSTS_DBLOADED, trustsfinishinit);
68
69 if ( trusts_loaded ) {
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
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
92 if (tgh_ext != -1)
93 releasenodeext(tgh_ext);
94 if (tgb_ext != -1)
95 releasenodeext(tgb_ext);
96 if (tgn_ext != -1)
97 releasenickext(tgn_ext);
98
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
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 }
165 }
166
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;
174 }