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