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