ffi-tox 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -48,4 +48,4 @@ int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
48
48
  }
49
49
  #endif
50
50
 
51
- #endif
51
+ #endif
@@ -65,14 +65,19 @@ int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
65
65
 
66
66
  uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0};
67
67
  uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES];
68
- uint8_t zeroes[crypto_box_BOXZEROBYTES] = {0};
69
68
 
70
69
  memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */
71
70
 
72
71
  crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key);
73
72
 
74
- /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */
75
- if (memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0)
73
+ /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero
74
+ apparently memcmp should not be used so we do this instead:*/
75
+ uint32_t i;
76
+ uint32_t check = 0;
77
+ for(i = 0; i < crypto_box_BOXZEROBYTES; ++i) {
78
+ check |= temp_encrypted[i] ^ 0;
79
+ }
80
+ if(check != 0)
76
81
  return -1;
77
82
 
78
83
  /* unpad the encrypted message */
@@ -92,7 +97,6 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
92
97
 
93
98
  uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES];
94
99
  uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0};
95
- uint8_t zeroes[crypto_box_ZEROBYTES] = {0};
96
100
 
97
101
  memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */
98
102
 
@@ -100,8 +104,14 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
100
104
  nonce, public_key, secret_key) == -1)
101
105
  return -1;
102
106
 
103
- /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */
104
- if (memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0)
107
+ /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero
108
+ apparently memcmp should not be used so we do this instead:*/
109
+ uint32_t i;
110
+ uint32_t check = 0;
111
+ for(i = 0; i < crypto_box_ZEROBYTES; ++i) {
112
+ check |= temp_plain[i] ^ 0;
113
+ }
114
+ if(check != 0)
105
115
  return -1;
106
116
 
107
117
  /* unpad the plain message */
@@ -365,6 +375,7 @@ int crypto_kill(int crypt_connection_id)
365
375
  if (crypto_connections[crypt_connection_id].status != 0) {
366
376
  crypto_connections[crypt_connection_id].status = 0;
367
377
  kill_connection(crypto_connections[crypt_connection_id].number);
378
+ memset(&crypto_connections[crypt_connection_id], 0 ,sizeof(Crypto_Connection));
368
379
  crypto_connections[crypt_connection_id].number = ~0;
369
380
  return 0;
370
381
  }
@@ -164,25 +164,42 @@ void shutdown_networking()
164
164
  return;
165
165
  }
166
166
 
167
- /* resolves provided address to a binary data in network byte order
168
- address is ASCII null terminated string
169
- address should represent IPv4, IPv6 or a hostname
170
- on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
171
- on failure returns -1 */
172
- int resolve_addr(const char *address)
167
+ /*
168
+ resolve_addr():
169
+ address should represent IPv4 or a hostname with A record
170
+
171
+ returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
172
+ returns 0 on failure
173
+
174
+ TODO: Fix ipv6 support
175
+ */
176
+ uint32_t resolve_addr(const char *address)
173
177
  {
174
- struct addrinfo hints;
178
+ struct addrinfo *server = NULL;
179
+ struct addrinfo hints;
180
+ int rc;
181
+ uint32_t addr;
182
+
175
183
  memset(&hints, 0, sizeof(hints));
176
- hints.ai_family = AF_UNSPEC; //support both IPv4 and IPv6
177
- hints.ai_socktype = SOCK_DGRAM; //type of socket Tox uses
184
+ hints.ai_family = AF_INET; // IPv4 only right now.
185
+ hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
178
186
 
179
- struct addrinfo *server = NULL;
187
+ rc = getaddrinfo(address, "echo", &hints, &server);
180
188
 
181
- int success = getaddrinfo(address, "echo", &hints, &server);
182
- if(success != 0)
183
- return -1;
189
+ // Lookup failed.
190
+ if(rc != 0) {
191
+ return 0;
192
+ }
193
+
194
+ // IPv4 records only..
195
+ if(server->ai_family != AF_INET) {
196
+ freeaddrinfo(server);
197
+ return 0;
198
+ }
199
+
200
+
201
+ addr = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
184
202
 
185
- int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
186
203
  freeaddrinfo(server);
187
- return resolved;
204
+ return addr;
188
205
  }
@@ -56,11 +56,7 @@
56
56
  /* we use libsodium by default */
57
57
  #include <sodium.h>
58
58
  #else
59
-
60
- /* TODO: Including stuff like this is bad. This needs fixing.
61
- We keep support for the original NaCl for now. */
62
- #include "../nacl/build/Linux/include/amd64/crypto_box.h"
63
-
59
+ #include <crypto_box.h>
64
60
  #endif
65
61
 
66
62
  #ifdef __cplusplus
@@ -120,12 +116,16 @@ int init_networking(IP ip, uint16_t port);
120
116
  /* function to cleanup networking stuff(doesn't do much right now) */
121
117
  void shutdown_networking();
122
118
 
123
- /* resolves provided address to a binary data in network byte order
124
- address is ASCII null terminated string
125
- address should represent IPv4, IPv6 or a hostname
126
- on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
127
- on failure returns -1 */
128
- int resolve_addr(const char *address);
119
+ /*
120
+ resolve_addr():
121
+ address should represent IPv4 or a hostname with A record
122
+
123
+ returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
124
+ returns 0 on failure
125
+
126
+ TODO: Fix ipv6 support
127
+ */
128
+ uint32_t resolve_addr(const char *address);
129
129
 
130
130
  #ifdef __cplusplus
131
131
  }
@@ -0,0 +1,25 @@
1
+ # Tox User Commands
2
+ Here's a list of commands that nTox accepts,
3
+ which can all be used by starting your line with
4
+ a */*. Currently there can be no spaces before this.
5
+
6
+ * */f* [ID]
7
+ + Add a friend with ID [ID].
8
+ * */d*
9
+ + Call doMessenger() which does...something?
10
+ * */m* \[FRIEND\_NUM\] \[MESSAGE\]
11
+ + Message \[FRIEND\_NUM\] \[MESSAGE\].
12
+ * */n* \[NAME\]
13
+ + Change your username to \[NAME\].
14
+ * */l*
15
+ + Print your list of friends. (like you have any)
16
+ * */s* \[STATUS\]
17
+ + Set your status to \[STATUS\].
18
+ * */a* \[ID\]
19
+ + Accept friend request from \[ID\].
20
+ * */i*
21
+ + Print useful info about your client.
22
+ * */h*
23
+ + Print some help.
24
+ * */q/*
25
+ + Quit Tox. (why ;_;)
@@ -0,0 +1,40 @@
1
+ # Tox nutzen
2
+ 1. Tox erstellen
3
+ 2. Fehler korrigieren
4
+ 3. Im IRC nach Hilfe fragen
5
+ 4. Auf Debug-Reise für Entwickler
6
+ 5. Tox wirklich erstellen
7
+ 6. ???
8
+
9
+ Trotz der ganzen Arbeit, die wir bisher in Tox
10
+ gesteckt haben, gibt es noch keine richtige
11
+ Anleitung, wie man Tox _benutzt_.
12
+ Dies ist ein anwenderfreundlicher Versuch.
13
+
14
+ 1. Verbinde dich zum Netzwerk!
15
+ + Du musst dich zu einem Bootstrap-Server verbinden, um einen öffentlichen Schlüssel zu erhalten.
16
+ + Wo finde ich einen öffentlichen Server? Zur Zeit hier:
17
+ (die Hilfe-Nachricht von nTox ohne Kommandos hilft auch)
18
+ + 198.46.136.167 33445 728925473812C7AAC482BE7250BCCAD0B8CB9F737BF3D42ABD34459C1768F854
19
+ + 192.81.133.111 33445 8CD5A9BF0A6CE358BA36F7A653F99FA6B258FF756E490F52C1F98CC420F78858
20
+ + 66.175.223.88 33445 AC4112C975240CAD260BB2FCD134266521FAAF0A5D159C5FD3201196191E4F5D
21
+ + 192.184.81.118 33445 5CD7EB176C19A2FD840406CD56177BB8E75587BB366F7BB3004B19E3EDC04143
22
+ 2. Finde einen Freund!
23
+ + Jetzt, da du im Netzwerk bist, brauchst du einen Freund. Um einen zu bekommen,
24
+ musst du eine Anfrage senden oder erhalten. Was eine Anfrage ist?
25
+ Es ist wie eine Freundschaftsanfrage, jedoch benutzen wir unglaublich schaurige
26
+ und kryptische Nummern anstatt Namen. When nTox startet, erscheint _deine_ lange,
27
+ schaurige Nummer, auch *öffentlicher Schlüssel* genannt. Diesen kannst du an
28
+ andere Personen weitergeben und sie können dich als "Freund" hinzufügen. Oder du
29
+ fügst andere Personen mit dem */f*-Befehl hinzu, wenn du möchtest.
30
+ 3. Chatte drauf los!
31
+ + Benutze nun den */m*-Befehl, um eine Nachricht an jemanden zu senden. Wow, du chattest!
32
+ 4. Mach etwas kaputt!
33
+ + Jep, pre-alpha-alpha-Software stürzt manchmal ab. Wir arbeiten daran.
34
+ + Bitte melde alle Abstürze entweder an die GitHub-Seite oder #tox-dev im freenode-IRC.
35
+ 5. Nichts ist kaputt, aber was bedeutet */f*?
36
+ + nTox liest einen Text als Befehl, wenn das erste Zeichen ein Schrägstrich ist ('/').
37
+ Du kannst alle Befehle in commands.md nachlesen.
38
+ 6. Benutze und unterstütze Tox!
39
+ + Programmiere, debugge, dokumentiere, übersetze für uns, oder sprich einfach über uns!
40
+ + Je mehr Interesse wir erhalten, desto mehr Arbeit wird getan und desto besser wird Tox.
@@ -0,0 +1,38 @@
1
+ # Using Tox
2
+ 1. [Build Tox](../INSTALL.md)
3
+ 2. Fix errors
4
+ 3. Consult IRC for help
5
+ 4. Go on debugging journey for devs
6
+ 5. Build Tox for real
7
+ 6. ???
8
+
9
+ For all the work we've put into Tox so far,
10
+ there isn't yet a decent guide for how you _use_
11
+ Tox. Here's a user-friendly attempt at it.
12
+
13
+ 1. Connect to the network!
14
+ + You need to connect to a bootstrapping server, to give you a public key.
15
+ + Where can I find a public server? Right here, as of now:
16
+ (the help message from running `nTox` with no args will help)
17
+ + `198.46.136.167 33445 728925473812C7AAC482BE7250BCCAD0B8CB9F737BF3D42ABD34459C1768F854`
18
+ + `192.81.133.111 33445 8CD5A9BF0A6CE358BA36F7A653F99FA6B258FF756E490F52C1F98CC420F78858`
19
+ + `66.175.223.88 33445 AC4112C975240CAD260BB2FCD134266521FAAF0A5D159C5FD3201196191E4F5D`
20
+ + `192.184.81.118 33445 5CD7EB176C19A2FD840406CD56177BB8E75587BB366F7BB3004B19E3EDC04143`
21
+ 2. Find a friend!
22
+ + Now that you're on the network, you need a friend. To get one of those,
23
+ you need to to send or receive a request. What's a request, you ask?
24
+ It's like a friend request, but we use really scary and cryptic numbers
25
+ instead of names. When `nTox` starts, it shows _your_ long, scary number,
26
+ called your *public key*. Give that to people, and they can add you as
27
+ a "friend". Or, you can add someone else, with the `/f` command, if you like.
28
+ 3. Chat it up!
29
+ + Now use the `/m` command to send a message to someone. Wow, you're chatting!
30
+ 4. But something broke!
31
+ + Yeah, pre-alpha-alpha software tends to do that. We're working on it.
32
+ + Please report all crashes to either the GitHub page, or `#tox-dev` on freenode.
33
+ 5. Nothing broke, but what does `/f` mean?
34
+ + `nTox` parses text as a command if the first character is a forward-slash (`/`).
35
+ You can check all commands in commands.md.
36
+ 6. Use and support Tox!
37
+ + Code for us, debug for us, document for us, translate for us, even just talk about us!
38
+ + The more interest we get, the more work gets done, the better Tox is.
@@ -9,10 +9,10 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testclient.cmake)
9
9
  include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testserver.cmake)
10
10
  include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Messenger_test.cmake)
11
11
  if(WIN32)
12
- include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox_win32.cmake)
12
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox_win32.cmake)
13
13
  endif()
14
14
 
15
15
  if(NOT WIN32)
16
- include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox.cmake)
17
- include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/toxic.cmake)
16
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox.cmake)
17
+ add_subdirectory(toxic)
18
18
  endif()
@@ -186,7 +186,7 @@ int main(int argc, char *argv[])
186
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
190
  else if(is_cryptoconnected(inconnection) == 4) {
191
191
  crypto_kill(inconnection);
192
192
  }
@@ -209,7 +209,7 @@ int main(int argc, char *argv[])
209
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
213
  else if(is_cryptoconnected(connection) == 4) {
214
214
  crypto_kill(connection);
215
215
  }
@@ -87,7 +87,7 @@ void print_friendlist()
87
87
  for(i = 0; i < 4; i++) {
88
88
  printf("ClientID: ");
89
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
91
  printf("0");
92
92
  printf("%hhX", friends_list[k].client_list[i].client_id[j]);
93
93
  }
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * Best used in combination with Lossless_UDP_testserver
6
6
  *
7
- * Compile with: gcc -O2 -Wall -o testclient ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c
7
+ * Compile with: gcc -O2 -Wall -lsodium -o testclient ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testclient.c
8
8
  *
9
9
  * Command line arguments are the ip and port to connect and send the file to.
10
10
  * EX: ./testclient 127.0.0.1 33445 filename.txt
@@ -211,4 +211,4 @@ int main(int argc, char *argv[])
211
211
  }
212
212
 
213
213
  return 0;
214
- }
214
+ }
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * Best used in combination with Lossless_UDP_testclient
6
6
  *
7
- * Compile with: gcc -O2 -Wall -o testserver ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c
7
+ * Compile with: gcc -O2 -Wall -lsodium -o testserver ../core/network.c ../core/Lossless_UDP.c Lossless_UDP_testserver.c
8
8
  *
9
9
  * Command line argument is the name of the file to save what we recieve to.
10
10
  * EX: ./testserver filename1.txt
@@ -198,4 +198,4 @@ int main(int argc, char *argv[])
198
198
  }
199
199
 
200
200
  return 0;
201
- }
201
+ }
@@ -26,4 +26,4 @@
26
26
 
27
27
  unsigned char * hex_string_to_bin(char hex_string[]);
28
28
 
29
- #endif // MISC_TOOLS_H
29
+ #endif // MISC_TOOLS_H
@@ -23,9 +23,9 @@
23
23
  #include "nTox.h"
24
24
  #include "misc_tools.h"
25
25
 
26
-
27
26
  #include <stdio.h>
28
27
  #include <time.h>
28
+
29
29
  #ifdef WIN32
30
30
  #define c_sleep(x) Sleep(1*x)
31
31
  #else
@@ -35,30 +35,33 @@
35
35
 
36
36
  char lines[HISTORY][STRING_LENGTH];
37
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
44
  uint8_t pending_requests[256][CLIENT_ID_SIZE];
41
- uint8_t num_requests;
45
+ uint8_t num_requests = 0;
42
46
 
43
47
  void new_lines(char *line)
44
48
  {
45
49
  int i;
46
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
54
  do_refresh();
51
55
  }
52
56
 
57
+
53
58
  void print_friendlist()
54
59
  {
55
60
  char name[MAX_NAME_LENGTH];
56
- uint32_t i;
57
-
58
61
  new_lines("[i] Friend List:");
59
- for (i=0; i <= num_requests; i++) {
62
+ uint32_t i;
63
+ for (i = 0; i <= num_requests; i++) {
60
64
  char fstring[128];
61
-
62
65
  getname(i, (uint8_t*)name);
63
66
  if (strlen(name) <= 0) {
64
67
  sprintf(fstring, "[i] Friend: NULL\n\tid: %i", i);
@@ -69,89 +72,164 @@ void print_friendlist()
69
72
  }
70
73
  }
71
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
+
72
96
  void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
73
97
  {
74
98
  if (line[0] == '/') {
75
- char command[STRING_LENGTH + 2] = "> ";
76
- strcat(command, line);
77
- new_lines(command);
78
- if (line[1] == 'f') { // add friend command: /f ID
99
+ char inpt_command = line[1];
100
+ char prompt[STRING_LENGTH+2] = "> ";
101
+ int prompt_offset = 3;
102
+ strcat(prompt, line);
103
+ new_lines(prompt);
104
+ if (inpt_command == 'f') { // add friend command: /f ID
79
105
  int i;
80
106
  char temp_id[128];
81
- for (i=0; i<128; i++)
82
- temp_id[i] = line[i+3];
107
+ for (i = 0; i < 128; i++)
108
+ temp_id[i] = line[i+prompt_offset];
83
109
  int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
84
110
  char numstring[100];
85
- sprintf(numstring, "[i] added friend %d", num);
111
+ switch (num) {
112
+ case -1:
113
+ sprintf(numstring, "[i] Incorrect key length");
114
+ break;
115
+ case -2:
116
+ sprintf(numstring, "[i] That appears to be your own key");
117
+ break;
118
+ case -3:
119
+ sprintf(numstring, "[i] Friend request already sent");
120
+ break;
121
+ case -4:
122
+ sprintf(numstring, "[i] Could not add friend");
123
+ break;
124
+ default:
125
+ sprintf(numstring, "[i] Added friend %d", num);
126
+ break;
127
+ }
86
128
  new_lines(numstring);
87
129
  do_refresh();
88
130
  }
89
- else if (line[1] == 'd') {
131
+ else if (inpt_command == 'd') {
90
132
  doMessenger();
91
133
  }
92
- else if (line[1] == 'm') { //message command: /m friendnumber messsage
93
- int i;
134
+ else if (inpt_command == 'm') { //message command: /m friendnumber messsage
94
135
  size_t len = strlen(line);
136
+ if(len < 3)
137
+ return;
138
+
95
139
  char numstring[len-3];
96
140
  char message[len-3];
97
- for (i=0; i<len; i++) {
141
+ int i;
142
+ for (i = 0; i < len; i++) {
98
143
  if (line[i+3] != ' ') {
99
144
  numstring[i] = line[i+3];
100
145
  } else {
101
146
  int j;
102
- for (j=i+1; j<len; j++)
147
+ for (j = (i+1); j < (len+1); j++)
103
148
  message[j-i-1] = line[j+3];
104
149
  break;
105
150
  }
106
151
  }
107
152
  int num = atoi(numstring);
108
- if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
153
+ if (m_sendmessage(num, (uint8_t*) message, strlen(message) + 1) != 1) {
109
154
  new_lines("[i] could not send message");
155
+ } else {
156
+ new_lines(format_message(message, -1));
110
157
  }
111
158
  }
112
- else if (line[1] == 'n') {
159
+ else if (inpt_command == 'n') {
113
160
  uint8_t name[MAX_NAME_LENGTH];
114
161
  int i = 0;
115
162
  size_t len = strlen(line);
116
- for (i=3; i<len; i++) {
163
+ for (i = 3; i < len; i++) {
117
164
  if (line[i] == 0 || line[i] == '\n') break;
118
- name[i - 3] = line[i];
165
+ name[i-3] = line[i];
119
166
  }
120
- name[i - 3] = 0;
121
- setname(name, i);
167
+ name[i-3] = 0;
168
+ setname(name, i - 2);
122
169
  char numstring[100];
123
170
  sprintf(numstring, "[i] changed nick to %s", (char*)name);
124
171
  new_lines(numstring);
125
172
  }
126
- else if (line[1] == 'l') {
173
+ else if (inpt_command == 'l') {
127
174
  print_friendlist();
128
175
  }
129
- else if (line[1] == 's') {
176
+ else if (inpt_command == 's') {
130
177
  uint8_t status[MAX_USERSTATUS_LENGTH];
131
178
  int i = 0;
132
179
  size_t len = strlen(line);
133
- for (i=3; i<len; i++) {
180
+ for (i = 3; i < len; i++) {
134
181
  if (line[i] == 0 || line[i] == '\n') break;
135
- status[i - 3] = line[i];
182
+ status[i-3] = line[i];
136
183
  }
137
- status[i - 3] = 0;
138
- m_set_userstatus(status, strlen((char*)status));
184
+ status[i-3] = 0;
185
+ m_set_userstatus(status, strlen((char*)status) + 1);
139
186
  char numstring[100];
140
187
  sprintf(numstring, "[i] changed status to %s", (char*)status);
141
188
  new_lines(numstring);
142
189
  }
143
- else if (line[1] == 'a') {
190
+ else if (inpt_command == 'a') {
144
191
  uint8_t numf = atoi(line + 3);
145
192
  char numchar[100];
146
- sprintf(numchar, "[i] friend request %u accepted", numf);
147
- new_lines(numchar);
148
193
  int num = m_addfriend_norequest(pending_requests[numf]);
149
- sprintf(numchar, "[i] added friendnumber %d", num);
150
- new_lines(numchar);
194
+ if (num != -1) {
195
+ sprintf(numchar, "[i] friend request %u accepted", numf);
196
+ new_lines(numchar);
197
+ sprintf(numchar, "[i] added friendnumber %d", num);
198
+ new_lines(numchar);
199
+ } else {
200
+ sprintf(numchar, "[i] failed to add friend");
201
+ new_lines(numchar);
202
+ }
151
203
  do_refresh();
152
-
153
204
  }
154
- else if (line[1] == 'q') { //exit
205
+ else if (inpt_command == 'h') { //help
206
+ new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)");
207
+ new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)");
208
+ }
209
+ else if (inpt_command == 'i') { //info
210
+ char idstring0[200];
211
+ char idstring1[PUB_KEY_BYTES][5];
212
+ char idstring2[PUB_KEY_BYTES][5];
213
+ int i;
214
+ for (i = 0; i < PUB_KEY_BYTES; i++)
215
+ {
216
+ if (self_public_key[i] < (PUB_KEY_BYTES/2))
217
+ strcpy(idstring1[i],"0");
218
+ else
219
+ strcpy(idstring1[i], "");
220
+ sprintf(idstring2[i], "%hhX", self_public_key[i]);
221
+ }
222
+ //
223
+ strcpy(idstring0,"[i] ID: ");
224
+ int j;
225
+ for (j = 0; j < PUB_KEY_BYTES; j++) {
226
+ strcat(idstring0,idstring1[j]);
227
+ strcat(idstring0,idstring2[j]);
228
+ }
229
+ new_lines(idstring0);
230
+ }
231
+
232
+ else if (inpt_command == 'q') { //exit
155
233
  endwin();
156
234
  exit(EXIT_SUCCESS);
157
235
  } else {
@@ -165,9 +243,9 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
165
243
 
166
244
  void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
167
245
  {
168
- int i = 0;
169
246
  strcpy(output,input);
170
247
  size_t len = strlen(output);
248
+ int i = 0;
171
249
  for (i = line_width; i < len; i = i + line_width) {
172
250
  while (output[i] != ' ' && i != 0) {
173
251
  i--;
@@ -181,9 +259,9 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
181
259
  int count_lines(char *string)
182
260
  {
183
261
  size_t len = strlen(string);
184
- int i;
185
262
  int count = 1;
186
- for (i=0; i < len; i++) {
263
+ int i;
264
+ for (i = 0; i < len; i++) {
187
265
  if (string[i] == '\n')
188
266
  count++;
189
267
  }
@@ -194,7 +272,7 @@ char *appender(char *str, const char c)
194
272
  {
195
273
  size_t len = strlen(str);
196
274
  if (len < STRING_LENGTH) {
197
- str[len + 1] = str[len];
275
+ str[len+1] = str[len];
198
276
  str[len] = c;
199
277
  }
200
278
  return str;
@@ -202,21 +280,21 @@ char *appender(char *str, const char c)
202
280
 
203
281
  void do_refresh()
204
282
  {
205
- int i;
206
283
  int count=0;
207
- int l;
208
284
  char wrap_output[STRING_LENGTH];
209
- for (i=0; i<HISTORY; i++) {
285
+ int L;
286
+ int i;
287
+ for (i = 0; i < HISTORY; i++) {
210
288
  wrap(wrap_output, lines[i], x);
211
- l = count_lines(wrap_output);
212
- count = count + l;
289
+ L = count_lines(wrap_output);
290
+ count = count + L;
213
291
  if (count < y) {
214
- move(y-1-count,0);
292
+ move(y-1-count, 0);
215
293
  printw(wrap_output);
216
294
  clrtoeol();
217
295
  }
218
296
  }
219
- move(y-1,0);
297
+ move(y-1, 0);
220
298
  clrtoeol();
221
299
  printw(">> ");
222
300
  printw(line);
@@ -247,12 +325,13 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length)
247
325
  timeinfo = localtime ( &rawtime );
248
326
  char* temp = asctime(timeinfo);
249
327
  size_t len = strlen(temp);
250
- temp[len-1]='\0';
328
+ temp[len-1] = '\0';
251
329
  sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // timestamp
252
- new_lines(msg);
330
+ new_lines(format_message((char*)string, friendnumber));
253
331
  }
254
332
 
255
- void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
333
+ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
334
+ {
256
335
  char name[MAX_NAME_LENGTH];
257
336
  getname(friendnumber, (uint8_t*)name);
258
337
  char msg[100+length];
@@ -260,7 +339,8 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) {
260
339
  new_lines(msg);
261
340
  }
262
341
 
263
- void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
342
+ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length)
343
+ {
264
344
  char name[MAX_NAME_LENGTH];
265
345
  getname(friendnumber, (uint8_t*)name);
266
346
  char msg[100+length+strlen(name)+1];
@@ -268,15 +348,17 @@ void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) {
268
348
  new_lines(msg);
269
349
  }
270
350
 
271
- void load_key(){
351
+ void load_key()
352
+ {
272
353
  FILE *data_file = NULL;
273
- if ((data_file = fopen("data","r"))) {
354
+ data_file = fopen("data","r");
355
+ if (data_file) {
274
356
  //load keys
275
357
  fseek(data_file, 0, SEEK_END);
276
358
  int size = ftell(data_file);
277
359
  fseek(data_file, 0, SEEK_SET);
278
360
  uint8_t data[size];
279
- if(fread(data, sizeof(uint8_t), size, data_file) != size){
361
+ if (fread(data, sizeof(uint8_t), size, data_file) != size){
280
362
  printf("[i] could not read data file\n[i] exiting\n");
281
363
  exit(1);
282
364
  }
@@ -287,7 +369,7 @@ void load_key(){
287
369
  uint8_t data[size];
288
370
  Messenger_save(data);
289
371
  data_file = fopen("data","w");
290
- if(fwrite(data, sizeof(uint8_t), size, data_file) != size){
372
+ if (fwrite(data, sizeof(uint8_t), size, data_file) != size){
291
373
  printf("[i] could not write data file\n[i] exiting\n");
292
374
  exit(1);
293
375
  }
@@ -317,34 +399,34 @@ int main(int argc, char *argv[])
317
399
  m_callback_namechange(print_nickchange);
318
400
  m_callback_userstatus(print_statuschange);
319
401
  char idstring0[200];
320
- char idstring1[32][5];
321
- char idstring2[32][5];
322
- uint32_t i;
323
- for(i = 0; i < 32; i++)
402
+ char idstring1[PUB_KEY_BYTES][5];
403
+ char idstring2[PUB_KEY_BYTES][5];
404
+ int i;
405
+ for(i = 0; i < PUB_KEY_BYTES; i++)
324
406
  {
325
- if(self_public_key[i] < 16)
407
+ if (self_public_key[i] < (PUB_KEY_BYTES / 2))
326
408
  strcpy(idstring1[i],"0");
327
409
  else
328
410
  strcpy(idstring1[i], "");
329
411
  sprintf(idstring2[i], "%hhX",self_public_key[i]);
330
412
  }
331
413
  strcpy(idstring0,"[i] your ID: ");
332
- for (i=0; i<32; i++) {
333
- strcat(idstring0,idstring1[i]);
334
- strcat(idstring0,idstring2[i]);
414
+ int j;
415
+ for (j = 0; j < PUB_KEY_BYTES; j++) {
416
+ strcat(idstring0,idstring1[j]);
417
+ strcat(idstring0,idstring2[j]);
335
418
  }
336
419
  initscr();
337
420
  noecho();
338
421
  raw();
339
- getmaxyx(stdscr,y,x);
422
+ getmaxyx(stdscr, y, x);
340
423
  new_lines(idstring0);
341
- new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)");
342
- new_lines("[i] /l list (list friends), /n nick (to change nickname), /q (to quit)");
424
+ new_lines(help);
343
425
  strcpy(line, "");
344
426
  IP_Port bootstrap_ip_port;
345
427
  bootstrap_ip_port.port = htons(atoi(argv[2]));
346
428
  int resolved_address = resolve_addr(argv[1]);
347
- if (resolved_address != -1)
429
+ if (resolved_address != 0)
348
430
  bootstrap_ip_port.ip.i = resolved_address;
349
431
  else
350
432
  exit(1);
@@ -362,7 +444,6 @@ int main(int argc, char *argv[])
362
444
  do_refresh();
363
445
 
364
446
  c = getch();
365
-
366
447
  if (c == ERR || c == 27)
367
448
  continue;
368
449
 
@@ -371,7 +452,7 @@ int main(int argc, char *argv[])
371
452
  line_eval(lines, line);
372
453
  strcpy(line, "");
373
454
  } else if (c == 127) {
374
- line[strlen(line) - 1] = '\0';
455
+ line[strlen(line)-1] = '\0';
375
456
  } else if (isalnum(c) || ispunct(c) || c == ' ') {
376
457
  strcpy(line, appender(line, (char) c));
377
458
  }