]>
Commit | Line | Data |
---|---|---|
2d4ba67d | 1 | #include <stdio.h> |
2129448c | 2 | #include <string.h> |
1bbe1ac3 CP |
3 | #include "../core/hooks.h" |
4 | #include "../core/error.h" | |
45989eab | 5 | #include "../core/nsmalloc.h" |
1bbe1ac3 | 6 | #include "trusts.h" |
2b6e02e2 | 7 | |
1bbe1ac3 CP |
8 | void trusts_registerevents(void); |
9 | void trusts_deregisterevents(void); | |
10 | ||
2d4ba67d | 11 | static void statusfn(int, void *); |
2b6e02e2 | 12 | |
83bccee3 | 13 | static int loaded; |
2129448c | 14 | static sstring *tgextnames[MAXTGEXTS]; |
1bbe1ac3 CP |
15 | |
16 | int trusts_thext, trusts_nextuserext; | |
17 | ||
2b6e02e2 | 18 | void _init(void) { |
1bbe1ac3 CP |
19 | trusts_thext = registernickext("trustth"); |
20 | if(trusts_thext == -1) { | |
21 | Error("trusts", ERR_ERROR, "Unable to register first nick extension."); | |
22 | return; | |
23 | } | |
24 | ||
25 | trusts_nextuserext = registernickext("trustnext"); | |
26 | if(trusts_thext == -1) { | |
27 | releasenickext(trusts_thext); | |
28 | Error("trusts", ERR_ERROR, "Unable to register second nick extension."); | |
29 | return; | |
30 | } | |
31 | ||
2b6e02e2 CP |
32 | if(!trusts_loaddb()) |
33 | return; | |
1bbe1ac3 | 34 | loaded = 1; |
2d4ba67d CP |
35 | |
36 | registerhook(HOOK_CORE_STATSREQUEST, statusfn); | |
1bbe1ac3 | 37 | trusts_registerevents(); |
2b6e02e2 CP |
38 | } |
39 | ||
83bccee3 | 40 | void _fini(void) { |
1bbe1ac3 CP |
41 | if(trusts_thext != -1) { |
42 | releasenickext(trusts_thext); | |
43 | releasenickext(trusts_nextuserext); | |
44 | } | |
45 | ||
46 | if(loaded) { | |
47 | deregisterhook(HOOK_CORE_STATSREQUEST, statusfn); | |
48 | trusts_deregisterevents(); | |
49 | } | |
2d4ba67d | 50 | |
83bccee3 | 51 | trusts_closedb(1); |
2d4ba67d | 52 | |
45989eab | 53 | nscheckfreeall(POOL_TRUSTS); |
45e8ce62 CP |
54 | } |
55 | ||
2d4ba67d CP |
56 | static void statusfn(int hooknum, void *arg) { |
57 | if((long)arg > 10) { | |
58 | char message[100]; | |
bf5b66e5 | 59 | int groupcount = 0, hostcount = 0, usercount = 0; |
2d4ba67d CP |
60 | trustgroup *tg; |
61 | trusthost *th; | |
62 | ||
63 | for(tg=tglist;tg;tg=tg->next) { | |
bf5b66e5 | 64 | usercount+=tg->count; |
2d4ba67d CP |
65 | groupcount++; |
66 | for(th=tg->hosts;th;th=th->next) | |
67 | hostcount++; | |
68 | } | |
69 | ||
bf5b66e5 | 70 | snprintf(message, sizeof(message), "Trusts :%7d groups, %7d hosts, %7d users", groupcount, hostcount, usercount); |
2d4ba67d CP |
71 | triggerhook(HOOK_CORE_STATSREPLY, message); |
72 | } | |
2b6e02e2 | 73 | } |
2129448c CP |
74 | |
75 | int findtgext(const char *name) { | |
76 | int i; | |
77 | ||
78 | for(i=0;i<MAXTGEXTS;i++) | |
79 | if(tgextnames[i] && !strcmp(name, tgextnames[i]->content)) | |
80 | return i; | |
81 | ||
82 | return -1; | |
83 | } | |
84 | ||
85 | int registertgext(const char *name) { | |
86 | int i; | |
87 | ||
88 | if(findtgext(name) != -1) { | |
89 | Error("trusts", ERR_WARNING, "Tried to register duplicate trust group extension: %s.", name); | |
90 | return -1; | |
91 | } | |
92 | ||
93 | for(i=0;i<MAXNICKEXTS;i++) { | |
94 | if(!tgextnames[i]) { | |
95 | tgextnames[i] = getsstring(name, 100); | |
96 | return i; | |
97 | } | |
98 | } | |
99 | ||
100 | Error("trusts", ERR_WARNING, "Tried to register too many trust group extensions: %s.", name); | |
101 | return -1; | |
102 | } | |
103 | ||
104 | void releasetgext(int index) { | |
105 | trustgroup *tg; | |
106 | ||
107 | freesstring(tgextnames[index]); | |
108 | tgextnames[index] = NULL; | |
109 | ||
110 | for(tg=tglist;tg;tg=tg->next) | |
111 | tg->exts[index] = NULL; | |
112 | } |