]> jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts.c
273e42312f7d714056391d2ab51e20f6ca8a5af1
[irc/quakenet/newserv.git] / trusts / trusts.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "../lib/version.h"
4 #include "../core/hooks.h"
5 #include "../core/error.h"
6 #include "../core/nsmalloc.h"
7 #include "../server/server.h"
8 #include "trusts.h"
9
10 MODULE_VERSION("");
11
12 void trusts_registerevents(void);
13 void trusts_deregisterevents(void);
14
15 static void statusfn(int, void *);
16 static void whoisfn(int, void *);
17
18 static sstring *tgextnames[MAXTGEXTS];
19
20 int trusts_thext, trusts_nextuserext;
21 int trustsdbloaded;
22
23 void _init(void) {
24 trusts_thext = registernickext("trustth");
25 if(trusts_thext == -1) {
26 Error("trusts", ERR_ERROR, "Unable to register first nick extension.");
27 return;
28 }
29
30 trusts_nextuserext = registernickext("trustnext");
31 if(trusts_thext == -1) {
32 releasenickext(trusts_thext);
33 Error("trusts", ERR_ERROR, "Unable to register second nick extension.");
34 return;
35 }
36
37 registerhook(HOOK_CORE_STATSREQUEST, statusfn);
38 registerhook(HOOK_CONTROL_WHOISREQUEST, &whoisfn);
39 trusts_registerevents();
40 }
41
42 void _fini(void) {
43 if(trusts_thext != -1) {
44 releasenickext(trusts_thext);
45 releasenickext(trusts_nextuserext);
46 }
47
48 deregisterhook(HOOK_CORE_STATSREQUEST, statusfn);
49 deregisterhook(HOOK_CONTROL_WHOISREQUEST, &whoisfn);
50 trusts_deregisterevents();
51
52 nscheckfreeall(POOL_TRUSTS);
53 }
54
55 static void whoisfn(int hooknum, void *arg) {
56 trusthost *th;
57 char message[512];
58 nick *np = (nick *)arg;
59
60 if(!np)
61 return;
62
63 th = gettrusthost(np);
64
65 if(!th)
66 return;
67
68 snprintf(message, sizeof(message), "Trustgroup: %s (#%d)", th->group->name->content, th->group->id);
69 triggerhook(HOOK_CONTROL_WHOISREPLY, message);
70 if(th->group->trustedfor>0) {
71 snprintf(message, sizeof(message), "Trustgroup: Usage: %d/%d", th->group->count, th->group->trustedfor);
72 triggerhook(HOOK_CONTROL_WHOISREPLY, message);
73 }
74 if(th->maxpernode>0) {
75 snprintf(message, sizeof(message), "Trusthost : %s", CIDRtostr(np->p_ipaddr, th->nodebits));
76 triggerhook(HOOK_CONTROL_WHOISREPLY, message);
77
78 patricia_node_t *node;
79 int usercount = 0;
80
81 node = refnode(iptree, &(np->p_ipaddr), th->nodebits);
82 usercount = node->usercount;
83 derefnode(iptree, node);
84
85 snprintf(message, sizeof(message), "Trusthost : Usage: %d/%d", usercount, th->maxpernode);
86 triggerhook(HOOK_CONTROL_WHOISREPLY, message);
87 } else {
88 snprintf(message, sizeof(message), "Trusthost : %s", CIDRtostr(th->ip, th->bits));
89 triggerhook(HOOK_CONTROL_WHOISREPLY, message);
90 }
91 }
92
93 static void statusfn(int hooknum, void *arg) {
94 if((long)arg > 10) {
95 char message[100];
96 int groupcount = 0, hostcount = 0, usercount = 0;
97 trustgroup *tg;
98 trusthost *th;
99
100 for(tg=tglist;tg;tg=tg->next) {
101 usercount+=tg->count;
102 groupcount++;
103 for(th=tg->hosts;th;th=th->next)
104 hostcount++;
105 }
106
107 snprintf(message, sizeof(message), "Trusts :%7d groups, %7d hosts, %7d users", groupcount, hostcount, usercount);
108 triggerhook(HOOK_CORE_STATSREPLY, message);
109 }
110 }
111
112 int findtgext(const char *name) {
113 int i;
114
115 for(i=0;i<MAXTGEXTS;i++)
116 if(tgextnames[i] && !strcmp(name, tgextnames[i]->content))
117 return i;
118
119 return -1;
120 }
121
122 int registertgext(const char *name) {
123 int i;
124
125 if(findtgext(name) != -1) {
126 Error("trusts", ERR_WARNING, "Tried to register duplicate trust group extension: %s.", name);
127 return -1;
128 }
129
130 for(i=0;i<MAXNICKEXTS;i++) {
131 if(!tgextnames[i]) {
132 tgextnames[i] = getsstring(name, 100);
133 return i;
134 }
135 }
136
137 Error("trusts", ERR_WARNING, "Tried to register too many trust group extensions: %s.", name);
138 return -1;
139 }
140
141 void releasetgext(int index) {
142 trustgroup *tg;
143
144 freesstring(tgextnames[index]);
145 tgextnames[index] = NULL;
146
147 for(tg=tglist;tg;tg=tg->next)
148 tg->exts[index] = NULL;
149 }
150
151 int trusts_fullyonline(void) {
152 if(myhub == -1)
153 return 0;
154
155 return serverlist[myhub].linkstate == LS_LINKED;
156 }
157