/*
 * globals.h
 * (C) James Craig 1998, Email: jcraig@mad.scientist.com
 * (C) Christian Magnusson 1999, Email: d1mag@dtek.chalmers.se
 * This program is copyright, and covered by the Gnu Public License.
 * The Q bot.
 */

#ifndef GLOBALS_H
#define GLOBALS_H

#include <time.h>
#include <stdarg.h>
#include "settings.h"
#include "dmalloc.h"
#include "version.h"
#include "bottypes.h"
#include "dl.h"

#ifdef NEW_MATCH
#include "match.h"
#endif

#ifndef MAX
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif


/*
  This file is filled with unsorted function prototypes and 
  global vars.
*/

extern char args[MAX_ARGS][BIGBUF];
extern int argcount;

extern char sender[BIGBUF];
extern struct activeuser *sender_au;
extern char command[BIGBUF];
extern char what[BIGBUF];

extern int maxuid;

API_PUBLIC extern int unload_one_command;
API_PUBLIC extern int reload_command;
API_PUBLIC extern char reload_command_name[256];

extern int nr_users_total;
extern int nr_opers_total;
extern time_t startup_time;
extern int nick_timeout;
extern int nick_number;
extern int burst_mode;
extern int trusted_users;

extern int max_users;
extern time_t max_users_time;
extern int debuglog;
extern int chanlimit_delay;

extern struct servers_stat serverlist[MAX_SERVERS+1];

extern char *USER_DBASE_FILE;
extern char *CHANNEL_DBASE_FILE;
extern char *GLINE_DBASE_FILE;
extern char *CHANTYPE_DBASE_FILE;
extern char *TRUSTED_DBASE_FILE;
extern char *COMMANDS_FILE;
extern char *MAX_USERS_FILE;
extern char *BOT_NAME;
extern char *SENDMAIL;
extern char *VIRTUAL_SERVER_NAME;
extern char *BOT_HOSTNAME;
extern char *VIRTUAL_SERVER_PASSWORD;
extern char *DEBUG_LOGPATH;
extern char *LOGPATH;
extern char *LOGPATH_MALLOC;
extern char *q_remote_server;
extern char *q_port;
extern char *q_database;
extern char *q_log;
extern char *q_server_number;
extern char *q_server_name;
extern char *q_server_passwd;
extern char *q_stats_dir;
extern char *q_stats_domain;
extern char *q_commands_dir;
extern char sock_name[BIGBUF];
extern char *sqlhost;
extern char *sqlusername;
extern char *sqlpassword;
extern char *sqldb;
extern char *sqllogtable;
extern unsigned int sqlport;

/*
  ban.c
*/
int addban_ch(struct chanopts *c, char *banstring, int placed, int kick);
int split_banstylename(char *ban, char *nick, char *user, char *host);
void delban_ch(struct chanopts *c, char *banstring, int permanent_too);
void updatebans(struct chanopts *c);
struct ban *isbanned(struct activeuser *u, struct chanopts *c);
int matches_ban(struct activeuser *u, struct ban *b);
char *banstylename(char *nick, char *user, char *host);


/*
  gline.c
*/
int gline_del(char *gline);
int matches_gline(struct activeuser *u, struct gline *g);
struct gline *gline_add(char *gline, char *duration, char *reason, int placedby, int force_update);

/*
  trusted.c
*/
struct trusted *trusted_add(char *host, int nr, long duration, char *reason, int placedby);
int trusted_del(char *host);

/*
  chantype
*/
char *get_chantype_name(int id);
struct chantype *chantype_add(char *name, int id, int priority);
struct chantype *chantype_del(char *name);


/*
  find.c
*/
dl_command_entry_t *findcommand(char *cmd);
struct activeuser *findauser(char *);
struct dbaseuser *finduserfromid(int);
struct dbaseuser *finduserfromnick(char *);
struct dbaseuser *finduserfromname(char *);
struct chanopts *findchannel(char *);
struct activeuser *find_yxx(char *victim);


/*
  newq.c
*/
void normnum(char* a);
int is_oper(void);
char *crypt_r(const char *key, const char *salt);
int add_command(char *cmd, void (*func)(char *), char *summary,
		char *help, int level);
int remove_command(char *cmd);
int server_to_yxx(char *name, char *pyxx);
void add_server(char *name, char yxx1, char yxx2, char connected_to_yxx1, char connected_to_yxx2);
int del_server(char *name);
void putserver(char *text, ...);
void putserver1(char *text);
void say(char *victim, char *words);
void say_y(char *pyxx, char *words);
void say_au(struct activeuser *au, char *words);
void say_sender(char *words);
void say_sender_(char *words, ...);
void privmsg(char *victim, char *what);
void dumpmessage(char *victim, char *message);
void dumpmessage_u(struct activeuser *u, char *message);
void joinchannel(struct chanopts *);
void burstchannel(struct chanopts *);
void settopic(struct chanopts *c, char *topic);
void startlink(void);
void ctcpreply(char *, char *);
char *gettime(void);
void partchannel(struct chanopts *c);
void dumpmessage(char *victim, char *message);
void notice(char *victim, char *words);
void pong(char *server);
void wallop(char *message, ...);
void wallop_d(int level, char *message, ...);
void opself(char *channel);
void ctcp(char *victim, char *what);
struct activeuser *nick_to_yxx(char *victim, char *tmp);
int clear_chanmodes(struct chanopts *c, int keep_nt);
void set_default_chanmodes(struct chanopts *c);
void add_userid_history(struct activeuser *u, int userid);
void dellimit(char *channel, int permanent);
void dellimit_ch(struct chanopts *c, int permanent);
void addlimit(char *channel, char *limit, int permanent);
void addlimit_ch(struct chanopts *c, char *limit, int permanent);
void delkey(char *channel, int permanent);
void delkey_ch(struct chanopts *c, int permanent);
void addkey(char *channel, char *key, int permanent);
void addkey_ch(struct chanopts *c, char *key, int permanent);



/*
  log.c
*/
void log(const char *format, ...);
void irclog(int level, const char *format, ...);
void irclog_(int level, const char *format);
void log_malloc(const char *format, ...);


/*
  u_database.c
*/
int removeauserfromchan(struct activeuser *u, char *channel, struct chanopts *c);
int userlevelonchan_d(struct dbaseuser *d, char *chan);
int delchannel(char *);
int save_everything();
void saveglinelist(void);
int loadglinelist(void);
int loadchannellist(void);
int savechannellist(void);
int loadtrustedlist(void);
int load_max_users(void);
void save_max_users(void);
void savetrustedlist(void);
void createmodes(int modenum, char *buffer);
int dbasecheck(struct dbaseuser *u, char *password);
void updatemodes(unsigned int *modes, char *modstr);
void saveudbase(void);
void reloadchannels(void);
void modecmp(int source, int destination, char *buffer);
void saveudbase(void);
int loadudbase(void);
void addauser(char *nick, char *username, char *hostname, char *fullname, char *server, char *modes, char *yxx);
struct chanmod *addausertochan(char *user, char *channel);
struct chanmod *addausertochan_u(struct activeuser *u, char *channel);
void updateauth(struct activeuser *u);
void print_userlist(long from_age, long to_age);
struct chanopts *addchannel(char *channel, int flags, int modes, int owner, char *topic);
int change_server_users(char pyxx1, char pyxx2, int users);
int change_server_opers(char pyxx1, char pyxx2, int users);
int load_chantypelist(void);
void save_chantypelist(void);
int userlevel(char *username);
int userlevel_u(struct activeuser *u);
void join_all_channels(int burst);
void addchanlimit_ch(struct chanopts *c, int limit, int delay);
void delchanlimit_ch(struct chanopts *c);


/*
  m_handlers.c
*/
char *getword(char *,int *);
#ifndef NEW_MATCH
  int match(char *pattern, char *name);
#endif
int is_empty_channel(char *channel);
int match_ignore_ctrlchar(char *pattern, char *name);
int match_include_ctrlchar(char *pattern, char *name);
void m_deluser(char *a);
void deop_au(struct activeuser *u, char *channel, int server_hack);
void addkey_ch(struct chanopts *c, char *key, int permanent);
void addkey(char *channel, char *key, int permanent);
void addlimit_ch(struct chanopts *c, char *limit, int permanent);
void addlimit(char *channel, char *limit, int permanent);
void dellimit_ch(struct chanopts *c, int permanent);
void dellimit(char *channel, int permanent);
void delkey_ch(struct chanopts *c, int permanent);
void delkey(char *channel, int permanent);

/*
  s_handlers.c
*/
void kick_user_reason_banned(struct activeuser *u, struct chanopts *c);
int handlejoin_u(struct activeuser *u, char *channel, int force_op_check);
void clear_if_empty_channel(struct chanopts *co);
void s_ctcp(void);
void s_nick(void);
void s_mode(void);
void s_server(void);
void s_privmsg(void);
void s_pong(void);
void s_kill(void);
void add_auth_history(struct chanopts *c, int userid, char *nick);
void disconnect_auser(struct activeuser *u);
void s_handleburst(char *buffer);
void s_handlenicks(char *buffer);
void s_handlenickc(char *buffer, struct activeuser *u);
void s_handlestats(void);
void s_gline(char *buffer);
void s_ping(void);
void s_gline(char *buffer);
void s_join(void);
void s_part(void);
void s_topic(char *buffer);
void s_quit(void);
void s_version(void);
void s_kick(void);

/*
  c_handlers.c
*/
void update_keys_limits(void);
void c_topic(char *buffer);
void c_ban(int direction, char *channel, char *ban);
void c_op(int direction, char *channel, char *victim);
void c_voice(int direction, char *channel, char *victim);
void c_op_set_modes(int direction, char *channel, struct activeuser *u);
int c_op_get_modes(char *channel, struct activeuser *u);
void c_voice_set_modes(int direction, char *channel, struct activeuser *u);
void c_key(int direction, char *channel, char *key);
void c_secret(int direction, char *channel);
void c_limit(int direction, char *channel, char *limit);
void c_opertopic(int direction, char *channel);
void c_nomsg(int direction, char *channel);
void c_invite(int direction, char *channel);
void c_moderate(int direction, char *channel);
void c_protect(int direction, char *channel);

/*
  sqllog.c
*/
int sqlqinit(void);
void sqlqlog(int t, char *cani, char *caus, char *caho, const char *formatstring, ...);
void sqlqclose(void);

/* SQLLog-Types */
#define QLOG_GOODAUTH 1
#define QLOG_BADAUTH  2
#define QLOG_HELLO    3
#define QLOG_EMAILSET 4
#define QLOG_PASSCHG  5
#define QLOG_CHANLEV  6

void Malloc_stat(void);
#ifdef DMALLOC
void *Malloc2(int sz, char *func, int line);
void Free2(void *ptr);
#endif

#endif
