diff options
author | Astonex <softukitu@gmail.com> | 2013-08-02 19:46:43 +0100 |
---|---|---|
committer | Astonex <softukitu@gmail.com> | 2013-08-02 19:46:43 +0100 |
commit | b1be9eebd0e093d919b9e1e9e2be6996294133ce (patch) | |
tree | 5f6d9c03a117ddc4e7cd98c816e168df96a2acbd /testing | |
parent | 1e016375166a23e259abd34a0c30cdbf5c3d119b (diff) |
Re-arranged the code, and added a maxnumfriends counter
Diffstat (limited to 'testing')
-rw-r--r-- | testing/nTox_win32.c | 283 | ||||
-rw-r--r-- | testing/nTox_win32.h | 7 |
2 files changed, 179 insertions, 111 deletions
diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c index 2394877f..d50ac311 100644 --- a/testing/nTox_win32.c +++ b/testing/nTox_win32.c | |||
@@ -28,6 +28,7 @@ | |||
28 | 28 | ||
29 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; | 29 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; |
30 | uint8_t num_requests = 0; | 30 | uint8_t num_requests = 0; |
31 | uint8_t maxnumfriends; | ||
31 | 32 | ||
32 | char line[STRING_LENGTH]; | 33 | char line[STRING_LENGTH]; |
33 | char users_id[200]; | 34 | char users_id[200]; |
@@ -115,30 +116,176 @@ void load_key() | |||
115 | fclose(data_file); | 116 | fclose(data_file); |
116 | } | 117 | } |
117 | 118 | ||
119 | void add_friend() | ||
120 | { | ||
121 | int i; | ||
122 | char temp_id[128]; | ||
123 | |||
124 | for (i = 0; i < 128; i++) | ||
125 | temp_id[i] = line[i+3]; | ||
126 | |||
127 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | ||
128 | |||
129 | if (num >= 0) { | ||
130 | char numstring[100]; | ||
131 | sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num); | ||
132 | printf(numstring); | ||
133 | ++maxnumfriends; | ||
134 | } | ||
135 | |||
136 | else if (num == -1) | ||
137 | printf("\nWrong key size\n\n"); | ||
138 | |||
139 | else if (num == -2) | ||
140 | printf("\nYou can't add yourself\n\n"); | ||
141 | |||
142 | else if (num == -3) | ||
143 | printf("\nYou already have this person added\n\n"); | ||
144 | |||
145 | else if (num == -4) | ||
146 | printf("\nUndefined error when adding friend"); | ||
147 | } | ||
148 | |||
149 | void list_friends() | ||
150 | { | ||
151 | int activefriends = 0; | ||
152 | int i; | ||
153 | |||
154 | for (i = 0; i <= maxnumfriends; i++) { | ||
155 | if (m_friendstatus(i) == 4) | ||
156 | activefriends++; | ||
157 | } | ||
158 | |||
159 | printf("\n[i] Friend List | Total: %d\n\n", activefriends); | ||
160 | |||
161 | for (i = 0; i <= getnumfriends(); i++) { | ||
162 | char name[MAX_NAME_LENGTH]; | ||
163 | getname(i, (uint8_t*)name); | ||
164 | |||
165 | if (m_friendstatus(i) == 4) | ||
166 | printf("[%d] %s\n\n", i, (uint8_t*)name); | ||
167 | } | ||
168 | } | ||
169 | |||
170 | void delete_friend() | ||
171 | { | ||
172 | size_t len = strlen(line); | ||
173 | char numstring[len-3]; | ||
174 | int i; | ||
175 | |||
176 | for (i = 0; i < len; i++) { | ||
177 | if (line[i+3] != ' ') | ||
178 | numstring[i] = line[i+3]; | ||
179 | } | ||
180 | |||
181 | int num = atoi(numstring); | ||
182 | m_delfriend(num); | ||
183 | --maxnumfriends; | ||
184 | printf("\n\n"); | ||
185 | } | ||
186 | |||
187 | void message_friend() | ||
188 | { | ||
189 | size_t len = strlen(line); | ||
190 | char numstring[len-3]; | ||
191 | char message[len-3]; | ||
192 | int i; | ||
193 | |||
194 | for (i = 0; i < len; i++) { | ||
195 | |||
196 | if (line[i+3] != ' ') | ||
197 | numstring[i] = line[i+3]; | ||
198 | |||
199 | else { | ||
200 | int j; | ||
201 | |||
202 | for (j = (i+1); j < len; j++) | ||
203 | message[j-i-1] = line[j+3]; | ||
204 | |||
205 | break; | ||
206 | } | ||
207 | } | ||
208 | |||
209 | int num = atoi(numstring); | ||
210 | |||
211 | if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) | ||
212 | printf("\n[i] could not send message (they may be offline): %s\n", message); | ||
213 | |||
214 | else | ||
215 | printf("\n"); | ||
216 | } | ||
217 | |||
218 | void change_nickname() | ||
219 | { | ||
220 | uint8_t name[MAX_NAME_LENGTH]; | ||
221 | int i = 0; | ||
222 | size_t len = strlen(line); | ||
223 | |||
224 | for (i = 3; i < len; i++) { | ||
225 | |||
226 | if (line[i] == 0 || line[i] == '\n') | ||
227 | break; | ||
228 | |||
229 | name[i-3] = line[i]; | ||
230 | } | ||
231 | |||
232 | name[i-3] = 0; | ||
233 | setname(name, i); | ||
234 | char numstring[100]; | ||
235 | sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name); | ||
236 | printf(numstring); | ||
237 | |||
238 | FILE *name_file = NULL; | ||
239 | name_file = fopen("namefile.txt", "w"); | ||
240 | fprintf(name_file, "%s", (char*)name); | ||
241 | fclose(name_file); | ||
242 | } | ||
243 | |||
244 | void change_status() | ||
245 | { | ||
246 | uint8_t status[MAX_USERSTATUS_LENGTH]; | ||
247 | int i = 0; | ||
248 | size_t len = strlen(line); | ||
249 | |||
250 | for (i = 3; i < len; i++) { | ||
251 | if (line[i] == 0 || line[i] == '\n') | ||
252 | break; | ||
253 | |||
254 | status[i-3] = line[i]; | ||
255 | } | ||
256 | |||
257 | status[i-3] = 0; | ||
258 | m_set_userstatus(status, strlen((char*)status)); | ||
259 | char numstring[100]; | ||
260 | sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status); | ||
261 | printf(numstring); | ||
262 | |||
263 | FILE* status_file = NULL; | ||
264 | status_file = fopen("statusfile.txt", "w"); | ||
265 | fprintf(status_file, "%s", (char*)status); | ||
266 | fclose(status_file); | ||
267 | } | ||
268 | |||
269 | void accept_friend_request() | ||
270 | { | ||
271 | uint8_t numf = atoi(line + 3); | ||
272 | char numchar[100]; | ||
273 | sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf); | ||
274 | printf(numchar); | ||
275 | int num = m_addfriend_norequest(pending_requests[numf]); | ||
276 | sprintf(numchar, "\n[i] added friendnumber %d\n\n", num); | ||
277 | printf(numchar); | ||
278 | ++maxnumfriends; | ||
279 | } | ||
280 | |||
118 | void line_eval(char* line) | 281 | void line_eval(char* line) |
119 | { | 282 | { |
120 | if(line[0] == '/') { | 283 | if(line[0] == '/') { |
284 | |||
121 | char inpt_command = line[1]; | 285 | char inpt_command = line[1]; |
122 | /* Add friend */ | 286 | |
123 | if(inpt_command == 'f') { | 287 | if(inpt_command == 'f') { |
124 | int i; | 288 | add_friend(line); |
125 | char temp_id[128]; | ||
126 | for (i = 0; i < 128; i++) | ||
127 | temp_id[i] = line[i+3]; | ||
128 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | ||
129 | if (num >= 0) { | ||
130 | char numstring[100]; | ||
131 | sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num); | ||
132 | printf(numstring); | ||
133 | } | ||
134 | else if (num == -1) | ||
135 | printf("\nWrong key size\n\n"); | ||
136 | else if (num == -2) | ||
137 | printf("\nYou can't add yourself\n\n"); | ||
138 | else if (num == -3) | ||
139 | printf("\nYou already have this person added\n\n"); | ||
140 | else if (num == -4) | ||
141 | printf("\nUndefined error when adding friend"); | ||
142 | } | 289 | } |
143 | 290 | ||
144 | else if (inpt_command == 'r') { | 291 | else if (inpt_command == 'r') { |
@@ -147,111 +294,27 @@ void line_eval(char* line) | |||
147 | } | 294 | } |
148 | 295 | ||
149 | else if (inpt_command == 'l') { | 296 | else if (inpt_command == 'l') { |
150 | int activefriends = 0; | 297 | list_friends(line); |
151 | int i; | ||
152 | |||
153 | for (i = 0; i <= getnumfriends(); i++) | ||
154 | { | ||
155 | if (m_friendstatus(i) == 4) | ||
156 | activefriends++; | ||
157 | } | ||
158 | |||
159 | printf("\n[i] Friend List | Total: %d\n\n", activefriends); | ||
160 | |||
161 | for (i = 0; i <= getnumfriends(); i++) { | ||
162 | char name[MAX_NAME_LENGTH]; | ||
163 | getname(i, (uint8_t*)name); | ||
164 | if (m_friendstatus(i) == 4) | ||
165 | printf("[%d] %s\n\n", i, (uint8_t*)name); | ||
166 | } | ||
167 | } | 298 | } |
168 | 299 | ||
169 | else if (inpt_command == 'd') { | 300 | else if (inpt_command == 'd') { |
170 | size_t len = strlen(line); | 301 | delete_friend(line); |
171 | char numstring[len-3]; | ||
172 | int i; | ||
173 | for (i = 0; i < len; i++) { | ||
174 | if (line[i+3] != ' ') { | ||
175 | numstring[i] = line[i+3]; | ||
176 | } | ||
177 | } | ||
178 | int num = atoi(numstring); | ||
179 | m_delfriend(num); | ||
180 | printf("\n\n"); | ||
181 | } | 302 | } |
182 | /* Send message to friend */ | 303 | /* Send message to friend */ |
183 | else if (inpt_command == 'm') { | 304 | else if (inpt_command == 'm') { |
184 | size_t len = strlen(line); | 305 | message_friend(line); |
185 | char numstring[len-3]; | ||
186 | char message[len-3]; | ||
187 | int i; | ||
188 | for (i = 0; i < len; i++) { | ||
189 | if (line[i+3] != ' ') { | ||
190 | numstring[i] = line[i+3]; | ||
191 | } else { | ||
192 | int j; | ||
193 | for (j = (i+1); j < len; j++) | ||
194 | message[j-i-1] = line[j+3]; | ||
195 | break; | ||
196 | } | ||
197 | } | ||
198 | int num = atoi(numstring); | ||
199 | if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { | ||
200 | printf("\n[i] could not send message (they may be offline): %s\n", message); | ||
201 | } else { | ||
202 | //simply for aesthetics | ||
203 | printf("\n"); | ||
204 | } | ||
205 | } | 306 | } |
206 | 307 | ||
207 | else if (inpt_command == 'n') { | 308 | else if (inpt_command == 'n') { |
208 | uint8_t name[MAX_NAME_LENGTH]; | 309 | change_nickname(line); |
209 | int i = 0; | ||
210 | size_t len = strlen(line); | ||
211 | for (i = 3; i < len; i++) { | ||
212 | if (line[i] == 0 || line[i] == '\n') break; | ||
213 | name[i-3] = line[i]; | ||
214 | } | ||
215 | name[i-3] = 0; | ||
216 | setname(name, i); | ||
217 | char numstring[100]; | ||
218 | sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name); | ||
219 | printf(numstring); | ||
220 | |||
221 | FILE *name_file = NULL; | ||
222 | name_file = fopen("namefile.txt", "w"); | ||
223 | fprintf(name_file, "%s", (char*)name); | ||
224 | fclose(name_file); | ||
225 | } | 310 | } |
226 | 311 | ||
227 | else if (inpt_command == 's') { | 312 | else if (inpt_command == 's') { |
228 | uint8_t status[MAX_USERSTATUS_LENGTH]; | 313 | change_status(line); |
229 | int i = 0; | ||
230 | size_t len = strlen(line); | ||
231 | for (i = 3; i < len; i++) { | ||
232 | if (line[i] == 0 || line[i] == '\n') break; | ||
233 | status[i-3] = line[i]; | ||
234 | } | ||
235 | status[i-3] = 0; | ||
236 | m_set_userstatus(status, strlen((char*)status)); | ||
237 | char numstring[100]; | ||
238 | sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status); | ||
239 | printf(numstring); | ||
240 | |||
241 | FILE* status_file = NULL; | ||
242 | status_file = fopen("statusfile.txt", "w"); | ||
243 | fprintf(status_file, "%s", (char*)status); | ||
244 | fclose(status_file); | ||
245 | } | 314 | } |
246 | 315 | ||
247 | else if (inpt_command == 'a') { | 316 | else if (inpt_command == 'a') { |
248 | uint8_t numf = atoi(line + 3); | 317 | accept_friend_request(line); |
249 | char numchar[100]; | ||
250 | sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf); | ||
251 | printf(numchar); | ||
252 | int num = m_addfriend_norequest(pending_requests[numf]); | ||
253 | sprintf(numchar, "\n[i] added friendnumber %d\n\n", num); | ||
254 | printf(numchar); | ||
255 | } | 318 | } |
256 | /* EXIT */ | 319 | /* EXIT */ |
257 | else if (inpt_command == 'q') { | 320 | else if (inpt_command == 'q') { |
@@ -259,8 +322,6 @@ void line_eval(char* line) | |||
259 | m_set_userstatus(status, strlen((char*)status)); | 322 | m_set_userstatus(status, strlen((char*)status)); |
260 | exit(EXIT_SUCCESS); | 323 | exit(EXIT_SUCCESS); |
261 | } | 324 | } |
262 | } else { | ||
263 | //nothing atm | ||
264 | } | 325 | } |
265 | } | 326 | } |
266 | 327 | ||
diff --git a/testing/nTox_win32.h b/testing/nTox_win32.h index 374348a4..211ac95f 100644 --- a/testing/nTox_win32.h +++ b/testing/nTox_win32.h | |||
@@ -34,6 +34,13 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length); | |||
34 | void print_nickchange(int friendnumber, uint8_t *string, uint16_t length); | 34 | void print_nickchange(int friendnumber, uint8_t *string, uint16_t length); |
35 | void print_statuschange(int friendnumber, uint8_t *string, uint16_t length); | 35 | void print_statuschange(int friendnumber, uint8_t *string, uint16_t length); |
36 | void load_key(); | 36 | void load_key(); |
37 | void add_friend(); | ||
38 | void list_friends(); | ||
39 | void delete_friend(); | ||
40 | void message_friend(); | ||
41 | void change_nickname(); | ||
42 | void change_status(); | ||
43 | void accept_friend_request(); | ||
37 | void line_eval(char* line); | 44 | void line_eval(char* line); |
38 | void get_input(); | 45 | void get_input(); |
39 | 46 | ||