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"
12 void trusts_registerevents(void);
13 void trusts_deregisterevents(void);
15 static void statusfn(int, void *);
16 static void whoisfn(int, void *);
18 static sstring
*tgextnames
[MAXTGEXTS
];
20 int trusts_thext
, trusts_nextuserext
;
24 trusts_thext
= registernickext("trustth");
25 if(trusts_thext
== -1) {
26 Error("trusts", ERR_ERROR
, "Unable to register first nick extension.");
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.");
37 registerhook(HOOK_CORE_STATSREQUEST
, statusfn
);
38 registerhook(HOOK_CONTROL_WHOISREQUEST
, &whoisfn
);
39 trusts_registerevents();
43 if(trusts_thext
!= -1) {
44 releasenickext(trusts_thext
);
45 releasenickext(trusts_nextuserext
);
48 deregisterhook(HOOK_CORE_STATSREQUEST
, statusfn
);
49 deregisterhook(HOOK_CONTROL_WHOISREQUEST
, &whoisfn
);
50 trusts_deregisterevents();
52 nscheckfreeall(POOL_TRUSTS
);
55 static void whoisfn(int hooknum
, void *arg
) {
58 nick
*np
= (nick
*)arg
;
63 th
= gettrusthost(np
);
68 snprintf(message
, sizeof(message
), "Trustgroup: %s (#%d)", th
->group
->name
->content
, th
->group
->id
);
69 triggerhook(HOOK_CONTROL_WHOISREPLY
, message
);
71 if (th
->maxpernode
> 0) {
72 snprintf(message
, sizeof(message
), "Node : %s", CIDRtostr(np
->p_ipaddr
, th
->nodebits
));
73 triggerhook(HOOK_CONTROL_WHOISREPLY
, message
);
75 patricia_node_t
*node
;
78 node
= refnode(iptree
, &(np
->p_ipaddr
), th
->nodebits
);
79 usercount
= node
->usercount
;
80 derefnode(iptree
, node
);
82 snprintf(message
, sizeof(message
), "Node : Usage: %d/%d", usercount
, th
->maxpernode
);
83 triggerhook(HOOK_CONTROL_WHOISREPLY
, message
);
86 if (th
->group
->trustedfor
> 0) {
87 snprintf(message
, sizeof(message
), "Trusthost : %s", CIDRtostr(th
->ip
, th
->bits
));
88 triggerhook(HOOK_CONTROL_WHOISREPLY
, message
);
90 snprintf(message
, sizeof(message
), "Trustgroup : Usage: %d/%d", th
->group
->count
, th
->group
->trustedfor
);
91 triggerhook(HOOK_CONTROL_WHOISREPLY
, message
);
95 static void statusfn(int hooknum
, void *arg
) {
98 int groupcount
= 0, hostcount
= 0, usercount
= 0;
102 for(tg
=tglist
;tg
;tg
=tg
->next
) {
103 usercount
+=tg
->count
;
105 for(th
=tg
->hosts
;th
;th
=th
->next
)
109 snprintf(message
, sizeof(message
), "Trusts :%7d groups, %7d hosts, %7d users", groupcount
, hostcount
, usercount
);
110 triggerhook(HOOK_CORE_STATSREPLY
, message
);
114 int findtgext(const char *name
) {
117 for(i
=0;i
<MAXTGEXTS
;i
++)
118 if(tgextnames
[i
] && !strcmp(name
, tgextnames
[i
]->content
))
124 int registertgext(const char *name
) {
127 if(findtgext(name
) != -1) {
128 Error("trusts", ERR_WARNING
, "Tried to register duplicate trust group extension: %s.", name
);
132 for(i
=0;i
<MAXNICKEXTS
;i
++) {
134 tgextnames
[i
] = getsstring(name
, 100);
139 Error("trusts", ERR_WARNING
, "Tried to register too many trust group extensions: %s.", name
);
143 void releasetgext(int index
) {
146 freesstring(tgextnames
[index
]);
147 tgextnames
[index
] = NULL
;
149 for(tg
=tglist
;tg
;tg
=tg
->next
)
150 tg
->exts
[index
] = NULL
;
153 int trusts_fullyonline(void) {
157 return serverlist
[myhub
].linkstate
== LS_LINKED
;