]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts_commands.c
a01605d2d75a8b668cccb9757936889725652eed
3 #include "../control/control.h"
4 #include "../lib/irc_string.h"
5 #include "../lib/strlfunc.h"
6 #include "../core/nsmalloc.h"
9 static void registercommands ( int , void *);
10 static void deregistercommands ( int , void *);
12 void calculatespaces ( int spaces
, int width
, char * str
, char ** _prebuf
, char ** _postbuf
) {
13 static char prebuf
[ 512 ], postbuf
[ 512 ];
16 if ( spaces
+ 5 >= sizeof ( prebuf
)) {
17 prebuf
[ 0 ] = prebuf
[ 1 ] = '\0' ;
19 memset ( prebuf
, ' ' , spaces
);
20 prebuf
[ spaces
] = '\0' ;
23 spacelen
= width
- ( strlen ( str
) + spaces
);
24 if ( spacelen
<= 0 || spacelen
+ 5 >= sizeof ( postbuf
)) {
25 postbuf
[ 0 ] = postbuf
[ 1 ] = '\0' ;
27 memset ( postbuf
, ' ' , spacelen
);
28 postbuf
[ spacelen
] = '\0' ;
35 static void traverseandmark ( unsigned int marker
, trusthost
* th
) {
38 for ( th
= th
-> children
; th
; th
= th
-> nextbychild
) {
40 traverseandmark ( marker
, th
);
44 static void insertth ( array
* parents
, trusthost
* th
) {
46 trusthost
** p2
= ( trusthost
**)( parents
-> content
);
48 /* this eliminates common subtrees */
49 for ( i
= 0 ; i
< parents
-> cursi
; i
++)
53 if ( i
== parents
-> cursi
) {
54 int pos
= array_getfreeslot ( parents
);
55 (( trusthost
**)( parents
-> content
))[ pos
] = th
;
59 static void marktree ( array
* parents
, unsigned int marker
, trusthost
* th
) {
63 for ( pth
= th
-> parent
; pth
; pth
= pth
-> next
) {
64 insertth ( parents
, pth
);
70 insertth ( parents
, th
);
72 /* sadly we need to recurse down */
73 traverseandmark ( marker
, th
);
76 static void outputtree ( nick
* np
, unsigned int marker
, trustgroup
* originalgroup
, trusthost
* th
, int depth
) {
77 char * cidrstr
, * prespacebuf
, * postspacebuf
, parentbuf
[ 512 ];
79 if ( th
-> marker
!= marker
)
82 cidrstr
= trusts_cidr2str ( th
-> ip
, th
-> mask
);
83 calculatespaces ( depth
+ 1 , 20 + 1 , cidrstr
, & prespacebuf
, & postspacebuf
);
85 if ( th
-> group
== originalgroup
) {
90 /* show the ids of other groups */
92 snprintf ( parentbuf
, sizeof ( parentbuf
), "%-10d %s " , th
-> group
-> id
, th
-> group
-> name
-> content
);
95 controlreply ( np
, " %s%s%s %-10d %-10d %-21s %s " , prespacebuf
, cidrstr
, postspacebuf
, th
-> count
, th
-> maxusage
, ( th
-> count
> 0 )? "(now)" :(( th
-> lastseen
> 0 )? trusts_timetostr ( th
-> lastseen
): "(never)" ), parentbuf
);
97 for ( th
= th
-> children
; th
; th
= th
-> nextbychild
)
98 outputtree ( np
, marker
, originalgroup
, th
, depth
+ 1 );
101 static void displaygroup ( nick
* sender
, trustgroup
* tg
) {
106 time_t t
= time ( NULL
);
108 /* abusing the ternary operator a bit :( */
109 controlreply ( sender
, "Name: : %s " , tg
-> name
-> content
);
110 controlreply ( sender
, "Trusted for : %d " , tg
-> trustedfor
);
111 controlreply ( sender
, "Currently using : %d " , tg
-> count
);
112 controlreply ( sender
, "Clients per user : %d ( %s enforcing ident)" , tg
-> maxperident
, tg
-> mode
? "" : "not " );
113 controlreply ( sender
, "Contact: : %s " , tg
-> contact
-> content
);
114 controlreply ( sender
, "Expires in : %s " , ( tg
-> expires
> t
)? longtoduration ( tg
-> expires
- t
, 2 ): "(the past -- BUG)" );
115 controlreply ( sender
, "Last changed by : %s " , tg
-> createdby
-> content
);
116 controlreply ( sender
, "Comment: : %s " , tg
-> comment
-> content
);
117 controlreply ( sender
, "ID: : %u " , tg
-> id
);
118 controlreply ( sender
, "Last used : %s " , ( tg
-> count
> 0 )? "(now)" :(( tg
-> lastseen
> 0 )? trusts_timetostr ( tg
-> lastseen
): "(never)" ));
119 controlreply ( sender
, "Max usage : %d " , tg
-> maxusage
);
120 controlreply ( sender
, "Last max reset : %s " , tg
-> lastmaxusereset
? trusts_timetostr ( tg
-> lastmaxusereset
): "(never)" );
122 controlreply ( sender
, "Host Current Max Last seen Group ID Group name" );
124 marker
= nextthmarker ();
125 array_init (& parents
, sizeof ( trusthost
*));
127 for ( th
= tg
-> hosts
; th
; th
= th
-> next
)
128 marktree (& parents
, marker
, th
);
130 p2
= ( trusthost
**)( parents
. content
);
131 for ( i
= 0 ; i
< parents
. cursi
; i
++)
132 outputtree ( sender
, marker
, tg
, p2
[ i
], 0 );
134 array_free (& parents
);
136 controlreply ( sender
, "End of list." );
139 static int trusts_cmdtrustlist ( void * source
, int cargc
, char ** cargv
) {
140 nick
* sender
= source
;
142 trustgroup
* tg
= NULL
;
143 int id
, found
= 0 , remaining
= 50 ;
151 tg
= tg_strtotg ( name
+ 1 );
153 if (( id
= strtol ( name
, NULL
, 10 )))
157 displaygroup ( sender
, tg
);
161 for ( tg
= tglist
; tg
; tg
= tg
-> next
) {
162 if ( match ( tg
-> name
-> content
, name
))
165 displaygroup ( sender
, tg
);
166 if (-- remaining
== 0 ) {
167 controlreply ( sender
, "Maximum number of matches reached." );
174 controlreply ( sender
, "No matches found." );
179 static int comparetgs ( const void * _a
, const void * _b
) {
180 const trustgroup
* a
= _a
;
181 const trustgroup
* b
= _b
;
190 static int trusts_cmdtrustdump ( void * source
, int argc
, char ** argv
) {
192 trustgroup
* tg
, ** atg
;
193 unsigned int wanted
, max
, maxid
, totalcount
, i
, groupcount
, linecount
;
196 if (( argc
< 2 ) || ( argv
[ 0 ][ 0 ] != '#' ))
199 wanted
= atoi (& argv
[ 0 ][ 1 ]);
202 for ( maxid
= totalcount
= 0 , tg
= tglist
; tg
; tg
= tg
-> next
) {
203 if ( totalcount
== 0 || tg
-> id
> maxid
)
209 if ( maxid
> totalcount
) {
210 controlreply ( np
, "Start ID cannot exceed current maximum group ID (# %u )" , maxid
);
214 atg
= nsmalloc ( POOL_TRUSTS
, sizeof ( trusthost
*) * totalcount
);
216 controlreply ( np
, "Memory error." );
220 for ( i
= 0 , tg
= tglist
; i
< totalcount
&& th
; tg
= tg
-> next
, i
++)
223 qsort ( atg
, totalcount
, sizeof ( trustgroup
*), comparetgs
);
225 for ( i
= 0 ; i
< totalcount
; i
++)
226 if ( atg
[ i
]-> id
>= wanted
)
229 for ( groupcount
= linecount
= 0 ; i
< totalcount
; i
++) {
233 controlreply ( np
, "G, %s " , dumptg ( atg
[ i
], 1 ));
235 for ( th
= atg
[ i
]-> hosts
; th
; th
= th
-> next
) {
237 controlreply ( np
, "H, %s " , dumpth ( th
, 1 ));
243 nsfree ( POOL_TRUSTS
, atg
);
245 controlreply ( np
, "End of list, %u groups and %u lines returned." , groupcount
, linecount
);
249 static int commandsregistered
;
251 static void registercommands ( int hooknum
, void * arg
) {
252 if ( commandsregistered
)
254 commandsregistered
= 1 ;
256 registercontrolhelpcmd ( "trustlist" , NO_OPER
, 1 , trusts_cmdtrustlist
, "Usage: trustlist <#id|name|id> \n Shows trust data for the specified trust group." );
257 registercontrolhelpcmd ( "trustdump" , NO_OPER
, 2 , trusts_cmdtrustdump
, "Usage: trustdump <#id> <number>" );
260 static void deregistercommands ( int hooknum
, void * arg
) {
261 if (! commandsregistered
)
263 commandsregistered
= 0 ;
265 deregistercontrolcmd ( "trustlist" , trusts_cmdtrustlist
);
266 deregistercontrolcmd ( "trustdump" , trusts_cmdtrustdump
);
270 registerhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
271 registerhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
274 registercommands ( 0 , NULL
);
278 deregisterhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
279 deregisterhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
281 deregistercommands ( 0 , NULL
);