]>
jfr.im git - irc/quakenet/newserv.git/blob - trusts/trusts_commands.c
3 #include "../control/control.h"
4 #include "../lib/irc_string.h"
5 #include "../lib/strlfunc.h"
6 #include "../core/nsmalloc.h"
7 #include "../irc/irc.h"
8 #include "../newsearch/newsearch.h"
10 #include "newsearch/trusts_newsearch.h"
12 static void registercommands ( int , void *);
13 static void deregistercommands ( int , void *);
15 extern void printnick_channels ( searchCtx
*, nick
*, nick
*);
17 void calculatespaces ( int spaces
, int width
, char * str
, char ** _prebuf
, char ** _postbuf
) {
18 static char prebuf
[ 512 ], postbuf
[ 512 ];
21 if ( spaces
+ 5 >= sizeof ( prebuf
)) {
22 prebuf
[ 0 ] = prebuf
[ 1 ] = '\0' ;
24 memset ( prebuf
, ' ' , spaces
);
25 prebuf
[ spaces
] = '\0' ;
28 spacelen
= width
- ( strlen ( str
) + spaces
);
29 if ( spacelen
<= 0 || spacelen
+ 5 >= sizeof ( postbuf
)) {
30 postbuf
[ 0 ] = postbuf
[ 1 ] = '\0' ;
32 memset ( postbuf
, ' ' , spacelen
);
33 postbuf
[ spacelen
] = '\0' ;
40 static void traverseandmark ( unsigned int marker
, trusthost
* th
) {
43 for ( th
= th
-> children
; th
; th
= th
-> nextbychild
) {
45 traverseandmark ( marker
, th
);
49 static void insertth ( array
* parents
, trusthost
* th
) {
51 trusthost
** p2
= ( trusthost
**)( parents
-> content
);
53 /* this eliminates common subtrees */
54 for ( i
= 0 ; i
< parents
-> cursi
; i
++)
58 if ( i
== parents
-> cursi
) {
59 int pos
= array_getfreeslot ( parents
);
60 (( trusthost
**)( parents
-> content
))[ pos
] = th
;
64 static void marktree ( array
* parents
, unsigned int marker
, trusthost
* th
) {
68 for ( pth
= th
-> parent
; pth
; pth
= pth
-> parent
) {
69 insertth ( parents
, pth
);
75 insertth ( parents
, th
);
77 /* sadly we need to recurse down */
78 traverseandmark ( marker
, th
);
81 static void outputtree ( nick
* np
, unsigned int marker
, trustgroup
* originalgroup
, trusthost
* th
, int depth
) {
82 char * cidrstr
, * prespacebuf
, * postspacebuf
, parentbuf
[ 512 ];
84 if ( th
-> marker
!= marker
)
87 cidrstr
= trusts_cidr2str (& th
-> ip
, th
-> bits
);
88 calculatespaces ( depth
+ 1 , 30 + 1 , cidrstr
, & prespacebuf
, & postspacebuf
);
90 if ( th
-> group
== originalgroup
) {
95 /* show the ids of other groups */
97 snprintf ( parentbuf
, sizeof ( parentbuf
), "%-10d %s " , th
-> group
-> id
, th
-> group
-> name
-> content
);
100 controlreply ( np
, " %s%s%s %-10d %-10d %-21s %-15d /%-14d %s " , prespacebuf
, cidrstr
, postspacebuf
, th
-> count
, th
-> maxusage
, ( th
-> count
> 0 )? "(now)" :(( th
-> lastseen
> 0 )? trusts_timetostr ( th
-> lastseen
): "(never)" ), th
-> maxpernode
, ( irc_in_addr_is_ipv4 (& th
-> ip
))?( th
-> nodebits
- 96 ): th
-> nodebits
, parentbuf
);
102 /* Make sure we're not seeing this subtree again. */
105 for ( th
= th
-> children
; th
; th
= th
-> nextbychild
)
106 outputtree ( np
, marker
, originalgroup
, th
, depth
+ 1 );
109 static void displaygroup ( nick
* sender
, trustgroup
* tg
) {
114 time_t t
= getnettime ();
116 /* abusing the ternary operator a bit :( */
117 controlreply ( sender
, "Name: : %s " , tg
-> name
-> content
);
118 controlreply ( sender
, "Trusted for : %d " , tg
-> trustedfor
);
119 controlreply ( sender
, "Currently using : %d " , tg
-> count
);
120 controlreply ( sender
, "Clients per user : %d ( %s enforcing ident)" , tg
-> maxperident
, tg
-> mode
? "" : "not " );
121 controlreply ( sender
, "Contact: : %s " , tg
-> contact
-> content
);
122 controlreply ( sender
, "Expires in : %s " , ( tg
-> expires
)?(( tg
-> expires
> t
)? longtoduration ( tg
-> expires
- t
, 2 ): "the past (will be removed during next cleanup)" ): "never" );
123 controlreply ( sender
, "Last changed by : %s " , tg
-> createdby
-> content
);
124 controlreply ( sender
, "Comment: : %s " , tg
-> comment
-> content
);
125 controlreply ( sender
, "ID: : %u " , tg
-> id
);
126 controlreply ( sender
, "Last used : %s " , ( tg
-> count
> 0 )? "(now)" :(( tg
-> lastseen
> 0 )? trusts_timetostr ( tg
-> lastseen
): "(never)" ));
127 controlreply ( sender
, "Max usage : %d " , tg
-> maxusage
);
128 controlreply ( sender
, "Last max reset : %s " , tg
-> lastmaxusereset
? trusts_timetostr ( tg
-> lastmaxusereset
): "(never)" );
130 controlreply ( sender
, "Host Current Max Last seen Max per Node Node Mask Group ID Group name" );
132 marker
= nextthmarker ();
133 array_init (& parents
, sizeof ( trusthost
*));
135 for ( th
= tg
-> hosts
; th
; th
= th
-> next
)
136 marktree (& parents
, marker
, th
);
138 p2
= ( trusthost
**)( parents
. content
);
139 for ( i
= 0 ; i
< parents
. cursi
; i
++)
140 outputtree ( sender
, marker
, tg
, p2
[ i
], 0 );
142 array_free (& parents
);
144 controlreply ( sender
, "End of list." );
147 static int trusts_cmdtrustlist ( void * source
, int cargc
, char ** cargv
) {
148 nick
* sender
= source
;
149 trustgroup
* tg
= NULL
;
150 int found
= 0 , remaining
= 50 ;
153 struct irc_in_addr ip
;
161 tg
= tg_strtotg ( name
);
164 displaygroup ( sender
, tg
);
168 if ( ipmask_parse ( name
, & ip
, & bits
)) {
169 th
= th_getbyhost (& ip
);
172 controlreply ( sender
, "Specified IP address is not trusted." );
176 displaygroup ( sender
, th
-> group
);
180 for ( tg
= tglist
; tg
; tg
= tg
-> next
) {
181 if ( match ( name
, tg
-> name
-> content
))
184 displaygroup ( sender
, tg
);
185 if (-- remaining
== 0 ) {
186 controlreply ( sender
, "Maximum number of matches reached." );
193 controlreply ( sender
, "No matches found." );
198 static int comparetgs ( const void * _a
, const void * _b
) {
199 const trustgroup
* a
= _a
;
200 const trustgroup
* b
= _b
;
209 static int trusts_cmdtrustdump ( void * source
, int argc
, char ** argv
) {
211 trustgroup
* tg
, ** atg
;
212 unsigned int wanted
, max
, maxid
, totalcount
, i
, groupcount
, linecount
;
215 if (( argc
< 2 ) || ( argv
[ 0 ][ 0 ] != '#' ))
218 wanted
= atoi (& argv
[ 0 ][ 1 ]);
221 for ( maxid
= totalcount
= 0 , tg
= tglist
; tg
; tg
= tg
-> next
) {
222 if ( totalcount
== 0 || tg
-> id
> maxid
)
228 if ( maxid
> totalcount
) {
229 controlreply ( np
, "Start ID cannot exceed current maximum group ID (# %u )" , maxid
);
233 atg
= nsmalloc ( POOL_TRUSTS
, sizeof ( trusthost
*) * totalcount
);
235 controlreply ( np
, "Memory error." );
239 for ( i
= 0 , tg
= tglist
; i
< totalcount
&& tg
; tg
= tg
-> next
, i
++)
242 qsort ( atg
, totalcount
, sizeof ( trustgroup
*), comparetgs
);
244 for ( i
= 0 ; i
< totalcount
; i
++)
245 if ( atg
[ i
]-> id
>= wanted
)
248 for ( groupcount
= linecount
= 0 ; i
< totalcount
; i
++) {
252 controlreply ( np
, "G, %s " , dumptg ( atg
[ i
], 1 ));
254 for ( th
= atg
[ i
]-> hosts
; th
; th
= th
-> next
) {
256 controlreply ( np
, "H, %s " , dumpth ( th
, 1 ));
262 nsfree ( POOL_TRUSTS
, atg
);
264 controlreply ( np
, "End of list, %u groups and %u lines returned." , groupcount
, linecount
);
268 static int trusts_cmdtrustgline ( void * source
, int cargc
, char ** cargv
) {
270 nick
* sender
= source
;
277 tg
= tg_strtotg ( cargv
[ 0 ]);
279 controlreply ( sender
, "Couldn't look up trustgroup." );
285 duration
= durationtolong ( cargv
[ 2 ]);
286 if (( duration
<= 0 ) || ( duration
> MAXDURATION
)) {
287 controlreply ( sender
, "Invalid duration supplied." );
293 count
= trustgline ( tg
, user
, duration
, reason
);
295 controlwall ( NO_OPER
, NL_GLINES
| NL_TRUSTS
, " %s TRUSTGLINE'd user ' %s ' on group ' %s ', %d gline(s) set." , controlid ( sender
), user
, tg
-> name
-> content
, count
);
296 controlreply ( sender
, "Done. %d gline(s) set." , count
);
301 static int trusts_cmdtrustungline ( void * source
, int cargc
, char ** cargv
) {
303 nick
* sender
= source
;
310 tg
= tg_strtotg ( cargv
[ 0 ]);
312 controlreply ( sender
, "Couldn't look up trustgroup." );
318 count
= trustungline ( tg
, user
, 0 , "Deactivated." );
320 controlwall ( NO_OPER
, NL_GLINES
| NL_TRUSTS
, " %s TRUSTUNGLINE'd user ' %s ' on group ' %s ', %d gline(s) removed." , controlid ( sender
), user
, tg
-> name
-> content
, count
);
321 controlreply ( sender
, "Done. %d gline(s) removed." , count
);
326 static int trusts_cmdtrustspew ( void * source
, int cargc
, char ** cargv
) {
327 nick
* sender
= source
;
333 tree
= NSASTNode ( tgroup_parse
, NSASTLiteral ( cargv
[ 0 ]));
334 return ast_nicksearch (& tree
, controlreply
, sender
, NULL
, printnick_channels
, NULL
, NULL
, 2000 );
337 static int commandsregistered
;
339 static void registercommands ( int hooknum
, void * arg
) {
340 if ( commandsregistered
)
342 commandsregistered
= 1 ;
344 registercontrolhelpcmd ( "trustlist" , NO_OPER
, 1 , trusts_cmdtrustlist
, "Usage: trustlist <#id|name|IP> \n Shows trust data for the specified trust group." );
345 registercontrolhelpcmd ( "trustdump" , NO_OPER
, 2 , trusts_cmdtrustdump
, "Usage: trustdump <#id> <number>" );
346 registercontrolhelpcmd ( "trustgline" , NO_OPER
, 4 , trusts_cmdtrustgline
, "Usage: trustgline <#id|name> <user> <duration> <reason> \n Glines a user on all hosts of a trust group." );
347 registercontrolhelpcmd ( "trustungline" , NO_OPER
, 4 , trusts_cmdtrustungline
, "Usage: trustungline <#id|name> <user> \n Unglines a user on all hosts of a trust group." );
348 registercontrolhelpcmd ( "trustspew" , NO_OPER
, 1 , trusts_cmdtrustspew
, "Usage: trustspew <#id|name> \n Shows currently connected users for the specified trust group." );
351 static void deregistercommands ( int hooknum
, void * arg
) {
352 if (! commandsregistered
)
354 commandsregistered
= 0 ;
356 deregistercontrolcmd ( "trustlist" , trusts_cmdtrustlist
);
357 deregistercontrolcmd ( "trustdump" , trusts_cmdtrustdump
);
358 deregistercontrolcmd ( "trustgline" , trusts_cmdtrustgline
);
359 deregistercontrolcmd ( "trustungline" , trusts_cmdtrustungline
);
360 deregistercontrolcmd ( "trustspew" , trusts_cmdtrustspew
);
364 registerhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
365 registerhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
368 registercommands ( 0 , NULL
);
372 deregisterhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
373 deregisterhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
375 deregistercommands ( 0 , NULL
);