]>
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"
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 ];
80 if ( th
-> marker
!= marker
)
83 cidrstr
= trusts_cidr2str ( th
-> ip
, th
-> mask
);
84 calculatespaces ( depth
+ 1 , 20 + 1 , cidrstr
, & prespacebuf
, & postspacebuf
);
86 if ( th
-> group
== originalgroup
) {
91 /* show the ids of other groups */
93 snprintf ( parentbuf
, sizeof ( parentbuf
), "%-10d %s " , th
-> group
-> id
, th
-> group
-> name
-> content
);
97 mask
= th
-> nodebits
- 96 ;
99 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
, mask
, parentbuf
);
101 for ( th
= th
-> children
; th
; th
= th
-> nextbychild
)
102 outputtree ( np
, marker
, originalgroup
, th
, depth
+ 1 );
105 static void displaygroup ( nick
* sender
, trustgroup
* tg
) {
110 time_t t
= time ( NULL
);
112 /* abusing the ternary operator a bit :( */
113 controlreply ( sender
, "Name: : %s " , tg
-> name
-> content
);
114 controlreply ( sender
, "Trusted for : %d " , tg
-> trustedfor
);
115 controlreply ( sender
, "Currently using : %d " , tg
-> count
);
116 controlreply ( sender
, "Clients per user : %d ( %s enforcing ident)" , tg
-> maxperident
, tg
-> mode
? "" : "not " );
117 controlreply ( sender
, "Contact: : %s " , tg
-> contact
-> content
);
118 controlreply ( sender
, "Expires in : %s " , ( tg
-> expires
)?(( tg
-> expires
> t
)? longtoduration ( tg
-> expires
- t
, 2 ): "(the past -- BUG)" ): "never" );
119 controlreply ( sender
, "Last changed by : %s " , tg
-> createdby
-> content
);
120 controlreply ( sender
, "Comment: : %s " , tg
-> comment
-> content
);
121 controlreply ( sender
, "ID: : %u " , tg
-> id
);
122 controlreply ( sender
, "Last used : %s " , ( tg
-> count
> 0 )? "(now)" :(( tg
-> lastseen
> 0 )? trusts_timetostr ( tg
-> lastseen
): "(never)" ));
123 controlreply ( sender
, "Max usage : %d " , tg
-> maxusage
);
124 controlreply ( sender
, "Last max reset : %s " , tg
-> lastmaxusereset
? trusts_timetostr ( tg
-> lastmaxusereset
): "(never)" );
126 controlreply ( sender
, "Host Current Max Last seen Max per Node Node Mask Group ID Group name" );
128 marker
= nextthmarker ();
129 array_init (& parents
, sizeof ( trusthost
*));
131 for ( th
= tg
-> hosts
; th
; th
= th
-> next
)
132 marktree (& parents
, marker
, th
);
134 p2
= ( trusthost
**)( parents
. content
);
135 for ( i
= 0 ; i
< parents
. cursi
; i
++)
136 outputtree ( sender
, marker
, tg
, p2
[ i
], 0 );
138 array_free (& parents
);
140 controlreply ( sender
, "End of list." );
143 static int trusts_cmdtrustlist ( void * source
, int cargc
, char ** cargv
) {
144 nick
* sender
= source
;
145 trustgroup
* tg
= NULL
;
146 int found
= 0 , remaining
= 50 ;
157 tg
= tg_strtotg ( name
);
160 displaygroup ( sender
, tg
);
164 if ( trusts_parsecidr ( name
, & ip
, & mask
)) {
165 th
= th_getbyhost ( ip
);
168 controlreply ( sender
, "Specified IP address is not trusted." );
172 displaygroup ( sender
, th
-> group
);
176 for ( tg
= tglist
; tg
; tg
= tg
-> next
) {
177 if ( match ( name
, tg
-> name
-> content
))
180 displaygroup ( sender
, tg
);
181 if (-- remaining
== 0 ) {
182 controlreply ( sender
, "Maximum number of matches reached." );
189 controlreply ( sender
, "No matches found." );
194 static int comparetgs ( const void * _a
, const void * _b
) {
195 const trustgroup
* a
= _a
;
196 const trustgroup
* b
= _b
;
205 static int trusts_cmdtrustdump ( void * source
, int argc
, char ** argv
) {
207 trustgroup
* tg
, ** atg
;
208 unsigned int wanted
, max
, maxid
, totalcount
, i
, groupcount
, linecount
;
211 if (( argc
< 2 ) || ( argv
[ 0 ][ 0 ] != '#' ))
214 wanted
= atoi (& argv
[ 0 ][ 1 ]);
217 for ( maxid
= totalcount
= 0 , tg
= tglist
; tg
; tg
= tg
-> next
) {
218 if ( totalcount
== 0 || tg
-> id
> maxid
)
224 if ( maxid
> totalcount
) {
225 controlreply ( np
, "Start ID cannot exceed current maximum group ID (# %u )" , maxid
);
229 atg
= nsmalloc ( POOL_TRUSTS
, sizeof ( trusthost
*) * totalcount
);
231 controlreply ( np
, "Memory error." );
235 for ( i
= 0 , tg
= tglist
; i
< totalcount
&& tg
; tg
= tg
-> next
, i
++)
238 qsort ( atg
, totalcount
, sizeof ( trustgroup
*), comparetgs
);
240 for ( i
= 0 ; i
< totalcount
; i
++)
241 if ( atg
[ i
]-> id
>= wanted
)
244 for ( groupcount
= linecount
= 0 ; i
< totalcount
; i
++) {
248 controlreply ( np
, "G, %s " , dumptg ( atg
[ i
], 1 ));
250 for ( th
= atg
[ i
]-> hosts
; th
; th
= th
-> next
) {
252 controlreply ( np
, "H, %s " , dumpth ( th
, 1 ));
258 nsfree ( POOL_TRUSTS
, atg
);
260 controlreply ( np
, "End of list, %u groups and %u lines returned." , groupcount
, linecount
);
264 static int trusts_cmdtrustgline ( void * source
, int cargc
, char ** cargv
) {
266 nick
* sender
= source
;
273 tg
= tg_strtotg ( cargv
[ 0 ]);
275 controlreply ( sender
, "Couldn't look up trustgroup." );
281 duration
= durationtolong ( cargv
[ 2 ]);
282 if (( duration
<= 0 ) || ( duration
> MAXDURATION
)) {
283 controlreply ( sender
, "Invalid duration supplied." );
289 count
= trustgline ( tg
, user
, duration
, reason
);
291 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
);
292 controlreply ( sender
, "Done. %d gline(s) set." , count
);
297 static int trusts_cmdtrustungline ( void * source
, int cargc
, char ** cargv
) {
299 nick
* sender
= source
;
306 tg
= tg_strtotg ( cargv
[ 0 ]);
308 controlreply ( sender
, "Couldn't look up trustgroup." );
314 count
= trustungline ( tg
, user
, 0 , "Deactivated." );
316 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
);
317 controlreply ( sender
, "Done. %d gline(s) removed." , count
);
322 static int commandsregistered
;
324 static void registercommands ( int hooknum
, void * arg
) {
325 if ( commandsregistered
)
327 commandsregistered
= 1 ;
329 registercontrolhelpcmd ( "trustlist" , NO_OPER
, 1 , trusts_cmdtrustlist
, "Usage: trustlist <#id|name|IP> \n Shows trust data for the specified trust group." );
330 registercontrolhelpcmd ( "trustdump" , NO_OPER
, 2 , trusts_cmdtrustdump
, "Usage: trustdump <#id> <number>" );
331 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." );
332 registercontrolhelpcmd ( "trustungline" , NO_OPER
, 4 , trusts_cmdtrustungline
, "Usage: trustungline <#id|name> <user> \n Unglines a user on all hosts of a trust group." );
335 static void deregistercommands ( int hooknum
, void * arg
) {
336 if (! commandsregistered
)
338 commandsregistered
= 0 ;
340 deregistercontrolcmd ( "trustlist" , trusts_cmdtrustlist
);
341 deregistercontrolcmd ( "trustdump" , trusts_cmdtrustdump
);
342 deregistercontrolcmd ( "trustgline" , trusts_cmdtrustgline
);
343 deregistercontrolcmd ( "trustungline" , trusts_cmdtrustungline
);
347 registerhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
348 registerhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
351 registercommands ( 0 , NULL
);
355 deregisterhook ( HOOK_TRUSTS_DB_LOADED
, registercommands
);
356 deregisterhook ( HOOK_TRUSTS_DB_CLOSED
, deregistercommands
);
358 deregistercommands ( 0 , NULL
);