/*
**   YAM2NN - Usenet access for YAM p7 and newer
**   Copyright (C) 1999 Karol Bryd <kbryd@femina.com.pl>
**
**   This program is free software; you can redistribute it and/or
**   modify it under the terms of the GNU General Public License
**   as published by the Free Software Foundation; either version 2
**   of the License, or (at your option) any later version.
**
**   This program is distributed in the hope that it will be useful,
**   but WITHOUT ANY WARRANTY; without even the implied warranty of
**   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**   GNU General Public License for more details.
**
**   You should have received a copy of the GNU General Public License
**   along with this program; if not, write to the Free Software
**   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "compiler.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/memory.h>
#include <dos/var.h>
#include <dos/dos.h>
#include <dos/datetime.h>

#include <devices/timer.h>

#include <libraries/asl.h>
#include <libraries/mui.h>
#include <libraries/reqtools.h>
#include <utility/tagitem.h>
#include <MUI/NList_mcc.h>
#include <MUI/NListview_mcc.h>
#include <MUI/Busy_mcc.h>

#include <sys/types.h>
#include <netinet/in.h>

#include <proto/locale.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/exec.h>
#include <proto/utility.h>
#include <proto/dos.h>
#include <proto/rexxsyslib.h>
#include <proto/timer.h>

#ifdef __SASC
#include <proto/socket.h>
#include <proto/muimaster.h>
#include <proto/reqtools.h>
#endif

#include <rexx/rxslib.h>
#include <rexx/rexxio.h>
#include <rexx/errors.h>

#include "killfile.h"
#include "get_folder_path.h"
#include "config.h"

#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))

#define ID_SKIPALL 1
#define ID_PERFORM 2

#define ALWAYS 0
#define NEVER 1
#define SKIPPED 2

#define STATUS_ACTIVE 1
#define STATUS_INACTIVE 2

struct NewsMessage
{
    struct NewsMessage *next;  // pionter for next message
    struct NewsMessage *prev;
    struct List        *list;  // list of nodes with message's content
    struct Article     *art;   // pointer for structure Article (described later)

    char   *FullName;          // full name of file to which message will be written
    char   *MsgID;             // message id of message
    long   length;             // length of message

    struct timeval start;      // start time
    struct timeval end;        // end time
};


struct NewsNode
{
    struct NewsNode *next;
    struct NewsNode *prev;
    struct List     *mesg;    // list of downloaded messages
    char  *ngname;             // newsgroup name
    char  *path;               // path to a folder with messages
    char  *host;               // server name
    char  *last_id;            // MessageID of last downloaded message
    int    port;                // port number
    int    sd;                  // file descriptor
    long   last;                // last downloaded message
    long   last_pos;            // position of last dn message in the MUI list
    long   maxlen;              // maximum length of a message we can d/n
    long   maxmsg;              // maximum number of messages
    long   initfetch;           // initial fetch - how many fetch for the first time
    long   flushthreshold;
    long   preselect_type;
    char   folder[80];          // folder name
    char   update;              // do we need to update folders?
    long   status;              // status: active or inactive

// added on 17-apr-99

    long total_size;          // length of all articles to download from this ng
    long size_downloaded;     // length of all downloaded articles
    long num_articles;        // number of articles to download
    long art_downloaded;      // number of downloaded articles
};

// holds informations about one article on the server

struct Article
{
    long Num;                 // article number

    char *Subject;            // subject
    char *From;               // from
    char *Date;
    char *Msg_ID;
    char *References;
    char *Xref;

    char  FileName[20];        // file name, for example 07234.012
    char  FullName[108];       // file name with path
    char *MessageID;       // Message-ID field extracted from the message
    long  Size;                // message size
    long  Lines;               // number of lines
    char  Status;              // status, skip or get
    char  Importance;          // currently not used
    int   Score;               // score
};

extern APTR initmui(void);
extern void setmui(struct NewsNode *nn);
extern void fail(char *str, int ret);
extern int do_connect(char *host, int port);
extern int ngets(char *p, int fd);
extern int nwrite(int socket, char *s);
extern void updategadgets(int type);

extern struct timerequest *create_timer(ULONG unit);
extern void delete_timer(struct timerequest *tr);

extern struct ReqToolsBase   *ReqToolsBase;
extern struct IntuitionBase  *IntuitionBase;

#ifndef __GNUC__
extern struct Library        *RexxSysBase;
extern struct Library        *LocaleBase;
extern struct Library        *TimerBase;
#else
extern struct RxsLib         *RexxSysBase;
extern struct LocaleBase     *LocaleBase;
extern struct Device         *TimerBase;
#endif

extern struct timerequest    *tr;

extern struct Library        *SocketBase;
extern struct Library        *MUIMasterBase;
extern struct Library        *MiamiBase;

extern struct List           *newsgroupslist;
extern struct List           *folders;
extern struct MsgPort        *port, *ARexxPort;

extern APTR app;

#ifndef __SASC
extern char *Stptok(const char *s, char *tok, int toklen, const char *brk);
extern char *Stpblk(const char *str);
extern int Astcsma(char *s, char *p);

#define stpblk Stpblk
#define stptok Stptok
#define astcsma Astcsma

#endif

