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