]>
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); | |
17 | return; | |
18 | } | |
19 | ||
20 | setnextbytrust(sender, th->users); | |
21 | th->users = sender; | |
22 | ||
45e8ce62 CP |
23 | /* sucks we have to do this, at least until we get priority hooks */ |
24 | __counthandler(HOOK_TRUSTS_NEWNICK, sender); | |
9097ab05 CP |
25 | |
26 | arg[0] = sender; | |
27 | arg[1] = (void *)(long)moving; | |
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 CP |
39 | |
40 | if(!th) | |
41 | return; | |
42 | ||
45e8ce62 | 43 | __counthandler(HOOK_TRUSTS_LOSTNICK, sender); |
9097ab05 CP |
44 | |
45 | arg[0] = sender; | |
46 | arg[1] = (void *)(long)moving; | |
47 | triggerhook(HOOK_TRUSTS_LOSTNICK, arg); | |
1bbe1ac3 CP |
48 | |
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); |
1bbe1ac3 | 74 | trusthost *th = gettrusthost((nick *)arg); |
4be1aaf2 | 75 | trustgroup *tg = th->group; |
1bbe1ac3 | 76 | |
4be1aaf2 | 77 | tg->lastseen = th->lastseen = t; |
1bbe1ac3 CP |
78 | if(hooknum == HOOK_TRUSTS_NEWNICK) { |
79 | th->count++; | |
4be1aaf2 CP |
80 | if(th->count > th->maxusage) |
81 | th->maxusage = th->count; | |
82 | ||
83 | tg->count++; | |
84 | if(tg->count > tg->maxusage) | |
85 | tg->maxusage = tg->count; | |
1bbe1ac3 CP |
86 | } else { |
87 | th->count--; | |
4be1aaf2 | 88 | tg->count--; |
1bbe1ac3 CP |
89 | } |
90 | } | |
91 | ||
a99a2041 CP |
92 | static int hooksregistered; |
93 | ||
1bbe1ac3 CP |
94 | static void __dbloaded(int hooknum, void *arg) { |
95 | int i; | |
96 | nick *np; | |
97 | ||
a99a2041 CP |
98 | if(hooksregistered) |
99 | return; | |
100 | hooksregistered = 1; | |
101 | ||
1bbe1ac3 CP |
102 | registerhook(HOOK_NICK_NEWNICK, __newnick); |
103 | registerhook(HOOK_NICK_LOSTNICK, __lostnick); | |
104 | ||
45e8ce62 | 105 | /* registerhook(HOOK_TRUSTS_NEWNICK, __counthandler); |
1bbe1ac3 | 106 | registerhook(HOOK_TRUSTS_LOSTNICK, __counthandler); |
45e8ce62 | 107 | */ |
1bbe1ac3 CP |
108 | |
109 | /* we could do it by host, but hosts and ips are not bijective :( */ | |
110 | for(i=0;i<NICKHASHSIZE;i++) | |
111 | for(np=nicktable[i];np;np=np->next) | |
112 | __newnick(0, np); | |
113 | } | |
114 | ||
83bccee3 | 115 | static void __dbclosed(int hooknum, void *arg) { |
a99a2041 CP |
116 | if(!hooksregistered) |
117 | return; | |
118 | hooksregistered = 0; | |
119 | ||
120 | deregisterhook(HOOK_NICK_NEWNICK, __newnick); | |
121 | deregisterhook(HOOK_NICK_LOSTNICK, __lostnick); | |
1bbe1ac3 | 122 | |
45e8ce62 | 123 | /* |
a99a2041 CP |
124 | deregisterhook(HOOK_TRUSTS_NEWNICK, __counthandler); |
125 | deregisterhook(HOOK_TRUSTS_LOSTNICK, __counthandler); | |
45e8ce62 | 126 | */ |
1bbe1ac3 | 127 | } |
83bccee3 CP |
128 | |
129 | void trusts_deregisterevents(void) { | |
130 | deregisterhook(HOOK_TRUSTS_DB_LOADED, __dbloaded); | |
131 | deregisterhook(HOOK_TRUSTS_DB_CLOSED, __dbclosed); | |
132 | ||
133 | __dbclosed(0, NULL); | |
134 | } | |
135 | ||
136 | void trusts_registerevents(void) { | |
137 | registerhook(HOOK_TRUSTS_DB_LOADED, __dbloaded); | |
138 | registerhook(HOOK_TRUSTS_DB_CLOSED, __dbclosed); | |
139 | ||
140 | if(trustsdbloaded) | |
141 | __dbloaded(0, NULL); | |
142 | } |