]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/usercmds/cleanupdb.c
Merge.
[irc/quakenet/newserv.git] / chanserv / usercmds / cleanupdb.c
1 /* Automatically generated by refactor.pl.
2 *
3 *
4 * CMDNAME: cleanupdb
5 * CMDLEVEL: QCMD_DEV
6 * CMDARGS: 0
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.
12 */
13
14 #include "../chanserv.h"
15 #include "../../lib/irc_string.h"
16 #include <stdio.h>
17 #include <string.h>
18
19 int csu_docleanupdb(void *source, int cargc, char **cargv) {
20 nick *sender=source;
21 reguser *vrup, *srup, *founder;
22 regchanuser *rcup, *nrcup;
23 authname *anp;
24 int i,j;
25 time_t t;
26 long to_age, unused_age, maxchan_age;
27 int expired = 0, unauthed = 0, chansvaped = 0;
28 chanindex *cip, *ncip;
29 regchan *rcp;
30
31 t = time(NULL);
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);
35
36 cs_log(sender, "CLEANUPDB started");
37
38 chanservsendmessage(sender, "Scanning regusers...");
39 for (i=0;i<REGUSERHASHSIZE;i++) {
40 for (vrup=regusernicktable[i]; vrup; vrup=srup) {
41 srup=vrup->nextbyname;
42 if (!(anp=findauthname(vrup->ID)))
43 continue; /* should maybe raise hell instead */
44
45 if(!anp->nicks && !UHasStaffPriv(vrup) && !UIsCleanupExempt(vrup)) {
46 if(vrup->lastauth && (vrup->lastauth < to_age)) {
47 expired++;
48 cs_log(sender, "CLEANUPDB inactive user %s %u", vrup->username, vrup->ID);
49 } else if(!vrup->lastauth && (vrup->created < unused_age)) {
50 unauthed++;
51 cs_log(sender, "CLEANUPDB unused user %s %u", vrup->username, vrup->ID);
52 } else {
53 continue;
54 }
55
56 cs_removeuser(vrup);
57 }
58 }
59 }
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 }
78
79 /* Get rid of any dead chanlev entries */
80 for (j=0;j<REGCHANUSERHASHSIZE;j++) {
81 for (rcup=rcp->regusers[j];rcup;rcup=nrcup) {
82 nrcup=rcup->nextbychan;
83
84 if (!rcup->flags) {
85 chanservsendmessage(sender, "Removing user %s from channel %s (no flags)",rcup->user->username,rcp->index->name->content);
86 csdb_deletechanuser(rcup);
87 delreguserfromchannel(rcp, rcup->user);
88 }
89 }
90 }
91 }
92 }
93
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);
96 return CMD_OK;
97 }