diff options
Diffstat (limited to 'testing')
-rw-r--r-- | testing/CMakeLists.txt | 4 | ||||
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 4 | ||||
-rw-r--r-- | testing/DHT_test.c | 6 | ||||
-rw-r--r-- | testing/Lossless_UDP_testclient.c | 2 | ||||
-rw-r--r-- | testing/Lossless_UDP_testserver.c | 2 | ||||
-rw-r--r-- | testing/cmake/DHT_cryptosendfiletest.cmake | 3 | ||||
-rw-r--r-- | testing/cmake/DHT_sendfiletest.cmake | 2 | ||||
-rw-r--r-- | testing/cmake/DHT_test.cmake | 3 | ||||
-rw-r--r-- | testing/cmake/Lossless_UDP_testclient.cmake | 2 | ||||
-rw-r--r-- | testing/cmake/Lossless_UDP_testserver.cmake | 2 | ||||
-rw-r--r-- | testing/cmake/Messenger_test.cmake | 2 | ||||
-rw-r--r-- | testing/cmake/nTox.cmake | 7 | ||||
-rw-r--r-- | testing/misc_tools.h | 2 | ||||
-rw-r--r-- | testing/nTox.c | 211 | ||||
-rw-r--r-- | testing/nTox.h | 1 | ||||
-rw-r--r-- | testing/nTox_win32.c | 87 | ||||
-rw-r--r-- | testing/nTox_win32.h | 1 | ||||
-rw-r--r-- | testing/toxic/CMakeLists.txt (renamed from testing/cmake/toxic.cmake) | 10 | ||||
-rw-r--r-- | testing/toxic/chat.c | 155 | ||||
-rw-r--r-- | testing/toxic/friendlist.c | 166 | ||||
-rw-r--r-- | testing/toxic/main.c | 103 | ||||
-rw-r--r-- | testing/toxic/prompt.c | 118 | ||||
-rw-r--r-- | testing/toxic/windows.h | 13 |
23 files changed, 721 insertions, 185 deletions
diff --git a/testing/CMakeLists.txt b/testing/CMakeLists.txt index 988efe42..abbc278e 100644 --- a/testing/CMakeLists.txt +++ b/testing/CMakeLists.txt | |||
@@ -11,8 +11,8 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Messenger_test.cmake) | |||
11 | if(WIN32) | 11 | if(WIN32) |
12 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox_win32.cmake) | 12 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox_win32.cmake) |
13 | endif() | 13 | endif() |
14 | |||
14 | if(NOT WIN32) | 15 | if(NOT WIN32) |
15 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox.cmake) | 16 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox.cmake) |
16 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/toxic.cmake) | 17 | add_subdirectory(toxic) |
17 | |||
18 | endif() | 18 | endif() |
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index c7c33531..888dac0f 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c | |||
@@ -186,7 +186,7 @@ int main(int argc, char *argv[]) | |||
186 | fclose(file2); | 186 | fclose(file2); |
187 | } | 187 | } |
188 | } | 188 | } |
189 | /* if buffer is empty and the connection timed out. */ | 189 | /* if buffer is empty and the connection timed out. */ |
190 | else if(is_cryptoconnected(inconnection) == 4) { | 190 | else if(is_cryptoconnected(inconnection) == 4) { |
191 | crypto_kill(inconnection); | 191 | crypto_kill(inconnection); |
192 | } | 192 | } |
@@ -209,7 +209,7 @@ int main(int argc, char *argv[]) | |||
209 | fclose(file2); | 209 | fclose(file2); |
210 | } | 210 | } |
211 | } | 211 | } |
212 | /* if buffer is empty and the connection timed out. */ | 212 | /* if buffer is empty and the connection timed out. */ |
213 | else if(is_cryptoconnected(connection) == 4) { | 213 | else if(is_cryptoconnected(connection) == 4) { |
214 | crypto_kill(connection); | 214 | crypto_kill(connection); |
215 | } | 215 | } |
diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 2e9c2ac2..588450e2 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c | |||
@@ -87,7 +87,7 @@ void print_friendlist() | |||
87 | for(i = 0; i < 4; i++) { | 87 | for(i = 0; i < 4; i++) { |
88 | printf("ClientID: "); | 88 | printf("ClientID: "); |
89 | for(j = 0; j < 32; j++) { | 89 | for(j = 0; j < 32; j++) { |
90 | if(0 <= friends_list[k].client_list[i].client_id[j] && friends_list[k].client_list[i].client_id[j] < 16) | 90 | if(friends_list[k].client_list[i].client_id[j] < 16) |
91 | printf("0"); | 91 | printf("0"); |
92 | printf("%hhX", friends_list[k].client_list[i].client_id[j]); | 92 | printf("%hhX", friends_list[k].client_list[i].client_id[j]); |
93 | } | 93 | } |
@@ -134,7 +134,9 @@ int main(int argc, char *argv[]) | |||
134 | 134 | ||
135 | char temp_id[128]; | 135 | char temp_id[128]; |
136 | printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); | 136 | printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); |
137 | scanf("%s", temp_id); | 137 | if(scanf("%s", temp_id) != 1) |
138 | exit(0); | ||
139 | |||
138 | DHT_addfriend(hex_string_to_bin(temp_id)); | 140 | DHT_addfriend(hex_string_to_bin(temp_id)); |
139 | 141 | ||
140 | /* initialize networking */ | 142 | /* initialize networking */ |
diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index c1eaa69d..b96756ab 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c | |||
@@ -211,4 +211,4 @@ int main(int argc, char *argv[]) | |||
211 | } | 211 | } |
212 | 212 | ||
213 | return 0; | 213 | return 0; |
214 | } \ No newline at end of file | 214 | } |
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index 4b437f0e..29b0ade5 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c | |||
@@ -198,4 +198,4 @@ int main(int argc, char *argv[]) | |||
198 | } | 198 | } |
199 | 199 | ||
200 | return 0; | 200 | return 0; |
201 | } \ No newline at end of file | 201 | } |
diff --git a/testing/cmake/DHT_cryptosendfiletest.cmake b/testing/cmake/DHT_cryptosendfiletest.cmake index c98a2bcd..5470d02a 100644 --- a/testing/cmake/DHT_cryptosendfiletest.cmake +++ b/testing/cmake/DHT_cryptosendfiletest.cmake | |||
@@ -4,6 +4,7 @@ project(DHT_cryptosendfiletest C) | |||
4 | set(exe_name DHT_cryptosendfiletest) | 4 | set(exe_name DHT_cryptosendfiletest) |
5 | 5 | ||
6 | add_executable(${exe_name} | 6 | add_executable(${exe_name} |
7 | DHT_cryptosendfiletest.c misc_tools.c) | 7 | DHT_cryptosendfiletest.c |
8 | misc_tools.c) | ||
8 | 9 | ||
9 | linkCoreLibraries(${exe_name}) | 10 | linkCoreLibraries(${exe_name}) |
diff --git a/testing/cmake/DHT_sendfiletest.cmake b/testing/cmake/DHT_sendfiletest.cmake index 93737914..298db46c 100644 --- a/testing/cmake/DHT_sendfiletest.cmake +++ b/testing/cmake/DHT_sendfiletest.cmake | |||
@@ -4,6 +4,6 @@ project(DHT_sendfiletest C) | |||
4 | set(exe_name DHT_sendfiletest) | 4 | set(exe_name DHT_sendfiletest) |
5 | 5 | ||
6 | add_executable(${exe_name} | 6 | add_executable(${exe_name} |
7 | DHT_sendfiletest.c) | 7 | DHT_sendfiletest.c) |
8 | 8 | ||
9 | linkCoreLibraries(${exe_name}) | 9 | linkCoreLibraries(${exe_name}) |
diff --git a/testing/cmake/DHT_test.cmake b/testing/cmake/DHT_test.cmake index 74fdf35d..bb5bf05f 100644 --- a/testing/cmake/DHT_test.cmake +++ b/testing/cmake/DHT_test.cmake | |||
@@ -4,6 +4,7 @@ project(DHT_test C) | |||
4 | set(exe_name DHT_test) | 4 | set(exe_name DHT_test) |
5 | 5 | ||
6 | add_executable(${exe_name} | 6 | add_executable(${exe_name} |
7 | DHT_test.c misc_tools.c) | 7 | DHT_test.c |
8 | misc_tools.c) | ||
8 | 9 | ||
9 | linkCoreLibraries(${exe_name}) | 10 | linkCoreLibraries(${exe_name}) |
diff --git a/testing/cmake/Lossless_UDP_testclient.cmake b/testing/cmake/Lossless_UDP_testclient.cmake index e894d228..5f651807 100644 --- a/testing/cmake/Lossless_UDP_testclient.cmake +++ b/testing/cmake/Lossless_UDP_testclient.cmake | |||
@@ -4,6 +4,6 @@ project(Lossless_UDP_testclient C) | |||
4 | set(exe_name Lossless_UDP_testclient) | 4 | set(exe_name Lossless_UDP_testclient) |
5 | 5 | ||
6 | add_executable(${exe_name} | 6 | add_executable(${exe_name} |
7 | Lossless_UDP_testclient.c) | 7 | Lossless_UDP_testclient.c) |
8 | 8 | ||
9 | linkCoreLibraries(${exe_name}) | 9 | linkCoreLibraries(${exe_name}) |
diff --git a/testing/cmake/Lossless_UDP_testserver.cmake b/testing/cmake/Lossless_UDP_testserver.cmake index 04306c1a..26f9302e 100644 --- a/testing/cmake/Lossless_UDP_testserver.cmake +++ b/testing/cmake/Lossless_UDP_testserver.cmake | |||
@@ -4,6 +4,6 @@ project(Lossless_UDP_testserver C) | |||
4 | set(exe_name Lossless_UDP_testserver) | 4 | set(exe_name Lossless_UDP_testserver) |
5 | 5 | ||
6 | add_executable(${exe_name} | 6 | add_executable(${exe_name} |
7 | Lossless_UDP_testserver.c) | 7 | Lossless_UDP_testserver.c) |
8 | 8 | ||
9 | linkCoreLibraries(${exe_name}) | 9 | linkCoreLibraries(${exe_name}) |
diff --git a/testing/cmake/Messenger_test.cmake b/testing/cmake/Messenger_test.cmake index b2f54d0a..15fcd77b 100644 --- a/testing/cmake/Messenger_test.cmake +++ b/testing/cmake/Messenger_test.cmake | |||
@@ -4,6 +4,6 @@ project(Messenger_test C) | |||
4 | set(exe_name Messenger_test) | 4 | set(exe_name Messenger_test) |
5 | 5 | ||
6 | add_executable(${exe_name} | 6 | add_executable(${exe_name} |
7 | Messenger_test.c misc_tools.c) | 7 | Messenger_test.c misc_tools.c) |
8 | 8 | ||
9 | linkCoreLibraries(${exe_name}) | 9 | linkCoreLibraries(${exe_name}) |
diff --git a/testing/cmake/nTox.cmake b/testing/cmake/nTox.cmake index 4c6905e5..44476b8f 100644 --- a/testing/cmake/nTox.cmake +++ b/testing/cmake/nTox.cmake | |||
@@ -4,8 +4,9 @@ project(nTox C) | |||
4 | set(exe_name nTox) | 4 | set(exe_name nTox) |
5 | 5 | ||
6 | add_executable(${exe_name} | 6 | add_executable(${exe_name} |
7 | nTox.c misc_tools.c) | 7 | nTox.c misc_tools.c) |
8 | 8 | ||
9 | target_link_libraries(${exe_name} ncurses) | 9 | target_link_libraries(${exe_name} |
10 | ncurses) | ||
10 | 11 | ||
11 | linkCoreLibraries(${exe_name}) | 12 | linkCoreLibraries(${exe_name}) |
diff --git a/testing/misc_tools.h b/testing/misc_tools.h index 29b37ce5..5079e15d 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h | |||
@@ -26,4 +26,4 @@ | |||
26 | 26 | ||
27 | unsigned char * hex_string_to_bin(char hex_string[]); | 27 | unsigned char * hex_string_to_bin(char hex_string[]); |
28 | 28 | ||
29 | #endif // MISC_TOOLS_H \ No newline at end of file | 29 | #endif // MISC_TOOLS_H |
diff --git a/testing/nTox.c b/testing/nTox.c index 960dfb27..7c84871f 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -20,12 +20,12 @@ | |||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | |||
24 | #include "nTox.h" | 23 | #include "nTox.h" |
25 | #include "misc_tools.h" | 24 | #include "misc_tools.h" |
26 | 25 | ||
27 | #include <stdio.h> | 26 | #include <stdio.h> |
28 | #include <time.h> | 27 | #include <time.h> |
28 | |||
29 | #ifdef WIN32 | 29 | #ifdef WIN32 |
30 | #define c_sleep(x) Sleep(1*x) | 30 | #define c_sleep(x) Sleep(1*x) |
31 | #else | 31 | #else |
@@ -35,31 +35,75 @@ | |||
35 | 35 | ||
36 | char lines[HISTORY][STRING_LENGTH]; | 36 | char lines[HISTORY][STRING_LENGTH]; |
37 | char line[STRING_LENGTH]; | 37 | char line[STRING_LENGTH]; |
38 | int x,y; | 38 | |
39 | char *help = "[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)\n" | ||
40 | "[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"; | ||
41 | int x, y; | ||
42 | |||
39 | 43 | ||
40 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; | 44 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; |
41 | uint8_t num_requests; | 45 | uint8_t num_requests = 0; |
42 | 46 | ||
43 | void new_lines(char *line) | 47 | void new_lines(char *line) |
44 | { | 48 | { |
45 | int i; | 49 | int i; |
46 | for (i = HISTORY-1; i > 0; i--) | 50 | for (i = HISTORY-1; i > 0; i--) |
47 | strcpy(lines[i],lines[i-1]); | 51 | strcpy(lines[i], lines[i-1]); |
48 | 52 | ||
49 | strcpy(lines[0],line); | 53 | strcpy(lines[0], line); |
50 | do_refresh(); | 54 | do_refresh(); |
51 | } | 55 | } |
52 | 56 | ||
57 | |||
58 | void print_friendlist() | ||
59 | { | ||
60 | char name[MAX_NAME_LENGTH]; | ||
61 | new_lines("[i] Friend List:"); | ||
62 | uint32_t i; | ||
63 | for (i = 0; i <= num_requests; i++) { | ||
64 | char fstring[128]; | ||
65 | getname(i, (uint8_t*)name); | ||
66 | if (strlen(name) <= 0) { | ||
67 | sprintf(fstring, "[i] Friend: NULL\n\tid: %i", i); | ||
68 | } else { | ||
69 | sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t*)name, i); | ||
70 | } | ||
71 | new_lines(fstring); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | char *format_message(char *message, int friendnum) | ||
76 | { | ||
77 | char name[MAX_NAME_LENGTH]; | ||
78 | if (friendnum != -1) { | ||
79 | getname(friendnum, (uint8_t*)name); | ||
80 | } else { | ||
81 | getself_name((uint8_t*)name); | ||
82 | } | ||
83 | char *msg = malloc(100+strlen(message)+strlen(name)+1); | ||
84 | |||
85 | time_t rawtime; | ||
86 | struct tm * timeinfo; | ||
87 | time ( &rawtime ); | ||
88 | timeinfo = localtime ( &rawtime ); | ||
89 | char* time = asctime(timeinfo); | ||
90 | size_t len = strlen(time); | ||
91 | time[len-1] = '\0'; | ||
92 | sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message); // timestamp | ||
93 | return msg; | ||
94 | } | ||
95 | |||
53 | void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | 96 | void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) |
54 | { | 97 | { |
55 | if (line[0] == '/') { | 98 | if (line[0] == '/') { |
56 | char command[STRING_LENGTH + 2] = "> "; | 99 | char inpt_command = line[1]; |
57 | strcat(command, line); | 100 | char prompt[STRING_LENGTH + 2] = "> "; |
58 | new_lines(command); | 101 | strcat(prompt, line); |
59 | if (line[1] == 'f') { // add friend command: /f ID | 102 | new_lines(prompt); |
103 | if (inpt_command == 'f') { // add friend command: /f ID | ||
60 | int i; | 104 | int i; |
61 | char temp_id[128]; | 105 | char temp_id[128]; |
62 | for (i=0; i<128; i++) | 106 | for (i = 0; i < 128; i++) |
63 | temp_id[i] = line[i+3]; | 107 | temp_id[i] = line[i+3]; |
64 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | 108 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); |
65 | char numstring[100]; | 109 | char numstring[100]; |
@@ -67,82 +111,121 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
67 | new_lines(numstring); | 111 | new_lines(numstring); |
68 | do_refresh(); | 112 | do_refresh(); |
69 | } | 113 | } |
70 | else if (line[1] == 'd') { | 114 | else if (inpt_command == 'd') { |
71 | doMessenger(); | 115 | doMessenger(); |
72 | } | 116 | } |
73 | else if (line[1] == 'm') { //message command: /m friendnumber messsage | 117 | else if (inpt_command == 'm') { //message command: /m friendnumber messsage |
74 | int i; | ||
75 | size_t len = strlen(line); | 118 | size_t len = strlen(line); |
76 | char numstring[len-3]; | 119 | char numstring[len-3]; |
77 | char message[len-3]; | 120 | char message[len-3]; |
78 | for (i=0; i<len; i++) { | 121 | int i; |
122 | for (i = 0; i < len; i++) { | ||
79 | if (line[i+3] != ' ') { | 123 | if (line[i+3] != ' ') { |
80 | numstring[i] = line[i+3]; | 124 | numstring[i] = line[i+3]; |
81 | } else { | 125 | } else { |
82 | int j; | 126 | int j; |
83 | for (j=i+1; j<len; j++) | 127 | for (j = (i+1); j < len; j++) |
84 | message[j-i-1] = line[j+3]; | 128 | message[j-i-1] = line[j+3]; |
85 | break; | 129 | break; |
86 | } | 130 | } |
87 | } | 131 | } |
88 | int num = atoi(numstring); | 132 | int num = atoi(numstring); |
89 | if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { | 133 | if (m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { |
90 | new_lines("[i] could not send message"); | 134 | new_lines("[i] could not send message"); |
135 | } else { | ||
136 | new_lines(format_message(message, -1)); | ||
91 | } | 137 | } |
92 | } | 138 | } |
93 | else if (line[1] == 'n') { | 139 | else if (inpt_command == 'n') { |
94 | uint8_t name[MAX_NAME_LENGTH]; | 140 | uint8_t name[MAX_NAME_LENGTH]; |
95 | int i = 0; | 141 | int i = 0; |
96 | size_t len = strlen(line); | 142 | size_t len = strlen(line); |
97 | for (i=3; i<len; i++) { | 143 | for (i = 3; i < len; i++) { |
98 | if (line[i] == 0 || line[i] == '\n') break; | 144 | if (line[i] == 0 || line[i] == '\n') break; |
99 | name[i - 3] = line[i]; | 145 | name[i-3] = line[i]; |
100 | } | 146 | } |
101 | name[i - 3] = 0; | 147 | name[i-3] = 0; |
102 | setname(name, i); | 148 | setname(name, i); |
103 | char numstring[100]; | 149 | char numstring[100]; |
104 | sprintf(numstring, "[i] changed nick to %s", (char*)name); | 150 | sprintf(numstring, "[i] changed nick to %s", (char*)name); |
105 | new_lines(numstring); | 151 | new_lines(numstring); |
106 | } | 152 | } |
107 | else if (line[1] == 's') { | 153 | else if (inpt_command == 'l') { |
154 | print_friendlist(); | ||
155 | } | ||
156 | else if (inpt_command == 's') { | ||
108 | uint8_t status[MAX_USERSTATUS_LENGTH]; | 157 | uint8_t status[MAX_USERSTATUS_LENGTH]; |
109 | int i = 0; | 158 | int i = 0; |
110 | size_t len = strlen(line); | 159 | size_t len = strlen(line); |
111 | for (i=3; i<len; i++) { | 160 | for (i = 3; i < len; i++) { |
112 | if (line[i] == 0 || line[i] == '\n') break; | 161 | if (line[i] == 0 || line[i] == '\n') break; |
113 | status[i - 3] = line[i]; | 162 | status[i-3] = line[i]; |
114 | } | 163 | } |
115 | status[i - 3] = 0; | 164 | status[i-3] = 0; |
116 | m_set_userstatus(status, strlen((char*)status)); | 165 | m_set_userstatus(status, strlen((char*)status)); |
117 | char numstring[100]; | 166 | char numstring[100]; |
118 | sprintf(numstring, "[i] changed status to %s", (char*)status); | 167 | sprintf(numstring, "[i] changed status to %s", (char*)status); |
119 | new_lines(numstring); | 168 | new_lines(numstring); |
120 | } | 169 | } |
121 | else if (line[1] == 'a') { | 170 | else if (inpt_command == 'a') { |
122 | uint8_t numf = atoi(line + 3); | 171 | uint8_t numf = atoi(line + 3); |
123 | char numchar[100]; | 172 | char numchar[100]; |
124 | sprintf(numchar, "[i] friend request %u accepted", numf); | ||
125 | new_lines(numchar); | ||
126 | int num = m_addfriend_norequest(pending_requests[numf]); | 173 | int num = m_addfriend_norequest(pending_requests[numf]); |
127 | sprintf(numchar, "[i] added friendnumber %d", num); | 174 | if (num != -1) { |
128 | new_lines(numchar); | 175 | sprintf(numchar, "[i] friend request %u accepted", numf); |
176 | new_lines(numchar); | ||
177 | sprintf(numchar, "[i] added friendnumber %d", num); | ||
178 | new_lines(numchar); | ||
179 | } else { | ||
180 | sprintf(numchar, "[i] failed to add friend"); | ||
181 | new_lines(numchar); | ||
182 | } | ||
129 | do_refresh(); | 183 | do_refresh(); |
130 | |||
131 | } | 184 | } |
132 | else if (line[1] == 'q') { //exit | 185 | else if (inpt_command == 'h') { //help |
186 | new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)"); | ||
187 | new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"); | ||
188 | } | ||
189 | else if (inpt_command == 'i') { //info | ||
190 | char idstring0[200]; | ||
191 | char idstring1[PUB_KEY_BYTES][5]; | ||
192 | char idstring2[PUB_KEY_BYTES][5]; | ||
193 | int i; | ||
194 | for (i = 0; i < PUB_KEY_BYTES; i++) | ||
195 | { | ||
196 | if (self_public_key[i] < (PUB_KEY_BYTES/2)) | ||
197 | strcpy(idstring1[i],"0"); | ||
198 | else | ||
199 | strcpy(idstring1[i], ""); | ||
200 | sprintf(idstring2[i], "%hhX", self_public_key[i]); | ||
201 | } | ||
202 | // | ||
203 | strcpy(idstring0,"[i] ID: "); | ||
204 | int j; | ||
205 | for (j = 0; j < PUB_KEY_BYTES; j++) { | ||
206 | strcat(idstring0,idstring1[j]); | ||
207 | strcat(idstring0,idstring2[j]); | ||
208 | } | ||
209 | new_lines(idstring0); | ||
210 | } | ||
211 | |||
212 | else if (inpt_command == 'q') { //exit | ||
133 | endwin(); | 213 | endwin(); |
134 | exit(EXIT_SUCCESS); | 214 | exit(EXIT_SUCCESS); |
215 | } else { | ||
216 | new_lines("[i] invalid command"); | ||
135 | } | 217 | } |
136 | } else { | 218 | } else { |
219 | new_lines("[i] invalid command"); | ||
137 | //new_lines(line); | 220 | //new_lines(line); |
138 | } | 221 | } |
139 | } | 222 | } |
140 | 223 | ||
141 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) | 224 | void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) |
142 | { | 225 | { |
143 | int i = 0; | ||
144 | strcpy(output,input); | 226 | strcpy(output,input); |
145 | size_t len = strlen(output); | 227 | size_t len = strlen(output); |
228 | int i = 0; | ||
146 | for (i = line_width; i < len; i = i + line_width) { | 229 | for (i = line_width; i < len; i = i + line_width) { |
147 | while (output[i] != ' ' && i != 0) { | 230 | while (output[i] != ' ' && i != 0) { |
148 | i--; | 231 | i--; |
@@ -156,9 +239,9 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) | |||
156 | int count_lines(char *string) | 239 | int count_lines(char *string) |
157 | { | 240 | { |
158 | size_t len = strlen(string); | 241 | size_t len = strlen(string); |
159 | int i; | ||
160 | int count = 1; | 242 | int count = 1; |
161 | for (i=0; i < len; i++) { | 243 | int i; |
244 | for (i = 0; i < len; i++) { | ||
162 | if (string[i] == '\n') | 245 | if (string[i] == '\n') |
163 | count++; | 246 | count++; |
164 | } | 247 | } |
@@ -169,7 +252,7 @@ char *appender(char *str, const char c) | |||
169 | { | 252 | { |
170 | size_t len = strlen(str); | 253 | size_t len = strlen(str); |
171 | if (len < STRING_LENGTH) { | 254 | if (len < STRING_LENGTH) { |
172 | str[len + 1] = str[len]; | 255 | str[len+1] = str[len]; |
173 | str[len] = c; | 256 | str[len] = c; |
174 | } | 257 | } |
175 | return str; | 258 | return str; |
@@ -177,21 +260,21 @@ char *appender(char *str, const char c) | |||
177 | 260 | ||
178 | void do_refresh() | 261 | void do_refresh() |
179 | { | 262 | { |
180 | int i; | ||
181 | int count=0; | 263 | int count=0; |
182 | int l; | ||
183 | char wrap_output[STRING_LENGTH]; | 264 | char wrap_output[STRING_LENGTH]; |
184 | for (i=0; i<HISTORY; i++) { | 265 | int L; |
266 | int i; | ||
267 | for (i = 0; i < HISTORY; i++) { | ||
185 | wrap(wrap_output, lines[i], x); | 268 | wrap(wrap_output, lines[i], x); |
186 | l = count_lines(wrap_output); | 269 | L = count_lines(wrap_output); |
187 | count = count + l; | 270 | count = count + L; |
188 | if (count < y) { | 271 | if (count < y) { |
189 | move(y-1-count,0); | 272 | move(y-1-count, 0); |
190 | printw(wrap_output); | 273 | printw(wrap_output); |
191 | clrtoeol(); | 274 | clrtoeol(); |
192 | } | 275 | } |
193 | } | 276 | } |
194 | move(y-1,0); | 277 | move(y-1, 0); |
195 | clrtoeol(); | 278 | clrtoeol(); |
196 | printw(">> "); | 279 | printw(">> "); |
197 | printw(line); | 280 | printw(line); |
@@ -222,12 +305,13 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length) | |||
222 | timeinfo = localtime ( &rawtime ); | 305 | timeinfo = localtime ( &rawtime ); |
223 | char* temp = asctime(timeinfo); | 306 | char* temp = asctime(timeinfo); |
224 | size_t len = strlen(temp); | 307 | size_t len = strlen(temp); |
225 | temp[len-1]='\0'; | 308 | temp[len-1] = '\0'; |
226 | sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp | 309 | sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp |
227 | new_lines(msg); | 310 | new_lines(format_message((char*)string, friendnumber)); |
228 | } | 311 | } |
229 | 312 | ||
230 | void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { | 313 | void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) |
314 | { | ||
231 | char name[MAX_NAME_LENGTH]; | 315 | char name[MAX_NAME_LENGTH]; |
232 | getname(friendnumber, (uint8_t*)name); | 316 | getname(friendnumber, (uint8_t*)name); |
233 | char msg[100+length]; | 317 | char msg[100+length]; |
@@ -235,7 +319,8 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) { | |||
235 | new_lines(msg); | 319 | new_lines(msg); |
236 | } | 320 | } |
237 | 321 | ||
238 | void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { | 322 | void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) |
323 | { | ||
239 | char name[MAX_NAME_LENGTH]; | 324 | char name[MAX_NAME_LENGTH]; |
240 | getname(friendnumber, (uint8_t*)name); | 325 | getname(friendnumber, (uint8_t*)name); |
241 | char msg[100+length+strlen(name)+1]; | 326 | char msg[100+length+strlen(name)+1]; |
@@ -243,15 +328,17 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { | |||
243 | new_lines(msg); | 328 | new_lines(msg); |
244 | } | 329 | } |
245 | 330 | ||
246 | void load_key(){ | 331 | void load_key() |
332 | { | ||
247 | FILE *data_file = NULL; | 333 | FILE *data_file = NULL; |
248 | if ((data_file = fopen("data","r"))) { | 334 | data_file = fopen("data","r"); |
335 | if (data_file) { | ||
249 | //load keys | 336 | //load keys |
250 | fseek(data_file, 0, SEEK_END); | 337 | fseek(data_file, 0, SEEK_END); |
251 | int size = ftell(data_file); | 338 | int size = ftell(data_file); |
252 | fseek(data_file, 0, SEEK_SET); | 339 | fseek(data_file, 0, SEEK_SET); |
253 | uint8_t data[size]; | 340 | uint8_t data[size]; |
254 | if(fread(data, sizeof(uint8_t), size, data_file) != size){ | 341 | if (fread(data, sizeof(uint8_t), size, data_file) != size){ |
255 | printf("[i] could not read data file\n[i] exiting\n"); | 342 | printf("[i] could not read data file\n[i] exiting\n"); |
256 | exit(1); | 343 | exit(1); |
257 | } | 344 | } |
@@ -262,7 +349,7 @@ void load_key(){ | |||
262 | uint8_t data[size]; | 349 | uint8_t data[size]; |
263 | Messenger_save(data); | 350 | Messenger_save(data); |
264 | data_file = fopen("data","w"); | 351 | data_file = fopen("data","w"); |
265 | if(fwrite(data, sizeof(uint8_t), size, data_file) != size){ | 352 | if (fwrite(data, sizeof(uint8_t), size, data_file) != size){ |
266 | printf("[i] could not write data file\n[i] exiting\n"); | 353 | printf("[i] could not write data file\n[i] exiting\n"); |
267 | exit(1); | 354 | exit(1); |
268 | } | 355 | } |
@@ -292,28 +379,29 @@ int main(int argc, char *argv[]) | |||
292 | m_callback_namechange(print_nickchange); | 379 | m_callback_namechange(print_nickchange); |
293 | m_callback_userstatus(print_statuschange); | 380 | m_callback_userstatus(print_statuschange); |
294 | char idstring0[200]; | 381 | char idstring0[200]; |
295 | char idstring1[32][5]; | 382 | char idstring1[PUB_KEY_BYTES][5]; |
296 | char idstring2[32][5]; | 383 | char idstring2[PUB_KEY_BYTES][5]; |
297 | uint32_t i; | 384 | int i; |
298 | for(i = 0; i < 32; i++) | 385 | for(i = 0; i < PUB_KEY_BYTES; i++) |
299 | { | 386 | { |
300 | if(self_public_key[i] < 16) | 387 | if (self_public_key[i] < (PUB_KEY_BYTES / 2)) |
301 | strcpy(idstring1[i],"0"); | 388 | strcpy(idstring1[i],"0"); |
302 | else | 389 | else |
303 | strcpy(idstring1[i], ""); | 390 | strcpy(idstring1[i], ""); |
304 | sprintf(idstring2[i], "%hhX",self_public_key[i]); | 391 | sprintf(idstring2[i], "%hhX",self_public_key[i]); |
305 | } | 392 | } |
306 | strcpy(idstring0,"[i] your ID: "); | 393 | strcpy(idstring0,"[i] your ID: "); |
307 | for (i=0; i<32; i++) { | 394 | int j; |
308 | strcat(idstring0,idstring1[i]); | 395 | for (j = 0; j < PUB_KEY_BYTES; j++) { |
309 | strcat(idstring0,idstring2[i]); | 396 | strcat(idstring0,idstring1[j]); |
397 | strcat(idstring0,idstring2[j]); | ||
310 | } | 398 | } |
311 | initscr(); | 399 | initscr(); |
312 | noecho(); | 400 | noecho(); |
313 | raw(); | 401 | raw(); |
314 | getmaxyx(stdscr,y,x); | 402 | getmaxyx(stdscr, y, x); |
315 | new_lines(idstring0); | 403 | new_lines(idstring0); |
316 | new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status), /n nick (to change nickname), /q (to quit)"); | 404 | new_lines(help); |
317 | strcpy(line, ""); | 405 | strcpy(line, ""); |
318 | IP_Port bootstrap_ip_port; | 406 | IP_Port bootstrap_ip_port; |
319 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 407 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
@@ -336,7 +424,6 @@ int main(int argc, char *argv[]) | |||
336 | do_refresh(); | 424 | do_refresh(); |
337 | 425 | ||
338 | c = getch(); | 426 | c = getch(); |
339 | |||
340 | if (c == ERR || c == 27) | 427 | if (c == ERR || c == 27) |
341 | continue; | 428 | continue; |
342 | 429 | ||
@@ -345,7 +432,7 @@ int main(int argc, char *argv[]) | |||
345 | line_eval(lines, line); | 432 | line_eval(lines, line); |
346 | strcpy(line, ""); | 433 | strcpy(line, ""); |
347 | } else if (c == 127) { | 434 | } else if (c == 127) { |
348 | line[strlen(line) - 1] = '\0'; | 435 | line[strlen(line)-1] = '\0'; |
349 | } else if (isalnum(c) || ispunct(c) || c == ' ') { | 436 | } else if (isalnum(c) || ispunct(c) || c == ' ') { |
350 | strcpy(line, appender(line, (char) c)); | 437 | strcpy(line, appender(line, (char) c)); |
351 | } | 438 | } |
diff --git a/testing/nTox.h b/testing/nTox.h index 9b69d959..9d82556c 100644 --- a/testing/nTox.h +++ b/testing/nTox.h | |||
@@ -39,6 +39,7 @@ | |||
39 | #include "../core/network.h" | 39 | #include "../core/network.h" |
40 | #define STRING_LENGTH 256 | 40 | #define STRING_LENGTH 256 |
41 | #define HISTORY 50 | 41 | #define HISTORY 50 |
42 | #define PUB_KEY_BYTES 32 | ||
42 | 43 | ||
43 | void new_lines(char *line); | 44 | void new_lines(char *line); |
44 | void line_eval(char lines[HISTORY][STRING_LENGTH], char *line); | 45 | void line_eval(char lines[HISTORY][STRING_LENGTH], char *line); |
diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c index 514ce463..ad114f46 100644 --- a/testing/nTox_win32.c +++ b/testing/nTox_win32.c | |||
@@ -27,7 +27,7 @@ | |||
27 | #include <process.h> | 27 | #include <process.h> |
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; | 30 | uint8_t num_requests = 0; |
31 | 31 | ||
32 | char line[STRING_LENGTH]; | 32 | char line[STRING_LENGTH]; |
33 | char users_id[200]; | 33 | char users_id[200]; |
@@ -89,14 +89,13 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) | |||
89 | void load_key() | 89 | void load_key() |
90 | { | 90 | { |
91 | FILE *data_file = NULL; | 91 | FILE *data_file = NULL; |
92 | 92 | data_file = fopen("data","r"); | |
93 | if ((data_file = fopen("data", "r"))) { | 93 | if (data_file) { |
94 | fseek(data_file, 0, SEEK_END); | 94 | fseek(data_file, 0, SEEK_END); |
95 | int size = ftell(data_file); | 95 | int size = ftell(data_file); |
96 | fseek(data_file, 0, SEEK_SET); | 96 | fseek(data_file, 0, SEEK_SET); |
97 | uint8_t data[size]; | 97 | uint8_t data[size]; |
98 | 98 | if (fread(data, sizeof(uint8_t), size, data_file) != size) { | |
99 | if(fread(data, sizeof(uint8_t), size, data_file) != size) { | ||
100 | printf("\n[i] Could not read the data file. Exiting."); | 99 | printf("\n[i] Could not read the data file. Exiting."); |
101 | exit(1); | 100 | exit(1); |
102 | } | 101 | } |
@@ -108,23 +107,23 @@ void load_key() | |||
108 | Messenger_save(data); | 107 | Messenger_save(data); |
109 | data_file = fopen("data", "w"); | 108 | data_file = fopen("data", "w"); |
110 | 109 | ||
111 | if(fwrite(data, sizeof(uint8_t), size, data_file) != size) { | 110 | if (fwrite(data, sizeof(uint8_t), size, data_file) != size) { |
112 | printf("\n[i] Could not write data to file. Exiting."); | 111 | printf("\n[i] Could not write data to file. Exiting."); |
113 | exit(1); | 112 | exit(1); |
114 | } | 113 | } |
115 | } | 114 | } |
116 | |||
117 | fclose(data_file); | 115 | fclose(data_file); |
118 | } | 116 | } |
119 | 117 | ||
120 | void line_eval(char* line) | 118 | void line_eval(char* line) |
121 | { | 119 | { |
122 | if(line[0] == '/') { | 120 | if(line[0] == '/') { |
121 | char inpt_command = line[1]; | ||
123 | /* Add friend */ | 122 | /* Add friend */ |
124 | if(line[1] == 'f') { | 123 | if(inpt_command == 'f') { |
125 | int i; | 124 | int i; |
126 | char temp_id[128]; | 125 | char temp_id[128]; |
127 | for (i=0; i<128; i++) | 126 | for (i = 0; i < 128; i++) |
128 | temp_id[i] = line[i+3]; | 127 | temp_id[i] = line[i+3]; |
129 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | 128 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); |
130 | char numstring[100]; | 129 | char numstring[100]; |
@@ -132,17 +131,16 @@ void line_eval(char* line) | |||
132 | printf(numstring); | 131 | printf(numstring); |
133 | } | 132 | } |
134 | 133 | ||
135 | else if (line[1] == 'r') { | 134 | else if (inpt_command == 'r') { |
136 | do_header(); | 135 | do_header(); |
137 | printf("\n\n"); | 136 | printf("\n\n"); |
138 | } | 137 | } |
139 | 138 | ||
140 | else if (line[1] == 'l') { | 139 | else if (inpt_command == 'l') { |
141 | printf("\n[i] Friend List | Total: %d\n\n", getnumfriends()); | 140 | printf("\n[i] Friend List | Total: %d\n\n", getnumfriends()); |
142 | 141 | ||
143 | int i; | 142 | int i; |
144 | 143 | for (i = 0; i < getnumfriends(); i++) { | |
145 | for (i=0; i < getnumfriends(); i++) { | ||
146 | char name[MAX_NAME_LENGTH]; | 144 | char name[MAX_NAME_LENGTH]; |
147 | getname(i, (uint8_t*)name); | 145 | getname(i, (uint8_t*)name); |
148 | if (m_friendstatus(i) == 0) | 146 | if (m_friendstatus(i) == 0) |
@@ -152,11 +150,11 @@ void line_eval(char* line) | |||
152 | } | 150 | } |
153 | } | 151 | } |
154 | 152 | ||
155 | else if (line[1] == 'd') { | 153 | else if (inpt_command == 'd') { |
156 | size_t len = strlen(line); | 154 | size_t len = strlen(line); |
157 | char numstring[len-3]; | 155 | char numstring[len-3]; |
158 | int i; | 156 | int i; |
159 | for (i=0; i<len; i++) { | 157 | for (i = 0; i < len; i++) { |
160 | if (line[i+3] != ' ') { | 158 | if (line[i+3] != ' ') { |
161 | numstring[i] = line[i+3]; | 159 | numstring[i] = line[i+3]; |
162 | } | 160 | } |
@@ -166,17 +164,17 @@ void line_eval(char* line) | |||
166 | printf("\n\n"); | 164 | printf("\n\n"); |
167 | } | 165 | } |
168 | /* Send message to friend */ | 166 | /* Send message to friend */ |
169 | else if (line[1] == 'm') { | 167 | else if (inpt_command == 'm') { |
170 | int i; | ||
171 | size_t len = strlen(line); | 168 | size_t len = strlen(line); |
172 | char numstring[len-3]; | 169 | char numstring[len-3]; |
173 | char message[len-3]; | 170 | char message[len-3]; |
174 | for (i=0; i<len; i++) { | 171 | int i; |
172 | for (i = 0; i < len; i++) { | ||
175 | if (line[i+3] != ' ') { | 173 | if (line[i+3] != ' ') { |
176 | numstring[i] = line[i+3]; | 174 | numstring[i] = line[i+3]; |
177 | } else { | 175 | } else { |
178 | int j; | 176 | int j; |
179 | for (j=i+1; j<len; j++) | 177 | for (j = (i+1); j < len; j++) |
180 | message[j-i-1] = line[j+3]; | 178 | message[j-i-1] = line[j+3]; |
181 | break; | 179 | break; |
182 | } | 180 | } |
@@ -190,37 +188,37 @@ void line_eval(char* line) | |||
190 | } | 188 | } |
191 | } | 189 | } |
192 | 190 | ||
193 | else if (line[1] == 'n') { | 191 | else if (inpt_command == 'n') { |
194 | uint8_t name[MAX_NAME_LENGTH]; | 192 | uint8_t name[MAX_NAME_LENGTH]; |
195 | int i = 0; | 193 | int i = 0; |
196 | size_t len = strlen(line); | 194 | size_t len = strlen(line); |
197 | for (i=3; i<len; i++) { | 195 | for (i = 3; i < len; i++) { |
198 | if (line[i] == 0 || line[i] == '\n') break; | 196 | if (line[i] == 0 || line[i] == '\n') break; |
199 | name[i - 3] = line[i]; | 197 | name[i-3] = line[i]; |
200 | } | 198 | } |
201 | name[i - 3] = 0; | 199 | name[i-3] = 0; |
202 | setname(name, i); | 200 | setname(name, i); |
203 | char numstring[100]; | 201 | char numstring[100]; |
204 | sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name); | 202 | sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name); |
205 | printf(numstring); | 203 | printf(numstring); |
206 | } | 204 | } |
207 | 205 | ||
208 | else if (line[1] == 's') { | 206 | else if (inpt_command == 's') { |
209 | uint8_t status[MAX_USERSTATUS_LENGTH]; | 207 | uint8_t status[MAX_USERSTATUS_LENGTH]; |
210 | int i = 0; | 208 | int i = 0; |
211 | size_t len = strlen(line); | 209 | size_t len = strlen(line); |
212 | for (i=3; i<len; i++) { | 210 | for (i = 3; i < len; i++) { |
213 | if (line[i] == 0 || line[i] == '\n') break; | 211 | if (line[i] == 0 || line[i] == '\n') break; |
214 | status[i - 3] = line[i]; | 212 | status[i-3] = line[i]; |
215 | } | 213 | } |
216 | status[i - 3] = 0; | 214 | status[i-3] = 0; |
217 | m_set_userstatus(status, strlen((char*)status)); | 215 | m_set_userstatus(status, strlen((char*)status)); |
218 | char numstring[100]; | 216 | char numstring[100]; |
219 | sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status); | 217 | sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status); |
220 | printf(numstring); | 218 | printf(numstring); |
221 | } | 219 | } |
222 | 220 | ||
223 | else if (line[1] == 'a') { | 221 | else if (inpt_command == 'a') { |
224 | uint8_t numf = atoi(line + 3); | 222 | uint8_t numf = atoi(line + 3); |
225 | char numchar[100]; | 223 | char numchar[100]; |
226 | sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf); | 224 | sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf); |
@@ -230,12 +228,10 @@ void line_eval(char* line) | |||
230 | printf(numchar); | 228 | printf(numchar); |
231 | } | 229 | } |
232 | /* EXIT */ | 230 | /* EXIT */ |
233 | else if (line[1] == 'q') { | 231 | else if (inpt_command == 'q') { |
234 | exit(EXIT_SUCCESS); | 232 | exit(EXIT_SUCCESS); |
235 | } | 233 | } |
236 | } | 234 | } else { |
237 | |||
238 | else { | ||
239 | //nothing atm | 235 | //nothing atm |
240 | } | 236 | } |
241 | } | 237 | } |
@@ -255,12 +251,10 @@ int main(int argc, char *argv[]) | |||
255 | printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); | 251 | printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); |
256 | exit(0); | 252 | exit(0); |
257 | } | 253 | } |
258 | |||
259 | if (initMessenger() == -1) { | 254 | if (initMessenger() == -1) { |
260 | printf("initMessenger failed"); | 255 | printf("initMessenger failed"); |
261 | exit(0); | 256 | exit(0); |
262 | } | 257 | } |
263 | |||
264 | if (argc > 4) { | 258 | if (argc > 4) { |
265 | if(strncmp(argv[4], "nokey", 6) < 0) { | 259 | if(strncmp(argv[4], "nokey", 6) < 0) { |
266 | } | 260 | } |
@@ -272,26 +266,25 @@ int main(int argc, char *argv[]) | |||
272 | m_callback_friendmessage(print_message); | 266 | m_callback_friendmessage(print_message); |
273 | m_callback_namechange(print_nickchange); | 267 | m_callback_namechange(print_nickchange); |
274 | m_callback_userstatus(print_statuschange); | 268 | m_callback_userstatus(print_statuschange); |
275 | 269 | char idstring1[PUB_KEY_BYTES][5]; | |
276 | char idstring1[32][5]; | 270 | char idstring2[PUB_KEY_BYTES][5]; |
277 | char idstring2[32][5]; | 271 | int i; |
278 | uint32_t i; | 272 | for(i = 0; i < PUB_KEY_BYTES; i++) |
279 | for(i = 0; i < 32; i++) | ||
280 | { | 273 | { |
281 | if(self_public_key[i] < 16) | 274 | if(self_public_key[i] < (PUB_KEY_BYTES/2)) |
282 | strcpy(idstring1[i],"0"); | 275 | strcpy(idstring1[i],"0"); |
283 | else | 276 | else |
284 | strcpy(idstring1[i], ""); | 277 | strcpy(idstring1[i], ""); |
285 | sprintf(idstring2[i], "%hhX",self_public_key[i]); | 278 | sprintf(idstring2[i], "%hhX",self_public_key[i]); |
286 | } | 279 | } |
287 | strcpy(users_id,"[i] your ID: "); | 280 | strcpy(users_id,"[i] your ID: "); |
288 | for (i=0; i<32; i++) { | 281 | int j; |
289 | strcat(users_id,idstring1[i]); | 282 | for (j = 0; j < PUB_KEY_BYTES; j++) { |
290 | strcat(users_id,idstring2[i]); | 283 | strcat(users_id,idstring1[j]); |
284 | strcat(users_id,idstring2[j]); | ||
291 | } | 285 | } |
292 | 286 | ||
293 | do_header(); | 287 | do_header(); |
294 | |||
295 | IP_Port bootstrap_ip_port; | 288 | IP_Port bootstrap_ip_port; |
296 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 289 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
297 | int resolved_address = resolve_addr(argv[1]); | 290 | int resolved_address = resolve_addr(argv[1]); |
@@ -301,12 +294,9 @@ int main(int argc, char *argv[]) | |||
301 | exit(1); | 294 | exit(1); |
302 | 295 | ||
303 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); | 296 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); |
304 | |||
305 | int c; | 297 | int c; |
306 | int on = 0; | 298 | int on = 0; |
307 | |||
308 | _beginthread(get_input, 0, NULL); | 299 | _beginthread(get_input, 0, NULL); |
309 | |||
310 | while(1) { | 300 | while(1) { |
311 | if (on == 1 && DHT_isconnected() == -1) { | 301 | if (on == 1 && DHT_isconnected() == -1) { |
312 | printf("\n---------------------------------"); | 302 | printf("\n---------------------------------"); |
@@ -314,16 +304,13 @@ int main(int argc, char *argv[]) | |||
314 | printf("\n---------------------------------\n\n"); | 304 | printf("\n---------------------------------\n\n"); |
315 | on = 0; | 305 | on = 0; |
316 | } | 306 | } |
317 | |||
318 | if (on == 0 && DHT_isconnected()) { | 307 | if (on == 0 && DHT_isconnected()) { |
319 | printf("\n[i] Connected to DHT"); | 308 | printf("\n[i] Connected to DHT"); |
320 | printf("\n---------------------------------\n\n"); | 309 | printf("\n---------------------------------\n\n"); |
321 | on = 1; | 310 | on = 1; |
322 | } | 311 | } |
323 | |||
324 | doMessenger(); | 312 | doMessenger(); |
325 | Sleep(1); | 313 | Sleep(1); |
326 | } | 314 | } |
327 | |||
328 | return 0; | 315 | return 0; |
329 | } \ No newline at end of file | 316 | } \ No newline at end of file |
diff --git a/testing/nTox_win32.h b/testing/nTox_win32.h index dacabc74..374348a4 100644 --- a/testing/nTox_win32.h +++ b/testing/nTox_win32.h | |||
@@ -27,6 +27,7 @@ | |||
27 | #include "../core/network.h" | 27 | #include "../core/network.h" |
28 | 28 | ||
29 | #define STRING_LENGTH 256 | 29 | #define STRING_LENGTH 256 |
30 | #define PUB_KEY_BYTES 32 | ||
30 | 31 | ||
31 | void do_header(); | 32 | void do_header(); |
32 | void print_message(int friendnumber, uint8_t * string, uint16_t length); | 33 | void print_message(int friendnumber, uint8_t * string, uint16_t length); |
diff --git a/testing/cmake/toxic.cmake b/testing/toxic/CMakeLists.txt index 1ffab788..f30d8e9e 100644 --- a/testing/cmake/toxic.cmake +++ b/testing/toxic/CMakeLists.txt | |||
@@ -4,8 +4,12 @@ project(toxic C) | |||
4 | set(exe_name toxic) | 4 | set(exe_name toxic) |
5 | 5 | ||
6 | add_executable(${exe_name} | 6 | add_executable(${exe_name} |
7 | toxic/main.c toxic/prompt.c) | 7 | main.c |
8 | 8 | prompt.c | |
9 | target_link_libraries(${exe_name} curses) | 9 | friendlist.c |
10 | chat.c) | ||
11 | |||
12 | target_link_libraries(${exe_name} | ||
13 | curses) | ||
10 | 14 | ||
11 | linkCoreLibraries(${exe_name}) | 15 | linkCoreLibraries(${exe_name}) |
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c new file mode 100644 index 00000000..dceb1d7b --- /dev/null +++ b/testing/toxic/chat.c | |||
@@ -0,0 +1,155 @@ | |||
1 | /* | ||
2 | * Toxic -- Tox Curses Client | ||
3 | */ | ||
4 | |||
5 | #include <curses.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <string.h> | ||
8 | #include <stdint.h> | ||
9 | #include <ctype.h> | ||
10 | |||
11 | #include "../../core/Messenger.h" | ||
12 | #include "../../core/network.h" | ||
13 | |||
14 | #include "windows.h" | ||
15 | |||
16 | typedef struct { | ||
17 | int friendnum; | ||
18 | |||
19 | char line[256]; | ||
20 | size_t pos; | ||
21 | |||
22 | WINDOW* history; | ||
23 | WINDOW* linewin; | ||
24 | |||
25 | } ChatContext; | ||
26 | |||
27 | extern void fix_name(uint8_t* name); | ||
28 | |||
29 | |||
30 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { | ||
31 | ChatContext* ctx = (ChatContext*) self->x; | ||
32 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | ||
33 | |||
34 | if(ctx->friendnum != num) | ||
35 | return; | ||
36 | |||
37 | getname(num, (uint8_t*) &nick); | ||
38 | |||
39 | msg[len-1] = '\0'; | ||
40 | nick[MAX_NAME_LENGTH-1] = '\0'; | ||
41 | |||
42 | fix_name(msg); | ||
43 | fix_name(nick); | ||
44 | |||
45 | wattron(ctx->history, COLOR_PAIR(4)); | ||
46 | wprintw(ctx->history, "%s: ", nick); | ||
47 | wattroff(ctx->history, COLOR_PAIR(4)); | ||
48 | |||
49 | wprintw(ctx->history, "%s\n", msg); | ||
50 | |||
51 | self->blink = true; | ||
52 | } | ||
53 | |||
54 | static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) { | ||
55 | ChatContext* ctx = (ChatContext*) self->x; | ||
56 | |||
57 | if(ctx->friendnum != num) | ||
58 | return; | ||
59 | |||
60 | nick[len-1] = '\0'; | ||
61 | fix_name(nick); | ||
62 | |||
63 | wattron(ctx->history, COLOR_PAIR(3)); | ||
64 | wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick); | ||
65 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
66 | } | ||
67 | |||
68 | static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) { | ||
69 | |||
70 | } | ||
71 | |||
72 | static void chat_onKey(ToxWindow* self, int key) { | ||
73 | ChatContext* ctx = (ChatContext*) self->x; | ||
74 | |||
75 | if(isprint(key)) { | ||
76 | |||
77 | if(ctx->pos != sizeof(ctx->line)-1) { | ||
78 | ctx->line[ctx->pos++] = key; | ||
79 | ctx->line[ctx->pos] = '\0'; | ||
80 | } | ||
81 | } | ||
82 | else if(key == '\n') { | ||
83 | wattron(ctx->history, COLOR_PAIR(1)); | ||
84 | wprintw(ctx->history, "you: ", ctx->line); | ||
85 | wattroff(ctx->history, COLOR_PAIR(1)); | ||
86 | |||
87 | wprintw(ctx->history, "%s\n", ctx->line); | ||
88 | |||
89 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | ||
90 | wattron(ctx->history, COLOR_PAIR(3)); | ||
91 | wprintw(ctx->history, " * Failed to send message.\n"); | ||
92 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
93 | } | ||
94 | |||
95 | ctx->line[0] = '\0'; | ||
96 | ctx->pos = 0; | ||
97 | } | ||
98 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | ||
99 | if(ctx->pos != 0) { | ||
100 | ctx->line[--ctx->pos] = '\0'; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | } | ||
105 | |||
106 | static void chat_onDraw(ToxWindow* self) { | ||
107 | int x, y; | ||
108 | ChatContext* ctx = (ChatContext*) self->x; | ||
109 | |||
110 | getmaxyx(self->window, y, x); | ||
111 | |||
112 | (void) x; | ||
113 | if(y < 3) | ||
114 | return; | ||
115 | |||
116 | wclear(ctx->linewin); | ||
117 | mvwhline(ctx->linewin, 0, 0, '_', COLS); | ||
118 | mvwprintw(ctx->linewin, 1, 0, "%s\n", ctx->line); | ||
119 | |||
120 | wrefresh(self->window); | ||
121 | } | ||
122 | |||
123 | static void chat_onInit(ToxWindow* self) { | ||
124 | int x, y; | ||
125 | ChatContext* ctx = (ChatContext*) self->x; | ||
126 | |||
127 | getmaxyx(self->window, y, x); | ||
128 | |||
129 | ctx->history = subwin(self->window, y - 4, x, 0, 0); | ||
130 | scrollok(ctx->history, 1); | ||
131 | |||
132 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); | ||
133 | } | ||
134 | |||
135 | ToxWindow new_chat(int friendnum) { | ||
136 | ToxWindow ret; | ||
137 | |||
138 | memset(&ret, 0, sizeof(ret)); | ||
139 | |||
140 | ret.onKey = &chat_onKey; | ||
141 | ret.onDraw = &chat_onDraw; | ||
142 | ret.onInit = &chat_onInit; | ||
143 | ret.onMessage = &chat_onMessage; | ||
144 | ret.onNickChange = &chat_onNickChange; | ||
145 | ret.onStatusChange = &chat_onStatusChange; | ||
146 | |||
147 | snprintf(ret.title, sizeof(ret.title), "[chat %d]", friendnum); | ||
148 | |||
149 | ChatContext* x = calloc(1, sizeof(ChatContext)); | ||
150 | x->friendnum = friendnum; | ||
151 | |||
152 | ret.x = (void*) x; | ||
153 | |||
154 | return ret; | ||
155 | } | ||
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c new file mode 100644 index 00000000..f9a413f9 --- /dev/null +++ b/testing/toxic/friendlist.c | |||
@@ -0,0 +1,166 @@ | |||
1 | /* | ||
2 | * Toxic -- Tox Curses Client | ||
3 | */ | ||
4 | |||
5 | #include <curses.h> | ||
6 | #include <string.h> | ||
7 | #include <stdint.h> | ||
8 | #include <ctype.h> | ||
9 | |||
10 | #include "../../core/Messenger.h" | ||
11 | #include "../../core/network.h" | ||
12 | |||
13 | #include "windows.h" | ||
14 | |||
15 | extern int add_window(ToxWindow w); | ||
16 | extern int focus_window(int num); | ||
17 | extern ToxWindow new_chat(int friendnum); | ||
18 | |||
19 | #define MAX_FRIENDS_NUM 100 | ||
20 | |||
21 | typedef struct { | ||
22 | uint8_t name[MAX_NAME_LENGTH]; | ||
23 | uint8_t status[MAX_USERSTATUS_LENGTH]; | ||
24 | int num; | ||
25 | int chatwin; | ||
26 | } friend_t; | ||
27 | |||
28 | static friend_t friends[MAX_FRIENDS_NUM]; | ||
29 | static int num_friends = 0; | ||
30 | static int num_selected = 0; | ||
31 | |||
32 | |||
33 | void fix_name(uint8_t* name) { | ||
34 | |||
35 | // Remove all non alphanumeric characters. | ||
36 | uint8_t* p = name; | ||
37 | uint8_t* q = name; | ||
38 | |||
39 | while(*p != 0) { | ||
40 | if(isprint(*p)) { | ||
41 | *q++ = *p; | ||
42 | } | ||
43 | |||
44 | p++; | ||
45 | } | ||
46 | |||
47 | *q = 0; | ||
48 | } | ||
49 | |||
50 | void friendlist_onMessage(ToxWindow* self, int num, uint8_t* str, uint16_t len) { | ||
51 | |||
52 | if(num >= num_friends) | ||
53 | return; | ||
54 | |||
55 | if(friends[num].chatwin == -1) { | ||
56 | friends[num].chatwin = add_window(new_chat(num)); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) { | ||
61 | |||
62 | if(len >= MAX_NAME_LENGTH || num >= num_friends) | ||
63 | return; | ||
64 | |||
65 | memcpy((char*) &friends[num].name, (char*) str, len); | ||
66 | friends[num].name[len] = 0; | ||
67 | fix_name(friends[num].name); | ||
68 | } | ||
69 | |||
70 | void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) { | ||
71 | |||
72 | if(len >= MAX_USERSTATUS_LENGTH || num >= num_friends) | ||
73 | return; | ||
74 | |||
75 | memcpy((char*) &friends[num].status, (char*) str, len); | ||
76 | friends[num].status[len] = 0; | ||
77 | fix_name(friends[num].status); | ||
78 | } | ||
79 | |||
80 | int friendlist_onFriendAdded(int num) { | ||
81 | |||
82 | if(num_friends == MAX_FRIENDS_NUM) | ||
83 | return -1; | ||
84 | |||
85 | friends[num_friends].num = num; | ||
86 | getname(num, friends[num_friends].name); | ||
87 | strcpy((char*) friends[num_friends].name, "unknown"); | ||
88 | strcpy((char*) friends[num_friends].status, "unknown"); | ||
89 | friends[num_friends].chatwin = -1; | ||
90 | |||
91 | num_friends++; | ||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | static void friendlist_onKey(ToxWindow* self, int key) { | ||
96 | |||
97 | if(key == KEY_UP) { | ||
98 | if(num_selected != 0) | ||
99 | num_selected--; | ||
100 | } | ||
101 | else if(key == KEY_DOWN) { | ||
102 | if(num_friends != 0) | ||
103 | num_selected = (num_selected+1) % num_friends; | ||
104 | } | ||
105 | else if(key == '\n') { | ||
106 | |||
107 | if(friends[num_selected].chatwin != -1) | ||
108 | return; | ||
109 | |||
110 | friends[num_selected].chatwin = add_window(new_chat(num_selected)); | ||
111 | focus_window(friends[num_selected].chatwin); | ||
112 | } | ||
113 | } | ||
114 | |||
115 | static void friendlist_onDraw(ToxWindow* self) { | ||
116 | size_t i; | ||
117 | |||
118 | wclear(self->window); | ||
119 | |||
120 | if(num_friends == 0) { | ||
121 | wprintw(self->window, "Empty. Add some friends! :-)\n"); | ||
122 | } | ||
123 | else { | ||
124 | wattron(self->window, COLOR_PAIR(2) | A_BOLD); | ||
125 | wprintw(self->window, "Open chat with.. (up/down keys, enter)\n"); | ||
126 | wattroff(self->window, COLOR_PAIR(2) | A_BOLD); | ||
127 | } | ||
128 | |||
129 | wprintw(self->window, "\n"); | ||
130 | |||
131 | for(i=0; i<num_friends; i++) { | ||
132 | |||
133 | if(i == num_selected) wattron(self->window, COLOR_PAIR(3)); | ||
134 | wprintw(self->window, " [#%d] ", friends[i].num); | ||
135 | if(i == num_selected) wattroff(self->window, COLOR_PAIR(3)); | ||
136 | |||
137 | attron(A_BOLD); | ||
138 | wprintw(self->window, "%s ", friends[i].name); | ||
139 | attroff(A_BOLD); | ||
140 | |||
141 | wprintw(self->window, "(%s)\n", friends[i].status); | ||
142 | } | ||
143 | |||
144 | wrefresh(self->window); | ||
145 | } | ||
146 | |||
147 | static void friendlist_onInit(ToxWindow* self) { | ||
148 | |||
149 | } | ||
150 | |||
151 | |||
152 | ToxWindow new_friendlist() { | ||
153 | ToxWindow ret; | ||
154 | |||
155 | memset(&ret, 0, sizeof(ret)); | ||
156 | |||
157 | ret.onKey = &friendlist_onKey; | ||
158 | ret.onDraw = &friendlist_onDraw; | ||
159 | ret.onInit = &friendlist_onInit; | ||
160 | ret.onMessage = &friendlist_onMessage; | ||
161 | ret.onNickChange = &friendlist_onNickChange; | ||
162 | ret.onStatusChange = &friendlist_onStatusChange; | ||
163 | strcpy(ret.title, "[friends]"); | ||
164 | |||
165 | return ret; | ||
166 | } | ||
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 0aad6777..fffc3f84 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -14,6 +14,10 @@ | |||
14 | #include "windows.h" | 14 | #include "windows.h" |
15 | 15 | ||
16 | extern ToxWindow new_prompt(); | 16 | extern ToxWindow new_prompt(); |
17 | extern ToxWindow new_friendlist(); | ||
18 | |||
19 | extern int friendlist_onFriendAdded(int num); | ||
20 | |||
17 | extern int add_req(uint8_t* public_key); // XXX | 21 | extern int add_req(uint8_t* public_key); // XXX |
18 | 22 | ||
19 | #define TOXWINDOWS_MAX_NUM 32 | 23 | #define TOXWINDOWS_MAX_NUM 32 |
@@ -25,21 +29,59 @@ static ToxWindow* prompt; | |||
25 | 29 | ||
26 | // CALLBACKS START | 30 | // CALLBACKS START |
27 | void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { | 31 | void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { |
32 | size_t i; | ||
28 | int n = add_req(public_key); | 33 | int n = add_req(public_key); |
29 | 34 | ||
30 | wprintw(prompt->window, "\nFriend request.\nUse \"accept %d\" to accept it.\n", n); | 35 | wprintw(prompt->window, "\nFriend request from:\n"); |
36 | |||
37 | for(i=0; i<32; i++) { | ||
38 | wprintw(prompt->window, "%02x", public_key[i] & 0xff); | ||
39 | } | ||
40 | wprintw(prompt->window, "\n"); | ||
41 | |||
42 | wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); | ||
43 | |||
44 | for(i=0; i<w_num; i++) { | ||
45 | if(windows[i].onFriendRequest != NULL) | ||
46 | windows[i].onFriendRequest(&windows[i], public_key, data, length); | ||
47 | } | ||
31 | } | 48 | } |
32 | 49 | ||
33 | void on_message(int friendnumber, uint8_t* string, uint16_t length) { | 50 | void on_message(int friendnumber, uint8_t* string, uint16_t length) { |
51 | size_t i; | ||
52 | |||
34 | wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string); | 53 | wprintw(prompt->window, "\n(message) %d: %s!\n", friendnumber, string); |
54 | |||
55 | for(i=0; i<w_num; i++) { | ||
56 | if(windows[i].onMessage != NULL) | ||
57 | windows[i].onMessage(&windows[i], friendnumber, string, length); | ||
58 | } | ||
35 | } | 59 | } |
36 | 60 | ||
37 | void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { | 61 | void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { |
38 | wprintw(prompt->window, "\n(nick) %d: %s!\n", friendnumber, string); | 62 | size_t i; |
63 | |||
64 | wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); | ||
65 | |||
66 | for(i=0; i<w_num; i++) { | ||
67 | if(windows[i].onNickChange != NULL) | ||
68 | windows[i].onNickChange(&windows[i], friendnumber, string, length); | ||
69 | } | ||
39 | } | 70 | } |
40 | 71 | ||
41 | void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { | 72 | void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { |
42 | wprintw(prompt->window, "\n(status) %d: %s!\n", friendnumber, string); | 73 | size_t i; |
74 | |||
75 | wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string); | ||
76 | |||
77 | for(i=0; i<w_num; i++) { | ||
78 | if(windows[i].onStatusChange != NULL) | ||
79 | windows[i].onStatusChange(&windows[i], friendnumber, string, length); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | void on_friendadded(int friendnumber) { | ||
84 | friendlist_onFriendAdded(friendnumber); | ||
43 | } | 85 | } |
44 | // CALLBACKS END | 86 | // CALLBACKS END |
45 | 87 | ||
@@ -73,7 +115,7 @@ static void init_tox() { | |||
73 | m_callback_userstatus(on_statuschange); | 115 | m_callback_userstatus(on_statuschange); |
74 | } | 116 | } |
75 | 117 | ||
76 | static int add_window(ToxWindow w) { | 118 | int add_window(ToxWindow w) { |
77 | if(w_num == TOXWINDOWS_MAX_NUM) | 119 | if(w_num == TOXWINDOWS_MAX_NUM) |
78 | return -1; | 120 | return -1; |
79 | 121 | ||
@@ -88,14 +130,22 @@ static int add_window(ToxWindow w) { | |||
88 | windows[w_num++] = w; | 130 | windows[w_num++] = w; |
89 | w.onInit(&w); | 131 | w.onInit(&w); |
90 | 132 | ||
91 | return w_num; | 133 | return w_num - 1; |
134 | } | ||
135 | |||
136 | int focus_window(int num) { | ||
137 | if(num >= w_num || num < 0) | ||
138 | return -1; | ||
139 | |||
140 | w_active = num; | ||
141 | return 0; | ||
92 | } | 142 | } |
93 | 143 | ||
94 | static void init_windows() { | 144 | static void init_windows() { |
95 | w_num = 0; | 145 | w_num = 0; |
96 | w_active = 0; | 146 | w_active = 0; |
97 | 147 | ||
98 | if(add_window(new_prompt()) == -1) { | 148 | if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) { |
99 | fprintf(stderr, "add_window() failed.\n"); | 149 | fprintf(stderr, "add_window() failed.\n"); |
100 | 150 | ||
101 | endwin(); | 151 | endwin(); |
@@ -134,14 +184,18 @@ static void load_data() { | |||
134 | 184 | ||
135 | if(buf == NULL) { | 185 | if(buf == NULL) { |
136 | fprintf(stderr, "malloc() failed.\n"); | 186 | fprintf(stderr, "malloc() failed.\n"); |
187 | |||
137 | fclose(fd); | 188 | fclose(fd); |
189 | endwin(); | ||
138 | exit(1); | 190 | exit(1); |
139 | } | 191 | } |
140 | 192 | ||
141 | if(fread(buf, len, 1, fd) != 1){ | 193 | if(fread(buf, len, 1, fd) != 1){ |
142 | fprintf(stderr, "fread() failed.\n"); | 194 | fprintf(stderr, "fread() failed.\n"); |
195 | |||
143 | free(buf); | 196 | free(buf); |
144 | fclose(fd); | 197 | fclose(fd); |
198 | endwin(); | ||
145 | exit(1); | 199 | exit(1); |
146 | } | 200 | } |
147 | 201 | ||
@@ -153,6 +207,7 @@ static void load_data() { | |||
153 | 207 | ||
154 | if(buf == NULL) { | 208 | if(buf == NULL) { |
155 | fprintf(stderr, "malloc() failed.\n"); | 209 | fprintf(stderr, "malloc() failed.\n"); |
210 | endwin(); | ||
156 | exit(1); | 211 | exit(1); |
157 | } | 212 | } |
158 | 213 | ||
@@ -161,14 +216,18 @@ static void load_data() { | |||
161 | fd = fopen("data", "w"); | 216 | fd = fopen("data", "w"); |
162 | if(fd == NULL) { | 217 | if(fd == NULL) { |
163 | fprintf(stderr, "fopen() failed.\n"); | 218 | fprintf(stderr, "fopen() failed.\n"); |
219 | |||
164 | free(buf); | 220 | free(buf); |
221 | endwin(); | ||
165 | exit(1); | 222 | exit(1); |
166 | } | 223 | } |
167 | 224 | ||
168 | if(fwrite(buf, len, 1, fd) != 1){ | 225 | if(fwrite(buf, len, 1, fd) != 1){ |
169 | fprintf(stderr, "fwrite() failed.\n"); | 226 | fprintf(stderr, "fwrite() failed.\n"); |
227 | |||
170 | free(buf); | 228 | free(buf); |
171 | fclose(fd); | 229 | fclose(fd); |
230 | endwin(); | ||
172 | exit(1); | 231 | exit(1); |
173 | } | 232 | } |
174 | } | 233 | } |
@@ -178,6 +237,7 @@ static void load_data() { | |||
178 | } | 237 | } |
179 | 238 | ||
180 | static void draw_bar() { | 239 | static void draw_bar() { |
240 | static int odd = 0; | ||
181 | size_t i; | 241 | size_t i; |
182 | 242 | ||
183 | attron(COLOR_PAIR(4)); | 243 | attron(COLOR_PAIR(4)); |
@@ -186,12 +246,26 @@ static void draw_bar() { | |||
186 | 246 | ||
187 | move(LINES - 1, 0); | 247 | move(LINES - 1, 0); |
188 | 248 | ||
249 | attron(COLOR_PAIR(4) | A_BOLD); | ||
250 | printw(" TOXIC 1.0 |"); | ||
251 | attroff(COLOR_PAIR(4) | A_BOLD); | ||
252 | |||
189 | for(i=0; i<w_num; i++) { | 253 | for(i=0; i<w_num; i++) { |
190 | if(i == w_active) { | 254 | if(i == w_active) { |
191 | attron(A_BOLD); | 255 | attron(A_BOLD); |
192 | } | 256 | } |
193 | 257 | ||
194 | printw(" %s ", windows[i].title); | 258 | odd = (odd+1) % 2; |
259 | |||
260 | if(windows[i].blink && odd) { | ||
261 | attron(COLOR_PAIR(3)); | ||
262 | } | ||
263 | |||
264 | printw(" %s", windows[i].title); | ||
265 | |||
266 | if(windows[i].blink && odd) { | ||
267 | attron(COLOR_PAIR(3)); | ||
268 | } | ||
195 | 269 | ||
196 | if(i == w_active) { | 270 | if(i == w_active) { |
197 | attroff(A_BOLD); | 271 | attroff(A_BOLD); |
@@ -201,6 +275,11 @@ static void draw_bar() { | |||
201 | refresh(); | 275 | refresh(); |
202 | } | 276 | } |
203 | 277 | ||
278 | void prepare_window(WINDOW* w) { | ||
279 | mvwin(w, 0, 0); | ||
280 | wresize(w, LINES-2, COLS); | ||
281 | } | ||
282 | |||
204 | int main(int argc, char* argv[]) { | 283 | int main(int argc, char* argv[]) { |
205 | int ch; | 284 | int ch; |
206 | ToxWindow* a; | 285 | ToxWindow* a; |
@@ -211,14 +290,22 @@ int main(int argc, char* argv[]) { | |||
211 | init_windows(); | 290 | init_windows(); |
212 | 291 | ||
213 | while(true) { | 292 | while(true) { |
293 | // Update tox. | ||
214 | do_tox(); | 294 | do_tox(); |
215 | 295 | ||
296 | // Draw. | ||
216 | a = &windows[w_active]; | 297 | a = &windows[w_active]; |
298 | prepare_window(a->window); | ||
299 | a->blink = false; | ||
217 | a->onDraw(a); | 300 | a->onDraw(a); |
218 | draw_bar(); | 301 | draw_bar(); |
219 | 302 | ||
303 | // Handle input. | ||
220 | ch = getch(); | 304 | ch = getch(); |
221 | if(ch != ERR) { | 305 | if(ch == '\t') { |
306 | w_active = (w_active + 1) % w_num; | ||
307 | } | ||
308 | else if(ch != ERR) { | ||
222 | a->onKey(a, ch); | 309 | a->onKey(a, ch); |
223 | } | 310 | } |
224 | 311 | ||
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 4a59cc7b..f1b6e689 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c | |||
@@ -15,6 +15,8 @@ | |||
15 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX | 15 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX |
16 | uint8_t num_requests=0; // XXX | 16 | uint8_t num_requests=0; // XXX |
17 | 17 | ||
18 | extern void on_friendadded(int friendnumber); | ||
19 | |||
18 | // XXX: | 20 | // XXX: |
19 | int add_req(uint8_t* public_key) { | 21 | int add_req(uint8_t* public_key) { |
20 | memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); | 22 | memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); |
@@ -40,8 +42,7 @@ static int prompt_buf_pos=0; | |||
40 | 42 | ||
41 | static void execute(ToxWindow* self, char* cmd) { | 43 | static void execute(ToxWindow* self, char* cmd) { |
42 | 44 | ||
43 | // quit/exit: Exit program. | 45 | if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) { |
44 | if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) { | ||
45 | endwin(); | 46 | endwin(); |
46 | exit(0); | 47 | exit(0); |
47 | } | 48 | } |
@@ -53,33 +54,32 @@ static void execute(ToxWindow* self, char* cmd) { | |||
53 | 54 | ||
54 | ip = strchr(cmd, ' '); | 55 | ip = strchr(cmd, ' '); |
55 | if(ip == NULL) { | 56 | if(ip == NULL) { |
57 | wprintw(self->window, "Invalid syntax.\n"); | ||
56 | return; | 58 | return; |
57 | } | 59 | } |
58 | |||
59 | ip++; | 60 | ip++; |
60 | 61 | ||
61 | port = strchr(ip, ' '); | 62 | port = strchr(ip, ' '); |
62 | if(port == NULL) { | 63 | if(port == NULL) { |
64 | wprintw(self->window, "Invalid syntax.\n"); | ||
63 | return; | 65 | return; |
64 | } | 66 | } |
65 | |||
66 | port[0] = 0; | 67 | port[0] = 0; |
67 | port++; | 68 | port++; |
68 | 69 | ||
69 | key = strchr(port, ' '); | 70 | key = strchr(port, ' '); |
70 | if(key == NULL) { | 71 | if(key == NULL) { |
72 | wprintw(self->window, "Invalid syntax.\n"); | ||
71 | return; | 73 | return; |
72 | } | 74 | } |
73 | |||
74 | key[0] = 0; | 75 | key[0] = 0; |
75 | key++; | 76 | key++; |
76 | 77 | ||
77 | if(atoi(port) == 0) { | 78 | if(atoi(port) == 0) { |
79 | wprintw(self->window, "Invalid syntax.\n"); | ||
78 | return; | 80 | return; |
79 | } | 81 | } |
80 | 82 | ||
81 | wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key); | ||
82 | |||
83 | dht.port = htons(atoi(port)); | 83 | dht.port = htons(atoi(port)); |
84 | 84 | ||
85 | int resolved_address = resolve_addr(ip); | 85 | int resolved_address = resolve_addr(ip); |
@@ -91,38 +91,62 @@ static void execute(ToxWindow* self, char* cmd) { | |||
91 | DHT_bootstrap(dht, hex_string_to_bin(key)); | 91 | DHT_bootstrap(dht, hex_string_to_bin(key)); |
92 | } | 92 | } |
93 | else if(!strncmp(cmd, "add ", strlen("add "))) { | 93 | else if(!strncmp(cmd, "add ", strlen("add "))) { |
94 | uint8_t id_bin[32]; | ||
95 | size_t i; | ||
96 | char xx[3]; | ||
97 | uint32_t x; | ||
98 | |||
94 | char* id; | 99 | char* id; |
95 | char* msg; | 100 | char* msg; |
96 | int num; | 101 | int num; |
97 | 102 | ||
98 | id = strchr(cmd, ' '); | 103 | id = strchr(cmd, ' '); |
99 | |||
100 | if(id == NULL) { | 104 | if(id == NULL) { |
105 | wprintw(self->window, "Invalid syntax.\n"); | ||
101 | return; | 106 | return; |
102 | } | 107 | } |
103 | |||
104 | id++; | 108 | id++; |
105 | 109 | ||
106 | msg = strchr(id, ' '); | 110 | msg = strchr(id, ' '); |
107 | if(msg == NULL) { | 111 | if(msg != NULL) { |
112 | msg[0] = 0; | ||
113 | msg++; | ||
114 | } | ||
115 | else msg = ""; | ||
116 | |||
117 | if(strlen(id) != 2*32) { | ||
118 | wprintw(self->window, "Invalid ID length.\n"); | ||
108 | return; | 119 | return; |
109 | } | 120 | } |
110 | 121 | ||
111 | msg[0] = 0; | 122 | for(i=0; i<32; i++) { |
112 | msg++; | 123 | xx[0] = id[2*i]; |
124 | xx[1] = id[2*i+1]; | ||
125 | xx[2] = '\0'; | ||
126 | |||
127 | if(sscanf(xx, "%02x", &x) != 1) { | ||
128 | wprintw(self->window, "Invalid ID.\n"); | ||
129 | return; | ||
130 | } | ||
131 | |||
132 | id_bin[i] = x; | ||
133 | } | ||
134 | |||
135 | num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1); | ||
113 | 136 | ||
114 | num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1); | ||
115 | wprintw(self->window, "Friend added as %d.\n", num); | 137 | wprintw(self->window, "Friend added as %d.\n", num); |
138 | on_friendadded(num); | ||
116 | } | 139 | } |
117 | else if(!strncmp(cmd, "status ", strlen("status "))) { | 140 | else if(!strncmp(cmd, "status ", strlen("status "))) { |
118 | char* msg; | 141 | char* msg; |
119 | 142 | ||
120 | msg = strchr(cmd, ' '); | 143 | msg = strchr(cmd, ' '); |
121 | if(msg == NULL) { | 144 | if(msg == NULL) { |
145 | wprintw(self->window, "Invalid syntax.\n"); | ||
122 | return; | 146 | return; |
123 | } | 147 | } |
124 | |||
125 | msg++; | 148 | msg++; |
149 | |||
126 | m_set_userstatus((uint8_t*) msg, strlen(msg)+1); | 150 | m_set_userstatus((uint8_t*) msg, strlen(msg)+1); |
127 | wprintw(self->window, "Status set to: %s.\n", msg); | 151 | wprintw(self->window, "Status set to: %s.\n", msg); |
128 | } | 152 | } |
@@ -133,33 +157,22 @@ static void execute(ToxWindow* self, char* cmd) { | |||
133 | if(nick == NULL) { | 157 | if(nick == NULL) { |
134 | return; | 158 | return; |
135 | } | 159 | } |
136 | |||
137 | nick++; | 160 | nick++; |
161 | |||
138 | setname((uint8_t*) nick, strlen(nick)+1); | 162 | setname((uint8_t*) nick, strlen(nick)+1); |
139 | wprintw(self->window, "Nickname set to: %s.\n", nick); | 163 | wprintw(self->window, "Nickname set to: %s.\n", nick); |
140 | } | 164 | } |
141 | else if(!strcmp(cmd, "myid")) { | 165 | else if(!strcmp(cmd, "myid")) { |
142 | // XXX: Clean this up | 166 | char id[32*2 + 1] = {0}; |
143 | char idstring0[200]; | 167 | size_t i; |
144 | char idstring1[32][5]; | 168 | |
145 | char idstring2[32][5]; | 169 | for(i=0; i<32; i++) { |
146 | uint32_t i; | 170 | char xx[3]; |
147 | 171 | snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); | |
148 | for(i = 0; i < 32; i++) { | 172 | strcat(id, xx); |
149 | if(self_public_key[i] < 16) | ||
150 | strcpy(idstring1[i], "0"); | ||
151 | else | ||
152 | strcpy(idstring1[i], ""); | ||
153 | |||
154 | sprintf(idstring2[i], "%hhX", self_public_key[i]); | ||
155 | } | 173 | } |
156 | 174 | ||
157 | for (i=0; i<32; i++) { | 175 | wprintw(self->window, "%s\n", id); |
158 | strcat(idstring0, idstring1[i]); | ||
159 | strcat(idstring0, idstring2[i]); | ||
160 | } | ||
161 | |||
162 | wprintw(self->window, "%s\n", idstring0); | ||
163 | } | 176 | } |
164 | else if(!strncmp(cmd, "accept ", strlen("accept "))) { | 177 | else if(!strncmp(cmd, "accept ", strlen("accept "))) { |
165 | char* id; | 178 | char* id; |
@@ -167,17 +180,26 @@ static void execute(ToxWindow* self, char* cmd) { | |||
167 | 180 | ||
168 | id = strchr(cmd, ' '); | 181 | id = strchr(cmd, ' '); |
169 | if(id == NULL) { | 182 | if(id == NULL) { |
183 | wprintw(self->window, "Invalid syntax.\n"); | ||
170 | return; | 184 | return; |
171 | } | 185 | } |
172 | id++; | 186 | id++; |
173 | 187 | ||
174 | num = atoi(id); | 188 | num = atoi(id); |
175 | if(num >= num_requests) { | 189 | if(num >= num_requests) { |
190 | wprintw(self->window, "Invalid syntax.\n"); | ||
176 | return; | 191 | return; |
177 | } | 192 | } |
178 | 193 | ||
179 | num = m_addfriend_norequest(pending_requests[num]); | 194 | num = m_addfriend_norequest(pending_requests[num]); |
180 | wprintw(self->window, "Friend accepted as: %d.\n", num); | 195 | |
196 | if(num == -1) { | ||
197 | wprintw(self->window, "Failed to add friend.\n"); | ||
198 | } | ||
199 | else { | ||
200 | wprintw(self->window, "Friend accepted as: %d.\n", num); | ||
201 | on_friendadded(num); | ||
202 | } | ||
181 | } | 203 | } |
182 | else if(!strncmp(cmd, "msg ", strlen("msg "))) { | 204 | else if(!strncmp(cmd, "msg ", strlen("msg "))) { |
183 | char* id; | 205 | char* id; |
@@ -186,26 +208,29 @@ static void execute(ToxWindow* self, char* cmd) { | |||
186 | id = strchr(cmd, ' '); | 208 | id = strchr(cmd, ' '); |
187 | 209 | ||
188 | if(id == NULL) { | 210 | if(id == NULL) { |
211 | wprintw(self->window, "Invalid syntax.\n"); | ||
189 | return; | 212 | return; |
190 | } | 213 | } |
191 | |||
192 | id++; | 214 | id++; |
193 | 215 | ||
194 | msg = strchr(id, ' '); | 216 | msg = strchr(id, ' '); |
195 | if(msg == NULL) { | 217 | if(msg == NULL) { |
218 | wprintw(self->window, "Invalid syntax.\n"); | ||
196 | return; | 219 | return; |
197 | } | 220 | } |
198 | |||
199 | msg[0] = 0; | 221 | msg[0] = 0; |
200 | msg++; | 222 | msg++; |
201 | 223 | ||
202 | if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) { | 224 | if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) { |
203 | wprintw(self->window, "Error occurred while sending message.\n"); | 225 | wprintw(self->window, "Error occurred while sending message.\n"); |
204 | } | 226 | } |
205 | else { | 227 | else { |
206 | wprintw(self->window, "Message successfully sent.\n"); | 228 | wprintw(self->window, "Message successfully sent.\n"); |
207 | } | 229 | } |
208 | } | 230 | } |
231 | else { | ||
232 | wprintw(self->window, "Invalid syntax.\n"); | ||
233 | } | ||
209 | } | 234 | } |
210 | 235 | ||
211 | static void prompt_onKey(ToxWindow* self, int key) { | 236 | static void prompt_onKey(ToxWindow* self, int key) { |
@@ -231,7 +256,7 @@ static void prompt_onKey(ToxWindow* self, int key) { | |||
231 | } | 256 | } |
232 | 257 | ||
233 | // BACKSPACE key: Remove one character from line. | 258 | // BACKSPACE key: Remove one character from line. |
234 | else if(key == 0x107) { | 259 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { |
235 | 260 | ||
236 | if(prompt_buf_pos != 0) { | 261 | if(prompt_buf_pos != 0) { |
237 | prompt_buf[--prompt_buf_pos] = 0; | 262 | prompt_buf[--prompt_buf_pos] = 0; |
@@ -242,9 +267,6 @@ static void prompt_onKey(ToxWindow* self, int key) { | |||
242 | static void prompt_onDraw(ToxWindow* self) { | 267 | static void prompt_onDraw(ToxWindow* self) { |
243 | int x, y; | 268 | int x, y; |
244 | 269 | ||
245 | mvwin(self->window,0,0); | ||
246 | wresize(self->window, LINES-2, COLS); | ||
247 | |||
248 | getyx(self->window, y, x); | 270 | getyx(self->window, y, x); |
249 | (void) x; | 271 | (void) x; |
250 | 272 | ||
@@ -260,7 +282,7 @@ static void prompt_onDraw(ToxWindow* self) { | |||
260 | 282 | ||
261 | static void print_usage(ToxWindow* self) { | 283 | static void print_usage(ToxWindow* self) { |
262 | wattron(self->window, COLOR_PAIR(2) | A_BOLD); | 284 | wattron(self->window, COLOR_PAIR(2) | A_BOLD); |
263 | wprintw(self->window, "Usage:\n"); | 285 | wprintw(self->window, "Commands:\n"); |
264 | wattroff(self->window, A_BOLD); | 286 | wattroff(self->window, A_BOLD); |
265 | 287 | ||
266 | wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); | 288 | wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); |
@@ -270,6 +292,12 @@ static void print_usage(ToxWindow* self) { | |||
270 | wprintw(self->window, " accept <number> : Accept friend request\n"); | 292 | wprintw(self->window, " accept <number> : Accept friend request\n"); |
271 | wprintw(self->window, " myid : Print your ID\n"); | 293 | wprintw(self->window, " myid : Print your ID\n"); |
272 | wprintw(self->window, " quit/exit : Exit program\n"); | 294 | wprintw(self->window, " quit/exit : Exit program\n"); |
295 | |||
296 | |||
297 | wattron(self->window, A_BOLD); | ||
298 | wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); | ||
299 | wattroff(self->window, A_BOLD); | ||
300 | |||
273 | wattroff(self->window, COLOR_PAIR(2)); | 301 | wattroff(self->window, COLOR_PAIR(2)); |
274 | } | 302 | } |
275 | 303 | ||
@@ -283,6 +311,8 @@ static void prompt_onInit(ToxWindow* self) { | |||
283 | ToxWindow new_prompt() { | 311 | ToxWindow new_prompt() { |
284 | ToxWindow ret; | 312 | ToxWindow ret; |
285 | 313 | ||
314 | memset(&ret, 0, sizeof(ret)); | ||
315 | |||
286 | ret.onKey = &prompt_onKey; | 316 | ret.onKey = &prompt_onKey; |
287 | ret.onDraw = &prompt_onDraw; | 317 | ret.onDraw = &prompt_onDraw; |
288 | ret.onInit = &prompt_onInit; | 318 | ret.onInit = &prompt_onInit; |
diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 2e082ca9..dde1430f 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h | |||
@@ -1,10 +1,23 @@ | |||
1 | /* | ||
2 | * Toxic -- Tox Curses Client | ||
3 | */ | ||
4 | |||
5 | #include <stdbool.h> | ||
6 | |||
1 | typedef struct ToxWindow_ ToxWindow; | 7 | typedef struct ToxWindow_ ToxWindow; |
2 | 8 | ||
3 | struct ToxWindow_ { | 9 | struct ToxWindow_ { |
4 | void(*onKey)(ToxWindow*, int); | 10 | void(*onKey)(ToxWindow*, int); |
5 | void(*onDraw)(ToxWindow*); | 11 | void(*onDraw)(ToxWindow*); |
6 | void(*onInit)(ToxWindow*); | 12 | void(*onInit)(ToxWindow*); |
13 | void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t); | ||
14 | void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t); | ||
15 | void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t); | ||
16 | void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t); | ||
7 | char title[256]; | 17 | char title[256]; |
8 | 18 | ||
19 | void* x; | ||
20 | bool blink; | ||
21 | |||
9 | WINDOW* window; | 22 | WINDOW* window; |
10 | }; | 23 | }; |