1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include "array.h"
#include "better_string.h"
#include "hash_table.h"
#include <openssl/sha.h>
#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
|