Orca
A bot framework that is easy to reason about, easy to debug, and easy to use.
discord-internal.h
Go to the documentation of this file.
1 
8 #ifndef DISCORD_INTERNAL_H
9 #define DISCORD_INTERNAL_H
10 
11 #include <inttypes.h>
12 #include <pthread.h>
13 #include "uthash.h"
14 
15 #include "json-scanf.h"
16 #include "json-actor.h"
17 #include "json-actor-boxed.h"
18 
19 #include "logconf.h" /* struct logconf */
20 #include "user-agent.h"
21 #include "websockets.h"
22 #include "cee-utils.h"
24 
32  struct user_agent *ua;
33  struct { /* RATELIMITING STRUCTURE */
35  pthread_mutex_t lock;
36  } ratelimit;
37  struct discord *p_client;
38 };
39 
48 void discord_adapter_init(struct discord_adapter *adapter, struct logconf *config, struct sized_buffer *token);
49 
56 
69 ORCAcode discord_adapter_run(
70  struct discord_adapter *adapter,
71  struct ua_resp_handle *resp_handle,
72  struct sized_buffer *req_body,
73  enum http_method http_method,
74  char endpoint[], ...);
75 
82  char route[256];
83  char hash[128];
84  int busy;
85  int remaining;
86  int64_t reset_after_ms;
89 
90  pthread_mutex_t lock;
91  pthread_cond_t cond;
92  UT_hash_handle hh;
93 };
94 
100 void discord_buckets_cleanup(struct discord_adapter *adapter);
101 
109 void discord_bucket_try_cooldown(struct discord_bucket *bucket);
110 
119 struct discord_bucket* discord_bucket_try_get(struct discord_adapter *adapter, const char route[]);
120 
130 void discord_bucket_build(struct discord_adapter *adapter, struct discord_bucket *bucket, const char route[], struct ua_info *info);
131 
133  char *start;
134  size_t size;
135  message_cb cb;
136 };
137 
146  struct websockets *ws;
147  struct { /* RECONNECT STRUCTURE */
148  bool enable;
149  int attempt;
150  int threshold;
151  } reconnect;
153  bool is_ready;
154  bool shutdown;
155 
157  char session_id[512];
158 
159  // https://discord.com/developers/docs/topics/gateway#payloads-gateway-payload-structure
160  struct { /* GATEWAY PAYLOAD STRUCTURE */
161  enum discord_gateway_opcodes opcode;
163  char event_name[64];
164  struct sized_buffer event_data;
165  } payload;
166 
167  // Discord expects a proccess called heartbeating in order to keep the client-server connection alive
168  // https://discord.com/developers/docs/topics/gateway#heartbeating
169  struct { /* HEARTBEAT STRUCTURE */
172  } hbeat;
173 
175 
176  struct sized_buffer prefix;
177  struct discord_gateway_cmd_cbs on_default_cmd, *on_cmd;
178  size_t num_cmd;
179 
180  struct { /* CALLBACKS STRUCTURE */
197  sb_message_cb sb_on_message_create;
199  sb_message_cb sb_on_message_update;
208  } cbs;
209 
210  enum discord_event_handling_mode (*blocking_event_handler)(void *cxt);
211 
212  int ping_ms;
213 
214  pthread_mutex_t lock;
215 
216  struct discord_user *bot;
217  struct sized_buffer sb_bot;
218 
219  struct discord *p_client;
220 };
221 
230 void discord_gateway_init(struct discord_gateway *gw, struct logconf *config, struct sized_buffer *token);
231 
238 
246 
254 
263 void discord_gateway_reconnect(struct discord_gateway *gw, bool resume);
264 
265 
271 struct discord {
272  struct sized_buffer token;
275 
276  struct logconf config;
277 
278 #define NUM_VCS 512
279  struct discord_voice vcs[NUM_VCS];
280  struct discord_voice_cbs voice_cbs;
281 
282  void *data;
283 };
284 
286  char event_name[64];
287  pthread_t tid;
288  struct sized_buffer data;
290  enum discord_gateway_events event;
291  void (*on_event)(
292  struct discord_gateway *gw,
293  struct sized_buffer *data);
294  bool is_main_thread;
295 };
296 
297 #endif // DISCORD_INTERNAL_H
discord_gateway::on_channel_pins_update
channel_pins_update_cb on_channel_pins_update
triggers when a channel pinned messages updates
Definition: discord-internal.h:195
discord_gateway::on_message_create
message_cb on_message_create
triggers when a message is created
Definition: discord-internal.h:196
discord_gateway::on_guild_member_remove
guild_member_remove_cb on_guild_member_remove
triggers when a guild member is removed from a guild
Definition: discord-internal.h:188
voice_state_update_cb
void(* voice_state_update_cb)(struct discord *client, const struct discord_user *bot, const struct discord_voice_state *voice_state)
Voice State Update callback.
Definition: discord.h:263
discord_gateway::on_guild_member_update
guild_member_cb on_guild_member_update
triggers when a guild member status is updated (ex: receive role)
Definition: discord-internal.h:189
discord::data
void * data
space for user arbitrary data
Definition: discord-internal.h:282
discord_bucket_try_cooldown
void discord_bucket_try_cooldown(struct discord_bucket *bucket)
Check bucket for ratelimit cooldown.
discord_gateway::bot
struct discord_user * bot
the bot user structure
Definition: discord-internal.h:216
discord_bucket::reset_tstamp
u64_unix_ms_t reset_tstamp
timestamp of when cooldown timer resets
Definition: discord-internal.h:87
discord_gateway::sb_bot
struct sized_buffer sb_bot
Definition: discord-internal.h:217
discord_bucket::remaining
int remaining
connections this bucket can do before waiting for cooldown
Definition: discord-internal.h:85
discord_bucket
The bucket struct that will handle ratelimiting.
Definition: discord-internal.h:81
channel_cb
void(* channel_cb)(struct discord *client, const struct discord_user *bot, const struct discord_channel *channel)
Channel Create/Update/Delete callback.
Definition: discord.h:196
ua_resp_handle
Definition: user-agent.h:49
ua_info
Definition: user-agent.h:101
discord_adapter::buckets
struct discord_bucket * buckets
Endpoint/routes discovered, check a endpoint/bucket match with tree search functions.
Definition: discord-internal.h:34
discord_gateway::session_id
char session_id[512]
the session id (for resuming lost connections)
Definition: discord-internal.h:157
idle_cb
void(* idle_cb)(struct discord *client, const struct discord_user *bot)
Idle callback.
Definition: discord.h:78
discord_adapter_cleanup
void discord_adapter_cleanup(struct discord_adapter *adapter)
Free a Discord Adapter handle.
discord_adapter_init
void discord_adapter_init(struct discord_adapter *adapter, struct logconf *config, struct sized_buffer *token)
Initialize the fields of a Discord Adapter handle.
discord_buckets_cleanup
void discord_buckets_cleanup(struct discord_adapter *adapter)
Free client buckets.
discord_event_cxt::p_gw
struct discord_gateway * p_gw
the gateway client
Definition: discord-internal.h:289
discord_session
Definition: discord.h:282
discord_gateway::on_channel_delete
channel_cb on_channel_delete
triggers when a channel is deleted
Definition: discord-internal.h:194
message_reaction_remove_emoji_cb
void(* message_reaction_remove_emoji_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t channel_id, const u64_snowflake_t message_id, const u64_snowflake_t guild_id, const struct discord_emoji *emoji)
Message Reaction Remove Emoji callback.
Definition: discord.h:251
discord_event_cxt
Definition: discord-internal.h:285
discord_gateway_reconnect
void discord_gateway_reconnect(struct discord_gateway *gw, bool resume)
Gracefully reconnect a ongoing Discord connection over WebSockets.
discord_gateway::on_message_reaction_add
message_reaction_add_cb on_message_reaction_add
triggers when a reaction is added to a message
Definition: discord-internal.h:202
discord_gateway::on_message_reaction_remove
message_reaction_remove_cb on_message_reaction_remove
triggers when a reaction is removed from a message
Definition: discord-internal.h:203
discord_gateway
The handle used for establishing a Discord Gateway connection via WebSockets.
Definition: discord-internal.h:145
discord_adapter_run
ORCAcode discord_adapter_run(struct discord_adapter *adapter, struct ua_resp_handle *resp_handle, struct sized_buffer *req_body, enum http_method http_method, char endpoint[],...)
Start a HTTP Request to Discord.
discord_gateway::on_guild_role_delete
guild_role_delete_cb on_guild_role_delete
triggers when a guild role is deleted
Definition: discord-internal.h:186
discord_gateway::session
struct discord_session session
on-going Discord Gateway session
Definition: discord-internal.h:174
event_raw_cb
void(* event_raw_cb)(struct discord *client, enum discord_gateway_events event, struct sized_buffer *sb_bot, struct sized_buffer *event_data)
Raw Event callback.
Definition: discord.h:87
discord_gateway_init
void discord_gateway_init(struct discord_gateway *gw, struct logconf *config, struct sized_buffer *token)
Initialize the fields of Discord Gateway handle.
guild_member_cb
void(* guild_member_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t guild_id, const struct discord_guild_member *member)
Guild Member Add/Update callback.
Definition: discord.h:122
discord::token
struct sized_buffer token
the bot token
Definition: discord-internal.h:272
message_reaction_remove_cb
void(* message_reaction_remove_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t user_id, const u64_snowflake_t channel_id, const u64_snowflake_t message_id, const u64_snowflake_t guild_id, const struct discord_emoji *emoji)
Message Reaction Remove callback.
Definition: discord.h:229
discord_gateway::ws
struct websockets * ws
the websockets handle that connects to Discord
Definition: discord-internal.h:146
message_delete_cb
void(* message_delete_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t id, const u64_snowflake_t channel_id, const u64_snowflake_t guild_id)
Message Delete callback.
Definition: discord.h:171
discord_adapter
The handle used for performing HTTP Requests.
Definition: discord-internal.h:31
discord_gateway_cmd_cbs
Definition: discord-internal.h:132
discord_gateway::on_idle
idle_cb on_idle
triggers on every event loop iteration
Definition: discord-internal.h:181
discord_gateway::sb_on_message_create
sb_message_cb sb_on_message_create
Definition: discord-internal.h:197
discord_gateway::interval_ms
u64_unix_ms_t interval_ms
fixed interval between heartbeats
Definition: discord-internal.h:170
discord_gateway_run
void discord_gateway_run(struct discord_gateway *gw)
Start a Discord connection over WebSockets.
message_cb
void(* message_cb)(struct discord *client, const struct discord_user *bot, const struct discord_message *message)
Message Create/Update callback.
Definition: discord.h:156
discord_gateway::seq_number
int seq_number
field 's'
Definition: discord-internal.h:162
guild_role_delete_cb
void(* guild_role_delete_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t guild_id, const u64_snowflake_t role_id)
Guild Role Delete callback.
Definition: discord.h:110
discord_bucket::route
char route[256]
this bucket 'key'
Definition: discord-internal.h:82
discord_gateway_cleanup
void discord_gateway_cleanup(struct discord_gateway *gw)
Free a Discord Gateway handle.
discord_gateway::ping_ms
int ping_ms
latency between client and websockets server, calculated by the interval between HEARTBEAT and HEARTB...
Definition: discord-internal.h:212
discord_gateway::on_message_update
message_cb on_message_update
trigger when a message is updated
Definition: discord-internal.h:198
discord_adapter::lock
pthread_mutex_t lock
Mutex used when adding to or searching for buckets.
Definition: discord-internal.h:35
discord_event_handling_mode
discord_event_handling_mode
Definition: discord.h:644
discord_bucket_try_get
struct discord_bucket * discord_bucket_try_get(struct discord_adapter *adapter, const char route[])
Get existing bucket with route.
discord_gateway::on_guild_member_add
guild_member_cb on_guild_member_add
triggers when a guild member joins a guild
Definition: discord-internal.h:187
discord_gateway::event_data
struct sized_buffer event_data
field 'd'
Definition: discord-internal.h:164
guild_role_cb
void(* guild_role_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t guild_id, const struct discord_permissions_role *role)
Guild Role Create/Update callback.
Definition: discord.h:100
discord_voice_cbs
Definition: discord-voice-connections.h:69
discord_gateway::event_name
char event_name[64]
field 't'
Definition: discord-internal.h:163
discord_bucket::hash
char hash[128]
the unique hash associated with this bucket
Definition: discord-internal.h:83
discord_gateway::p_client
struct discord * p_client
pointer to client this struct is part of
Definition: discord-internal.h:219
discord_gateway::on_message_delete_bulk
message_delete_bulk_cb on_message_delete_bulk
triggers when a bulk of messages is deleted
Definition: discord-internal.h:201
u64_unix_ms_t
uint64_t u64_unix_ms_t
unix time in ms
Definition: discord.h:26
discord
The Discord Client structure.
Definition: discord-internal.h:271
guild_ban_cb
void(* guild_ban_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t guild_id, const struct discord_user *user)
Guild Ban Add/Remove callback.
Definition: discord.h:144
discord_gateway::on_message_reaction_remove_emoji
message_reaction_remove_emoji_cb on_message_reaction_remove_emoji
triggers when all occurences of a specific reaction is removed from a message
Definition: discord-internal.h:205
discord_bucket::hh
UT_hash_handle hh
makes this structure hashable
Definition: discord-internal.h:92
discord_gateway::on_voice_server_update
voice_server_update_cb on_voice_server_update
triggers when a voice server is updated
Definition: discord-internal.h:207
discord_gateway::is_ready
bool is_ready
can start sending/receiving additional events to discord
Definition: discord-internal.h:153
discord::adapter
struct discord_adapter adapter
the HTTP adapter for performing requests
Definition: discord-internal.h:273
discord_gateway::sb_on_message_update
sb_message_cb sb_on_message_update
Definition: discord-internal.h:199
discord_voice
Discord Voice Connection handle, contain information about its active session.
Definition: discord-voice-connections.h:88
discord_gateway::on_guild_ban_remove
guild_ban_cb on_guild_ban_remove
triggers when a ban is removed
Definition: discord-internal.h:191
discord_gateway::on_event_raw
event_raw_cb on_event_raw
triggers for every event if set, receive its raw JSON string
Definition: discord-internal.h:182
discord_bucket::reset_after_ms
int64_t reset_after_ms
how long until cooldown timer resets
Definition: discord-internal.h:86
discord::gw
struct discord_gateway gw
the WebSockets handle for establishing a connection to Discord
Definition: discord-internal.h:274
discord_bucket::update_tstamp
u64_unix_ms_t update_tstamp
timestamp of the most recent request
Definition: discord-internal.h:88
voice_server_update_cb
void(* voice_server_update_cb)(struct discord *client, const struct discord_user *bot, const char *token, const u64_snowflake_t guild_id, const char *endpoint)
Voice Server Update callback.
Definition: discord.h:271
discord_bucket::lock
pthread_mutex_t lock
synchronize buckets between threads
Definition: discord-internal.h:90
message_reaction_add_cb
void(* message_reaction_add_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t user_id, const u64_snowflake_t channel_id, const u64_snowflake_t message_id, const u64_snowflake_t guild_id, const struct discord_guild_member *member, const struct discord_emoji *emoji)
Message Reaction Add callback.
Definition: discord.h:216
discord_gateway::on_message_delete
message_delete_cb on_message_delete
triggers when a message is deleted
Definition: discord-internal.h:200
guild_member_remove_cb
void(* guild_member_remove_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t guild_id, const struct discord_user *user)
Guild Member Remove callback.
Definition: discord.h:132
discord_gateway::threshold
int threshold
max amount of reconnects before giving up
Definition: discord-internal.h:150
discord_gateway::on_channel_update
channel_cb on_channel_update
triggers when a channel is updated
Definition: discord-internal.h:193
discord_gateway::on_guild_role_create
guild_role_cb on_guild_role_create
triggers when a guild role is created
Definition: discord-internal.h:184
discord_event_cxt::tid
pthread_t tid
the thread id
Definition: discord-internal.h:287
discord_bucket::busy
int busy
amount of busy connections that have not yet finished its requests
Definition: discord-internal.h:84
discord_gateway::tstamp
u64_unix_ms_t tstamp
start pulse timestamp in milliseconds
Definition: discord-internal.h:171
discord_gateway::on_guild_ban_add
guild_ban_cb on_guild_ban_add
triggers when a ban occurs
Definition: discord-internal.h:190
discord-voice-connections.h
File containing internal functions and datatypes for Voice Connections.
discord_gateway::enable
bool enable
will attempt reconnecting if true
Definition: discord-internal.h:148
discord_gateway::on_guild_role_update
guild_role_cb on_guild_role_update
triggers when a guild role is updated
Definition: discord-internal.h:185
discord_gateway::id
struct discord_gateway_identify * id
this info sent expecting a connection authentication
Definition: discord-internal.h:156
websockets.h
File containing internal functions and datatypes for WebSockets client.
discord_gateway::prefix
struct sized_buffer prefix
the prefix expected before every command
Definition: discord-internal.h:176
discord_gateway::opcode
enum discord_gateway_opcodes opcode
field 'op'
Definition: discord-internal.h:161
discord_user
User Structure.
Definition: user.h:45
discord_gateway::attempt
int attempt
current reconnect attempt (resets to 0 when succesful)
Definition: discord-internal.h:149
discord_gateway::on_message_reaction_remove_all
message_reaction_remove_all_cb on_message_reaction_remove_all
triggers when all reactions are removed from a message
Definition: discord-internal.h:204
discord_gateway::on_channel_create
channel_cb on_channel_create
triggers when a channel is created
Definition: discord-internal.h:192
message_reaction_remove_all_cb
void(* message_reaction_remove_all_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t channel_id, const u64_snowflake_t message_id, const u64_snowflake_t guild_id)
Message Reaction Remove All callback.
Definition: discord.h:241
discord_gateway::on_ready
idle_cb on_ready
triggers when connection first establishes
Definition: discord-internal.h:183
discord_gateway_identify
Identify Structure.
Definition: gateway.h:120
discord_adapter::ua
struct user_agent * ua
The user agent handle for performing requests.
Definition: discord-internal.h:32
discord::config
struct logconf config
store bot.config file contents and sync logging between adapter/gw
Definition: discord-internal.h:276
discord_adapter::p_client
struct discord * p_client
pointer to client this struct is part of
Definition: discord-internal.h:37
discord_gateway::is_resumable
bool is_resumable
will attempt to resume session if connection shutsdowns
Definition: discord-internal.h:152
discord_event_cxt::data
struct sized_buffer data
a copy of payload data
Definition: discord-internal.h:288
discord_bucket_build
void discord_bucket_build(struct discord_adapter *adapter, struct discord_bucket *bucket, const char route[], struct ua_info *info)
Update the bucket with response header data.
discord_gateway::on_cmd
struct discord_gateway_cmd_cbs on_default_cmd * on_cmd
user's command/callback pair and amount of callback pairs
Definition: discord-internal.h:177
message_delete_bulk_cb
void(* message_delete_bulk_cb)(struct discord *client, const struct discord_user *bot, const ja_u64 **ids, const u64_snowflake_t channel_id, const u64_snowflake_t guild_id)
Message Delete Bulk callback.
Definition: discord.h:182
channel_pins_update_cb
void(* channel_pins_update_cb)(struct discord *client, const struct discord_user *bot, const u64_snowflake_t guild_id, const u64_snowflake_t channel_id, const u64_unix_ms_t last_pin_timestamp)
Channel Pins Update callback.
Definition: discord.h:205
discord_gateway::shutdown
bool shutdown
if true shutdown websockets connection as soon as possible
Definition: discord-internal.h:154
discord_gateway::on_voice_state_update
voice_state_update_cb on_voice_state_update
triggers when a voice state is updated
Definition: discord-internal.h:206
discord_gateway_shutdown
void discord_gateway_shutdown(struct discord_gateway *gw)
Gracefully exits a ongoing Discord connection over WebSockets.