1 #ifndef FRIEND_H
2 #define FRIEND_H
3 
4 #include "messages.h"
5 
6 #include <tox/tox.h>
7 
8 typedef struct avatar AVATAR;
9 typedef struct edit_change EDIT_CHANGE;
10 typedef struct file_transfer FILE_TRANSFER;
11 typedef uint8_t *UTOX_IMAGE;
12 typedef unsigned int ALuint;
13 
14 typedef enum {
15     ADDF_NONE,
16     ADDF_SENT,
17     ADDF_DISCOVER,
18     ADDF_BADNAME,        // either invalid tox id or just friend's public key (missing nospam)
19     ADDF_NONAME,
20     ADDF_TOOLONG,        // if message length is too long.
21     ADDF_NOMESSAGE,      // if no message (message length must be >= 1 byte).
22     ADDF_OWNKEY,         // if user's own key.
23     ADDF_ALREADYSENT,    // if friend request already sent or already a friend.
24     ADDF_UNKNOWN,        // for unknown error.
25     ADDF_BADCHECKSUM,    // if bad checksum in address.
26     ADDF_SETNEWNOSPAM,   // if the friend was already there but the nospam was different.
27     ADDF_NOMEM,          // if increasing the friend list size fails.
28     ADDF_NOFREQUESTSENT, // friend added but no frequest sent
29 } DNS_STATE;
30 
31 typedef struct friend_meta_data {
32     uint8_t version;
33 
34     uint8_t ft_autoaccept : 1;
35     uint8_t ft_autoaccept_path : 1;
36     uint8_t skip_msg_logging : 1;
37     uint8_t unused : 5;
38 
39     uint8_t zero[30];
40 
41     size_t alias_length;
42     size_t ft_autoaccept_path_length;
43 
44     uint8_t data[];
45 } FRIEND_META_DATA;
46 #define METADATA_VERSION 0
47 
48 
49 typedef struct friend_meta_data_old {
50     size_t  alias_length;
51     uint8_t data[];
52 } FRIEND_META_DATA_OLD;
53 
54 
55 
56 #define TOX_FRIEND_ID_STR_SIZE TOX_PUBLIC_KEY_SIZE * 2
57 typedef struct utox_friend {
58     uint8_t id_bin[TOX_PUBLIC_KEY_SIZE];
59     char    id_str[TOX_FRIEND_ID_STR_SIZE];
60     uint8_t number;
61 
62     char *name;
63     char *alias;
64     char *status_message;
65 
66     uint8_t *typed;
67 
68     size_t name_length;
69     size_t alias_length;
70     size_t status_length;
71     size_t typed_length;
72 
73     /* Friend Status */
74     uint8_t status;
75     bool    online;
76     bool    typing;
77     bool    video_inline;
78 
79     AVATAR *avatar;
80 
81     /* Messages */
82     bool          skip_msg_logging;
83     bool          unread_msg;
84     MESSAGES      msg;
85     EDIT_CHANGE **edit_history;
86     uint16_t      edit_history_cur, edit_history_length;
87 
88     /* Audio / Video */
89     int32_t  call_state_self, call_state_friend;
90     uint16_t video_width, video_height;
91     ALuint   audio_dest;
92     time_t call_started;
93 
94     /* File transfers */
95     bool ft_autoaccept;
96 
97     FILE_TRANSFER  *ft_incoming;
98     uint16_t        ft_incoming_size;
99     uint16_t        ft_incoming_active_count;
100 
101     FILE_TRANSFER  *ft_outgoing;
102     uint16_t        ft_outgoing_size;
103     uint16_t        ft_outgoing_active_count;
104 } FRIEND;
105 
106 typedef struct utox_friend_request {
107     uint16_t number;
108     uint8_t  bin_id[TOX_ADDRESS_SIZE];
109 
110     char *msg;
111     size_t length;
112 } FREQUEST;
113 
114 // add friend page
115 extern uint8_t addfriend_status;
116 
117 #define UTOX_FRIEND_NAME(f) ((f->alias) ? f->alias : f->name)
118 #define UTOX_FRIEND_NAME_LENGTH(f) ((f->alias) ? f->alias_length : f->name_length)
119 
120 /*
121  * Gets the friend at position friend_number
122  */
123 FRIEND *get_friend(uint32_t friend_number);
124 
125 FRIEND *get_friend_by_id(const char *id_str);
126 
127 FREQUEST *get_frequest(uint16_t frequest_number);
128 
129 /* Add a new friend request */
130 uint16_t friend_request_new(const uint8_t *id, const uint8_t *msg, size_t length);
131 void friend_request_free(uint16_t number);
132 
133 /*
134  * Frees all of your friends
135  */
136 void free_friends(void);
137 
138 void utox_friend_init(Tox *tox, uint32_t friend_number);
139 
140 void utox_friend_list_init(Tox *tox);
141 
142 void friend_setname(FRIEND *f, uint8_t *name, size_t length);
143 void friend_set_alias(FRIEND *f, uint8_t *alias, uint16_t length);
144 void friend_sendimage(FRIEND *f, NATIVE_IMAGE *native_image, uint16_t width, uint16_t height, UTOX_IMAGE png_image,
145                       size_t png_size);
146 void friend_recvimage(FRIEND *f, NATIVE_IMAGE *native_image, uint16_t width, uint16_t height);
147 
148 void friend_notify_msg(FRIEND *f, const char *msg, size_t msg_length);
149 
150 /* set friend online status. Returns: true if status changed, false otherwise */
151 bool friend_set_online(FRIEND *f, bool online);
152 
153 void friend_set_typing(FRIEND *f, int typing);
154 
155 void friend_addid(uint8_t *id, char *msg, uint16_t msg_length);
156 void friend_add(char *name, uint16_t length, char *msg, uint16_t msg_length);
157 
158 void friend_history_clear(FRIEND *f);
159 
160 void friend_free(FRIEND *f);
161 
162 /* Searches for a friend using the specified name */
163 FRIEND *find_friend_by_name(uint8_t *name);
164 
165 /* Notifies the user that a friend is online or offline */
166 void friend_notify_status(FRIEND *f, const uint8_t *msg, size_t msg_length, char *state);
167 
168 // Saves user meta data to disk
169 void utox_write_metadata(FRIEND *f);
170 
171 /** convert string to tox id
172  *  on success: returns 1
173  *  on failure: returns 0
174  *  notes: dest must be TOX_FRIEND_ADDRESS_SIZE bytes large, some data may be written to dest even on failure
175  */
176 bool string_to_id(uint8_t *dest, char *src);
177 
178 /* same as id_to_string(), but for TOX_PUBLIC_KEY_SIZE
179  */
180 void cid_to_string(char *dest, uint8_t *src);
181 
182 #endif
183