diff options
Diffstat (limited to 'testing/nTox_win32.c')
-rw-r--r-- | testing/nTox_win32.c | 290 |
1 files changed, 290 insertions, 0 deletions
diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c new file mode 100644 index 00000000..51f31001 --- /dev/null +++ b/testing/nTox_win32.c | |||
@@ -0,0 +1,290 @@ | |||
1 | /* nTox_win32.c | ||
2 | * | ||
3 | * Textual frontend for Tox - Windows version | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #include "nTox_win32.h" | ||
25 | #include "misc_tools.h" | ||
26 | |||
27 | #include <process.h> | ||
28 | |||
29 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; | ||
30 | uint8_t num_requests; | ||
31 | |||
32 | char line[STRING_LENGTH]; | ||
33 | |||
34 | void do_header() | ||
35 | { | ||
36 | printf("\n[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)\n\n"); | ||
37 | } | ||
38 | |||
39 | void print_request(uint8_t *public_key, uint8_t *data, uint16_t length) | ||
40 | { | ||
41 | printf("\n[i] received friend request with message\n"); | ||
42 | printf((char *)data); | ||
43 | char numchar[100]; | ||
44 | sprintf(numchar, "\n[i] accept request with /a %u\n", num_requests); | ||
45 | printf(numchar); | ||
46 | memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); | ||
47 | ++num_requests; | ||
48 | } | ||
49 | |||
50 | void print_message(int friendnumber, uint8_t * string, uint16_t length) | ||
51 | { | ||
52 | char name[MAX_NAME_LENGTH]; | ||
53 | getname(friendnumber, (uint8_t*)name); | ||
54 | char msg[100+length+strlen(name)+1]; | ||
55 | time_t rawtime; | ||
56 | struct tm * timeinfo; | ||
57 | time (&rawtime); | ||
58 | timeinfo = localtime (&rawtime); | ||
59 | char* temp = asctime(timeinfo); | ||
60 | size_t len = strlen(temp); | ||
61 | temp[len-1]='\0'; | ||
62 | sprintf(msg, "\n[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp | ||
63 | printf(msg); | ||
64 | } | ||
65 | |||
66 | void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) | ||
67 | { | ||
68 | char name[MAX_NAME_LENGTH]; | ||
69 | getname(friendnumber, (uint8_t*)name); | ||
70 | char msg[100+length]; | ||
71 | sprintf(msg, "\n[i] [%d] %s is now known as %s.", friendnumber, name, string); | ||
72 | printf(msg); | ||
73 | } | ||
74 | |||
75 | void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { | ||
76 | char name[MAX_NAME_LENGTH]; | ||
77 | getname(friendnumber, (uint8_t*)name); | ||
78 | char msg[100+length+strlen(name)+1]; | ||
79 | sprintf(msg, "\n[i] [%d] %s's status changed to %s.", friendnumber, name, string); | ||
80 | printf(msg); | ||
81 | } | ||
82 | |||
83 | |||
84 | void load_key() | ||
85 | { | ||
86 | FILE *data_file = NULL; | ||
87 | |||
88 | if ((data_file = fopen("data", "r"))) { | ||
89 | fseek(data_file, 0, SEEK_END); | ||
90 | int size = ftell(data_file); | ||
91 | fseek(data_file, 0, SEEK_SET); | ||
92 | uint8_t data[size]; | ||
93 | |||
94 | if(fread(data, sizeof(uint8_t), size, data_file) != size) { | ||
95 | printf("\n[i] Could not read the data file. Exiting."); | ||
96 | exit(1); | ||
97 | } | ||
98 | |||
99 | Messenger_load(data, size); | ||
100 | } else { | ||
101 | int size = Messenger_size(); | ||
102 | uint8_t data[size]; | ||
103 | Messenger_save(data); | ||
104 | data_file = fopen("data", "w"); | ||
105 | |||
106 | if(fwrite(data, sizeof(uint8_t), size, data_file) != size) { | ||
107 | printf("\n[i] Could not write data to file. Exiting."); | ||
108 | exit(1); | ||
109 | } | ||
110 | } | ||
111 | |||
112 | fclose(data_file); | ||
113 | } | ||
114 | |||
115 | void line_eval(char* line) | ||
116 | { | ||
117 | if(line[0] == '/') { | ||
118 | /* Add friend */ | ||
119 | if(line[1] == 'f') { | ||
120 | int i; | ||
121 | char temp_id[128]; | ||
122 | for (i=0; i<128; i++) | ||
123 | temp_id[i] = line[i+3]; | ||
124 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | ||
125 | char numstring[100]; | ||
126 | sprintf(numstring, "\n[i] added friend %d\n\n", num); | ||
127 | printf(numstring); | ||
128 | } | ||
129 | |||
130 | else if (line[1] == 'd') { | ||
131 | doMessenger(); | ||
132 | } | ||
133 | /* Send message to friend */ | ||
134 | else if (line[1] == 'm') { | ||
135 | int i; | ||
136 | size_t len = strlen(line); | ||
137 | char numstring[len-3]; | ||
138 | char message[len-3]; | ||
139 | for (i=0; i<len; i++) { | ||
140 | if (line[i+3] != ' ') { | ||
141 | numstring[i] = line[i+3]; | ||
142 | } else { | ||
143 | int j; | ||
144 | for (j=i+1; j<len; j++) | ||
145 | message[j-i-1] = line[j+3]; | ||
146 | break; | ||
147 | } | ||
148 | } | ||
149 | int num = atoi(numstring); | ||
150 | if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { | ||
151 | printf("\n[i] could not send message: %s\n\n", message); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | else if (line[1] == 'n') { | ||
156 | uint8_t name[MAX_NAME_LENGTH]; | ||
157 | int i = 0; | ||
158 | size_t len = strlen(line); | ||
159 | for (i=3; i<len; i++) { | ||
160 | if (line[i] == 0 || line[i] == '\n') break; | ||
161 | name[i - 3] = line[i]; | ||
162 | } | ||
163 | name[i - 3] = 0; | ||
164 | setname(name, i); | ||
165 | char numstring[100]; | ||
166 | sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name); | ||
167 | printf(numstring); | ||
168 | } | ||
169 | |||
170 | else if (line[1] == 's') { | ||
171 | uint8_t status[MAX_USERSTATUS_LENGTH]; | ||
172 | int i = 0; | ||
173 | size_t len = strlen(line); | ||
174 | for (i=3; i<len; i++) { | ||
175 | if (line[i] == 0 || line[i] == '\n') break; | ||
176 | status[i - 3] = line[i]; | ||
177 | } | ||
178 | status[i - 3] = 0; | ||
179 | m_set_userstatus(status, strlen((char*)status)); | ||
180 | char numstring[100]; | ||
181 | sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status); | ||
182 | printf(numstring); | ||
183 | } | ||
184 | |||
185 | else if (line[1] == 'a') { | ||
186 | uint8_t numf = atoi(line + 3); | ||
187 | char numchar[100]; | ||
188 | sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf); | ||
189 | printf(numchar); | ||
190 | int num = m_addfriend_norequest(pending_requests[numf]); | ||
191 | sprintf(numchar, "\n[i] added friendnumber %d\n\n", num); | ||
192 | printf(numchar); | ||
193 | } | ||
194 | /* EXIT */ | ||
195 | else if (line[1] == 'q') { | ||
196 | exit(EXIT_SUCCESS); | ||
197 | } | ||
198 | } | ||
199 | |||
200 | else { | ||
201 | //nothing atm | ||
202 | } | ||
203 | } | ||
204 | |||
205 | void get_input() | ||
206 | { | ||
207 | while(1) { | ||
208 | fgets(line, STRING_LENGTH, stdin); | ||
209 | line_eval(line); | ||
210 | strcpy(line, ""); | ||
211 | } | ||
212 | } | ||
213 | |||
214 | int main(int argc, char *argv[]) | ||
215 | { | ||
216 | if (argc < 4) { | ||
217 | printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); | ||
218 | exit(0); | ||
219 | } | ||
220 | |||
221 | if (initMessenger() == -1) { | ||
222 | printf("initMessenger failed"); | ||
223 | exit(0); | ||
224 | } | ||
225 | |||
226 | |||
227 | if (argc > 4) { | ||
228 | if(strncmp(argv[4], "nokey", 6) < 0) { | ||
229 | //nothing | ||
230 | } else { | ||
231 | load_key(); | ||
232 | } | ||
233 | } | ||
234 | |||
235 | m_callback_friendrequest(print_request); | ||
236 | m_callback_friendmessage(print_message); | ||
237 | m_callback_namechange(print_nickchange); | ||
238 | m_callback_userstatus(print_statuschange); | ||
239 | |||
240 | system("cls"); | ||
241 | |||
242 | char idstring0[200]; | ||
243 | char idstring1[32][5]; | ||
244 | char idstring2[32][5]; | ||
245 | uint32_t i; | ||
246 | for(i = 0; i < 32; i++) | ||
247 | { | ||
248 | if(self_public_key[i] < 16) | ||
249 | strcpy(idstring1[i],"0"); | ||
250 | else | ||
251 | strcpy(idstring1[i], ""); | ||
252 | sprintf(idstring2[i], "%hhX",self_public_key[i]); | ||
253 | } | ||
254 | strcpy(idstring0,"\n[i] your ID: "); | ||
255 | for (i=0; i<32; i++) { | ||
256 | strcat(idstring0,idstring1[i]); | ||
257 | strcat(idstring0,idstring2[i]); | ||
258 | } | ||
259 | |||
260 | printf(idstring0); | ||
261 | do_header(); | ||
262 | |||
263 | |||
264 | IP_Port bootstrap_ip_port; | ||
265 | bootstrap_ip_port.port = htons(atoi(argv[2])); | ||
266 | int resolved_address = resolve_addr(argv[1]); | ||
267 | if (resolved_address != -1) | ||
268 | bootstrap_ip_port.ip.i = resolved_address; | ||
269 | else | ||
270 | exit(1); | ||
271 | |||
272 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); | ||
273 | |||
274 | |||
275 | int c; | ||
276 | int on = 0; | ||
277 | |||
278 | _beginthread(get_input, 0, NULL); | ||
279 | |||
280 | while(1) { | ||
281 | if (on == 0 && DHT_isconnected()) { | ||
282 | printf("\n[i] connected to DHT\n\n"); | ||
283 | on = 1; | ||
284 | } | ||
285 | |||
286 | doMessenger(); | ||
287 | } | ||
288 | |||
289 | return 0; | ||
290 | } \ No newline at end of file | ||