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