#include <string.h>
#include <time.h>
#include <stdarg.h>
+#include <stdint.h>
+
#include "../lib/sstring.h"
#include "../core/schedule.h"
#include "../lib/flags.h"
#include "../channel/channel.h"
#include "../parser/parser.h"
#include "../localuser/localuserchannel.h"
+
+#ifndef CS_NODB
#include "../dbapi/dbapi.h"
+#endif
#define CS_PARANOID
/* Q9 Version */
#define QVERSION "1.00"
+/* Achievements: Nothing before ACHIEVEMENTS_START
+ * opt-in only after ACHIEVEMENTS_END
+ * Titles are valid only between ACHIEVEMENTS_START and ACHIEVEMENTS_END */
+
+//#define ACH_TEST_APRIL1
+
+#ifdef ACH_TEST_APRIL1
+
+#define ACHIEVEMENTS_START 1269702917
+#define ACHIEVEMENTS_END 1270162800
+
+#elif defined(ACH_TEST_POSTAPRIL1)
+
+#define ACHIEVEMENTS_START 0
+#define ACHIEVEMENTS_END 1269702917
+
+#else
+
+#define ACHIEVEMENTS_START 1270076400
+#define ACHIEVEMENTS_END 1270162800
+
+#endif
+
/* Mini-hash of known users on channels to make lookups faster;
* how big do we make it? */
#define REGCHANUSERHASHSIZE 5
#define MAXSUSPENDHIT 500
/* Maximum number of times a user may attempt to auth */
-#define MAXAUTHATTEMPT 5
+#define MAXAUTHATTEMPT 10
/* Maximum number of hellos in a session */
-#define MAXHELLOS 3
+#define MAXHELLOS 2
/* Maximum number of times a user may actually be authed */
#define MAXAUTHCOUNT 2
/* Cleanup options */
#define CLEANUP_ACCOUNT_INACTIVE 80
-#define CLEANUP_ACCOUNT_UNUSED 10
+#define CLEANUP_ACCOUNT_UNUSED 3
#define CLEANUP_CHANNEL_INACTIVE 40
+#define CLEANUP_AUTHHISTORY 60
+
+#define CLEANUP_MIN_CHAN_SIZE 2
+
/* Sizes of the main hashes */
#define REGUSERHASHSIZE 60000
#define MAILDOMAINHASHSIZE 60000
#define ENTROPYSOURCE "/dev/urandom"
#define ENTROPYLEN 8
+/* Minimum acceptable reason length for stuff like deluser */
+#define MIN_REASONLEN 20
+
#include "chanserv_messages.h"
+#define CSMIN(a, b) ((a)<(b)?(a):(b))
+#define CSMAX(a, b) ((a)>(b)?(a):(b))
+
/* List of privileged operations */
#define QPRIV_SUSPENDBYPASS 1
#define QPRIV_VIEWEMAIL 109
#define QPRIV_VIEWCHANSUSPENSION 110
#define QPRIV_VIEWSUSPENDEDBY 111
+#define QPRIV_VIEWWALLMESSAGE 112
#define QPRIV_CHANGECHANFLAGS 200
#define QPRIV_CHANGECHANLEV 201
/* User flags */
-#define QUFLAG_NOINFO 0x0001 /* +s */
+/* to reenable this also grep for NOINFO... as some code has been commented out */
+//#define QUFLAG_NOINFO 0x0001 /* +s */
+#define QUFLAG_INACTIVE 0x0001 /* +I */
#define QUFLAG_GLINE 0x0002 /* +g */
#define QUFLAG_NOTICE 0x0004 /* +n */
#define QUFLAG_STAFF 0x0008 /* +q */
#define QUFLAG_INFO 0x0400 /* +i */
#define QUFLAG_DELAYEDGLINE 0x0800 /* +G */
#define QUFLAG_NOAUTHLIMIT 0x1000 /* +L */
+#define QUFLAG_ACHIEVEMENTS 0x2000 /* +c */
#define QUFLAG_CLEANUPEXEMPT 0x4000 /* +D */
#define QUFLAG_TRUST 0x8000 /* +T */
-#define QUFLAG_ALL 0xdfff
+#define QUFLAG_ALL 0xffff
-#define UIsNoInfo(x) ((x)->flags & QUFLAG_NOINFO)
+//#define UIsNoInfo(x) ((x)->flags & QUFLAG_NOINFO)
+#define UIsNoInfo(x) 0
+#define UIsInactive(x) ((x)->flags & QUFLAG_INACTIVE)
#define UIsGline(x) ((x)->flags & QUFLAG_GLINE)
#define UIsNotice(x) ((x)->flags & QUFLAG_NOTICE)
#define UIsSuspended(x) ((x)->flags & QUFLAG_SUSPENDED)
#define UIsNoAuthLimit(x) ((x)->flags & QUFLAG_NOAUTHLIMIT)
#define UIsCleanupExempt(x) ((x)->flags & QUFLAG_CLEANUPEXEMPT)
#define UIsStaff(x) ((x)->flags & QUFLAG_STAFF)
+#define UIsAchievements(x) ((x)->flags & QUFLAG_ACHIEVEMENTS)
#define UHasSuspension(x) ((x)->flags & (QUFLAG_GLINE|QUFLAG_DELAYEDGLINE|QUFLAG_SUSPENDED))
-#define UHasStaffPriv(x) ((x)->flags * (QUFLAG_STAFF | QUFLAG_HELPER | QUFLAG_OPER | QUFLAG_ADMIN | QUFLAG_DEV))
+#define UHasStaffPriv(x) ((x)->flags & (QUFLAG_STAFF | QUFLAG_HELPER | QUFLAG_OPER | QUFLAG_ADMIN | QUFLAG_DEV))
#define UHasHelperPriv(x) ((x)->flags & (QUFLAG_HELPER | QUFLAG_OPER | QUFLAG_ADMIN | QUFLAG_DEV))
#define UHasOperPriv(x) ((x)->flags & (QUFLAG_OPER | QUFLAG_ADMIN | QUFLAG_DEV))
#define UHasAdminPriv(x) ((x)->flags & (QUFLAG_ADMIN | QUFLAG_DEV))
+#define USetInactive(x) ((x)->flags |= QUFLAG_INACTIVE)
#define USetGline(x) ((x)->flags |= QUFLAG_GLINE)
#define USetNotice(x) ((x)->flags |= QUFLAG_NOTICE)
#define USetSuspended(x) ((x)->flags |= QUFLAG_SUSPENDED)
#define USetCleanupExempt(x) ((x)->flags |= QUFLAG_CLEANUPEXEMPT)
#define USetTrust(x) ((x)->flags |= QUFLAG_TRUST)
+#define UClearInactive(x) ((x)->flags &= ~QUFLAG_INACTIVE)
#define UClearGline(x) ((x)->flags &= ~QUFLAG_GLINE)
#define UClearNotice(x) ((x)->flags &= ~QUFLAG_NOTICE)
#define UClearSuspended(x) ((x)->flags &= ~QUFLAG_SUSPENDED)
/* email */
#define MAX_RESEND_TIME 2*3600L /* cooling off period */
-#define VALID_EMAIL "^[-_.+[:alpha:][:digit:]]+(\\.[-_[:digit:][:alpha:]]+)*@([[:digit:][:alpha:]](-?[[:digit:][:alpha:]])*\\.)+[[:alpha:]]{2}([zmuvtgo]|fo|me|seum|op|ro)?$"
+#define VALID_EMAIL "^[-_.+[:alpha:][:digit:]]+(\\.[-_[:digit:][:alpha:]]+)*@([[:digit:][:alpha:]](-?[[:digit:][:alpha:]])*\\.)+[[:alpha:]]{2}([zmuvtgol]|fo|me|seum|op|ro)?$"
-#define VALID_ACCOUNT_NAME "^[-a-z0-9]*$"
+#define VALID_ACCOUNT_NAME "^[a-z][-a-z0-9]+$"
#define QMAIL_NEWACCOUNT 1 /* new account */
#define QMAIL_REQPW 2 /* requestpassword */
#define QMAIL_NEWPW 3 /* new password */
#define QMAIL_RESET 4 /* reset account */
#define QMAIL_NEWEMAIL 5 /* new email address */
+#define QMAIL_ACTIVATEEMAIL 6 /* new style new account */
/* Channel flags */
#define QCFLAG_VOICEALL 0x1000 /* +v */
#define QCFLAG_WELCOME 0x2000 /* +w */
#define QCFLAG_SUSPENDED 0x4000 /* +z */
+#define QCFLAG_ACHIEVEMENTS 0x8000 /* +h */
#define CIsAutoOp(x) ((x)->flags & QCFLAG_AUTOOP)
#define CIsBitch(x) ((x)->flags & QCFLAG_BITCH)
#define CIsSuspended(x) ((x)->flags & QCFLAG_SUSPENDED)
#define CIsInfo(x) ((x)->flags & QCFLAG_INFO)
#define CIsNoInfo(x) ((x)->flags & QCFLAG_NOINFO)
+#define CIsAchievements(x) ((x)->flags & QCFLAG_ACHIEVEMENTS)
#define CSetAutoOp(x) ((x)->flags |= QCFLAG_AUTOOP)
#define CSetBitch(x) ((x)->flags |= QCFLAG_BITCH)
#define QCFLAG_USERCONTROL (QCFLAG_AUTOOP|QCFLAG_BITCH|QCFLAG_AUTOLIMIT| \
QCFLAG_ENFORCE|QCFLAG_FORCETOPIC|QCFLAG_AUTOVOICE| \
QCFLAG_PROTECT|QCFLAG_TOPICSAVE|QCFLAG_VOICEALL| \
- QCFLAG_WELCOME|QCFLAG_KNOWNONLY)
+ QCFLAG_WELCOME|QCFLAG_KNOWNONLY|QCFLAG_ACHIEVEMENTS )
-#define QCFLAG_ALL 0x7fff
+#define QCFLAG_ALL 0xffff
/* Channel user ("chanlev") flags */
#define QCMD_OPER 0x0020 /* Only available to opers */
#define QCMD_ADMIN 0x0040 /* Only available to admins */
#define QCMD_DEV 0x0080 /* Only available to developers */
+#define QCMD_STAFF 0x0200 /* Only available to staff */
#define QCMD_ALIAS 0x0100 /* Don't list on SHOWCOMMANDS */
+#define QCMD_HIDDEN QCMD_ALIAS
+
+#define QCMD_ACHIEVEMENTS 0x0400 /* Achievement-related commands */
+#define QCMD_TITLES 0x0800 /* Title-related commands */
#define CS_INIT_DB 0x1 /* Loading database.. */
#define CS_INIT_NOUSER 0x2 /* Loaded DB, waiting for user to be created */
short chanoppos; /* Position in the array */
} regchan;
+struct achievement_record;
+
/* Registered user */
typedef struct reguser {
#ifdef CS_PARANOID
int stealcount; /* How many times we've had to free the nick up */
nick *fakeuser; /* If we had to "take" the nick, here's the pointer */
+ time_t lastpasschange;
+
struct reguser *nextbydomain;
struct reguser *nextbyname;
struct reguser *nextbyID;
extern int chanserv_init_status;
extern int chanservdb_ready;
+
+#ifndef CS_NODB
extern DBModuleIdentifier q9dbid, q9adbid, q9cdbid, q9udbid;
+#endif
extern maildomain *maildomainnametable[MAILDOMAINHASHSIZE];
extern maildomain *maildomainIDtable[MAILDOMAINHASHSIZE];
const char *cs_cralgorithmlist(void);
int cs_checkhashpass(const char *username, const char *password, const char *junk, const char *hash);
char *csc_generateresetcode(time_t lockuntil, char *username);
+int csc_verifyqticket(char *data, char *digest);
/* chanservuser.c */
void chanservreguser(void *arg);
void chanservjoinchan(channel *cp);
-#define chanservsendmessage(np, fmt, args...) chanservsendmessage_real(np, 0, fmt , ## args)
-#define chanservsendmessageoneline(np, fmt, args...) chanservsendmessage_real(np, 1, fmt , ## args)
-void chanservsendmessage_real(nick *np, int oneline, char *message, ... );
-void chanservwallmessage(char *message, ... );
+void chanservpartchan(channel *cp, char *reason);
+#define chanservsendmessage(np, fmt, ...) chanservsendmessage_real(np, 0, fmt , ##__VA_ARGS__)
+#define chanservsendmessageoneline(np, fmt, ...) chanservsendmessage_real(np, 1, fmt , ##__VA_ARGS__)
+void chanservsendmessage_real(nick *np, int oneline, char *message, ... ) __attribute__ ((format (printf, 3, 4)));
+void chanservwallmessage(char *message, ... ) __attribute__ ((format (printf, 1, 2)));
void chanservcommandinit();
void chanservcommandclose();
void chanservstdmessage(nick *np, int messageid, ... );
void cs_checkbans(channel *cp);
void cs_schedupdate(chanindex *cip, int mintime, int maxtime);
void cs_timerfunc(void *arg);
-void cs_removechannel(regchan *rcp);
+void cs_removechannel(regchan *rcp, char *reason);
int cs_removechannelifempty(nick *sender, regchan *rcp);
void cs_doallautomodes(nick *np);
void cs_checknickbans(nick *np);
typedef int (*UnbanFN)(void *arg, struct chanban *ban);
int cs_unbanfn(nick *sender, chanindex *cip, UnbanFN fn, void *arg, int removepermbans, int abortonfailure);
void cs_logchanop(regchan *rcp, char *nick, reguser *rup);
+int checkreason(nick *np, char *reason);
/* chanservstdcmds.c */
int cs_doshowcommands(void *source, int cargc, char **cargv);
void cs_handleopchange(int hooknum, void *arg);
void cs_handlenewban(int hooknum, void *arg);
void cs_handlechanlostuser(int hooknum, void *arg);
+int cs_ischannelactive(channel *cp, regchan *rcp);
/* chanservmessages.c */
void initmessages();
/* chanservlog.c */
void cs_initlog();
void cs_closelog();
-void cs_log(nick *np, char *event, ...);
+void cs_log(nick *np, char *event, ...) __attribute__ ((format (printf, 2, 3)));
/* chanservdump.c */
int dumplastjoindata(const char *filename);
void csdb_createmaildomain(maildomain *mdp);
void csdb_updatemaildomain(maildomain *mdp);
void csdb_chanlevhistory_insert(regchan *rcp, nick *np, reguser *trup, flag_t oldflags, flag_t newflags);
-void csdb_accounthistory_insert(nick *np, char *oldpass, char *newpass, sstring *oldemail, sstring *newemail);
-void csdb_cleanuphistories();
+void csdb_accounthistory_insert(nick *np, char *oldpass, char *newpass, char *oldemail, char *newemail);
+void csdb_cleanuphistories(time_t);
void csdb_deletemaillock(maillock *mlp);
void csdb_createmaillock(maillock *mlp);
void csdb_updatemaillock(maillock *mlp);
/* q9snprintf.c */
void q9snprintf(char *buf, size_t size, const char *format, const char *args, ...);
void q9vsnprintf(char *buf, size_t size, const char *format, const char *args, va_list ap);
+void q9strftime(char *buf, size_t size, time_t t);
/* chanserv_flags.c */
-flag_t cs_accountflagmap(reguser *rup);
-flag_t cs_accountflagmap_str(char *flags);
+u_int64_t cs_accountflagmap(reguser *rup);
+u_int64_t cs_accountflagmap_str(char *flags);
#endif