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