/*
 * globals.h
 * (C) Peter Salanki 2002
 * This program is copyright, and covered by the Gnu Public License.
 * The Natasha bot.
 * sorcer@linux.se
 */

#include "settings.h"
#include <mysql/mysql.h>

#define COLORS '\003'
#define BOLD '\002'
#define UNDERLINE '\0031'

/* Define some RFC 1459 numerics */
#define ERR_INVITEONLYCHAN "473" 
#define ERR_BANNEDFROMCHAN "474" 
#define ERR_BADCHANNELKEY "475" 
#define ERR_CHANNELISFULL "471"            
#define ERR_NOSUCHCHANNEL "403"               
#define RPL_TOPIC "332"
#define RPL_NOTOPIC "331"
#define RPL_WHOISCHANNELS "319"
#define ERR_TOOMANYCHANNELS "405"
#define ERR_BADCHANMASK "476"
#define ERR_NICKNAMEINUSE "433"
#define RPL_WHOISUSER "311"
#define RPL_WHOREPLY "352" // <channel> <user> <host> <server> <nick> <H|G>[*][@|+] :<hopcount> <real name>
#define RPL_WHOISCHANNELS "319"    
#define RPL_WHOISOPERATOR "313"
#define RPL_WHOISUSER "311" // <nick> <user> <host> * :<real name>
#define RPL_ENDOFWHO "315"
#define RPL_ENDOFWHOIS "318"
#define RPL_QAUTH "330"
#define RPL_USERHOST "302"

/* Level defines */
#define UNAUTHED 0
#define NORMAL 1
#define SERVICE_FRIEND 2
#define HELPER 7
#define QUEUE_WORKER 8
#define TECHNICIAN 20
#define MASTER_TECHNICIAN 21
#define IRC_OPERATOR 22

#define BOT_NORMAL 30
#define BOT_PRIVATE 36
#define BOT_CHECK 40

/* Channel level defines */
#define CHAN_NOACCESS 0
#define CHAN_FRIEND 1
#define CHAN_VOICE 2
#define CHAN_OP 3
#define CHAN_MASTER 4
#define CHAN_OWNER 5

extern MYSQL mysql, *sock;
extern MYSQL_RES *res;
extern MYSQL_ROW row;

extern MYSQL logmysql, *logsock;
extern MYSQL_RES *logres;
extern MYSQL_ROW logrow;

extern struct arm *firstarm;
extern struct arm *lastarm;

extern struct module *firstmodule;
extern struct module *lastmodule;

extern struct activeuser *firstauser;
extern struct activeuser *lastauser;

extern struct channel *firstchan;
extern struct channel *lastchan;

extern struct chanuser *firstchanuser;
extern struct chanuser *lastchanuser;

extern struct activechanuser *firstacu;
extern struct activechanuser *lastacu;

extern int lines;
extern int rejoint;
extern int reloadmsgmod;
extern int reloadircmod;
extern short connectarms;
#ifdef SAFEHOME
extern char homekey[KEYLEN];
#endif
extern char compiledate[];

extern pthread_mutex_t mysqlmutex;
extern pthread_mutex_t logmysqlmutex;
extern pthread_mutex_t globalusermutex;
extern pthread_mutex_t globalchannelmutex;
extern pthread_mutex_t globalchanusermutex;
extern pthread_mutex_t globalacumutex;
extern pthread_mutex_t globalmodmutex;
extern pthread_mutex_t globalarmmutex;
extern pthread_mutex_t globalbanmutex;

extern pthread_mutex_t globalmutex;

/* Typedefs */
typedef void MODULE_INIT;
typedef void MODULE_DESTROY;
typedef short BOOL;
typedef struct arm* ARM;
typedef struct module* MODULE;
typedef struct activechanuser* ACU;
typedef struct activeuser* USER;
typedef struct channel* CHANNEL;

/*
  bot.c
*/

char *STR_replace_c (char *source, char *old, char *new, char *dest);
int regex_match(char *regex, char *string);
void dbconnect (void);
void logdbconnect (void);
void dbquery (char *query);
void logdbquery (char *query);
char *escapequery(char *query);
void loadmodulesinconf (void);
char *loaddatafromfile (char *file);
int writedatatofile (char *file, char *data);
void puttoserver (char *text, struct arm *a);
void linkbreak (struct arm *a);
void startlink (struct arm *a);
int countargs(char *string);
int checkargs(int numargs, int type, char *string, struct arm *a);
#ifdef Q_AUTOAUTH
void add (struct activeuser *u);
#endif
#ifdef SAFEHOME
void securehome (void);
#endif
void violation (char *sender, char action[256], char *channel);
void quit (char reason[]);
char *getline (struct arm *a);
void parse (char *buffer, struct arm *a);
void *armthread (void* ptr);

/*
  irccommands.c
*/

void say (char *victim, char *words, struct arm *a);
void bestsay (char *victim, char *words);
#ifdef CNOTICE
void cnotice (char *victim, char *words); 
#endif
void serverpong(char data[], struct arm *a); 
void joinchannel (struct channel *c);
void rejoinchannel (char chaname[CHANNELLEN], struct arm *a);
void settopic (char *, char *);
void ctcpreply (char *victim, char *what, struct arm *a);
void puttime (struct arm *a);
void partchannel (struct channel *c, char reason[]);
void dumpmessage (char *victim, char *message);
void pong (struct arm *a);
void act (struct channel *c, char *words);
void ctcp (char *victim, char *what, struct arm *a);
void who (struct channel *c);
void cyclechannel (struct channel *c, char *reason);
void op (char *channel, char *nick);
void deop (char *channel, char *nick);
void voice (char *channel, char *nick);
void devoice (char *channel, char *nick);
void invite (struct channel *c, char *nick);
void kick (char *channel, char *nick, char reason[]);
void privmsg (char *victim, char *words);
void exprivmsg (char *victim, char *words, struct arm *a);
#ifdef L
void l_getop (struct channel *c);
void l_clearbans (struct channel *c);
void l_invite (struct channel *c);
#endif
#ifdef Q
void o_requestop(struct channel *c);
#endif
void nick (char *nick, struct arm *a);

/*
  database.c
*/

void TimerChannelLoop(void);
void TimerUserLoop(void); 
struct activeuser *DelOld(struct activeuser *u);
void DelOldChannelBans(struct channel *c);
void CloseEndedVote(struct channel *c);
void ReJoinChannelCheck(struct channel *c);
#ifdef Q_AUTOAUTH
int ReCheckAuth(struct activeuser *u);
#endif
int userlevel (char *username);
void joinchannels (struct arm *a);
char *authtotext(int auth);
void chanlevtotext(int level, char *text);
int texttochanlev(char *text);
int modfunc(char *module, char *function);
int amodfunc(char *module, char *function, struct arm *arm);
int irc_strcmp(const char *a, const char *b);
void loadarms (void);
void loadchannellist ();
void loadchannel (char *channame);
int chanuserlevel (char *username, char *channel);
int chanusergreet (char *username, char *channel, char *ret);
void delallachanusers (struct channel *c);
int delallauserchans (struct activeuser *u);
struct arm *bestarm(void);
void dbchanmode (int id, char option[], char value[]);
void addlog(char *nick, char *channame, char action[10], char *msg);
void flushlog(void);
int isOpLessChan(struct channel *c);
int ServiceChannelCount (void);
int OnJoinVoteMsg (struct channel *c, struct activeuser *u);
int StrDurationToSeconds (char *duration);
int dbCount (char *query);
int AddRegexBadChan (char *regex, char *reason, int finalduration, int adder);
int AddBadChan (char *channel, char *reason, int finalduration, int adder);
int AddRegexBadChan (char *regex, char *reason, int finalduration, int adder);
int DelBadChan (char *regex);
void CleanUpOldBadChans (void);
#ifdef SPAMSCAN
int SOnChannel(struct channel *c);
int SpamChannel(struct channel *c);
int SpamChannelWait(struct channel *c);
#endif
void EndDbQuery(void);
void EndLogDbQuery(void);
void OutOfChannel(struct channel *c);

/*
  memory.c
*/

/* Adds */
void addchanuser (struct channel *channel, int uid, int level, char *greet);
void addban (char *channel, char *host, char *reason, int setby, int expire);
void putserver (char *data, struct arm *a);
void addchannel (char *channel, short id, char *modes, char *key, short autoop, short autovoice, short bitch, short ftopic, char *mvoice, short advertise, char *flood, short peak, short greet, short tv, short vote, short infobot, short stats, struct arm *arm, char *onjoin);
void adddbchannel (char *channame, struct arm *a);
void addarm (char *nick, int aid, int type);
void loadmodule (char path[100], short fatal);
struct activeuser *addauser (char *nick, char *username, char *hostname);
struct aculink *addaculink (struct activeuser *u, struct activechanuser *acu);
struct activechanuser *addachanuser (char *nick, char *channel);
struct vote *AllocateVote(struct channel *c);

/* Dels */
void deluser (struct activeuser *u);
void delaculink (struct activeuser *u, struct activechanuser *acu);
int delachanuser (char *nick, char *channel);
void deldbchannel (char *channame);
void deldbarm (char *arm);
void delban (char *host, char *channel);
void delarm (char *arm);
void delsendqueue (struct arm *a);
void unloadmodule (char *name);
void delchannel (struct channel *c);
int FreeVote (struct channel *c);

/* Finds */
struct module *findmodule (char name[20]);
struct module *findmodulebypath (char path[100]);
struct arm *findarm (char *nick);
struct arm *findarmbyid (int id);
struct arm *findarmbytype (int type);
struct activeuser *findauser (char *nick);
struct channel *findchannel (char *name);
struct channel *findchannelbyid (int id);
struct chanuser *findchanuser (int uid, int cid);
struct activechanuser *findachanuser (char *nick, char *channel);
struct banlist *findban (char *host, char *channel);

/*
  channels.c
*/

void setrightmode(char *channel, char *nick);
int c_join (char *channame, struct arm *a);
int c_part (char *channame, char *reason);
int c_move (char *from, char *to);
void c_setlev (int uid, char *channame, int level);
void c_setgreet (int uid, char *channame, char *greet);
