#ifndef ONLINELIB_HEADER
#define ONLINELIB_HEADER
#ifdef WIN32
#define USING_STATIC_LIBICONV 1
#endif 
#include "ziconv.h"
#include "tcplib.h"
#include "defines.h"

#ifndef ONLINELIB_TAGSZ
#define ONLINELIB_TAGSZ 1
#endif

struct online_connection;
typedef struct online_connection OnlineCon;

struct online_connection {
	Chan *c;
	
	char *user;

	void (*cb)(void*,OnlineCon*);
	void *data;

	char result[BFSZ];
	char *ptr;

	char tag[ONLINELIB_TAGSZ];
	int itag;

	int loggedin;
	int idling;
	int stop_idle;
    int stop_idle_pending;
    int dropout_type;

	FILE *log;			// Debug logging of communications on this IMAP channel
};

/**/
OnlineCon *ol_new();
void ol_free(OnlineCon **ol);
int ol_log_open(OnlineCon *ol, char *fname);

void ol_set_cb(OnlineCon *ol, void (*cb)(void*,OnlineCon*), void *data);

int ol_connect(OnlineCon *ol, char *host, int port);
void ol_disconnect(OnlineCon *ol);

int ol_login(OnlineCon *ol, char *user, char *pass, char *devid);
int ol_login_sm(OnlineCon *ol, char *user, char *pass);
int ol_quit(OnlineCon *ol);

int ol_command(OnlineCon *ol, int read_response, char *format, ...);

int ol_set_status(OnlineCon *ol, char *status);
int ol_set_device(OnlineCon *ol, char *device);
int ol_set_client(OnlineCon *ol, char *client);

int ol_idle(OnlineCon *ol);

int ol_send_command(OnlineCon *ol, char *bf, int x);
int ol_read_response(OnlineCon *ol);

int ol_ok(OnlineCon *ol);
int ol_minus(OnlineCon *ol);
int ol_plus(OnlineCon *ol);
int ol_star(OnlineCon *ol);
int ol_idle_response(OnlineCon *ol);

int ol_parse(OnlineCon *ol, char *res, int bfsz);


int ol_get_picture(OnlineCon *ol, char *email, char **data_out, int *length_out, int *timestamp_out);
int ol_set_picture(OnlineCon *ol, int length, void *data, int *timestamp_out);

#define ONLINE_DROPOUT_UNKNOWN 1
#define ONLINE_DROPOUT_SLEEP 2
#define ONLINE_DROPOUT_HEARTBEAT 3


#endif
