summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-02-02 17:40:00 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-02-06 11:45:50 +0000
commita3a0e553f3f06cf97d58347901fd478db81d571b (patch)
tree9e527534153ccffe53141c7c41aa77b611b00abc /testing
parentf71ec8dd02ef6e12b8ac954e79795a74a8323550 (diff)
Move irc_syncbot to the toxins repository.
Diffstat (limited to 'testing')
-rw-r--r--testing/BUILD.bazel9
-rw-r--r--testing/Makefile.inc13
-rw-r--r--testing/irc_syncbot.c383
3 files changed, 0 insertions, 405 deletions
diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel
index ee97fd1f..1413acc9 100644
--- a/testing/BUILD.bazel
+++ b/testing/BUILD.bazel
@@ -44,15 +44,6 @@ cc_binary(
44) 44)
45 45
46cc_binary( 46cc_binary(
47 name = "irc_syncbot",
48 srcs = ["irc_syncbot.c"],
49 deps = [
50 ":misc_tools",
51 "//c-toxcore/toxcore",
52 ],
53)
54
55cc_binary(
56 name = "tox_shell", 47 name = "tox_shell",
57 srcs = ["tox_shell.c"], 48 srcs = ["tox_shell.c"],
58 linkopts = ["-lutil"], 49 linkopts = ["-lutil"],
diff --git a/testing/Makefile.inc b/testing/Makefile.inc
index b489a701..7d5b41e7 100644
--- a/testing/Makefile.inc
+++ b/testing/Makefile.inc
@@ -51,19 +51,6 @@ tox_shell_LDADD = $(LIBSODIUM_LDFLAGS) \
51 -lutil 51 -lutil
52 52
53 53
54noinst_PROGRAMS += irc_syncbot
55
56irc_syncbot_SOURCES = ../testing/irc_syncbot.c
57
58irc_syncbot_CFLAGS = $(LIBSODIUM_CFLAGS) \
59 $(NACL_CFLAGS)
60
61irc_syncbot_LDADD = $(LIBSODIUM_LDFLAGS) \
62 $(NACL_LDFLAGS) \
63 libtoxcore.la \
64 $(LIBSODIUM_LIBS) \
65 $(NACL_OBJECTS) \
66 $(NACL_LIBS)
67endif 54endif
68 55
69EXTRA_DIST += $(top_srcdir)/testing/misc_tools.c 56EXTRA_DIST += $(top_srcdir)/testing/misc_tools.c
diff --git a/testing/irc_syncbot.c b/testing/irc_syncbot.c
deleted file mode 100644
index 329dfb85..00000000
--- a/testing/irc_syncbot.c
+++ /dev/null
@@ -1,383 +0,0 @@
1#define _XOPEN_SOURCE 600
2
3#include <stdint.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <time.h>
8
9
10#include <arpa/inet.h>
11#include <errno.h>
12#include <fcntl.h>
13#include <netdb.h>
14#include <netinet/in.h>
15#include <sys/socket.h>
16#include <sys/time.h>
17#include <sys/types.h>
18#include <unistd.h>
19
20#include <sys/ioctl.h>
21
22#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__MACH__)
23#define MSG_NOSIGNAL 0
24#endif
25
26#if defined(__FreeBSD__) && !defined(MSG_NOSIGNAL)
27#define MSG_NOSIGNAL 0x20000 // only defined on FreeBSD for some __POSIX_VISIBLE, causes compilation failures
28#endif
29
30//IRC name and channel.
31#define IRC_NAME "Tox_syncbot"
32#define IRC_CHANNEL "#tox-real-ontopic"
33
34//IRC server ip and port.
35static uint8_t ip[4] = {127, 0, 0, 1};
36static uint16_t port = 6667;
37
38#define SILENT_TIMEOUT 20
39
40static int sock;
41
42#define SERVER_CONNECT "NICK " IRC_NAME "\nUSER " IRC_NAME " 8 * :" IRC_NAME "\n"
43#define CHANNEL_JOIN "JOIN " IRC_CHANNEL "\n"
44
45/* In toxcore/network.c */
46uint64_t current_time_monotonic(void);
47
48static uint64_t get_monotime_sec(void)
49{
50 return current_time_monotonic() / 1000;
51}
52
53static int reconnect(void)
54{
55 int new_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
56
57 if (new_sock < 0) {
58 printf("error socket\n");
59 return -1;
60 }
61
62 struct sockaddr_storage addr = {0};
63
64 size_t addrsize;
65
66 struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
67
68 addrsize = sizeof(struct sockaddr_in);
69
70 addr4->sin_family = AF_INET;
71
72 memcpy(&addr4->sin_addr, ip, 4);
73
74 addr4->sin_port = htons(port);
75
76 if (connect(new_sock, (struct sockaddr *)&addr, addrsize) != 0) {
77 printf("error connect\n");
78 close(new_sock);
79 return -1;
80 }
81
82 send(new_sock, SERVER_CONNECT, sizeof(SERVER_CONNECT) - 1, MSG_NOSIGNAL);
83
84 return new_sock;
85}
86
87#include "../toxcore/ccompat.h"
88#include "../toxcore/tox.h"
89#include "misc_tools.c"
90
91static int current_group = -1;
92
93static void callback_group_invite(Tox *tox, uint32_t fid, TOX_CONFERENCE_TYPE type, const uint8_t *data, size_t length,
94 void *userdata)
95{
96 if (current_group == -1) {
97 current_group = tox_conference_join(tox, fid, data, length, nullptr);
98 }
99}
100
101static void callback_friend_message(Tox *tox, uint32_t fid, TOX_MESSAGE_TYPE type, const uint8_t *message,
102 size_t length,
103 void *userdata)
104{
105 if (length == 1 && *message == 'c') {
106 if (tox_conference_delete(tox, current_group, nullptr) == 0) {
107 current_group = -1;
108 }
109 }
110
111 if (length == 1 && *message == 'i') {
112 tox_conference_invite(tox, fid, current_group, nullptr);
113 }
114
115 if (length == 1 && *message == 'j' && sock >= 0) {
116 send(sock, CHANNEL_JOIN, sizeof(CHANNEL_JOIN) - 1, MSG_NOSIGNAL);
117 }
118}
119
120static void copy_groupmessage(Tox *tox, uint32_t groupnumber, uint32_t friendgroupnumber, TOX_MESSAGE_TYPE type,
121 const uint8_t *message, size_t length,
122 void *userdata)
123{
124 if (tox_conference_peer_number_is_ours(tox, groupnumber, friendgroupnumber, nullptr)) {
125 return;
126 }
127
128 TOX_ERR_CONFERENCE_PEER_QUERY error;
129 size_t namelen = tox_conference_peer_get_name_size(tox, groupnumber, friendgroupnumber, &error);
130 uint8_t name[TOX_MAX_NAME_LENGTH];
131 tox_conference_peer_get_name(tox, groupnumber, friendgroupnumber, name, nullptr);
132
133 if (namelen == 0 || error != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
134 memcpy(name, "<unknown>", 9);
135 namelen = 9;
136 }
137
138 uint8_t sendbuf[2048];
139 uint16_t send_len = 0;
140
141 memcpy(sendbuf, "PRIVMSG " IRC_CHANNEL " :", sizeof("PRIVMSG " IRC_CHANNEL " :"));
142 send_len += sizeof("PRIVMSG " IRC_CHANNEL " :") - 1;
143 memcpy(sendbuf + send_len, name, namelen);
144 send_len += namelen;
145 sendbuf[send_len] = ':';
146 send_len += 1;
147 sendbuf[send_len] = ' ';
148 send_len += 1;
149 memcpy(sendbuf + send_len, message, length);
150 send_len += length;
151 unsigned int i;
152
153 for (i = 0; i < send_len; ++i) {
154 if (sendbuf[i] == '\n') {
155 sendbuf[i] = '|';
156 }
157
158 if (sendbuf[i] == 0) {
159 sendbuf[i] = ' ';
160 }
161 }
162
163 sendbuf[send_len] = '\n';
164 send_len += 1;
165
166 if (sock >= 0) {
167 send(sock, sendbuf, send_len, MSG_NOSIGNAL);
168 }
169}
170
171static void send_irc_group(Tox *tox, uint8_t *msg, uint16_t len)
172{
173 if (len > 1350 || len == 0 || len == 1) {
174 return;
175 }
176
177 --len;
178
179 if (*msg != ':') {
180 return;
181 }
182
183 VLA(uint8_t, req, len);
184 unsigned int i;
185
186 unsigned int spaces = 0;
187
188 for (i = 0; i < (len - 1); ++i) {
189 if (msg[i + 1] == ' ') {
190 ++spaces;
191 } else {
192 if (spaces >= 3 && msg[i + 1] == ':') {
193 break;
194 }
195 }
196
197 req[i] = msg[i + 1];
198 }
199
200 unsigned int req_len = i;
201 req[i] = 0;
202
203 VLA(uint8_t, message, len);
204 uint16_t length = 0;
205
206 uint8_t *pmsg = (uint8_t *)strstr((char *)req, " PRIVMSG");
207
208 if (pmsg == nullptr) {
209 return;
210 }
211
212 uint8_t *dt;
213
214 for (dt = req, i = 0; dt != pmsg && *dt != '!'; ++dt, ++i) {
215 message[i] = *dt;
216 ++length;
217 }
218
219 message[length] = ':';
220 length += 1;
221 message[length] = ' ';
222 length += 1;
223
224 if ((req_len + 2) >= len) {
225 return;
226 }
227
228 memcpy(message + length, msg + req_len + 2, len - (req_len + 2));
229 length += len - (req_len + 2);
230 tox_conference_send_message(tox, current_group, TOX_MESSAGE_TYPE_NORMAL, message, length, nullptr);
231}
232
233static Tox *init_tox(int argc, char *argv[])
234{
235 uint8_t ipv6enabled = 1; /* x */
236 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
237
238 if (argvoffset < 0) {
239 exit(1);
240 }
241
242 /* with optional --ipvx, now it can be 1-4 arguments... */
243 if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) {
244 printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]);
245 exit(0);
246 }
247
248 Tox *tox = tox_new(0, 0);
249
250 if (!tox) {
251 exit(1);
252 }
253
254 tox_self_set_name(tox, (const uint8_t *)IRC_NAME, sizeof(IRC_NAME) - 1, 0);
255 tox_callback_friend_message(tox, &callback_friend_message);
256 tox_callback_conference_invite(tox, &callback_group_invite);
257 tox_callback_conference_message(tox, &copy_groupmessage);
258
259 char temp_id[128];
260 printf("\nEnter the address of irc_syncbots master (38 bytes HEX format):\n");
261
262 if (scanf("%s", temp_id) != 1) {
263 exit(1);
264 }
265
266 uint16_t bootstrap_port = atoi(argv[argvoffset + 2]);
267 unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
268 tox_bootstrap(tox, argv[argvoffset + 1], bootstrap_port, binary_string, 0);
269 free(binary_string);
270
271 uint8_t *bin_id = hex_string_to_bin(temp_id);
272 uint32_t num = tox_friend_add(tox, bin_id, (const uint8_t *)"Install Gentoo", sizeof("Install Gentoo") - 1, 0);
273 free(bin_id);
274
275 if (num == UINT32_MAX) {
276 printf("\nSomething went wrong when adding friend.\n");
277 exit(1);
278 }
279
280 return tox;
281}
282
283int main(int argc, char *argv[])
284{
285 Tox *tox = init_tox(argc, argv);
286
287 sock = reconnect();
288
289 if (sock < 0) {
290 return 1;
291 }
292
293 uint64_t last_get = get_monotime_sec();
294 int connected = 0, ping_sent = 0;
295
296 while (1) {
297 int count = 0;
298 ioctl(sock, FIONREAD, &count);
299
300 if (count > 0) {
301 last_get = get_monotime_sec();
302 ping_sent = 0;
303 uint8_t *data = (uint8_t *)calloc(count + 1, 1);
304 recv(sock, data, count, MSG_NOSIGNAL);
305 printf("%s", data);
306
307 if (!connected) {
308 connected = 1;
309 }
310
311 if (count > 6 && data[0] == 'P' && data[1] == 'I' && data[2] == 'N' && data[3] == 'G') {
312 data[1] = 'O';
313 unsigned int i;
314
315 for (i = 0; i < count; ++i) {
316 if (data[i] == '\n') {
317 ++i;
318 break;
319 }
320 }
321
322 send(sock, data, i, MSG_NOSIGNAL);
323 }
324
325 unsigned int i, p_i = 0;
326
327 for (i = 1; data[0] == ':' && i < count; ++i) {
328 if (data[i] == ' ') {
329 if (i + 5 < count && memcmp(data + i, " 404 ", 5) == 0) {
330 connected = 1;
331 }
332
333 break;
334 }
335
336 if (data[i] == ':') {
337 break;
338 }
339 }
340
341 for (i = 0; i < count; ++i) {
342 if (data[i] == '\n' && i != 0) {
343 send_irc_group(tox, data + p_i, i - p_i);
344 p_i = i + 1;
345 }
346 }
347
348 free(data);
349 }
350
351 if (connected == 1) {
352 send(sock, CHANNEL_JOIN, sizeof(CHANNEL_JOIN) - 1, MSG_NOSIGNAL);
353 connected = 2;
354 }
355
356 if (!ping_sent && last_get + (SILENT_TIMEOUT / 2) < get_monotime_sec()) {
357 unsigned int p_s = sizeof("PING :test\n") - 1;
358
359 if (send(sock, "PING :test\n", p_s, MSG_NOSIGNAL) == p_s) {
360 ping_sent = 1;
361 }
362 }
363
364 int error = 0;
365 socklen_t len = sizeof(error);
366
367 if (sock < 0 || last_get + SILENT_TIMEOUT < get_monotime_sec()
368 || getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) != 0) {
369 close(sock);
370 printf("reconnect\n");
371 sock = reconnect();
372
373 if (sock >= 0) {
374 last_get = get_monotime_sec();
375 connected = 0;
376 ping_sent = 0;
377 }
378 }
379
380 tox_iterate(tox, nullptr);
381 usleep(1000 * 50);
382 }
383}