#ifndef PROTOCOL_H #define PROTOCOL_H #include #include #include #include "array.h" #include "better_string.h" #include "hash_table.h" #include #define MAX_OP_LEN 10 #define MAX_ARG_LEN 50 #define MAX_ARGS 5 #define MAX_BUFSIZE 4096 #define DEFAULT_PORT 11111 #define MAX_CONNECTIONS 32768 #define DEFAULT_TIMEOUT 6000 #define USERNAME_SIZE 30 #define KEYLEN 512 typedef struct { array_t *friends; /* if NULL then guest */ unsigned char *passhash; array_t *autojoin; array_t *dms; string_t *name; } account_t; typedef struct { /*! @brief nick who sent the message */ string_t *nick; /*! @brief message content */ string_t *msg; /*! @brief Unix time */ time_t time; } message_t; /*! @brief describes multiple user chats */ typedef struct CHANNEL_STRUCT { account_t *admin; /*! @brief account_t array */ array_t *mods; /*! @brief No guests */ bool registered_only; /*! @brief account_t array * if set, allowed_users creates a private conversation with only * these users */ array_t *allowed_users; /*! @brief Explicitly banned users, account_t array */ array_t *banned_users; /*! @brief account_t array */ array_t *users; /*! @brief Description of channel can be set after creation */ string_t *desc; /*! @brief message logs */ array_t *msgs; } channel_t; /*! @brief struct that contains DM */ typedef struct DM_STRUCT { account_t *src; array_t *msgs; } dm_t; typedef struct SERVER_STRUCT server_t; /*! @brief wrapper for server handler that conforms to C spec */ typedef struct SFUNC_STRUCT { /*! @brief In C spec, function pointers are not the same size as regular pointers */ void (*f)(server_t *, list_t *); } sfunc_t; /*! @brief Server state encapsulation */ struct SERVER_STRUCT { /*! @brief account name => user_t */ ht_t *accmap; /*! @brief username => user_t */ ht_t *usermap; /*! @brief account name => friends */ ht_t *friendmap; /*! @brief fd (int) => string_t */ ht_t *fdmap; /*! @brief channel name => channel_t */ ht_t *chanmap; /*! @brief op name => sfunc_t */ ht_t *opmap; struct pollfd *fds; int nfds; size_t fd_capacity; }; string_t *date_str(); bool same_day(struct tm *date1, struct tm *date2); string_t *encode_chanstate(ht_t *chans); string_t *encode_usersstate(ht_t *u); message_t *init_message(string_t *nick, string_t *msg, time_t time); void cop_ok(server_t *s, list_t *args); void cop_nop(server_t *s, list_t *args); server_t *init_server(size_t size); void server_addop(server_t *s, void (*sfunc)(server_t *s, list_t *args)); void server_execop(server_t *s, list_t *stk); #endif