]> jfr.im git - irc/quakenet/newserv.git/blame - chanserv/usercmds/cleanupdb.c
Merge.
[irc/quakenet/newserv.git] / chanserv / usercmds / cleanupdb.c
CommitLineData
7ac9dbae
P
1/* Automatically generated by refactor.pl.
2 *
3 *
4 * CMDNAME: cleanupdb
85174237 5 * CMDLEVEL: QCMD_DEV
7ac9dbae 6 * CMDARGS: 0
85174237 7 * CMDDESC: Clean up database.
7ac9dbae
P
8 * CMDFUNC: csu_docleanupdb
9 * CMDPROTO: int csu_docleanupdb(void *source, int cargc, char **cargv);
85174237 10 * CMDHELP: Usage: cleanupdb
a6f1f6b6 11 * CMDHELP: Cleans up inactive accounts, unused accounts and inactive channels.
7ac9dbae
P
12 */
13
14#include "../chanserv.h"
15#include "../../lib/irc_string.h"
16#include <stdio.h>
17#include <string.h>
18
19int csu_docleanupdb(void *source, int cargc, char **cargv) {
20 nick *sender=source;
a6f1f6b6 21 reguser *vrup, *srup, *founder;
e9484491 22 regchanuser *rcup, *nrcup;
23b85e10 23 authname *anp;
e9484491 24 int i,j;
cf50e022 25 time_t t;
a6f1f6b6
CP
26 long to_age, unused_age, maxchan_age;
27 int expired = 0, unauthed = 0, chansvaped = 0;
28 chanindex *cip, *ncip;
29 regchan *rcp;
cf50e022
CP
30
31 t = time(NULL);
a6f1f6b6
CP
32 to_age = t - (CLEANUP_ACCOUNT_INACTIVE * 3600 * 24);
33 unused_age = t - (CLEANUP_ACCOUNT_UNUSED * 3600 * 24);
34 maxchan_age = t - (CLEANUP_CHANNEL_INACTIVE * 3600 * 24);
7ac9dbae 35
a6f1f6b6
CP
36 cs_log(sender, "CLEANUPDB started");
37
38 chanservsendmessage(sender, "Scanning regusers...");
7ac9dbae
P
39 for (i=0;i<REGUSERHASHSIZE;i++) {
40 for (vrup=regusernicktable[i]; vrup; vrup=srup) {
41 srup=vrup->nextbyname;
23b85e10 42 if (!(anp=findauthname(vrup->ID)))
43 continue; /* should maybe raise hell instead */
a6f1f6b6 44
cf50e022
CP
45 if(!anp->nicks && !UHasHelperPriv(vrup) && !UIsCleanupExempt(vrup)) {
46 if(vrup->lastauth && (vrup->lastauth < to_age)) {
47 expired++;
a6f1f6b6 48 cs_log(sender, "CLEANUPDB inactive user %s", vrup->username);
cf50e022
CP
49 } else if(!vrup->lastauth && (vrup->created < unused_age)) {
50 unauthed++;
a6f1f6b6 51 cs_log(sender, "CLEANUPDB unused user %s", vrup->username);
cf50e022
CP
52 } else {
53 continue;
54 }
55
7ac9dbae 56 cs_removeuser(vrup);
7ac9dbae
P
57 }
58 }
59 }
a6f1f6b6
CP
60
61 chanservsendmessage(sender, "Scanning chanindicies...");
62 for (i=0;i<CHANNELHASHSIZE;i++) {
63 for (cip=chantable[i];cip;cip=ncip) {
64 ncip=cip->next;
65 if (!(rcp=cip->exts[chanservext]))
66 continue;
67
68 if(rcp->lastactive < maxchan_age) {
69 /* don't remove channels with the original founder as an oper */
70 founder=findreguserbyID(rcp->founder);
71 if(founder && UHasOperPriv(founder))
72 continue;
73
74 cs_log(sender, "CLEANUPDB inactive channel %s", cip->name?cip->name->content:"??");
75 cs_removechannel(rcp);
76 chansvaped++;
77 }
374a30be 78
79 /* Get rid of any dead chanlev entries */
80 for (j=0;j<REGCHANUSERHASHSIZE;j++) {
e9484491 81 for (rcup=rcp->regusers[j];rcup;rcup=nrcup) {
374a30be 82 nrcup=rcup->nextbychan;
83
84 if (!rcup->flags) {
e9484491 85 chanservsendmessage(sender, "Removing user %s from channel %s (no flags)",rcup->user->username,rcp->index->name->content);
374a30be 86 csdb_deletechanuser(rcup);
87 delreguserfromchannel(rcp, rcup->user);
374a30be 88 }
89 }
90 }
a6f1f6b6
CP
91 }
92 }
7ac9dbae 93
a6f1f6b6
CP
94 cs_log(sender, "CLEANUPDB complete %d inactive accounts %d unused accounts %d channels", expired, unauthed, chansvaped);
95 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);
80d2de64 96 return CMD_OK;
7ac9dbae 97}