//$Id: client.h,v 1.5 1997/11/30 12:16:42 parallax Exp $
//class Client actually handles the guts of IRC parsing

#ifndef CLIENT_H
#define CLIENT_H

#include "dcc.h"
#include <qstring.h>
#include <qobject.h>
#include <qsocknot.h>
#include <qlist.h>
//KDE
#include <kprocess.h>

class BanRequest
{
private:
    QString nick;
    QString channel;
    QString reason;
    int type;
public:
    enum { NORMAL, HOST, DOMAIN, NICK };
    BanRequest(const char *n, const char *c, const char *r=0, int t=NORMAL) { nick = n; channel = c; reason = r; type = t;}
    const char *Nick() { return nick; }
    const char *Channel() { return channel; }
    const char *Reason() { return reason; }
    int Type() { return type; }
};

class Client : public QObject {
    Q_OBJECT
public:
    Client(QObject *parent=0, const char *name=0);
    virtual ~Client(); 
    static int ConnectToServer(const char *server, int port);
    void ConnectClient(int fd); // connects the client to the server connection
    bool DisconnectFromServer(const char *msg);
    bool SendToServer(const char *buff);
    void ParseServer(const char *data);
    void ParseUser(const char *data, const char *win);
    QString UnixTimeToDate ( const char *);
    static const char *LocalAddress() { return localAddress; }
    const char *Version() { return version; }
    bool Registered() { return registered; }
    bool Connected() { return connected; }
private slots:
    void ReadData(); // read data from the socket
    void WriteData(); // write data to the socket
private:
    void ParseNumeric( int, char*, char*);
    void ParseWord( char*, char*, char*);

    void HandleEvent(int type, const char *arg1, const char *arg2=0);
    void Output(int type, const char *window, const char *txt);   
    void HandlePrivmsg( char *, char *, char *, char *);
    void HandleMode( char *, char *);
    bool HandleBuiltInCommand ( const char *cmd, const char *rest, const char *win);

    bool registered;
    bool connected; // holds connnection state
    QSocketNotifier *rsn, *wsn; //socket notifiers make our job MUCH easier   
    QString incomplete; // sometimes lag means we don't get the whole thing
    QString serverName;
    QString serverPort;
    QString version;
    static QString localAddress;
    QList<BanRequest> *kickBanList;
    //located in dcc.cpp
public:
    void CloseDCCChat(const char *nick);
    void SendToDCCChat(const char *nick, const char *buff);
private:
    void DoDCC(const char *cmd);
    void DCCSend(const char *nick, const char *file);
    void DCCGet(const char *nick);
    void DCCChat(const char *nick);
    int CreateDCCPassiveConnection(int &port); // creates a listening socket, returns fd, fills in the value for port
    QList <KProcess> *dccProcs;
    QList <DCCOffer> *dccOffers;
    QList <DCCChatSession> *dccChats;
    QList <KProcess> *processes;
    QList <QString> *procNames;
    QSocketNotifier *dccChatRead;//, *dccChatWrite;    
 private slots:
    void DCCProcessExited(KProcess *);
    void ProcessExited(KProcess *);
    void ReceiveDCCChatData(KProcess *, char *, int);
    
}; 
#endif
