diff options
author | Astonex <softukitu@gmail.com> | 2013-07-29 23:00:56 +0100 |
---|---|---|
committer | Astonex <softukitu@gmail.com> | 2013-07-29 23:00:56 +0100 |
commit | 5ad33e7398fb2ec0dd8d960f3e66371eb5b15782 (patch) | |
tree | 7fefee2935aed547eab8ba844822cec482073b23 /testing/nTox_win32.c | |
parent | d09bcccc67cd6598cd6483bebee16c8ab64d4c7c (diff) |
Essentially nTox without the ncurses for Windows. Slightly modified main()
Diffstat (limited to 'testing/nTox_win32.c')
-rw-r--r-- | testing/nTox_win32.c | 281 |
1 files changed, 281 insertions, 0 deletions
diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c new file mode 100644 index 00000000..340e0cb4 --- /dev/null +++ b/testing/nTox_win32.c | |||
@@ -0,0 +1,281 @@ | |||
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 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; | ||
28 | uint8_t num_requests; | ||
29 | |||
30 | char line[STRING_LENGTH]; | ||
31 | |||
32 | void do_header() | ||
33 | { | ||
34 | system("cls"); | ||
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\n\n"); | ||
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 | int main(int argc, char *argv[]) | ||
206 | { | ||
207 | if (argc < 4) { | ||
208 | printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); | ||
209 | exit(0); | ||
210 | } | ||
211 | |||
212 | if (initMessenger() == -1) { | ||
213 | printf("initMessenger failed"); | ||
214 | exit(0); | ||
215 | } | ||
216 | |||
217 | |||
218 | if (argc > 4) { | ||
219 | if(strncmp(argv[4], "nokey", 6) < 0) { | ||
220 | //nothing | ||
221 | } else { | ||
222 | load_key(); | ||
223 | } | ||
224 | } | ||
225 | |||
226 | m_callback_friendrequest(print_request); | ||
227 | m_callback_friendmessage(print_message); | ||
228 | m_callback_namechange(print_nickchange); | ||
229 | m_callback_userstatus(print_statuschange); | ||
230 | |||
231 | char idstring0[200]; | ||
232 | char idstring1[32][5]; | ||
233 | char idstring2[32][5]; | ||
234 | uint32_t i; | ||
235 | for(i = 0; i < 32; i++) | ||
236 | { | ||
237 | if(self_public_key[i] < 16) | ||
238 | strcpy(idstring1[i],"0"); | ||
239 | else | ||
240 | strcpy(idstring1[i], ""); | ||
241 | sprintf(idstring2[i], "%hhX",self_public_key[i]); | ||
242 | } | ||
243 | strcpy(idstring0,"\n[i] your ID: "); | ||
244 | for (i=0; i<32; i++) { | ||
245 | strcat(idstring0,idstring1[i]); | ||
246 | strcat(idstring0,idstring2[i]); | ||
247 | } | ||
248 | |||
249 | do_header(); | ||
250 | |||
251 | IP_Port bootstrap_ip_port; | ||
252 | bootstrap_ip_port.port = htons(atoi(argv[2])); | ||
253 | int resolved_address = resolve_addr(argv[1]); | ||
254 | if (resolved_address != -1) | ||
255 | bootstrap_ip_port.ip.i = resolved_address; | ||
256 | else | ||
257 | exit(1); | ||
258 | |||
259 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); | ||
260 | |||
261 | |||
262 | int c; | ||
263 | int on = 0; | ||
264 | |||
265 | while(1) { | ||
266 | if (on == 0 && DHT_isconnected()) { | ||
267 | printf("\n[i] connected to DHT\n\n"); | ||
268 | on = 1; | ||
269 | } | ||
270 | |||
271 | doMessenger(); | ||
272 | |||
273 | fgets(line, STRING_LENGTH, stdin); | ||
274 | |||
275 | line_eval(line); | ||
276 | |||
277 | strcpy(line, ""); | ||
278 | } | ||
279 | |||
280 | return 0; | ||
281 | } \ No newline at end of file | ||