]>
Commit | Line | Data |
---|---|---|
1 | #include <string.h> | |
2 | #include <assert.h> | |
3 | #include "../lib/version.h" | |
4 | #include "../nick/nick.h" | |
5 | #include "../chanindex/chanindex.h" | |
6 | #include "../channel/channel.h" | |
7 | #include "../core/hooks.h" | |
8 | #include "whowas.h" | |
9 | ||
10 | MODULE_VERSION(""); | |
11 | ||
12 | static int wwcnext, wwccext; | |
13 | ||
14 | static void wwc_refchannel(chanindex *cip) { | |
15 | uintptr_t *refcount = (uintptr_t *)&cip->exts[wwccext]; | |
16 | (*refcount)++; | |
17 | } | |
18 | ||
19 | static void wwc_derefchannel(chanindex *cip) { | |
20 | uintptr_t *refcount = (uintptr_t *)&cip->exts[wwccext]; | |
21 | (*refcount)--; | |
22 | ||
23 | assert(*refcount >= 0); | |
24 | ||
25 | if (*refcount == 0) | |
26 | releasechanindex(cip); | |
27 | } | |
28 | ||
29 | static void wwc_hook_joincreate(int hooknum, void *arg) { | |
30 | void **args = arg; | |
31 | channel *cp = args[0]; | |
32 | nick *np = args[1]; | |
33 | chanindex **wchans = np->exts[wwcnext]; | |
34 | ||
35 | if (!wchans) { | |
36 | wchans = calloc(sizeof(chanindex *), WW_MAXCHANNELS); | |
37 | np->exts[wwcnext] = wchans; | |
38 | } | |
39 | ||
40 | wwc_refchannel(cp->index); | |
41 | ||
42 | memmove(&wchans[1], &wchans[0], sizeof(chanindex *) * (WW_MAXCHANNELS - 1)); | |
43 | wchans[0] = cp->index; | |
44 | } | |
45 | ||
46 | static void wwc_hook_lostnick(int hooknum, void *arg) { | |
47 | nick *np = arg; | |
48 | chanindex **wchans = np->exts[wwcnext]; | |
49 | int i; | |
50 | ||
51 | if (!wchans) | |
52 | return; | |
53 | ||
54 | for (i = 0; i < WW_MAXCHANNELS; i++) { | |
55 | if (!wchans[i]) | |
56 | break; | |
57 | ||
58 | wwc_derefchannel(wchans[i]); | |
59 | } | |
60 | ||
61 | free(wchans); | |
62 | } | |
63 | ||
64 | static void wwc_hook_newrecord(int hooknum, void *arg) { | |
65 | void **args = arg; | |
66 | whowas *ww = args[0]; | |
67 | nick *np = args[1]; | |
68 | chanindex **wchans = np->exts[wwcnext]; | |
69 | int i; | |
70 | ||
71 | memset(ww->channels, 0, sizeof(ww->channels)); | |
72 | ||
73 | if (!wchans) | |
74 | return; | |
75 | ||
76 | for (i = 0; i < WW_MAXCHANNELS; i++) { | |
77 | if (!wchans[i]) | |
78 | break; | |
79 | ||
80 | wwc_refchannel(wchans[i]); | |
81 | ww->channels[i] = wchans[i]; | |
82 | } | |
83 | } | |
84 | ||
85 | static void wwc_hook_lostrecord(int hooknum, void *arg) { | |
86 | whowas *ww = arg; | |
87 | int i; | |
88 | ||
89 | for (i = 0; i < WW_MAXCHANNELS; i++) { | |
90 | if (!ww->channels[i]) | |
91 | break; | |
92 | ||
93 | wwc_derefchannel(ww->channels[i]); | |
94 | } | |
95 | } | |
96 | ||
97 | void _init(void) { | |
98 | wwcnext = registernickext("whowas_channels"); | |
99 | wwccext = registerchanext("whowas_channels"); | |
100 | ||
101 | registerhook(HOOK_CHANNEL_JOIN, &wwc_hook_joincreate); | |
102 | registerhook(HOOK_CHANNEL_CREATE, &wwc_hook_joincreate); | |
103 | registerhook(HOOK_NICK_LOSTNICK, &wwc_hook_lostnick); | |
104 | registerhook(HOOK_WHOWAS_NEWRECORD, &wwc_hook_newrecord); | |
105 | registerhook(HOOK_WHOWAS_LOSTRECORD, &wwc_hook_lostrecord); | |
106 | } | |
107 | ||
108 | void _fini(void) { | |
109 | releasenickext(wwcnext); | |
110 | releasechanext(wwccext); | |
111 | ||
112 | deregisterhook(HOOK_CHANNEL_JOIN, &wwc_hook_joincreate); | |
113 | deregisterhook(HOOK_CHANNEL_CREATE, &wwc_hook_joincreate); | |
114 | deregisterhook(HOOK_NICK_LOSTNICK, &wwc_hook_lostnick); | |
115 | deregisterhook(HOOK_WHOWAS_NEWRECORD, &wwc_hook_newrecord); | |
116 | deregisterhook(HOOK_WHOWAS_LOSTRECORD, &wwc_hook_lostrecord); | |
117 | } |