]>
Commit | Line | Data |
---|---|---|
1bbe1ac3 CP |
1 | #include "../core/hooks.h" |
2 | #include "trusts.h" | |
3 | ||
45e8ce62 CP |
4 | static void __counthandler(int hooknum, void *arg); |
5 | ||
9097ab05 | 6 | void trusts_newnick(nick *sender, int moving) { |
1bbe1ac3 CP |
7 | uint32_t host; |
8 | trusthost *th; | |
9097ab05 | 9 | void *arg[2]; |
1bbe1ac3 CP |
10 | |
11 | host = irc_in_addr_v4_to_int(&sender->p_ipaddr); | |
12 | th = th_getbyhost(host); | |
13 | ||
14 | settrusthost(sender, th); | |
15 | if(!th) { | |
16 | setnextbytrust(sender, NULL); | |
b9cafbd9 CP |
17 | } else { |
18 | setnextbytrust(sender, th->users); | |
19 | th->users = sender; | |
1bbe1ac3 CP |
20 | } |
21 | ||
b9cafbd9 CP |
22 | arg[0] = sender; |
23 | arg[1] = (void *)(long)moving; | |
1bbe1ac3 | 24 | |
45e8ce62 | 25 | /* sucks we have to do this, at least until we get priority hooks */ |
b9cafbd9 | 26 | __counthandler(HOOK_TRUSTS_NEWNICK, arg); |
9097ab05 | 27 | |
9097ab05 | 28 | triggerhook(HOOK_TRUSTS_NEWNICK, arg); |
1bbe1ac3 CP |
29 | } |
30 | ||
9097ab05 CP |
31 | static void __newnick(int hooknum, void *arg) { |
32 | trusts_newnick(arg, 0); | |
33 | } | |
34 | ||
35 | void trusts_lostnick(nick *sender, int moving) { | |
36 | nick *np, *lp; | |
1bbe1ac3 | 37 | trusthost *th = gettrusthost(sender); |
9097ab05 | 38 | void *arg[2]; |
1bbe1ac3 | 39 | |
9097ab05 CP |
40 | arg[0] = sender; |
41 | arg[1] = (void *)(long)moving; | |
b9cafbd9 CP |
42 | |
43 | __counthandler(HOOK_TRUSTS_LOSTNICK, arg); | |
9097ab05 | 44 | triggerhook(HOOK_TRUSTS_LOSTNICK, arg); |
1bbe1ac3 | 45 | |
b9cafbd9 CP |
46 | if(!th) |
47 | return; | |
48 | ||
1bbe1ac3 CP |
49 | /* |
50 | * we need to erase this nick from the trusthost list | |
51 | * stored in the ->nextbytrust (ext) pointers in each nick | |
52 | */ | |
53 | ||
54 | for(np=th->users,lp=NULL;np;lp=np,np=nextbytrust(np)) { | |
55 | if(np != sender) | |
56 | continue; | |
57 | ||
58 | if(lp) { | |
59 | setnextbytrust(lp, nextbytrust(np)); | |
60 | } else { | |
61 | th->users = nextbytrust(np); | |
62 | } | |
63 | ||
64 | break; | |
65 | } | |
66 | } | |
67 | ||
9097ab05 CP |
68 | static void __lostnick(int hooknum, void *arg) { |
69 | trusts_lostnick(arg, 0); | |
70 | } | |
71 | ||
1bbe1ac3 | 72 | static void __counthandler(int hooknum, void *arg) { |
4be1aaf2 | 73 | time_t t = time(NULL); |
b9cafbd9 CP |
74 | void **args = arg; |
75 | trusthost *th = gettrusthost((nick *)args[0]); | |
76 | trustgroup *tg; | |
77 | ||
78 | if(!th) | |
79 | return; | |
1bbe1ac3 | 80 | |
b9cafbd9 | 81 | tg = th->group; |
4be1aaf2 | 82 | tg->lastseen = th->lastseen = t; |
1bbe1ac3 CP |
83 | if(hooknum == HOOK_TRUSTS_NEWNICK) { |
84 | th->count++; | |
4be1aaf2 CP |
85 | if(th->count > th->maxusage) |
86 | th->maxusage = th->count; | |
87 | ||
88 | tg->count++; | |
89 | if(tg->count > tg->maxusage) | |
90 | tg->maxusage = tg->count; | |
1bbe1ac3 CP |
91 | } else { |
92 | th->count--; | |
4be1aaf2 | 93 | tg->count--; |
1bbe1ac3 CP |
94 | } |
95 | } | |
96 | ||
a99a2041 CP |
97 | static int hooksregistered; |
98 | ||
1bbe1ac3 CP |
99 | static void __dbloaded(int hooknum, void *arg) { |
100 | int i; | |
101 | nick *np; | |
102 | ||
a99a2041 CP |
103 | if(hooksregistered) |
104 | return; | |
105 | hooksregistered = 1; | |
106 | ||
1bbe1ac3 CP |
107 | registerhook(HOOK_NICK_NEWNICK, __newnick); |
108 | registerhook(HOOK_NICK_LOSTNICK, __lostnick); | |
109 | ||
45e8ce62 | 110 | /* registerhook(HOOK_TRUSTS_NEWNICK, __counthandler); |
1bbe1ac3 | 111 | registerhook(HOOK_TRUSTS_LOSTNICK, __counthandler); |
45e8ce62 | 112 | */ |
1bbe1ac3 CP |
113 | |
114 | /* we could do it by host, but hosts and ips are not bijective :( */ | |
115 | for(i=0;i<NICKHASHSIZE;i++) | |
116 | for(np=nicktable[i];np;np=np->next) | |
117 | __newnick(0, np); | |
118 | } | |
119 | ||
83bccee3 | 120 | static void __dbclosed(int hooknum, void *arg) { |
a99a2041 CP |
121 | if(!hooksregistered) |
122 | return; | |
123 | hooksregistered = 0; | |
124 | ||
125 | deregisterhook(HOOK_NICK_NEWNICK, __newnick); | |
126 | deregisterhook(HOOK_NICK_LOSTNICK, __lostnick); | |
1bbe1ac3 | 127 | |
45e8ce62 | 128 | /* |
a99a2041 CP |
129 | deregisterhook(HOOK_TRUSTS_NEWNICK, __counthandler); |
130 | deregisterhook(HOOK_TRUSTS_LOSTNICK, __counthandler); | |
45e8ce62 | 131 | */ |
1bbe1ac3 | 132 | } |
83bccee3 CP |
133 | |
134 | void trusts_deregisterevents(void) { | |
135 | deregisterhook(HOOK_TRUSTS_DB_LOADED, __dbloaded); | |
136 | deregisterhook(HOOK_TRUSTS_DB_CLOSED, __dbclosed); | |
137 | ||
138 | __dbclosed(0, NULL); | |
139 | } | |
140 | ||
141 | void trusts_registerevents(void) { | |
142 | registerhook(HOOK_TRUSTS_DB_LOADED, __dbloaded); | |
143 | registerhook(HOOK_TRUSTS_DB_CLOSED, __dbclosed); | |
144 | ||
145 | if(trustsdbloaded) | |
146 | __dbloaded(0, NULL); | |
147 | } |