my_teams
logger.h
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2024
3 ** teams [WSL: Arch]
4 ** File description:
5 ** logger
6 */
7 
8 #ifndef LOGGER_H_
9  #define LOGGER_H_
10 
11  #include "protocol.h"
12  #include "client.h"
13  #include "logging_client.h"
14  #include "events.h"
15  #include "events_structures.h"
16  #include "unused.h"
17  #include "logger2.h"
18  #include "logger_error.h"
19 
20  #include <string.h>
21  #include <stdint.h>
22 
23  #define UNCOMBINED_EVT(evt, sub_evt) ((evt << 8) + sub_evt)
24 
25 static inline void mt_log_client(const p_packet_t *payload, c_client_t *client)
26 {
27  user_t user = {0};
28 
29  memcpy(&user, payload->data, sizeof(user_t));
30  client->user = user;
31  client_event_logged_in(user.uuid, user.name);
32 }
33 
34 static inline void mt_logout_client(const p_packet_t *payload,
36 {
37  user_t user = {0};
38 
39  memcpy(&user, payload->data, sizeof(user_t));
40  client->user = user;
41  client_event_logged_out(user.uuid, user.name);
42  client->user = (user_t){0};
43 }
44 
45 static inline void mt_receive_message(const p_packet_t *payload,
47 {
48  private_message_t message = {0};
49 
50  memcpy(&message, payload->data, sizeof(private_message_t));
52  message.sender_uuid,
53  message.body
54  );
55 }
56 
57 static inline void mt_thread_reply_received(const p_packet_t *payload,
59 {
60  reply_ref_t reply = {0};
61 
62  memcpy(&reply, payload->data, sizeof(reply_ref_t));
64  reply.team_uuid,
65  reply.thread_uuid,
66  reply.user_uuid,
67  reply.reply_body
68  );
69 }
70 
71 static inline void mt_team_created(const p_packet_t *payload,
73 {
74  team_t team = {0};
75 
76  memcpy(&team, payload->data, sizeof(team_t));
78  team.uuid,
79  team.name,
80  team.description
81  );
82 }
83 
84 static inline void mt_channel_created(const p_packet_t *payload,
86 {
87  channel_t channel = {0};
88 
89  memcpy(&channel, payload->data, sizeof(channel_t));
91  channel.uuid,
92  channel.name,
93  channel.description
94  );
95 }
96 
97 static inline void mt_thread_created(const p_packet_t *payload,
99 {
100  thread_t thread = {0};
101 
102  memcpy(&thread, payload->data, sizeof(thread_t));
104  thread.uuid,
105  thread.user_uuid,
106  thread.timestamp,
107  thread.title,
108  thread.body
109  );
110 }
111 
112 static inline void mt_user(const p_packet_t *payload,
114 {
115  user_state_t user = {0};
116 
117  memcpy(&user, payload->data, sizeof(user_state_t));
119  user.uuid,
120  user.name,
121  user.is_logged
122  );
123 }
124 
125 static inline void mt_team(const p_packet_t *payload,
127 {
128  team_t team = {0};
129 
130  memcpy(&team, payload->data, sizeof(team_t));
132  team.uuid,
133  team.name,
134  team.description
135  );
136 }
137 
138 static inline void mt_private_message(const p_packet_t *payload,
140 {
141  private_message_t message = {0};
142 
143  memcpy(&message, payload->data, sizeof(private_message_t));
145  message.sender_uuid,
146  message.timestamp,
147  message.body
148  );
149 }
150 
151 static const struct {
152  uint16_t type;
153  void (*func)(const p_packet_t *payload, c_client_t *client);
154 } mt_logger[] = {
155  {EVT_LOGIN, mt_log_client},
156  {EVT_DISCONNECT, mt_logout_client},
157  {EVT_MESSAGE_RECEIVE, mt_receive_message},
158  {EVT_REPLY_CREATE, mt_thread_reply_received},
159  {EVT_TEAM_CREATE, mt_team_created},
160  {EVT_CHANNEL_CREATE, mt_channel_created},
161  {EVT_THREAD_CREATE, mt_thread_created},
162  {EVT_INFO_USER, mt_user},
163  {EVT_LIST_USERS, mt_user},
165  {EVT_LIST_MESSAGES, mt_private_message},
166  {UNCOMBINED_EVT(EVT_CONTINUE, EVT_LIST_MESSAGES), mt_private_message},
167  {EVT_LIST_SUBSCRIBED_IN_TEAM, mt_user},
169  {EVT_LIST_SUBSCRIBED_TEAMS, mt_team},
171  {EVT_SUBSCRIBE, mt_subscribe},
172  {EVT_UNSUBSCRIBE, mt_unsubscribe},
173  {EVT_LIST_TEAMS, mt_team},
175  {EVT_LIST_CHANNELS, mt_channel},
177  {EVT_LIST_THREADS, mt_thread},
179  {EVT_LIST_REPLIES, mt_reply},
181  {EVT_INFO_TEAM, mt_team},
182  {EVT_INFO_CHANNEL, mt_channel},
183  {EVT_INFO_THREAD, mt_thread},
184 
185  {EVT_ERROR_UNAUTHORIZED, mt_error_unauthorized},
186  {EVT_ERROR_ALREADY, mt_already_exist},
187  {EVT_ERROR_UNKNOWN_TEAM, mt_error_unknown_team},
188  {EVT_ERROR_UNKNOWN_CHANNEL, mt_error_unknown_channel},
189  {EVT_ERROR_UNKNOWN_THREAD, mt_error_unknown_thread},
190  {EVT_ERROR_UNKNOWN_USER, mt_error_unknown_user},
191  {INT16_MAX - 1, NULL}
192 };
193 
194 #endif /* !LOGGER_H_ */
int client(int ac, char **av)
main function for the client is only used by main (is returned by the main function)
Definition: client.c:176
@ EVT_ERROR_UNAUTHORIZED
Definition: events.h:78
@ EVT_ERROR_UNKNOWN_CHANNEL
Definition: events.h:75
@ EVT_LIST_USERS
Definition: events.h:21
@ EVT_REPLY_CREATE
Definition: events.h:62
@ EVT_LOGIN
Definition: events.h:17
@ EVT_LIST_MESSAGES
Definition: events.h:24
@ EVT_INFO_USER
Definition: events.h:48
@ EVT_TEAM_CREATE
Definition: events.h:64
@ EVT_ERROR_UNKNOWN_THREAD
Definition: events.h:76
@ EVT_MESSAGE_RECEIVE
Definition: events.h:60
@ EVT_ERROR_UNKNOWN_TEAM
Definition: events.h:74
@ EVT_LIST_CHANNELS
Definition: events.h:42
@ EVT_CONTINUE
Definition: events.h:70
@ EVT_LIST_SUBSCRIBED_IN_TEAM
Definition: events.h:27
@ EVT_UNSUBSCRIBE
Definition: events.h:30
@ EVT_LIST_TEAMS
Definition: events.h:40
@ EVT_CHANNEL_CREATE
Definition: events.h:66
@ EVT_ERROR_UNKNOWN_USER
Definition: events.h:77
@ EVT_THREAD_CREATE
Definition: events.h:68
@ EVT_SUBSCRIBE
Definition: events.h:26
@ EVT_ERROR_ALREADY
Definition: events.h:79
@ EVT_DISCONNECT
Definition: events.h:19
@ EVT_INFO_THREAD
Definition: events.h:54
@ EVT_INFO_TEAM
Definition: events.h:50
@ EVT_LIST_THREADS
Definition: events.h:44
@ EVT_INFO_CHANNEL
Definition: events.h:52
@ EVT_LIST_REPLIES
Definition: events.h:46
@ EVT_LIST_SUBSCRIBED_TEAMS
Definition: events.h:28
#define UNCOMBINED_EVT(evt, sub_evt)
Definition: logger.h:23
void(* func)(const p_packet_t *payload, c_client_t *client)
Definition: logger.h:153
uint16_t type
Definition: logger.h:152
int client_event_thread_reply_received(char const *team_uuid, char const *thread_uuid, char const *user_uuid, char const *reply_body)
Must be called when a new reply is posted in a thread.
int client_event_channel_created(char const *channel_uuid, char const *channel_name, char const *channel_description)
Must be called when a channel is created inside of a team.
int client_event_logged_out(char const *user_uuid, const char *user_name)
Must be called when a user logged out (/logout or lost connexion). When you log out (/logout) you sho...
int client_event_thread_created(char const *thread_uuid, char const *user_uuid, time_t thread_timestamp, char const *thread_title, char const *thread_body)
Must be called when a thread is created inside of a channel.
int client_print_teams(char const *team_uuid, char const *team_name, char const *team_description)
Must be called when you requested a list of teams from the server ex: asking the teams you are subscr...
int client_event_team_created(char const *team_uuid, char const *team_name, char const *team_description)
Must be called when a new team is created Every logged user should receive this event (the creator of...
int client_event_logged_in(char const *user_uuid, const char *user_name)
Must be called when a user successfully login to the server (/login). When you log in (/login) you sh...
int client_private_message_print_messages(char const *sender_uuid, time_t message_timestamp, char const *message_body)
Must be called when you requested a list of private messages ex: asking the private messages with a u...
int client_event_private_message_received(char const *user_uuid, char const *message_body)
Must be called when the current logged user receives a private message.
int client_print_users(char const *user_uuid, char const *user_name, int user_status)
Must be called when you requested a list of users from the server ex: asking the subscribed users to ...
Definition: client.h:32
Definition: events_structures.h:126
char description[MAX_DESCRIPTION_LENGTH]
Definition: events_structures.h:130
char name[MAX_NAME_LENGTH]
Definition: events_structures.h:129
char uuid[UUID_LENGTH]
Definition: events_structures.h:127
Represents a packet with type and data.
Definition: protocol.h:31
uint8_t data[DATA_SIZE]
Definition: protocol.h:33
Definition: events_structures.h:52
time_t timestamp
Definition: events_structures.h:56
char sender_uuid[UUID_LENGTH]
Definition: events_structures.h:53
char body[MAX_BODY_LENGTH]
Definition: events_structures.h:55
Definition: events_structures.h:178
char reply_body[MAX_BODY_LENGTH]
Definition: events_structures.h:182
char thread_uuid[UUID_LENGTH]
Definition: events_structures.h:181
char user_uuid[UUID_LENGTH]
Definition: events_structures.h:180
char team_uuid[UUID_LENGTH]
Definition: events_structures.h:179
Definition: events_structures.h:43
char description[MAX_DESCRIPTION_LENGTH]
Definition: events_structures.h:46
char uuid[UUID_LENGTH]
Definition: events_structures.h:44
char name[MAX_NAME_LENGTH]
Definition: events_structures.h:45
Definition: events_structures.h:147
time_t timestamp
Definition: events_structures.h:153
char user_uuid[UUID_LENGTH]
Definition: events_structures.h:149
char body[MAX_BODY_LENGTH]
Definition: events_structures.h:152
char title[MAX_NAME_LENGTH]
Definition: events_structures.h:151
char uuid[UUID_LENGTH]
Definition: events_structures.h:148
Definition: events_structures.h:33
bool is_logged
Definition: events_structures.h:36
char uuid[UUID_LENGTH]
Definition: events_structures.h:34
char name[MAX_NAME_LENGTH]
Definition: events_structures.h:35
Definition: events_structures.h:25
char name[MAX_NAME_LENGTH]
Definition: events_structures.h:27
char uuid[UUID_LENGTH]
Definition: events_structures.h:26
#define UNUSED
Definition: unused.h:12