summaryrefslogtreecommitdiff
path: root/core/Messenger.h
diff options
context:
space:
mode:
Diffstat (limited to 'core/Messenger.h')
-rw-r--r--core/Messenger.h134
1 files changed, 99 insertions, 35 deletions
diff --git a/core/Messenger.h b/core/Messenger.h
index 9352cfbb..fa69d104 100644
--- a/core/Messenger.h
+++ b/core/Messenger.h
@@ -73,6 +73,66 @@ typedef enum {
73 USERSTATUS_INVALID 73 USERSTATUS_INVALID
74} USERSTATUS; 74} USERSTATUS;
75 75
76typedef struct {
77 uint8_t client_id[CLIENT_ID_SIZE];
78 int crypt_connection_id;
79 uint64_t friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */
80 uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */
81 uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */
82 uint8_t name[MAX_NAME_LENGTH];
83 uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */
84 uint8_t *statusmessage;
85 uint16_t statusmessage_length;
86 uint8_t statusmessage_sent;
87 USERSTATUS userstatus;
88 uint8_t userstatus_sent;
89 uint16_t info_size; /* length of the info */
90 uint32_t message_id; /* a semi-unique id used in read receipts */
91 uint8_t receives_read_receipts; /* shall we send read receipts to this person? */
92} Friend;
93
94typedef struct Messenger {
95 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
96
97 uint8_t name[MAX_NAME_LENGTH];
98 uint16_t name_length;
99
100 uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH];
101 uint16_t statusmessage_length;
102
103 USERSTATUS userstatus;
104
105 Friend *friendlist;
106 uint32_t numfriends;
107
108 void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
109 uint8_t friend_message_isset;
110 void* friend_message_userdata;
111 void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
112 uint8_t friend_action_isset;
113 void* friend_action_userdata;
114 void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
115 uint8_t friend_namechange_isset;
116 void* friend_namechange_userdata;
117 void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
118 uint8_t friend_statusmessagechange_isset;
119 void* friend_statusmessagechange_userdata;
120 void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void*);
121 uint8_t friend_userstatuschange_isset;
122 void* friend_userstatuschange_userdata;
123 void (*read_receipt)(struct Messenger *m, int, uint32_t, void*);
124 uint8_t read_receipt_isset;
125 void* read_receipt_userdata;
126 void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void*);
127 uint8_t friend_statuschange_isset;
128 void* friend_statuschange_userdata;
129 void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void*);
130 uint8_t friend_connectionstatuschange_isset;
131 void* friend_connectionstatuschange_userdata;
132
133
134} Messenger;
135
76/* 136/*
77 * add a friend 137 * add a friend
78 * set the data that will be sent along with friend request 138 * set the data that will be sent along with friend request
@@ -85,33 +145,33 @@ typedef enum {
85 * return -4 if friend request already sent or already a friend 145 * return -4 if friend request already sent or already a friend
86 * return -5 for unknown error 146 * return -5 for unknown error
87 */ 147 */
88int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length); 148int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length);
89 149
90 150
91/* add a friend without sending a friendrequest. 151/* add a friend without sending a friendrequest.
92 returns the friend number if success 152 returns the friend number if success
93 return -1 if failure. */ 153 return -1 if failure. */
94int m_addfriend_norequest(uint8_t *client_id); 154int m_addfriend_norequest(Messenger *m, uint8_t *client_id);
95 155
96/* return the friend id associated to that client id. 156/* return the friend id associated to that client id.
97 return -1 if no such friend */ 157 return -1 if no such friend */
98int getfriend_id(uint8_t *client_id); 158int getfriend_id(Messenger *m, uint8_t *client_id);
99 159
100/* copies the public key associated to that friend id into client_id buffer. 160/* copies the public key associated to that friend id into client_id buffer.
101 make sure that client_id is of size CLIENT_ID_SIZE. 161 make sure that client_id is of size CLIENT_ID_SIZE.
102 return 0 if success 162 return 0 if success
103 return -1 if failure */ 163 return -1 if failure */
104int getclient_id(int friend_id, uint8_t *client_id); 164int getclient_id(Messenger *m, int friend_id, uint8_t *client_id);
105 165
106/* remove a friend */ 166/* remove a friend */
107int m_delfriend(int friendnumber); 167int m_delfriend(Messenger *m, int friendnumber);
108 168
109/* return 4 if friend is online 169/* return 4 if friend is online
110 return 3 if friend is confirmed 170 return 3 if friend is confirmed
111 return 2 if the friend request was sent 171 return 2 if the friend request was sent
112 return 1 if the friend was added 172 return 1 if the friend was added
113 return 0 if there is no friend with that number */ 173 return 0 if there is no friend with that number */
114int m_friendstatus(int friendnumber); 174int m_friendstatus(Messenger *m, int friendnumber);
115 175
116/* send a text chat message to an online friend 176/* send a text chat message to an online friend
117 returns the message id if packet was successfully put into the send queue 177 returns the message id if packet was successfully put into the send queue
@@ -120,13 +180,13 @@ int m_friendstatus(int friendnumber);
120 if one is received. 180 if one is received.
121 m_sendmessage_withid will send a message with the id of your choosing, 181 m_sendmessage_withid will send a message with the id of your choosing,
122 however we can generate an id for you by calling plain m_sendmessage. */ 182 however we can generate an id for you by calling plain m_sendmessage. */
123uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); 183uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length);
124uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); 184uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
125 185
126/* send an action to an online friend 186/* send an action to an online friend
127 returns 1 if packet was successfully put into the send queue 187 returns 1 if packet was successfully put into the send queue
128 return 0 if it was not */ 188 return 0 if it was not */
129int m_sendaction(int friendnumber, uint8_t *action, uint32_t length); 189int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length);
130 190
131/* Set our nickname 191/* Set our nickname
132 name must be a string of maximum MAX_NAME_LENGTH length. 192 name must be a string of maximum MAX_NAME_LENGTH length.
@@ -134,73 +194,73 @@ int m_sendaction(int friendnumber, uint8_t *action, uint32_t length);
134 length is the length of name with the NULL terminator 194 length is the length of name with the NULL terminator
135 return 0 if success 195 return 0 if success
136 return -1 if failure */ 196 return -1 if failure */
137int setname(uint8_t *name, uint16_t length); 197int setname(Messenger *m, uint8_t *name, uint16_t length);
138 198
139/* get our nickname 199/* get our nickname
140 put it in name 200 put it in name
141 return the length of the name*/ 201 return the length of the name*/
142uint16_t getself_name(uint8_t *name); 202uint16_t getself_name(Messenger *m, uint8_t *name);
143 203
144/* get name of friendnumber 204/* get name of friendnumber
145 put it in name 205 put it in name
146 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 206 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
147 return 0 if success 207 return 0 if success
148 return -1 if failure */ 208 return -1 if failure */
149int getname(int friendnumber, uint8_t *name); 209int getname(Messenger *m, int friendnumber, uint8_t *name);
150 210
151/* set our user status 211/* set our user status
152 you are responsible for freeing status after 212 you are responsible for freeing status after
153 returns 0 on success, -1 on failure */ 213 returns 0 on success, -1 on failure */
154int m_set_statusmessage(uint8_t *status, uint16_t length); 214int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length);
155int m_set_userstatus(USERSTATUS status); 215int m_set_userstatus(Messenger *m, USERSTATUS status);
156 216
157/* return the length of friendnumber's status message, 217/* return the length of friendnumber's status message,
158 including null 218 including null
159 pass it into malloc */ 219 pass it into malloc */
160int m_get_statusmessage_size(int friendnumber); 220int m_get_statusmessage_size(Messenger *m, int friendnumber);
161 221
162/* copy friendnumber's status message into buf, truncating if size is over maxlen 222/* copy friendnumber's status message into buf, truncating if size is over maxlen
163 get the size you need to allocate from m_get_statusmessage_size 223 get the size you need to allocate from m_get_statusmessage_size
164 The self variant will copy our own status message. */ 224 The self variant will copy our own status message. */
165int m_copy_statusmessage(int friendnumber, uint8_t *buf, uint32_t maxlen); 225int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen);
166int m_copy_self_statusmessage(uint8_t *buf, uint32_t maxlen); 226int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen);
167 227
168/* Return one of USERSTATUS values. 228/* Return one of USERSTATUS values.
169 * Values unknown to your application should be represented as USERSTATUS_NONE. 229 * Values unknown to your application should be represented as USERSTATUS_NONE.
170 * As above, the self variant will return our own USERSTATUS. 230 * As above, the self variant will return our own USERSTATUS.
171 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ 231 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */
172USERSTATUS m_get_userstatus(int friendnumber); 232USERSTATUS m_get_userstatus(Messenger *m, int friendnumber);
173USERSTATUS m_get_self_userstatus(void); 233USERSTATUS m_get_self_userstatus(Messenger *m);
174 234
175/* Sets whether we send read receipts for friendnumber. 235/* Sets whether we send read receipts for friendnumber.
176 * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ 236 * This function is not lazy, and it will fail if yesno is not (0 or 1).*/
177void m_set_sends_receipts(int friendnumber, int yesno); 237void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno);
178 238
179/* set the function that will be executed when a friend request is received. 239/* set the function that will be executed when a friend request is received.
180 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ 240 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
181void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); 241void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata);
182 242
183/* set the function that will be executed when a message from a friend is received. 243/* set the function that will be executed when a message from a friend is received.
184 function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ 244 function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
185void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); 245void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
186 246
187/* set the function that will be executed when an action from a friend is received. 247/* set the function that will be executed when an action from a friend is received.
188 function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ 248 function format is: function(int friendnumber, uint8_t * action, uint32_t length) */
189void m_callback_action(void (*function)(int, uint8_t *, uint16_t)); 249void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
190 250
191/* set the callback for name changes 251/* set the callback for name changes
192 function(int friendnumber, uint8_t *newname, uint16_t length) 252 function(int friendnumber, uint8_t *newname, uint16_t length)
193 you are not responsible for freeing newname */ 253 you are not responsible for freeing newname */
194void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); 254void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
195 255
196/* set the callback for status message changes 256/* set the callback for status message changes
197 function(int friendnumber, uint8_t *newstatus, uint16_t length) 257 function(int friendnumber, uint8_t *newstatus, uint16_t length)
198 you are not responsible for freeing newstatus */ 258 you are not responsible for freeing newstatus */
199void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); 259void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
200 260
201/* set the callback for status type changes 261/* set the callback for status type changes
202 function(int friendnumber, USERSTATUS kind) */ 262 function(int friendnumber, USERSTATUS kind) */
203void m_callback_userstatus(void (*function)(int, USERSTATUS)); 263void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata);
204 264
205/* set the callback for read receipts 265/* set the callback for read receipts
206 function(int friendnumber, uint32_t receipt) 266 function(int friendnumber, uint32_t receipt)
@@ -209,7 +269,7 @@ void m_callback_userstatus(void (*function)(int, USERSTATUS));
209 has been received on the other side. since core doesn't 269 has been received on the other side. since core doesn't
210 track ids for you, receipt may not correspond to any message 270 track ids for you, receipt may not correspond to any message
211 in that case, you should discard it. */ 271 in that case, you should discard it. */
212void m_callback_read_receipt(void (*function)(int, uint32_t)); 272void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata);
213 273
214/* set the callback for connection status changes 274/* set the callback for connection status changes
215 function(int friendnumber, uint8_t status) 275 function(int friendnumber, uint8_t status)
@@ -219,26 +279,30 @@ void m_callback_read_receipt(void (*function)(int, uint32_t));
219 note that this callback is not called when adding friends, thus the "after 279 note that this callback is not called when adding friends, thus the "after
220 being previously online" part. it's assumed that when adding friends, 280 being previously online" part. it's assumed that when adding friends,
221 their connection status is offline. */ 281 their connection status is offline. */
222void m_callback_connectionstatus(void (*function)(int, uint8_t)); 282void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata);
223 283
224/* run this at startup 284/* run this at startup
225 returns 0 if no connection problems 285 * returns allocated instance of Messenger on success
226 returns -1 if there are problems */ 286 * returns 0 if there are problems */
227int initMessenger(void); 287Messenger * initMessenger(void);
288
289/* run this before closing shop
290 * free all datastructures */
291void cleanupMessenger(Messenger *M);
228 292
229/* the main loop that needs to be run at least 200 times per second */ 293/* the main loop that needs to be run at least 200 times per second */
230void doMessenger(void); 294void doMessenger(Messenger *m);
231 295
232/* SAVING AND LOADING FUNCTIONS: */ 296/* SAVING AND LOADING FUNCTIONS: */
233 297
234/* returns the size of the messenger data (for saving) */ 298/* returns the size of the messenger data (for saving) */
235uint32_t Messenger_size(void); 299uint32_t Messenger_size(Messenger *m);
236 300
237/* save the messenger in data (must be allocated memory of size Messenger_size()) */ 301/* save the messenger in data (must be allocated memory of size Messenger_size()) */
238void Messenger_save(uint8_t *data); 302void Messenger_save(Messenger *m, uint8_t *data);
239 303
240/* load the messenger from data of size length */ 304/* load the messenger from data of size length */
241int Messenger_load(uint8_t *data, uint32_t length); 305int Messenger_load(Messenger *m, uint8_t *data, uint32_t length);
242 306
243#ifdef __cplusplus 307#ifdef __cplusplus
244} 308}