]>
Commit | Line | Data |
---|---|---|
1 | #include "../core/hooks.h" | |
2 | #include "../irc/irc.h" | |
3 | #include "trusts.h" | |
4 | ||
5 | static void __counthandler(int hooknum, void *arg); | |
6 | ||
7 | void trusts_newnick(nick *sender, int moving) { | |
8 | trusthost *th; | |
9 | void *arg[2]; | |
10 | struct irc_in_addr ipaddress; | |
11 | ||
12 | ip_canonicalize_tunnel(&ipaddress, &sender->ipaddress); | |
13 | ||
14 | th = th_getbyhost(&ipaddress); | |
15 | ||
16 | settrusthost(sender, th); | |
17 | if(!th) { | |
18 | setnextbytrust(sender, NULL); | |
19 | } else { | |
20 | setnextbytrust(sender, th->users); | |
21 | th->users = sender; | |
22 | } | |
23 | ||
24 | arg[0] = sender; | |
25 | arg[1] = (void *)(long)moving; | |
26 | ||
27 | /* sucks we have to do this, at least until we get priority hooks */ | |
28 | __counthandler(HOOK_TRUSTS_NEWNICK, arg); | |
29 | ||
30 | triggerhook(HOOK_TRUSTS_NEWNICK, arg); | |
31 | } | |
32 | ||
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; | |
39 | trusthost *th = gettrusthost(sender); | |
40 | void *arg[2]; | |
41 | ||
42 | arg[0] = sender; | |
43 | arg[1] = (void *)(long)moving; | |
44 | ||
45 | __counthandler(HOOK_TRUSTS_LOSTNICK, arg); | |
46 | triggerhook(HOOK_TRUSTS_LOSTNICK, arg); | |
47 | ||
48 | if(!th) | |
49 | return; | |
50 | ||
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 | ||
70 | static void __lostnick(int hooknum, void *arg) { | |
71 | trusts_lostnick(arg, 0); | |
72 | } | |
73 | ||
74 | static void __counthandler(int hooknum, void *arg) { | |
75 | time_t t = getnettime(); | |
76 | void **args = arg; | |
77 | trusthost *th = gettrusthost((nick *)args[0]); | |
78 | trustgroup *tg; | |
79 | ||
80 | if(!th) | |
81 | return; | |
82 | ||
83 | tg = th->group; | |
84 | tg->lastseen = th->lastseen = t; | |
85 | if(hooknum == HOOK_TRUSTS_NEWNICK) { | |
86 | th->count++; | |
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; | |
93 | } else { | |
94 | th->count--; | |
95 | tg->count--; | |
96 | } | |
97 | } | |
98 | ||
99 | static int hooksregistered; | |
100 | ||
101 | static void __dbloaded(int hooknum, void *arg) { | |
102 | int i; | |
103 | nick *np; | |
104 | ||
105 | if(hooksregistered) | |
106 | return; | |
107 | hooksregistered = 1; | |
108 | ||
109 | registerhook(HOOK_NICK_NEWNICK, __newnick); | |
110 | registerhook(HOOK_NICK_LOSTNICK, __lostnick); | |
111 | ||
112 | /* registerhook(HOOK_TRUSTS_NEWNICK, __counthandler); | |
113 | registerhook(HOOK_TRUSTS_LOSTNICK, __counthandler); | |
114 | */ | |
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 | ||
122 | static void __dbclosed(int hooknum, void *arg) { | |
123 | if(!hooksregistered) | |
124 | return; | |
125 | hooksregistered = 0; | |
126 | ||
127 | deregisterhook(HOOK_NICK_NEWNICK, __newnick); | |
128 | deregisterhook(HOOK_NICK_LOSTNICK, __lostnick); | |
129 | ||
130 | /* | |
131 | deregisterhook(HOOK_TRUSTS_NEWNICK, __counthandler); | |
132 | deregisterhook(HOOK_TRUSTS_LOSTNICK, __counthandler); | |
133 | */ | |
134 | } | |
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 | } |