]>
jfr.im git - irc/quakenet/newserv.git/blob - chanserv/usercmds/cleanupdb.c
1 /* Automatically generated by refactor.pl.
7 * CMDDESC: Clean up database.
8 * CMDFUNC: csu_docleanupdb
9 * CMDPROTO: int csu_docleanupdb(void *source, int cargc, char **cargv);
10 * CMDHELP: Usage: cleanupdb
11 * CMDHELP: Cleans up inactive accounts, unused accounts and inactive channels.
14 #include "../chanserv.h"
15 #include "../../lib/irc_string.h"
19 unsigned int cleanupdb_active
;
21 void csu_docleanupdb_real(DBConn
*dbconn
, void *arg
) {
22 nick
*sender
=getnickbynumeric((unsigned long)arg
);
23 reguser
*vrup
, *srup
, *founder
;
24 regchanuser
*rcup
, *nrcup
;
27 time_t t
, to_age
, unused_age
, maxchan_age
, authhistory_age
;
28 int expired
= 0, unauthed
= 0, chansvaped
= 0;
29 chanindex
*cip
, *ncip
;
32 unsigned int themarker
;
36 to_age
= t
- (CLEANUP_ACCOUNT_INACTIVE
* 3600 * 24);
37 unused_age
= t
- (CLEANUP_ACCOUNT_UNUSED
* 3600 * 24);
38 maxchan_age
= t
- (CLEANUP_CHANNEL_INACTIVE
* 3600 * 24);
39 authhistory_age
= t
- (CLEANUP_AUTHHISTORY
* 3600 * 24);
41 themarker
=nextauthnamemarker();
45 chanservsendmessage(sender
, "No DB connection, aborting cleanup.");
49 pgres
=dbgetresult(dbconn
);
51 if (!dbquerysuccessful(pgres
)) {
52 Error("chanserv", ERR_ERROR
, "Error loading cleanupdb data.");
54 chanservsendmessage(sender
, "DB error, aborting cleanup.");
58 while (dbfetchrow(pgres
)) {
59 id
=strtoul(dbgetvalue(pgres
, 0), NULL
, 10);
62 anp
->marker
=themarker
;
67 cs_log(sender
, "CLEANUPDB started");
70 chanservsendmessage(sender
, "Scanning regusers...");
71 for (i
=0;i
<REGUSERHASHSIZE
;i
++) {
72 for (vrup
=regusernicktable
[i
]; vrup
; vrup
=srup
) {
73 srup
=vrup
->nextbyname
;
74 if (!(anp
=findauthname(vrup
->ID
)))
75 continue; /* should maybe raise hell instead */
77 /* If this user has the right marker, this means the authtracker data
78 * indicates that they have been active recently */
79 if (anp
->marker
== themarker
)
82 if(!anp
->nicks
&& !UHasStaffPriv(vrup
) && !UIsCleanupExempt(vrup
)) {
83 if(vrup
->lastauth
&& (vrup
->lastauth
< to_age
)) {
85 cs_log(sender
, "CLEANUPDB inactive user %s %u", vrup
->username
, vrup
->ID
);
86 } else if(!vrup
->lastauth
&& (vrup
->created
< unused_age
)) {
88 cs_log(sender
, "CLEANUPDB unused user %s %u", vrup
->username
, vrup
->ID
);
99 chanservsendmessage(sender
, "Scanning chanindicies...");
101 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
102 for (cip
=chantable
[i
];cip
;cip
=ncip
) {
104 if (!(rcp
=cip
->exts
[chanservext
]))
107 if(rcp
->lastactive
< maxchan_age
) {
108 /* don't remove channels with the original founder as an oper */
109 founder
=findreguserbyID(rcp
->founder
);
110 if(founder
&& UHasOperPriv(founder
))
113 cs_log(sender
, "CLEANUPDB inactive channel %s", cip
->name
?cip
->name
->content
:"??");
114 cs_removechannel(rcp
, "Channel deleted due to lack of activity.");
118 /* Get rid of any dead chanlev entries */
119 for (j
=0;j
<REGCHANUSERHASHSIZE
;j
++) {
120 for (rcup
=rcp
->regusers
[j
];rcup
;rcup
=nrcup
) {
121 nrcup
=rcup
->nextbychan
;
125 chanservsendmessage(sender
, "Removing user %s from channel %s (no flags)",rcup
->user
->username
,rcp
->index
->name
->content
);
126 csdb_deletechanuser(rcup
);
127 delreguserfromchannel(rcp
, rcup
->user
);
135 chanservsendmessage(sender
, "Starting history database cleanup (will run in background).");
137 csdb_cleanuphistories(authhistory_age
);
139 cs_log(sender
, "CLEANUPDB complete %d inactive accounts %d unused accounts %d channels", expired
, unauthed
, chansvaped
);
141 chanservsendmessage(sender
, "Cleanup complete, %d accounts inactive for %d days, %d accounts weren't used within %d days, %d channels were inactive for %d days.", expired
, CLEANUP_ACCOUNT_INACTIVE
, unauthed
, CLEANUP_ACCOUNT_UNUSED
, chansvaped
, CLEANUP_CHANNEL_INACTIVE
);
147 int csu_docleanupdb(void *source
, int cargc
, char **cargv
) {
151 to_age
= time(NULL
) - (CLEANUP_ACCOUNT_INACTIVE
* 3600 * 24);
153 if (cleanupdb_active
) {
154 chanservsendmessage(sender
, "Cleanup already in progress.\n");
158 /* This query returns a single column containing the userids of all users
159 * who have active sessions now, or sessions which ended in the last
160 * CLEANUP_ACCOUNT_INACTIVE days. */
161 q9u_asyncquery(csu_docleanupdb_real
, (void *)sender
->numeric
,
162 "SELECT userID from chanserv.authhistory WHERE disconnecttime=0 OR disconnecttime > %d GROUP BY userID", to_age
);
164 chanservsendmessage(sender
, "Retrieving auth history data, cleanup will proceed when done.");