summaryrefslogtreecommitdiff
path: root/testing/nTox.c
diff options
context:
space:
mode:
authorOliver Hunt <oliver.huntuk@gmail.com>2013-07-13 18:31:16 +0100
committerOliver Hunt <oliver.huntuk@gmail.com>2013-07-13 23:42:05 +0100
commit5441562e11933d5a7213c450c63a6cbaf1c68c4b (patch)
tree467c8b0a15f5867d3abcf62125c4f8fc3ec88aac /testing/nTox.c
parent11a2bf0878404c8c6d8c01424218da49237b880a (diff)
Added ncurses tox client
Diffstat (limited to 'testing/nTox.c')
-rw-r--r--testing/nTox.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/testing/nTox.c b/testing/nTox.c
new file mode 100644
index 00000000..8d2ec1dd
--- /dev/null
+++ b/testing/nTox.c
@@ -0,0 +1,195 @@
1#include "nTox.h"
2char lines[HISTORY][STRING_LENGTH];
3char line[STRING_LENGTH];
4int x,y;
5
6void new_lines(char *line)
7{
8 int i;
9 for (i = HISTORY-1; i > 0; i--) {
10 strcpy(lines[i],lines[i-1]);
11 }
12 strcpy(lines[0],line);
13}
14
15unsigned char * hex_string_to_bin(char hex_string[])
16{
17 unsigned char * val = malloc(strlen(hex_string));
18 char * pos = hex_string;
19 int i=0;
20 while(i < strlen(hex_string))
21 {
22 sscanf(pos,"%2hhx",&val[i]);
23 pos+=2;
24 i++;
25 }
26 return val;
27}
28
29void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
30{
31 if (line[0] == '/') {
32 if (line[1] == 'f') { // add friend command: /f ID
33 int i;
34 char temp_id[128];
35 for (i=0; i<128; i++) {
36 temp_id[i] = line[i+3];
37 }
38 int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
39 char numstring[100];
40 sprintf(numstring, "%d", num);
41 new_lines(numstring);
42 do_refresh();
43 } else if (line[1] == 'd') {
44 doMessenger();
45 } else if (line[1] == 'm') { //message command: /m friendnumber messsage
46 int i;
47 int len = strlen(line);
48 char numstring[len-3];
49 char message[len-3];
50 for (i=0; i<len; i++) {
51 if (line[i+3] != ' ') {
52 numstring[i] = line[i+3];
53 } else {
54 int j;
55 for (j=i+1; j<len; j++) {
56 message[j] = line[j+3];
57 }
58 break;
59 }
60 }
61 int num = atoi(numstring);
62 m_sendmessage(num, (uint8_t*) message, sizeof((uint8_t*)message));
63 }
64 char command[STRING_LENGTH + 2] = "> ";
65 strcat(command, line);
66 new_lines(command);
67 } else {
68 new_lines(line);
69 }
70}
71
72void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
73{
74 int i = 0;
75 strcpy(output,input);
76 for (i=line_width; i < strlen(output); i = i + line_width) {
77 while (output[i] != ' ' && i != 0) {
78 i--;
79 }
80 if (i > 0) {
81 output[i] = '\n';
82 }
83 }
84}
85
86int count_lines(char *string)
87{
88 int len = strlen(string);
89 int i;
90 int count = 1;
91 for (i=0; i < len; i++) {
92 if (string[i] == '\n') {
93 count++;
94 }
95 }
96 return count;
97}
98
99char *appender(char *str, const char c)
100{
101 int len = strlen(str);
102 if (len < STRING_LENGTH) {
103 str[len + 1] = str[len];
104 str[len] = c;
105 }
106 return str;
107}
108
109void do_refresh()
110{
111 int i;
112 int count=0;
113 int l;
114 char wrap_output[STRING_LENGTH];
115 for (i=0; i<HISTORY; i++) {
116 wrap(wrap_output, lines[i], x);
117 l = count_lines(wrap_output);
118 count = count + l;
119 if (count < y) {
120 move(y-1-count,0);
121 printw(wrap_output);
122 clrtoeol();
123 }
124 }
125 move(y-1,0);
126 clrtoeol();
127 printw(">> ");
128 printw(line);
129 clrtoeol();
130 refresh();
131}
132void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
133{
134 new_lines("Friend request");
135 do_refresh();
136 if(memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 )
137 //if the request contained the message of peace the person is obviously a friend so we add him.
138 {
139 new_lines("Friend request accepted.");
140 do_refresh();
141 m_addfriend_norequest(public_key);
142 }
143}
144void print_message(int friendnumber, uint8_t * string, uint16_t length)
145{
146 new_lines("Message received");
147 do_refresh();
148 m_sendmessage(friendnumber, (uint8_t*)"Test1", 6);
149}
150int main(int argc, char *argv[])
151{
152 int c;
153 initMessenger();
154 m_callback_friendrequest(print_request);
155 m_callback_friendmessage(print_message);
156 char idstring0[200];
157 char idstring1[32][5];
158 char idstring2[32][5];
159 uint32_t i;
160 for(i = 0; i < 32; i++)
161 {
162 if(self_public_key[i] < 16) {
163 strcpy(idstring1[i],"0");
164 } else {
165 strcpy(idstring1[i], "");
166 }
167 sprintf(idstring2[i], "%hhX",self_public_key[i]);
168 }
169 strcpy(idstring0,"Your ID is: ");
170 for(i=0; i<32; i++) {
171 strcat(idstring0,idstring1[i]);
172 strcat(idstring0,idstring2[i]);
173 }
174 initscr();
175 noecho();
176 raw();
177 getmaxyx(stdscr,y,x);
178 new_lines(idstring0);
179 do_refresh();
180 strcpy(line, "");
181 while((c=getch())!=27) {
182 getmaxyx(stdscr,y,x);
183 if (c == '\n') {
184 line_eval(lines, line);
185 strcpy(line, "");
186 } else if (c == 127) {
187 line[strlen(line)-1] = '\0';
188 } else if (isalnum(c) || ispunct(c) || c == ' ') {
189 strcpy(line,appender(line, (char) c));
190 }
191 do_refresh();
192 }
193 endwin();
194 return 0;
195}