Tchatator413
TripEnArvor instant messaging protocol - JSON-based
Loading...
Searching...
No Matches
action.h
Go to the documentation of this file.
1
5
6#ifndef ACTION_H
7#define ACTION_H
8
9#include <json-c.h>
10#include <stdbool.h>
11#include <stdio.h>
12
13#include "const.h"
14#include "cfg.h"
15#include "db.h"
16#include "server.h"
17#include "types.h"
18
31
41
43typedef struct {
47 union {
48 struct {
50 char const *location;
53 json_object *obj_actual;
55 json_type expected;
56 } type;
57 struct {
59 char const *location;
60 } missing_key;
61 struct {
63 char const *location;
65 char const *reason;
67 json_object *obj_bad;
68 } invalid;
69 struct {
72 } rate_limit;
73 struct {
75 char const *name;
76 } invariant;
77 struct {
80 } other;
81 } info;
83
85typedef enum {
88
89#define X(name) action_type_##name,
90 X_ACTIONS(X)
91#undef X
93
95#define ACTION_TYPE(name) CAT(action_type_, name)
96
98typedef struct {
102 union {
103 action_error_t error;
104 struct {
105 api_key_t api_key;
106 slice_t password;
107 } login;
108 struct {
109 token_t token;
110 } logout, motd;
111 struct {
112 api_key_t api_key;
113 serial_t user_id;
114 } whois;
115 struct {
116 token_t token;
117 serial_t dest_user_id;
118 slice_t content;
119 } send;
120 struct {
121 token_t token;
122 page_number_t page;
123 } inbox, outbox;
124 struct {
125 token_t token;
126 serial_t msg_id;
127 slice_t new_content;
128 } edit;
129 struct {
130 token_t token;
131 serial_t msg_id;
132 } rm;
133 struct {
134 token_t token;
135 serial_t user_id;
136 } block, unblock, ban, unban;
137 } with;
138} action_t;
139
140typedef struct {
141 action_type_t type;
142 bool has_next_page;
143 union {
144 action_error_t error;
145 struct {
146 token_t token;
147 } login;
148 struct {
149 user_t user;
150 } whois;
151 struct {
152 serial_t msg_id;
153 } send;
154 msg_list_t motd, inbox, outbox;
155 /*struct {
156
157 } edit;
158 struct {
159
160 } rm;
161 struct {
162
163 } block;
164 struct {
165
166 } unblock;
167 struct {
168
169 } ban;
170 struct {
171
172 } unban;*/
173 } body;
174} response_t;
175
179
183response_t response_for_rate_limit(time_t next_request_at);
184
188void put_role(role_flags_t role, FILE *stream);
189
195action_t action_parse(cfg_t *cfg, db_t *db, json_object *obj);
196
203response_t action_evaluate(action_t const *action, cfg_t *cfg, db_t *db, server_t *server);
204
208json_object *response_to_json(response_t *response);
209
210#ifndef NDEBUG
214void action_explain(action_t const *action, FILE *output);
215#endif // NDEBUG
216
217#endif // ACTION_H
void put_role(role_flags_t role, FILE *stream)
Put an user role.
response_t response_for_rate_limit(time_t next_request_at)
Builds a response for a rate limit error.
void response_destroy(response_t *response)
Destroy a response.
action_error_type_t
Enumerates the type of errors that occur while parsing or running an action.
Definition action.h:33
@ action_error_type_missing_key
Parsing JSON missing key.
Definition action.h:35
@ action_error_type_type
Parsing JSON type error.
Definition action.h:34
@ action_error_type_other
Another error occured.
Definition action.h:39
@ action_error_type_invalid
Parsing JSON invalid value.
Definition action.h:36
@ action_error_type_invariant
An invariant was broken.
Definition action.h:38
@ action_error_type_rate_limit
The rate limit has been reached.
Definition action.h:37
json_object * response_to_json(response_t *response)
Convert an action response to JSON.
action_t action_parse(cfg_t *cfg, db_t *db, json_object *obj)
Parse an action from a JSON object.
void action_explain(action_t const *action, FILE *output)
Explain an action.
response_t action_evaluate(action_t const *action, cfg_t *cfg, db_t *db, server_t *server)
Evaluate an action.
action_type_t
Enumerates the types of actions, representing the various commands available.
Definition action.h:85
@ action_type_error
Special value for an action triggered an error.
Definition action.h:87
status_t
Status codes for the Tchatator413 protocol, modeled after HTTP status codes.
Definition action.h:21
@ status_bad_request
Bad request.
Definition action.h:22
@ status_payload_too_large
Payload too large.
Definition action.h:26
@ status_unauthorized
Unauthorized.
Definition action.h:23
@ status_internal_server_error
Internal server error.
Definition action.h:29
@ status_too_many_requests
Too many requests.
Definition action.h:28
@ status_forbidden
Forbidden.
Definition action.h:24
@ status_unprocessable_content
Unprocessable content.
Definition action.h:27
@ status_not_found
Not found.
Definition action.h:25
Tchatator413 server configuration - Interface.
struct cfg cfg_t
An opaque handle to a configuration object.
Definition cfg.h:14
General constants - Standalone header.
#define X_ACTIONS(X)
X-macro that expands to the list of actions.
Definition const.h:18
DAL - Interface.
void db_t
An opaque handle to a database connection.
Definition db.h:14
json-c wrapper. Include this instead of JSON-C (compile time error mechanisms)
Tchatator413 dynamic server state - Interface.
struct server server_t
Opaque type handle representing a server instance.
Definition server.h:23
An error that occured while parsing or running an action.
Definition action.h:43
action_error_type_t type
Type of the error.
Definition action.h:45
json_object * obj_bad
The faulty JSON object.
Definition action.h:67
json_type expected
The type obj_actual should have been of.
Definition action.h:55
time_t next_request_at
The Unix time at which the next request will be accepted.
Definition action.h:71
json_object * obj_actual
The JSON object that is of the wrong type.
Definition action.h:53
status_t status
A status code for the error.
Definition action.h:79
char const * name
The name of the invariant that was broken.
Definition action.h:75
char const * location
A human-friendly representation of the location of the error in the JSON request structure.
Definition action.h:50
char const * reason
The reason why the value is invalid.
Definition action.h:65
An action. Actions represent the commands the protocol implements.
Definition action.h:98
action_type_t type
Type of the action.
Definition action.h:100
Represents a list of messages.
Definition db.h:28
A memory slice.
Definition slice.h:12
Represents a user in the system.
Definition db.h:18
Version 4 UUID.
Definition uuid.h:15
General types - Standalone header.
int32_t serial_t
A PosrgreSQL SERIAL primary key value starting at 1. (1..2^31-1)
Definition types.h:22
enum ATTR_FLAG_ENUM role_flags_t
A bit flags enumeration representing the roles of an user.
int64_t token_t
A session token.
Definition types.h:18
int32_t page_number_t
A page number (1..2^31-1)
Definition types.h:20