/*
 ===========================================================================
 =                                                                         =
 =  (C) Copyright 1991 The Trustees of Indiana University                  =
 =                                                                         =
 =  Permission to use, copy, modify, and distribute this program for       =
 =  non-commercial use and without fee is hereby granted, provided that    =
 =  this copyright and permission notice appear on all copies and          =
 =  supporting documentation, the name of Indiana University not be used   =
 =  in advertising or publicity pertaining to distribution of the program  =
 =  without specific prior permission, and notice be given in supporting   =
 =  documentation that copying and distribution is by permission of        =
 =  Indiana University.                                                    =
 =                                                                         =
 =  Indiana University makes no representations about the suitability of   =
 =  this software for any purpose. It is provided "as is" without express  =
 =  or implied warranty.                                                   =
 =                                                                         =
 ===========================================================================
 =                                                                         =
 = File:                                                                   =
 =   IUPOP3_GENERAL.H                                                      =
 =                                                                         =
 = Authors:                                                                =
 =   Jacob Levanon & Larry Hughes                                          =
 =   Indiana University                                                    =
 =   University Computing Services, Network Applications                   =
 =                                                                         =
 = Credits:                                                                =
 =   This software is based on the Post Office Protocol version 3,         =
 =   as implemented by the University of California at Berkeley.           =
 =                                                                         =
 ===========================================================================
*/

#define TRUE            1
#define FALSE           0
#define NEGATIVE	-1
#define NEWLINE         '\n'
#define CRLF            "\r\n"
#define ENDMULTLINTRANS ".\r\n"

#define MAXHOSTNAMELEN  256
#define MAXUSERNAMELEN  13
#define MAXLINELEN      1024
#define MAXMSGLINELEN   1024
#define MAXARGCOUNT     5
#define MAXARGLEN       10

#define RFC_PORT        110
#define POP_OK          "+OK"
#define POP_ERR         "-ERR"
#define POP_SUCCESS     1
#define POP_FAILURE     0
#define POP_TERMINATE   '.'

#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))

#define pop_command         pop_args[0]	     /*  1st arg is a POP command   */
#define pop_subcommand      pop_args[1]      /*  if 1st token was XTND, then
					         the 2nd is a subcommand    */

typedef enum {					/*  POP processing states */
    auth1,					/*  Authorization	  */
    auth2,					/*  Authorization	  */
    trans,					/*  Transaction	          */
    update,					/*  Update		  */
    halt,					/*  Halt		  */
    error					/*  Error		  */
} state;

typedef struct {                                /*  POP command state info    */
    state       ValidCurrentState;              /*  Commans's operating state */
    char	*command;                       /*  The POP command	      */
    int         min_args;                       /*  Minimum args for command  */
    int         max_args;                       /*  Maximum args for command  */
    int         (*function) ();                 /*  Function for command      */
    state       PostState[2];                   /*  Post-command state	      */
} state_table;

typedef struct {                                /*  Table of extensions       */
    char	*subcommand;                    /*  The POP XTND subcommand   */
    int         min_args;                       /*  Minimum args to xtnd comm.*/
    int         max_args;                       /*  Maximum args to xtnd comm.*/
    int         (*function) ();                 /*  Function for command      */
    int         validate;                       /*  Validation required?      */
} xtnd_table;

typedef struct {                                /*  Message information       */
    int         number;                         /*  Message number	      */
    long        length;                         /*  Length of message (bytes) */
    int         lines;                          /*  Number of lines/message   */
    int         del_flag;                       /*  Is marked for deletion?   */
    int         retr_flag;                      /*  Is marked as retrieved    */
} Message;

typedef struct {                                /*  POP parameter block	      */
    char                server[MAXHOSTNAMELEN]; /*  The name of server host   */
    char                user[MAXUSERNAMELEN];   /*  Name of the POP user      */
    char		*pop_args[MAXARGCOUNT]; /*  Parse POP parameter list  */
    char		*client;                /*  Client's name	      */
    char		*ipaddr;                /*  Client's IP address	      */
    unsigned short      ipport;                 /*  Client's port	      */
    unsigned    short   msg_count;              /*  Number of new messages    */
    state               CurrentState;           /*  The current POP state     */
    Message		*mptr;                  /*  Message information list  */
    int                 msgs_deleted;           /*  Number mesgs for deletion */
    int                 last_msg;               /*  Last message processed    */
    long                bytes_deleted;          /*  Number of bytes to delete */
    long                newmail_size;           /*  Size of NEWMAIL in bytes  */
    int                 arg_count;              /*  Number of args to parse   */
    int                 sockfd;                 /*  socket in use   */
    unsigned    short	channel;		/*  VMS Mail specific */
    unsigned    int     user_context;           /*  VMS Mail specific */
    unsigned    int     file_context;           /*  VMS Mail specific */
    unsigned    int     message_context;        /*  VMS Mail specific */
    int                 newmail_selected;       /*  VMS Mail specific */
    char                mail_directory[256];    /*  VMS Mail specific */
    char                mail_file[256];         /*  VMS Mail specific */
    int                 in_use;                 /*  Multithread specific */
    int                 threadnum;              /*  Multithread specific */
    int                 connected;              /*  Multithread specific */
} POP;

/* 
** External defs for POP3 functions
*/
extern int  pop_dele();
extern int  pop_last();
extern int  pop_list();
extern int  pop_pass();
extern int  pop_quit();
extern int  pop_rset();
extern int  pop_send();
extern int  pop_stat();
extern int  pop_updt();
extern int  pop_user();
extern int  pop_xtnd();
