/*----------------------------------------------------------------------------*/
/*                                                                            */
/*  Copyright (c) 1996 -  2001 by                                             */
/*  blaxxun interactive Inc, San Francisco, California, USA                   */
/*  All rights reserved                                                       */
/*                                                                            */
/*  This software is furnished under a license  and may be  used and  copied  */
/*  only in  accordance of the  terms of such license and with the inclusion  */
/*  of the above copyright notice.  This software or any other copies therof  */
/*  may not be provided or otherwise made available to any other person.  No  */
/*  title to and ownership of the software is hereby transferred.             */
/*                                                                            */
/*  The information in this software is subject to change without notice and  */
/*  should not be construed as a commitment by blaxxun interactive Inc.       */
/*                                                                            */
/*----------------------------------------------------------------------------*/

/*
--------------------------------------------------------------------------------

  MODULE NAME:  $RCSfile: apextern.h,v $
  REVISION:     $Revision: 1.49 $
  DATE:         $Date: 2001/02/22 13:36:27 $
  AUTHOR:       $Author: kristof $
  STATE:        $State: Exp $

  CREATION DATE:       Oct 15, 1996

  =DocStart= Module apextern.h
  File apextern.h

  PROJECT:            VWP         - blaxxun virtual worlds platform
  SYSTEM:             server      - server processes
  COMPONENT:          apserver    - Community Server API server

  DESCRIPTION:
      External include file for Community Server API server

      This file contains all #defines and typedefs of the interface.

      All event functions and API calls have function prototypes here.

  =DocEnd=

--------------------------------------------------------------------------------
*/

#ifndef _APEXTERN_H_
#define _APEXTERN_H_

/*
 * make it an extern C declararation for CPLUSPLUS
 */
#ifdef __cplusplus
extern "C" {
#endif

static char * _APEXTERN_H_rcsid = "$Id: apextern.h,v 1.49 2001/02/22 13:36:27 kristof Exp $";

/*
 * the following function is needed to suppress compiler warnings, don't call it
 */
static void _APEXTERN_H_rcsid_fkt()
{
    if( ! *_APEXTERN_H_rcsid )
        *_APEXTERN_H_rcsid = 0;
    if( 0 )
        _APEXTERN_H_rcsid_fkt();
}

/*----------------------------------------------------------------------------*/
/*             SYSTEM INCLUDE FILES                                           */
/*----------------------------------------------------------------------------*/
#ifdef _WIN32

#include <windows.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <limits.h>
#include <process.h>
#include <share.h>
#include <ctype.h>
#include <winsock.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/locking.h>

#else /* UNIX */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <limits.h>
#include <ctype.h>

#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ioctl.h>

#include <netdb.h>
#include <netinet/in.h>

#endif


/*----------------------------------------------------------------------------*/
/*             GLOBAL INCLUDE FILES                                           */
/*----------------------------------------------------------------------------*/
#include "raextern.h"

/*----------------------------------------------------------------------------*/
/*             MACROS/CONSTANTS                                               */
/*----------------------------------------------------------------------------*/
/*
 * =DocStart= Makros process
 *
 * The following macros can be used to write logs or errors to stderr,
 * if doing so the parameters have to be in printf format and two (( and ))
 * have to be used
 *
 * Example:
 *
 *  PROCESS_ERR(("malloc failed! errmsg: %s!\n", strerror( errno ) ));
 *
 */
#ifndef PROCESS_LOG
#define PROCESS_LOG( X ) process_logout X
#endif

#ifndef PROCESS_ERR
#define PROCESS_ERR( X ) process_errout X
#endif

#ifndef PROCESS_TRA
#define PROCESS_TRA( X ) if( process.traceon ) process_traceout X
#endif

/* =DocEnd= */

/* =DocStart= Defines packet_h */
/*
 * Error return codes of the TCP/IP services library
 */
#define PACKET_ERR_SOCKET       -1001   /* socket() call failed               */
#define PACKET_ERR_BIND         -1002   /* bind() call failed                 */
#define PACKET_ERR_HOST         -1003   /* gethostbyname() call failed        */
#define PACKET_ERR_MALLOC       -1004   /* malloc() call failed               */
#define PACKET_ERR_CONNECTION   -1005   /* Invalid socket given as parameter  */
#define PACKET_ERR_EINTR        -1006   /* function interrupted by a signal   */
#define PACKET_ERR_TIMEOUT      -1007   /* Timeout waiting with select        */
#define PACKET_ERR_SELECT       -1008   /* Error waiting with select          */
#define PACKET_ERR_RECV         -1009   /* Network read error in receive call */
#define PACKET_ERR_LISTEN       -1010   /* listen() call failed               */
#define PACKET_ERR_ACCEPT       -1011   /* accept() call failed               */
#define PACKET_ERR_EWOULDBLOCK  -1012   /* operation (send,recv) would block  */
#define PACKET_ERR_CONNECT      -1013   /* connect() call failed              */
#define PACKET_ERR_SEND         -1014   /* send() call failed                 */
#define PACKET_ERR_INVAL        -1015   /* invalid argument                   */

/* =DocEnd= */

/*
 * =DocStart= Defines apextern_h
 */
/*
 * Defines for events that are usable, used in function chSessionRegisterEvent.
 */
#define apchEventUserNew             1
#define apchEventUserNewLog          2
#define apchEventUserDelete          3
#define apchEventUserSetAttr         4
#define apchEventUserSetAttrLog      5
#define apchEventUserText            6
#define apchEventUserTextLog         7
#define apchEventSceneNew            8
#define apchEventSceneDelete         9
#define apchEventUdpRequest         10
#define apchEventHttpRequest        11
#define apchEventUserPosition       12
#define apchEventUserPositionLog    13

/*
 * Defines for reasons why a user is denied
 * Used in event function chEventUserNew and function chUserSetDenied
 * and in event function chEventUserSetAttr and function chUserSetNewDataFailed.
 */
#define CH_AUTHORISATIONFAILURE     6
#define CH_BADVERSION               8
#define CH_BADSCENE                 9
#define CH_NEEDAUTHORISATION        10
#define CH_BADURL                   11
#define CH_NOMORE                   12
#define CH_BADNICKNAME              15
#define CH_BADINFO                  16
#define CH_BADATTR                  17

/*
 * Defines for codes of attributes for event functions 
 * chEventUserSetAttr and chEventUserSetAttrLog
 */
#define CH_URL_TYPE                  2
#define CH_NICKNAME_TYPE             3
#define CH_INFO_TYPE                10
#define CH_ATTR_TYPE                30

/*
 * Defines for shared event types 
 */
#define APSE_SFBOOL	    1
#define APSE_SFIMAGE	2
#define APSE_SFTIME	    3
#define APSE_SFCOLOR	4
#define APSE_MFCOLOR	5
#define APSE_SFFLOAT	6
#define APSE_MFFLOAT	7
#define APSE_SFINT32	8
#define APSE_MFINT32	9
#define APSE_SFNODE	    10
#define APSE_MFNODE	    11
#define APSE_SFROTATION	12
#define APSE_MFROTATION	13
#define APSE_SFSTRING	14
#define APSE_MFSTRING	15
#define APSE_SFVEC2F	16
#define APSE_MFVEC2F	17
#define APSE_SFVEC3F	18
#define APSE_MFVEC3F	19
#define APSE_SFENUM	    20
#define APSE_SFBITMASK	21
#define APSE_SFMATRIX	22
#define APSE_SFLONG	    23
#define APSE_MFLONG	    24

/*
 * membertypes
 */
#define CHDB_IS_BAD       0
#define CHDB_IS_VISITOR   1
#define CHDB_IS_MEMBER    2
#define CHDB_IS_ADMIN     3

/*
 * Access bits for rights_check
 */
#define CHDB_ACCESS_READ     0x4	/* read access    */
#define CHDB_ACCESS_WRITE    0x2	/* write access   */
#define CHDB_ACCESS_OWN      0x1	/* owner access   */
#define CHDB_ACCESS_RCHANGE  0x8	/* rights change  */
#define CHDB_ACCESS_ALL      0x7	/* rwo bits       */
#define CHDB_ACCESS_ALLBITS  0xf	/* all bits       */

/* 
 * datatype/table names for member and chat places
 */
#define CHDB_MEMBER_TYPE       "M"     /* member table        */
#define CHDB_MEMBERDATA_TYPE   "MD"    /* member data table   */
#define CHDB_MEMBERONLINE_TYPE "MO"    /* member online table */
#define CHDB_VISITOR_TYPE      "VI"    /* visitor table       */
#define CHDB_OBJECT_TYPE       "O"     /* places table        */
#define CHDB_HOME_TYPE         "H"     /* homesteading table  */
#define CHDB_COMM_TYPE         "C"     /* old community table */
#define CHDB_NEIGH_TYPE        "N"     /* old neighbor table  */
#define CHDB_BLOCK_TYPE        "B"     /* old block table     */
#define CHDB_PROP_TYPE         "P"     /* old property table  */
#define CHDB_CLUB_TYPE         "CL"    /* club table          */

/* 
 * attribute names
 */
#define CHDB_ATTR_ID    "ID"    /* id keytype          */
#define CHDB_ATTR_NNM   "NNM"   /* nickname keytype    */

/* 
 * db specific error codes
 */
#define CHDB_ERR_DUPKEY        -101    /* unique secodary key already exists */
#define CHDB_ERR_KEYNOTFOUND   -102    /* key not found during update */

/*
 * A makro that determines whether the end of an iterator list
 * is reached, use it with the Handle returned by chSessionGetNext
 * and chUserGetNext.
 */
#define CHEoList( H ) (( H.sessionid == -1 ) && ( H.clientid == -1 ))

/* =DocEnd= */

/*----------------------------------------------------------------------------*/
/*             TYPE DECLARATIONS                                              */
/*----------------------------------------------------------------------------*/
/*
 * =DocStart= Types apextern_h
 *
 */
/*
 * The eventTag passed to the EventRegister function
 */
typedef int  CHEvent_t;

/*
 * A generic 4 byte value
 */
typedef unsigned long  CH_4byte_t;

/*
 * A generic 2 byte value
 */
typedef unsigned short CH_2byte_t;

/* 
 * An IP address, always used in network byte order
 */
typedef unsigned long  CH_ip_t;

/*
 * A port number, always used in network byte order
 */
typedef unsigned short CH_port_t;

/*
 * A protocol version as used by the clients
 */
typedef unsigned long  CH_version_t;

/*
 * A Community Server ID
 */
typedef CH_4byte_t CH_id_t;

/*
 * A Client install ID as sent by the client
 */
typedef struct
{
    CH_4byte_t  installid1;
    CH_4byte_t  installid2;

} CHInstallId_t;

/*
 * A Handle as we pass it to event functions
 */
typedef struct 
{
    CH_id_t     sessionid;
    CH_id_t     clientid;
    CH_port_t   port;
    CH_ip_t     ip;
} CHHandle_t;

/*
 * A position as stored in memory
 */
typedef struct
{
    long   x;
    long   y;
    long   z;
} CHPosition_t;                 /* an x-y-z position                  */

/*
 * An orientation as stored in memory
 */
typedef struct
{
    int            x;           /* x component of unit vector         */
    int            y;           /* y component of unit vector         */
    int            z;           /* z component of unit vector         */
    unsigned int   a;           /* the angle                          */
} CHOrientation_t;              /* an orientation                     */

/*
 * A pointer to an event function
 */
typedef void (* CHHandlerFuncPtr_t) ( CHHandle_t handle, int code );

/*
 * The following type is used internally by the API server
 */
typedef struct apserver_s
{
    time_t            now;              /* the current time set by ap_reply   */
    CH_port_t         port;             /* port number used by API server     */
    CH_ip_t           ip;               /* ip address used by server          */
    int               socket;           /* socket used by API server          */
    time_t            lastlog;          /* last time log was printed          */
    volatile int      statusprint;      /* flag for status print, set by      */
                                        /* id_sigusr1_handler                 */

    void            * iter;             /* current iterator element           */
    char            * sessionpaa;       /* hash table for known sessions      */

    void            * sessionhead;      /* head of list of session we know    */
    void            * sessiontail;      /* tail of list of sesions we know    */

    char            * clientpaa;        /* hash table for known clients       */
                                        /* number of packets received of type */
    unsigned long     tusers;
    unsigned long     nusers;

    unsigned long     tscenes;
    unsigned long     nscenes;

    unsigned long     tsessions;
    unsigned long     nsessions;

    unsigned long     tevents;
    unsigned long     tapchEventPeriodic;
    unsigned long     tapchEventUserPacket;
    unsigned long     tapchEventSessionReqSubscribe;
    unsigned long     tapchEventSessionAckSubscribe;
    unsigned long     tapchEventUserNew;
    unsigned long     tapchEventUserNewLog;
    unsigned long     tapchEventUserDelete;
    unsigned long     tapchEventUserSetAttr;
    unsigned long     tapchEventUserSetAttrLog;
    unsigned long     tapchEventUserText;
    unsigned long     tapchEventUserTextLog;
    unsigned long     tapchEventUserPositionLog;
    unsigned long     tapchEventSceneNew;
    unsigned long     tapchEventSceneDelete;

    /*
     * packets received
     */
    unsigned long     nAPDataPacketsReceived;
    unsigned long     nAPDataReplaysReceived;

    unsigned long     n0packets;        /* length 0                           */
    unsigned long     nunknown;
    unsigned long     nreqsubscribe;
    unsigned long     nacksubscribe;
    unsigned long     nconfsubscribe;
    unsigned long     nlog;
    unsigned long     nquery;
    unsigned long     ngetid;
    unsigned long     nscenecreate;
    unsigned long     nscenedelete;
    unsigned long     ncupd;
    unsigned long     nsetx;
    unsigned long     nsetattr;
    unsigned long     ntextsend;
    unsigned long     ntextgrsend;
    unsigned long     nuserdata;
    unsigned long     ncommandfailed;

    /*
     * packets sent
     */
    unsigned long     nInitsubscribe;
    unsigned long     nCmdsubscribe;
    unsigned long     nAck;
    unsigned long     nFail;
    unsigned long     nHandle;
    unsigned long     nClientpacket;

    struct timeval    period;

    int (*usrdata_handler)();    /* Pointer to handler for unknown packets    */

    int sessionAllowAll;         /* allow sessions from any community server  */
    int useSEServer;             /* activate the SE server  */

    int useSOServer;             /* run an soserver, do the license check     */

} apserver_t;

/* =DocEnd= */

/*----------------------------------------------------------------------------*/
/*             VARIABLE DECLARATIONS                                          */
/*----------------------------------------------------------------------------*/
/*
 * =DocStart= Globals apextern_h
 */

/* =DocEnd= */

/*----------------------------------------------------------------------------*/
/*             FUNCTION PROTOTYPES                                            */
/*----------------------------------------------------------------------------*/

/*
 * =DocStart= Functions apextern_h
 */
extern int chSessionAllowAll(
int allow               /* flag: Allow sessions from any Community Server   */
);

extern int chSessionAllow(
CHHandle_t         sessionHandle        /* r: handle for session structure  */
);

extern int chSessionCreate(
char *             hostname,            /* r: name of host of ID server     */
CH_port_t          port,                /* r: port used by ID server        */
char *             addr,                /* r: internet address of ID server */
int                alen                 /* r: length of that address        */
);

extern int chSessionRegisterEvent(
CHHandle_t         sessionHandle,       /* r: handle for session structure  */
CHEvent_t          eventTag,            /* r: which event to register       */
CHHandlerFuncPtr_t eventFunc            /* r: function that handles event   */
);

extern int chSessionCmdSubscribe(
CHHandle_t         sessionHandle        /* r: handle for session structure  */
);

extern int chUserResetIter(
CHHandle_t     sessionHandle    /* r: handle for session structure          */
);

extern CHHandle_t chUserGetSceneHandle(
CHHandle_t     userHandle       /* r: handle for user structure             */
);

extern CHHandle_t chUserGetNext(
CHHandle_t     sessionHandle    /* r: handle for session structure          */
);

extern int chSceneResetIter(
CHHandle_t     sessionHandle    /* r: handle for session structure          */
);

extern CHHandle_t chSceneGetNext(
CHHandle_t     sessionHandle    /* r: handle for session structure          */
);

extern int chSceneUserResetIter(
CHHandle_t     sceneHandle      /* r: handle for scene structure            */
);

extern CHHandle_t chSceneUserGetNext(
CHHandle_t     sceneHandle      /* r: handle for scene structure            */
);

extern int chUserSetDenied(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        reasonCode                   /* r: reasoncode for user deny      */
);

extern int chUserSetInfo(
CHHandle_t  userHandle,                 /* r: handle for user structure     */
const char* info,                       /* r: info to set                   */
int         infolen                     /* r: length of that info           */
);

extern int chUserSetUrl(
CHHandle_t  userHandle,                 /* r: handle for user structure     */
const char* url,                        /* r: url to set                    */
int         urllen                      /* r: length of that url            */
);

extern int chUserSetNickname(
CHHandle_t  userHandle,                 /* r: handle for user structure     */
const char* nickname,                   /* r: nickname to set               */
int         nicknamelen                 /* r: length of that nickname       */
);

extern int chUserSetAttr(
CHHandle_t  userHandle,                 /* r: handle for user structure     */
CH_4byte_t  attr,                       /* r: attr to set                   */
int         attrlen                     /* r: length of that attr           */
);

extern int chUserSetApplicationData(
CHHandle_t  userHandle,                 /* r: handle for user structure     */
char       *data,                       /* r: data to set                   */
int         datalen                     /* r: length of that data           */
);

extern const char * chUserGetNewData(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern const char * chUserGetText(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern int chUserSetText(
CHHandle_t  userHandle,                 /* r: handle for user structure     */
const char* text,                       /* r: text to set                   */
int         textlen                     /* r: length of that text           */
);

extern int chUserSendText(
CHHandle_t  userHandle,                 /* r: handle for user structure     */
CHHandle_t *receiverHandle,             /* r: handle for receiving user     */
const char* text,                       /* r: text to set                   */
int         textlen,                    /* r: length of that text           */
const char* topic,                      /* r: topic of group to send to     */
int         topiclen                    /* r: length of that topic          */
);

extern const char * chUserGetGroupTopic(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern CH_id_t chUserGetGroupID(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern int chUserSetTextIgnore(
CHHandle_t userHandle                   /* r: handle for user structure     */
);

extern int chUserSetNewData(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int          code                       /* r: the code of the data to set   */
);

extern int chUserSetNewDataFailed(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        reasonCode                   /* r: reasoncode for user deny      */
);

extern time_t chUserGetCreateTime(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern time_t chUserGetLastTextTime(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern const char * chUserGetSceneName(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern const char * chUserGetSceneurl(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern const char * chUserGetNickname(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern const char * chUserGetAttr(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern char * chUserGetApplicationData(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern CHPosition_t chUserGetPosition(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern CHOrientation_t chUserGetOrientation(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern CH_4byte_t chUserGetStatusBits(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern const char * chUserGetInfo(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern const char * chUserGetUrl(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern const char * chUserGetUserid(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern const char * chUserGetPasswd(
CHHandle_t   userHandle,                /* r: handle for user structure     */
int        * len                        /* w: length of value on return     */
);

extern CH_ip_t chUserGetIp(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern CH_ip_t chUserGetRealIp(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern CH_id_t chUserGetId(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern CH_port_t chUserGetPort(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern CH_version_t chUserGetVersion(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern int chUserGetDenied(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern int chUserGetEncryption(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern CHInstallId_t chUserGetInstallId(
CHHandle_t   userHandle                 /* r: handle for user structure     */
);

extern const char * chSceneGetUrl(
CHHandle_t   sceneHandle,               /* r: handle for scene structure    */
int        * len                        /* w: length of value on return     */
);

extern const char * chSceneGetName(
CHHandle_t   sceneHandle,               /* r: handle for scene structure    */
int        * len                        /* w: length of value on return     */
);

extern char * chSceneGetApplicationData(
CHHandle_t   sceneHandle,               /* r: handle for scene structure    */
int        * len                        /* w: length of value on return     */
);

extern int chSceneSetApplicationData(
CHHandle_t   sceneHandle,               /* r: handle for scene structure    */
char        *data,                      /* r: data to set for scene         */
int          datalen                    /* r: length of that data           */
);

extern int chSceneSetTextEventStatus(
CHHandle_t   sceneHandle,   /* r: handle for scene structure    */
int          status         /* r: status to set                 */
);

extern int chSceneSetPositionEventStatus(
CHHandle_t   sceneHandle,   /* r: handle for scene structure    */
int          status         /* r: status to set                 */
);

extern int chSceneGetNofUsers(
CHHandle_t   sceneHandle                /* r: handle for scene structure    */
);

extern int chSceneGetKeepalive(
CHHandle_t   sceneHandle                /* r: handle for scene structure    */
);

extern int chSessionResetIter( void );

extern CHHandle_t chSessionGetNext( void );

extern int chSessionSetPeriod(
CHHandle_t     sessionHandle,   /* r: handle for session structure          */
struct timeval period           /* r: period to be used for the session     */
);

extern CH_port_t chSessionGetPort(
CHHandle_t   sessionHandle              /* r: handle for session structure  */
);

extern CH_id_t chSessionGetApiId(
CHHandle_t   sessionHandle              /* r: handle for session structure  */
);

extern CH_ip_t chSessionGetIdserverIP(
CHHandle_t   sessionHandle              /* r: handle for session structure  */
);

extern CH_port_t chSessionGetIdserverPort(
CHHandle_t   sessionHandle              /* r: handle for session structure  */
);

extern char * chSessionGetIdserverHostname(
CHHandle_t   sessionHandle              /* r: handle for session structure  */
);

extern time_t chSessionGetCreateTime(
CHHandle_t   sessionHandle              /* r: handle for session structure  */
);

extern time_t chSessionGetReqSubscribeTime(
CHHandle_t   sessionHandle              /* r: handle for session structure  */
);

extern time_t chSessionGetAckSubscribeTime(
CHHandle_t   sessionHandle              /* r: handle for session structure  */
);

time_t chSessionGetLastPacketTime(
CHHandle_t   sessionHandle              /* r: handle for session structure  */
);

extern int chSharedEventIsShared ( 
CHHandle_t userHandle            /* r: handle for user structure     */
);

extern int chSharedEventSetPrecision ( 
int precision       /* r: precision value */
);

extern int chSharedEventNoStore ( 
CHHandle_t userHandle            /* r: handle for user structure     */
);

extern int chSharedEventGet ( 
CHHandle_t userHandle,            /* r: handle for user structure     */
char **name,       /* w: name of the event  */
int  *typei,       /* w: type of the event  */
char **value,      /* w: value of the event */
int  *noOfValues   /* w: no of single values of the event, 
                         e.g. for an SFVec3f it would be 3 */
);

extern int chSharedEventCreateFloat ( 
char *name,       /* r: name of the event  */
int   typei,      /* r: type of the event  */
float value[],    /* r: array of floats */
int  noOfValues,  /* r: no of floats in value */
char **event      /* w: output event  */
);

extern int chSharedEventCreateString ( 
char *name,       /* r: name of the event  */
int   typei,      /* r: type of the event  */
char *value,      /* r: string value */
char **event      /* w: output event  */
);

extern int chSharedEventCreateLong ( 
char *name,       /* r: name of the event  */
int  typei,       /* r: type of the event  */
long value,       /* r: long value */
char **event      /* w; output event  */
);

extern int chSharedEventGetFloat ( 
char *value,      /* r: value of the event  */
float *floats,    /* r: array of floats */
int  noOfValues   /* r: no of floats in floats */
);

extern int chSharedEventGetLong ( 
char *value,      /* r: value of the event  */
long *lvalue      /* r: long value */
);

extern int chSharedEventGetString ( 
char *value,      /* r: value of the event  */
char **svalue     /* w: string output */
);

extern int chSharedEventStore ( 
const char *scene,/* r: name of the scene  */
char *event       /* r: the event  */
);

extern int chSharedEventFindStored ( 
const char *scene,/* r: name of the scene  */
char *name,       /* r: name of the event  */
int   typei,      /* r: type of the event  */
char **value,     /* w: value of the event */
int  *noOfValues  /* w: no of single values of the event,
                        e.g. for an SFVec3f it would be 3 */
);

extern int chSharedEventGetFirstStored ( 
char **scene,      /* w: name of the scene  */
char **name,       /* w: name of the event  */
int  *typei,       /* w: type of the event  */
char **value,      /* w: value of the event */
int  *noOfValues   /* w: no of single values of the event,
                         e.g. for an SFVec3f it would be 3 */
);

extern int chSharedEventGetNextStored ( 
char **scene,      /* w: name of the scene  */
char **name,       /* w: name of the event  */
int  *typei,       /* w: type of the event  */
char **value,      /* w: value of the event */
int  *noOfValues   /* w: no of single values of the event,
                         e.g. for an SFVec3f it would be 3 */
);

extern int chSharedEventDeleteStored ( 
const char *scene,/* r: name of the scene  */
char *name,       /* r: name of the event  */
int   typei       /* r: type of the event  */
);

/* =DocEnd= */
/*
 * =DocStart= Events apextern_h
 */
extern int chEventStartup(
int        argc,                        /* command line parameters          */
char      *argv[]                       /* comamnd line parameters          */
);

extern void chEventShutdown(
int exitcode                            /* return code for exit call        */
);

extern void chEventSessionReqSubscribe(
CHHandle_t sessionHandle,               /* r: handle for session structure  */
int        code                         /* r: unused in this function       */
);

extern void chEventSessionAckSubscribe(
CHHandle_t sessionHandle,               /* r: handle for session structure  */
int        code                         /* r: unused in this function       */
);

extern void chEventSessionPeriodic(
CHHandle_t sessionHandle                /* r: handle for session structure  */
);

extern void chEventUserNew(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        code                         /* r: unused in this function       */
);

extern void chEventUserNewLog(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        code                         /* r: unused in this function       */
);

extern void chEventUserDelete(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        code                         /* r: unused in this function       */
);

extern void chEventUserSetAttr(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        code                         /* r: code which attribute changed  */
);

extern void chEventUserSetAttrLog(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        code                         /* r: code which attribute changed  */
);

extern void chEventUserText(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        code                         /* r: unused in this function       */
);

extern void chEventUserTextLog(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        code                         /* r: unused in this function       */
);

extern void chEventUserPositionLog(
CHHandle_t userHandle,                  /* r: handle for user structure     */
int        code                         /* r: unused in this function       */
);

extern int chEventUsrData(
int            psocket,
char           * packet,                /* r: packet received               */
int              packetlen,             /* r: length of that packet         */
char           * sender,                /* r: socket address of sender      */
int              senderlen              /* r: length of that address        */
);

extern void chEventSceneNew(
CHHandle_t sceneHandle,                 /* r: handle for scene structure    */
int        code                         /* r: unused in this function       */
);

extern void chEventSceneDelete(
CHHandle_t sceneHandle,                 /* r: handle for scene structure    */
int        code                         /* r: unused in this function       */
);

extern void chEventUdpRequest(
char            *client,                /* client that sent the request     */
int              inargc,                /* number of arguments sent         */
char            *inargv[]               /* pointerarray of arguments sent   */
);

extern void chEventHttpRequest(
char  *connection                   /* r: connection that sent the request  */
);

/* =DocEnd= */
/*
 * =DocStart= User handling apextern_h
 */
extern void chSetUserDataHandler(
int        (*usrdata_handler)()     /* r: handler for unknown packets       */
) ;

extern int chSessionGetSocket(
CHHandle_t   sessionHandle          /* r: handle for session structure      */
);

extern int chSessionGetNofScenes(
CHHandle_t   sessionHandle        /* r: handle for session structure  */
);

extern int chCSUserPositionSend(
char            *pnickname,             /* r: nickname used by user          */
int              pnicknamelen,          /* r: length of that nickanme        */
char            *puniqueid,             /* r: unique id used by user         */
int              puniqueidlen,          /* r: length of that unique id       */
CHPosition_t    *pposition,             /* r: position to send               */
CHOrientation_t *porientation           /* r: orientation ot send            */
);

extern int chCSUserStatusBitsSet(
char            *pnickname,            /* r: nickname used by user          */
int              pnicknamelen,         /* r: length of that nickname        */
char            *puniqueid,            /* r: unique id used by user         */
int              puniqueidlen,         /* r: length of that unique id       */
CH_4byte_t       statusbits            /* r: status bits to set             */
);

extern int  chCSUserTextSend(
char       *pnickname,                  /* r: nickname used by user          */
int         pnicknamelen,               /* r: length of that nickanme        */
char       *puniqueid,                  /* r: unique id used by user         */
int         puniqueidlen,               /* r: length of that unique id       */
char       *ptopic,                     /* r: topic of group to send to      */
int         ptopiclen,                  /* r: length of that topic           */
char       *ptext,                      /* r: text to be sent                */
int         ptextlen                    /* r: length of that text            */
);

extern void chCSUserDelete(
char       *pnickname,                  /* r: nickname used by user          */
int         pnicknamelen,               /* r: length of that nickanme        */
char       *puniqueid,                  /* r: unique id used by user         */
int         puniqueidlen                /* r: length of that unique id       */
);

extern void chCSUserCreate(
CHHandle_t  psessionHandle,             /* r: handle for session structure   */
char       *puniqueid,                  /* r: unique id used by user         */
int         puniqueidlen,               /* r: length of that unique id       */
int         psocket,                    /* r: socket to send packets through */
char       *puserid,                    /* r: userid for GetId packet        */
int         puseridlen,                 /* r: length of that id              */
int         pencryption,                /* r: encryption used for passwd     */
char       *ppassword,                  /* r: password given by user         */
int         ppasswordlen,               /* r: length of that password        */
char       *pattributes,                /* r: application attributes of user */
int         pattributeslen,             /* r: length of these attributes     */
char       *psceneurl,                  /* r: url of scene to enter          */
int         psceneurllen,               /* r: length if that url             */
char       *pnickname,                  /* r: nickname used by user          */
int         pnicknamelen,               /* r: length of that nickanme        */
char       *pinfo,                      /* r: public info of user            */
int         pinfolen,                   /* r: length of that info            */
char       *pgroup,                     /* r: group user wants to join       */
int         pgrouplen,                  /* r: length of that group           */
char       *purl,                       /* r: avatar url of user             */
int         purllen,                    /* r: length of that url             */
CH_ip_t     pip,                        /* r: ip address of user             */
void        (*perrorfunc)(              /* r: function to call on error      */
                   char     *pnickname, /* r: nickname of user, as above     */
                   int       pnicknamelen,
                   char     *psceneurl, /* r: url of scene to enter          */
                   int       psceneurllen,
                   char     *puniqueid, /* r: unique id of user              */
                   int       puniqueidlen,
                   CH_id_t   reason,    /* r: reason for failure             */
                   int       code       /* r: code of packet causing failure */
                  )
);

extern void chCSPeriodic( void );

extern int  chCSUserDataHandler(
int         psocket,                    /* r: socket data was received at    */
char      * packet,                     /* r: packet received                */
int         packetlen,                  /* r: length of that packet          */
char      * sender,                     /* r: socket address of sender       */
int         senderlen                   /* r: length of that address         */
);

/* =DocEnd= */
/*
 * =DocStart= Database access apextern_h
 */

extern int chDBInit(
char * gcfg,             /* alternative global.cfg to read the parameters   */
char * host,             /* host and aport of dbserver host to contact      */
char * port              /* if NULL, the global.cfg is used                 */
);

extern int chDBInitFromScript();

extern char * chDBGetError();
extern char * chDBGetServerHost();
extern char * chDBGetServerPort();

extern int chDBExists(
char * datatype                 /* r: type of data to check                */
);

extern int chDBMget(
char *  datatype,               /* r: type of data concerned                */
char *  key,                	/* r: Key of value to get                   */
char *  val,                	/* r: value of the key                      */
int     nattr,                  /* r: number of attribute value pairs       */
char ** attr,                   /* r: attributes to get                     */
char ** avals                   /* w: vals of those attributes              */
);

extern char * chDBGet(
char * datatype,           /* r: type of data concerned                */
char * key,                /* r: Key of value to get                   */
char * val,                /* r: Value of that key                     */
char * attrname            /* r: Name of attribute to get from key     */
);

extern int chDBListKeys(
char *  datatype,           /* r: type of data concerned                */
char *  keytype,            /* r: Key of value to get                   */
char *  startkeyvalue,      /* r: start value for the key               */
char *  number,             /* r: number of keys to list                */
char *  direction,          /* r: direction of list                     */
char *  keyfilter,          /* r: filter for keys                       */
char ** keys                /* w: list of keys returned                 */
);

extern int chDBList(
char *  datatype,        /* r: type of data concerned                */
char *  keytype,         /* r: Key type defining the list order      */
char *  startkeytype,    /* r: Key type to position on a record      */
char *  startkeyvalue,   /* r: start value for the key               */
char *  number,          /* r: number of keys to list                */
char *  direction,       /* r: direction of list                     */
char *  keyfilter,       /* r: filter for keys                       */
int     nattr,           /* r: number of attributes to get           */
char ** attr,            /* r: attributes to get                     */
char ** vals             /* w: vals of found keys and req attributes */
);

extern int chDBMset(
char *  datatype,               /* r: type of data concerned                */
char *  keytype,                /* r: keytype of value to set               */
char *  key,                	/* r: value of the key                      */
int     nattr,                  /* r: number of attribute value pairs       */
char ** attr,                   /* r: attributes to set                     */
char ** avals                   /* r: vals of those attributes              */
);

extern int chDBMadd(
char *  datatype,               /* r: type of data concerned                */
char *  keytype,                /* r: keytype of value to set               */
char *  key,                	/* r: value of the key                      */
int     nattr,                  /* r: number of attribute value pairs       */
char ** attr,                   /* r: attributes to set                     */
char ** avals                   /* r: vals to add/sub to those attributes   */
);

extern int chDBAdd(
char *  datatype,               /* r: type of data concerned                */
char *  keytype,                /* r: keytype of value to change            */
char *  key,                    /* r: value of the key                      */
char *  attr,                   /* r: attribute to change                   */
char *  aval,                   /* r: value to add/sub to the attribute     */
char ** result                  /* w: result of add/sub in malloced memory  */
);

extern int chDBIns(
char *  datatype,               /* r: type of data concerned                */
int     nkeys,                	/* r: number of key value pairs             */
char ** keys,                	/* r: keys of the record                    */
char ** vals,                	/* r: vals of those keys                    */
int     nattr,                	/* r: number of attribute value pairs       */
char ** attr,                	/* r: attributes to set                     */
char ** avals                	/* r: vals of those attributes              */
);

extern int chDBDelete(
char * datatype,        /* r: type of data concerned                */
char * key,             /* r: Key of value to delete                */
char * val              /* r: value of key                          */
);

extern int chDBMgetCached(
char *  datatype,               /* r: type of data concerned                */
char *  kty,                	/* r: Keytype of value to get               */
char *  val,                	/* r: value of the key                      */
int     nattr,                  /* r: number of attribute value pairs       */
char ** attr,                   /* r: attributes to get                     */
char ** avals                   /* w: vals of those attributes              */
);

extern char * chDBGetCached(
char *  datatype,               /* r: type of data concerned                */
char *  key,                	/* r: Key of value to get                   */
char *  val,                	/* r: value of the key                      */
char * aname                    /* r: name of attributes to read            */
);

extern void chDBSetCacheTime(
time_t newtime                  /* r: expiration time of cache entries      */
);

extern int chDBGetObjectFromScene(
char *  scene,                  /* r: scene name                            */
char ** dty,            	    /* w: datatype of parent                    */
char ** key                     /* w: keyid of parent                       */
);

int chDBGetObjectFromSplitScene(
char *  scene,                  /* r: scene name                            */
char ** dty,                    /* w: datatype of object                    */
char ** key,                    /* w: key value of object                   */
char ** splitkey                /* w: split key value of object             */
);

extern int chDBGetParent(
char *  datatype,               /* r: type of data concerned                */
char *  key,                	/* r: Key of value to get                   */
char ** parentdty,            	/* w: datatype of parent                    */
char ** parentkey               /* w: keyid of parent                       */
);

extern int chDBGetMemberType(
char * nickname,            /* the nickname of the member                   */
char ** memberid,           /* w: id of the member                          */
char ** role                /* w: the role  of the member                   */
);

extern char * chDBGetDatatype(
char * pKey                 /* r: Key of value to get                   */
);

extern int chDBCheckRole(
char * mrole,                /* role string of the member        */
char * orole                 /* role string of the object        */
);

extern int chDBCheckRights(
int    mtype,            /* r: type of the member (optional)         */
char * memid,            /* r: member id (optional)                  */
char * nickname ,        /* r: nickname of the member                */
char * rolemask,         /* r: member's role mask, (optional)        */
char * odatatype,        /* r: datatype of object to check           */
char * okeytype,         /* r: keytype of object to check            */
char * okey,             /* r: key of object to check                */
int    accesstype        /* r: accesstype to check for               */
);

extern int chDBUnscrambleCookie(
char  * passwd,                  /* r: input buffer                  */
char ** cookietag,               /* w: tag read from cookie          */
char ** cookieid,                /* w: id read from cookie           */
char ** cookienickname,          /* w: nickname read from cookie     */
char ** cookiepasswd,            /* w: passwd read from cookie       */
char ** cookietime,              /* w: time read from cookie         */
char ** cookielang,              /* w: user language preference      */
char ** cookiecache              /* w: user cache string             */
);

extern int chSessionGetServerAddress (
char *  phostname,                      /* r  of host providing the service */
short   pport,                          /* r  of the service required       */
char ** ppsrvadr                        /*  w address of server             */
);

extern char * chSessionIp2Str(
CH_ip_t      ip                    /* r: ip address to convert         */
);

/*
 * Internal print functions used by the error and log macros,
 * don't use them directly !!!
 */
extern void process_errout(char *format, ...);
extern void process_logout(char *format, ...);
extern int process_traceout(char *format, ...);

extern int process_exitrc;

extern int process_get_chstart_line_with_comments(
char * processname, char * file
);

extern int process_get_chstart_line( char * processname, char * file );
extern int  process_startArg_isComment();
extern char * process_startArg( int i );

/* =DocEnd= */

#ifdef __cplusplus
}
#endif

#endif
