/* global.h     -global #defines and variables

   Author: Tano Fotang, 1997

 */


#ifndef _sula_global_h
#define _sula_global_h

#define MAXPUP	32
#define new_pup_max (fl_setpup_maxpup(fl_setpup_maxpup(0)+MAXPUP))
typedef char BOOL;

#ifndef COMMENT
#define COMMENT			'#'
#endif
#define SERV_DELIM		":"
#define SERV_DELIM_CHR		':'
#define MAXARGC	       		50	/* max # of args for cmds: argc */


#define PRINT	fprintf(stderr,"%s (%d)\n", __FILE__, __LINE__)

char           *Name;		/* your name.. */
/*char           *host;	  */	/* your machine */
char		*sula_lib;	/* /usr/local/lib/sula e.g */

int         MaxChanLen, MaxNickLen;
u_short         HistSize, mainHistSize;		/* current cmd list size in bytes */

char           *quit_text;	/* signoff msg */
char           *away_text;	/*default away text */
#if 0
/* no longer used; sula 0.2.64c */
char           *away2_text;	/*no longer away text */
#endif
/* 
 * post handler struct  */
typedef struct TPh {
	FL_OBJECT *ob;		/* the object*/
	char   *s;		/*  the tip */
} Ph;
int timeoutID;
extern void do_tips(int id, void *data);

typedef struct TBookmark {
  int             id;		/* oh well! */
  char           *channel;
  char           *title;
  char           *server;
  u_short         port;
  short int       bf;
  struct TBookmark *left;
  struct TBookmark *right;
} Bookmark;
typedef Bookmark *pBookmark;
Bookmark       *bmTree;		/* those currently bein used */
int             bm_id;		/* no of bookmarks added so far */
typedef struct gTChannel {	/*global chanlist
				   to keep track of servers and the channels that
				   we've joind on them */
  char           *name;		/* channel name? */
  char           *server;	/* on which server? */
  unsigned int    port;
  struct gTChannel *next;
} gChannel;

/*---------------------------------------------*/
typedef struct TUser {
  char           *name;		/* nick */
  char           *address;	/* user@host */
  char           *mode;
  char           *ircname;
  struct TUser   *next;
} User;

typedef struct TChannel {
  char           *name;		/* channel name? */
  char           *modes;
  char           *key;
  int             limit;
  char           *topic;
  User           *userStart;
  User           *userEnd;
  struct TChannel *left;
  struct TChannel *right;
} Channel;

/* for a server */
typedef struct TSender {	/* have sent stuff */
  char           *sender;		/* nick */
  u_long          what;		/*S_XX + pos_of_nick<<8  */
  struct TSender *left, *right;
} Sender;
Sender *senderStart, *senderCur;
typedef struct TLag{
	struct timeval	when;
	/*u_short wait;*/	/* wait this long b4 updating display */
	struct TLag *next;
}Lag;

#define SET_AWAY	(1<<0) /* set away*/
#define GOT_MOTD 	(1<<1) /* got server motd already*/
#define SERVER_FLAG_RESET (~(SET_AWAY|GOT_MOTD))
typedef struct TServer {
  int             fd;
  char           *servname;
  char           *nick;
  char		iNick;	/* list*/
  char           *email;
  char           *ircname;
  char           *mode;
  /*BOOL		away;*/
  u_char	flag; 	/* SET_AWAY|GOT_MOTD*/
  char		connecting;
  Lag		*lag;
  struct timeval t;/*lag*/
  u_short	port;
  char           *password;
  char            state;
  char           *lastInvite;
  unsigned        read;		/* total bytes read */
  short int       bf;
  struct TServer *left;
  struct TServer *right;
} Server;
typedef Server *pServer;
Server         *servTree, *servFdTree;
#define BY_FD		1
#define BY_NAME		0

	

typedef struct TNoChannel {
  char           *name;
  char           *key;
} NoChannel;
typedef struct TGo {
  char           *name;
  char		*key;
  struct TGo	*next;
} Go;

/********************************/
/*      c h a t                 */
/********************************/
typedef struct TDCCChat {	/* active dcc chat on a window */
  int             id;
  char           *nick;
  char           *email;
  int             fd;
  struct TDCCChat *left;
  struct TDCCChat *right;
} DCCChat;
typedef struct TChanWin {
  FD_chanwin     *chanwin;
  Server         *server;
  char           *query_nick;
  DCCChat        *dccCur;
  DCCChat        *dccStart;
  NoChannel      *lastPart;
  NoChannel      *lastJoin;
  Go             *goStart;
  Go             *goEnd;
  Channel        *chanCur;
  Channel        *chanStart;
  /*struct TChanWin *next;*/
} Winstruct;
Winstruct *winstruct;
int win_count;
#define WIN_ALLOC 5
typedef struct TCmdSent {
  char           *cmd;
  char           *arg;
  Winstruct      *win;
  struct TCmdSent *next;
} CmdSent;
CmdSent        *cmdsentStart, *cmdsentEnd;


typedef struct TDCCChatWin {	/* ALL active chat connections */
  int             id;		/*nicks arent unique so we use an id */
  int            *fd;		/*  &dccChat->fd */
  char           *nick;		/* no malloc; dccChat->nick */
  char           *email;	/* no malloc; dccChat->email  NULL if we sent the request */
  unsigned        read;
  Winstruct      *win;
  struct TDCCChatWin *next;
} DCCChatWin;
DCCChatWin     *dccchatwinStart, *dccchatwinEnd;


Winstruct      *tmp_win;	/* evrybody's window */

typedef struct THistory {
  char           *line;
  struct THistory *left;
  struct THistory *right;
} History;


gChannel       *chanStart, *chanEnd;
History        *histStart, *histCur;
History        *mainhistStart, *mainhistCur;	/* history for the one control window */
#define		MAX_HIST_SIZE	2000	/* 2000 bytes; currently unused */

extern int      save_history (const char *);
extern int      load_history (const char *);
extern int      AddToHistory (const char *);

extern void     load_userlist (Winstruct *, const char *, int);


FD_main_form   *main_form;

typedef struct TWinList {
  Winstruct      *win;
  struct TWinList *next;
} WinList;
WinList        *servWin;

extern void     err_dump (const char *,...);
extern void     err_msg (const char *,...);
extern void     err_quit (const char *,...);
extern void     err_ret (const char *,...);
extern void     err_sys (const char *,...);
extern void     berr_ret (const char *fmt,...);
extern void     berr_msg (const char *fmt,...);
extern void cleanup(void);

extern void    *build_winlistbyfd (int fd);
extern void    *build_winlistbyservname (char *name, int port);
extern void     UpdateWindows (int fd, char flag);
extern void     UpdateStatus (Winstruct * win);
extern void     fit_object_label (FL_OBJECT * ob, const char *label);
extern void     update_topic (Winstruct * win, const char *chan, const char *topic);

extern Winstruct *search_winlistbyfd (int fd, int *number);
extern Winstruct *search_winlist_byservname (char *servername, int port, int *n);
extern Server  *search_serverlistbyname (char *servername, int port, int *found);
extern int      connect_to_server (Server *, Winstruct * , const char *, int);
extern Server  *search_server_tree_byname (const char *, int);
/*extern Server  *search_server_tree_byfd (Server *, int);*/

extern int     insert_server(pServer * pp, Server * s, int flag);
extern int     remove_server_from_tree(char *servname, int port);
extern void    close_server_connectionbyname(char *servname, int port);
extern void     prepare_server_output (int fd, const char *msg);
extern int parse_input(int, const char *, int);


extern int      process_cmd (const char *str, Winstruct * win);

extern void     menu_setup (Winstruct *);
extern void 	go_menu_setup(Winstruct *win);
extern void     do_about (void);	/* copyrite info */
extern void     do_help (void);
extern void     do_setup (void);
extern Winstruct *new_chan_win (void);
extern void     set_object_attribs (Winstruct * win);
extern Winstruct *new_win (void);

extern void     addto_cmd_sent (Winstruct * win, const char *cmd, const char *arg);
extern int      sendto_server (Winstruct * win, const char *buf);

extern void     close_server_connection (int *fd);
extern void     new_close_connection (int *fd, char *reason, int update_win);
extern void connecting(char *, u_short, char);
/*RemoveChannels(Server * s);*/

extern int      save_servers (char *fname);
extern int      loadserverlist (const char *filename);
extern void     list_servers (void);


extern int      atclose_dec_count (FL_FORM * form, void *count);
extern int      atclose_reset (FL_FORM * form, void *count);
extern int      atclose_do_close (FL_FORM * form, void *fdui);
extern int      dont_close_win (FL_FORM * form, void *fdui);
extern int      set_cursor (FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);

/*
extern void     getstr (char **, const char *, const char *, const char *, double, int);
*/
extern char    *getstr2 (const char *, const char *, const char *, int);
extern int      confirm (const char *, const char *, double);
extern char    *format_msg (char *fmt,...);
extern void     old_str2args (char *buf, int *argc, char **argv[]);

/*extern char	*getmemory(unsigned);*/

#include "version.h"
#include "util.h"

#define PROMPT			"["sula_NAME"]> "
char           *copyright_file;	/* copyright filename */
char           *warranty_file;	/* warranty & bugs */
char		*gethostbynameprogram;/* external prog for gethostbynmae*/

u_short         connect_timeout;	/* timeout in secs for gethostbyname */
u_short		timeout;		/* timeout for getstr in secs*/
u_short		tips_tmout;		/* timeout for tipper in millisecs*/
u_short		lag_tmout;		/* autocheck lag frequency in secs */
u_char		max_go;

int browser_fontsyle, browser2_fontsyle;
int browser_fontsize, browser2_fontsize;
int browser_colour, browser2_colour;
int browser_selected_colour, browser2_selected_colour;
int browser_textcolour, browser2_textcolour;
int input_colour, input_fontstyle; /* of input line */

int target_fontsyle, target_fontsize;
int target_colour/* background */,target_colour2;/* labelcolur*/


/* values > 15 will be made value%=16! */
#define SS_MSG		( 0)
#define SS_NOTICE	( 1)
#define	SS_PING		( 2)
#define SS_CHAT		( 3)
#define	SS_FILESEND	( 4)
#define	SS_FILEGET	( 5)
#define	SS_ACTION	( SS_MSG)
#define	SS_INVITE	( 8)
#define SS_CTCP_OTHER	( 9)
#define SS_FINGER	( 10)
#define	SS_SED		( SS_MSG)
#define SS_ALL		(0xffff)
/*u_long		ssflags;		*//* SS_ALL & SS_XX| nick_len<<16*/
u_long		gflags;			/* global flags*/
u_long		sflags;			/* what to register for servers; S_XXX flags; see below */

#define S_MSG		(1 << SS_MSG)
#define S_NOTICE	(1 << SS_NOTICE)
#define	S_PING		(1 << SS_PING)
#define S_CHAT		(1 << SS_CHAT)
#define	S_FILESEND	(1 << SS_FILESEND)
#define	S_FILEGET	(1 << SS_FILEGET)
#define	S_ACTION	(1 << SS_ACTION)
#define	S_FINGER	(1 << SS_FINGER)
#define	S_INVITE	(1 << SS_INVITE)
#define	S_CTCP_OTHER	(1 << SS_CTCP_OTHER)
#define	S_SED		(1 << SS_SED)

#define FORCE_CONNECTION (1<<14)	/* try to connect even if external process
					couldnt get remote hostname. This may block client*/
#define EXTERNALGETHOST	 (1<<15)	/* use external program to get hostname */
#define EXTENDED_DCC	(1<<16)		/* use DCC RESUME and extended DCC */
#define SUPPRESS_MOTD	(1<<17)		/* suppress motd upon connecting*/

#define S_ALL		(0xffffffff)

#if 0
u_long		cflags;			/* what to register for chans; C_XXX */
/* ------cflags-----for channel -------------: */
#define C_MSG		(1 << 0)
#define C_NOTICE	(1 << 1)
#define C_TOPIC		(1 << 2)
#define C_KICK		(1 << 3)	/*last kicked some1*/
#define C_JOIN		(1 << 4)	/*last joined chan */
#define C_ACTION	(1 << 5)
#define C_NICK		(1 << 6)	/*last changed nick */
#define C_ALL		(0xffffffff)
#endif


/* g f l a g s */

#define SHOW_NUMERIC	(1 << 0)	/* just that */
#define AUTO_REJOIN	(1 << 1)	/* rejoin channel on kick */
#define AUTO_INVITE	(1 << 2)	/* auto accept invites*/
#define AUTO_GETFILE	(1 << 3)	/* auto dcc get*/
#define AUTO_CHAT	(1 << 4)	/* autoaccept dcc chat*/
#define INFORM_IGNORED	(1 << 5)	/* tell 'em they're bein ignored */

#define VERBOSE_CLIENT	(1 << 6)	/* be a verbose client (still figurin out what that shld mean)*/

#define SAVE_HISTORY	(1 << 7)	/* save cmd history on program exit */
#define HISTORY		(1 << 8)	/* add cmds to cmd history list*/

#define RM_ON_FWD	(1 << 9)	/* remove DCC requests after forwarding them*/
#define VERBOSE_CTCP	(1 << 10)

#define BKMRK_IG_SERVER	(1 << 11)	/* ignore bookmark server, use current server to join bookmarked channel*/
#define QUOTE_ML	(1 << 12)	/* quote multiline commands */

#define SHOW_RAW_MSG	(1 << 13)	/* show raw server output*/
#define SHOW_TIPS	(1 << 14)	/* display oneliners */

#define SAVE_BOOKMARKS	(1 << 15)	/* save bookmarks on program exit */

#define FINGER_UTMP	(1 << 16)	/* read utmp file for ctcp finger reply*/
#define FINGER		(1 << 17)	/* (dis)allow finger on you */

#define BEEP_AWAY	(1 << 18)	/* private msg/notice/invite*/
#define BEEP_DCC	(1 << 19)	/* when dcc request comes in*/
#define BEEP_ERR	(1 << 20)	/* bad command and other client errors*/
#define BEEP_SERVER_ERR	(1 << 21)	/* bad command to server*/
#define BEEP_MSG	(1 << 22)	/* when a msg/notice comes in*/

#define AUTO_CHECKLAG	(1 << 23)	/* auto check lag */
#define ENCRYPT_MSG	(1 << 24)	/* message de/encryption*/
#define MATCH_CASELESS	(1 << 25)	/* ignore case when matching patterns */

#define CTRL_WIN_SHOWN	(1 << 26)	/* control window is shown */	
#define ALL_GFLAGS	(0xffffffff)



#endif
