2013-07-26 09:45:56 +08:00
|
|
|
/* nTox.c
|
|
|
|
*
|
|
|
|
* Textual frontend for Tox.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2013-08-03 19:27:52 +08:00
|
|
|
*
|
2013-07-26 09:45:56 +08:00
|
|
|
*/
|
2013-08-27 19:06:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2013-08-30 19:53:51 +08:00
|
|
|
#include "config.h"
|
2013-08-27 19:06:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __WIN32__
|
2013-08-30 19:53:51 +08:00
|
|
|
#define _WIN32_WINNT 0x501
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2013-08-27 19:06:19 +08:00
|
|
|
#else
|
2013-08-30 19:53:51 +08:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netdb.h>
|
2013-08-27 19:06:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
#include "nTox.h"
|
2013-08-30 20:16:34 +08:00
|
|
|
#include "misc_tools.c"
|
2013-07-28 00:17:31 +08:00
|
|
|
|
2013-07-22 04:54:09 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2013-07-31 14:15:01 +08:00
|
|
|
|
2013-08-27 19:06:19 +08:00
|
|
|
#ifdef __WIN32__
|
2013-07-17 07:02:44 +08:00
|
|
|
#define c_sleep(x) Sleep(1*x)
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#define c_sleep(x) usleep(1000*x)
|
|
|
|
#endif
|
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
char lines[HISTORY][STRING_LENGTH];
|
|
|
|
char line[STRING_LENGTH];
|
2013-07-31 22:25:29 +08:00
|
|
|
|
2013-08-03 10:34:45 +08:00
|
|
|
char *help = "[i] commands:\n/f ID (to add friend)\n/m friendnumber message "
|
|
|
|
"(to send message)\n/s status (to change status)\n[i] /l list (l"
|
|
|
|
"ist friends)\n/h for help\n/i for info\n/n nick (to change nick"
|
|
|
|
"name)\n/q (to quit)";
|
2013-07-31 14:15:01 +08:00
|
|
|
int x, y;
|
2013-07-28 08:04:56 +08:00
|
|
|
|
2013-08-04 07:46:54 +08:00
|
|
|
typedef struct {
|
2013-08-23 22:26:52 +08:00
|
|
|
uint8_t id[TOX_CLIENT_ID_SIZE];
|
2013-08-04 07:46:54 +08:00
|
|
|
uint8_t accepted;
|
|
|
|
} Friend_request;
|
|
|
|
|
|
|
|
Friend_request pending_requests[256];
|
2013-07-31 17:52:08 +08:00
|
|
|
uint8_t num_requests = 0;
|
2013-07-28 08:04:56 +08:00
|
|
|
|
2013-08-27 19:06:19 +08:00
|
|
|
/*
|
|
|
|
resolve_addr():
|
|
|
|
address should represent IPv4 or a hostname with A record
|
|
|
|
|
|
|
|
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
|
|
|
|
returns 0 on failure
|
|
|
|
|
|
|
|
TODO: Fix ipv6 support
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint32_t resolve_addr(const char *address)
|
|
|
|
{
|
|
|
|
struct addrinfo *server = NULL;
|
|
|
|
struct addrinfo hints;
|
|
|
|
int rc;
|
|
|
|
uint32_t addr;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_INET; // IPv4 only right now.
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
|
|
|
|
|
|
|
|
#ifdef __WIN32__
|
|
|
|
int res;
|
|
|
|
WSADATA wsa_data;
|
|
|
|
|
|
|
|
res = WSAStartup(MAKEWORD(2, 2), &wsa_data);
|
2013-08-30 19:53:51 +08:00
|
|
|
|
|
|
|
if (res != 0) {
|
2013-08-27 19:06:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-08-30 19:53:51 +08:00
|
|
|
|
2013-08-27 19:06:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
rc = getaddrinfo(address, "echo", &hints, &server);
|
|
|
|
|
|
|
|
// Lookup failed.
|
|
|
|
if (rc != 0) {
|
|
|
|
#ifdef __WIN32__
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPv4 records only..
|
|
|
|
if (server->ai_family != AF_INET) {
|
|
|
|
freeaddrinfo(server);
|
|
|
|
#ifdef __WIN32__
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr;
|
|
|
|
|
|
|
|
freeaddrinfo(server);
|
|
|
|
#ifdef __WIN32__
|
2013-08-30 19:53:51 +08:00
|
|
|
WSACleanup();
|
2013-08-27 19:06:19 +08:00
|
|
|
#endif
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
void get_id(Tox *m, char *data)
|
2013-07-14 01:31:16 +08:00
|
|
|
{
|
2013-08-14 16:44:25 +08:00
|
|
|
sprintf(data, "[i] ID: ");
|
|
|
|
int offset = strlen(data);
|
2013-08-03 07:54:57 +08:00
|
|
|
int i = 0;
|
2013-08-23 22:26:52 +08:00
|
|
|
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
|
|
|
tox_getaddress(m, address);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
for (; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(data + 2 * i + offset, "%02X ", address[i]);
|
2013-08-03 07:51:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void new_lines(char *line)
|
|
|
|
{
|
|
|
|
int i = 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
for (i = HISTORY - 1; i > 0; i--)
|
|
|
|
strncpy(lines[i], lines[i - 1], STRING_LENGTH - 1);
|
2013-08-03 19:27:52 +08:00
|
|
|
|
2013-08-03 07:51:08 +08:00
|
|
|
strncpy(lines[0], line, STRING_LENGTH - 1);
|
2013-07-14 09:57:09 +08:00
|
|
|
do_refresh();
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
|
|
|
|
2013-07-31 02:30:33 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
void print_friendlist(Tox *m)
|
2013-07-30 15:19:07 +08:00
|
|
|
{
|
2013-08-23 22:26:52 +08:00
|
|
|
char name[TOX_MAX_NAME_LENGTH];
|
2013-08-03 09:22:02 +08:00
|
|
|
int i = 0;
|
2013-07-30 15:19:07 +08:00
|
|
|
new_lines("[i] Friend List:");
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
while (tox_getname(m, i, (uint8_t *)name) != -1) {
|
2013-08-03 09:22:02 +08:00
|
|
|
/* account for the longest name and the longest "base" string */
|
2013-08-23 22:26:52 +08:00
|
|
|
char fstring[TOX_MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")];
|
2013-08-03 09:22:02 +08:00
|
|
|
|
2013-07-30 15:19:07 +08:00
|
|
|
if (strlen(name) <= 0) {
|
2013-08-03 13:27:20 +08:00
|
|
|
sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i);
|
2013-07-30 15:26:58 +08:00
|
|
|
} else {
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(fstring, "[i] Friend: %s\n\tid: %i", (uint8_t *)name, i);
|
2013-07-30 15:19:07 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-03 12:45:34 +08:00
|
|
|
i++;
|
2013-07-30 15:19:07 +08:00
|
|
|
new_lines(fstring);
|
|
|
|
}
|
2013-08-03 09:22:02 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
if (i == 0)
|
2013-08-03 09:22:02 +08:00
|
|
|
new_lines("\tno friends! D:");
|
2013-07-30 15:19:07 +08:00
|
|
|
}
|
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
char *format_message(Tox *m, char *message, int friendnum)
|
2013-07-30 23:32:17 +08:00
|
|
|
{
|
2013-08-23 22:26:52 +08:00
|
|
|
char name[TOX_MAX_NAME_LENGTH];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-31 22:25:29 +08:00
|
|
|
if (friendnum != -1) {
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_getname(m, friendnum, (uint8_t *)name);
|
2013-07-31 16:36:02 +08:00
|
|
|
} else {
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_getselfname(m, (uint8_t *)name, sizeof(name));
|
2013-07-31 16:36:02 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
char *msg = malloc(100 + strlen(message) + strlen(name) + 1);
|
2013-07-31 22:25:29 +08:00
|
|
|
|
2013-07-30 23:32:17 +08:00
|
|
|
time_t rawtime;
|
2013-08-17 01:11:09 +08:00
|
|
|
struct tm *timeinfo;
|
2013-07-30 23:32:17 +08:00
|
|
|
time ( &rawtime );
|
|
|
|
timeinfo = localtime ( &rawtime );
|
2013-08-17 01:11:09 +08:00
|
|
|
char *time = asctime(timeinfo);
|
2013-07-30 23:32:17 +08:00
|
|
|
size_t len = strlen(time);
|
2013-08-17 01:11:09 +08:00
|
|
|
time[len - 1] = '\0';
|
|
|
|
|
2013-08-02 06:15:25 +08:00
|
|
|
if (friendnum != -1) {
|
|
|
|
sprintf(msg, "[%d] %s <%s> %s", friendnum, time, name, message);
|
|
|
|
} else {
|
|
|
|
// This message came from ourselves
|
|
|
|
sprintf(msg, "%s <%s> %s", time, name, message);
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-30 23:32:17 +08:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
/* forward declaration */
|
2013-09-13 15:16:48 +08:00
|
|
|
static int save_data(Tox *m);
|
2013-09-12 06:01:46 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
void line_eval(Tox *m, char *line)
|
2013-07-14 01:31:16 +08:00
|
|
|
{
|
|
|
|
if (line[0] == '/') {
|
2013-07-31 14:15:01 +08:00
|
|
|
char inpt_command = line[1];
|
2013-08-17 01:11:09 +08:00
|
|
|
char prompt[STRING_LENGTH + 2] = "> ";
|
2013-08-01 05:30:16 +08:00
|
|
|
int prompt_offset = 3;
|
2013-07-31 14:15:01 +08:00
|
|
|
strcat(prompt, line);
|
|
|
|
new_lines(prompt);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-31 14:15:01 +08:00
|
|
|
if (inpt_command == 'f') { // add friend command: /f ID
|
2013-07-14 01:31:16 +08:00
|
|
|
int i;
|
|
|
|
char temp_id[128];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-03 19:27:52 +08:00
|
|
|
for (i = 0; i < 128; i++)
|
2013-08-17 01:11:09 +08:00
|
|
|
temp_id[i] = line[i + prompt_offset];
|
2013-08-02 03:27:08 +08:00
|
|
|
|
2013-08-04 17:09:12 +08:00
|
|
|
unsigned char *bin_string = hex_string_to_bin(temp_id);
|
2013-08-23 22:26:52 +08:00
|
|
|
int num = tox_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
|
2013-08-04 17:09:12 +08:00
|
|
|
free(bin_string);
|
2013-07-14 01:31:16 +08:00
|
|
|
char numstring[100];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-01 05:30:16 +08:00
|
|
|
switch (num) {
|
2013-08-23 22:26:52 +08:00
|
|
|
case TOX_FAERR_TOOLONG:
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(numstring, "[i] Message is too long.");
|
|
|
|
break;
|
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
case TOX_FAERR_NOMESSAGE:
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(numstring, "[i] Please add a message to your request.");
|
|
|
|
break;
|
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
case TOX_FAERR_OWNKEY:
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(numstring, "[i] That appears to be your own ID.");
|
|
|
|
break;
|
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
case TOX_FAERR_ALREADYSENT:
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(numstring, "[i] Friend request already sent.");
|
|
|
|
break;
|
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
case TOX_FAERR_UNKNOWN:
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(numstring, "[i] Undefined error when adding friend.");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-09-12 06:01:46 +08:00
|
|
|
if (num >= 0)
|
|
|
|
sprintf(numstring, "[i] Added friend as %d.", num);
|
|
|
|
else
|
|
|
|
sprintf(numstring, "[i] Unknown error %i.", num);
|
2013-08-17 01:11:09 +08:00
|
|
|
break;
|
2013-08-01 05:30:16 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
new_lines(numstring);
|
|
|
|
do_refresh();
|
2013-08-17 01:11:09 +08:00
|
|
|
} else if (inpt_command == 'd') {
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_do(m);
|
2013-08-17 01:11:09 +08:00
|
|
|
} else if (inpt_command == 'm') { //message command: /m friendnumber messsage
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(line);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
if (len < 3)
|
2013-08-03 19:27:52 +08:00
|
|
|
return;
|
2013-08-02 04:15:55 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
char numstring[len - 3];
|
|
|
|
char message[len - 3];
|
2013-07-31 14:15:01 +08:00
|
|
|
int i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-31 14:15:01 +08:00
|
|
|
for (i = 0; i < len; i++) {
|
2013-08-17 01:11:09 +08:00
|
|
|
if (line[i + 3] != ' ') {
|
|
|
|
numstring[i] = line[i + 3];
|
2013-07-14 01:31:16 +08:00
|
|
|
} else {
|
|
|
|
int j;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
for (j = (i + 1); j < (len + 1); j++)
|
|
|
|
message[j - i - 1] = line[j + 3];
|
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
int num = atoi(numstring);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
if (tox_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) != 1) {
|
2013-07-30 02:17:30 +08:00
|
|
|
new_lines("[i] could not send message");
|
2013-07-30 23:10:20 +08:00
|
|
|
} else {
|
2013-08-11 11:24:11 +08:00
|
|
|
new_lines(format_message(m, message, -1));
|
2013-07-28 09:33:01 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
} else if (inpt_command == 'n') {
|
2013-08-23 22:26:52 +08:00
|
|
|
uint8_t name[TOX_MAX_NAME_LENGTH];
|
2013-07-19 01:56:50 +08:00
|
|
|
int i = 0;
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(line);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-31 14:15:01 +08:00
|
|
|
for (i = 3; i < len; i++) {
|
2013-07-19 01:56:50 +08:00
|
|
|
if (line[i] == 0 || line[i] == '\n') break;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
name[i - 3] = line[i];
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
name[i - 3] = 0;
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_setname(m, name, i - 2);
|
2013-07-19 01:56:50 +08:00
|
|
|
char numstring[100];
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(numstring, "[i] changed nick to %s", (char *)name);
|
2013-07-19 01:56:50 +08:00
|
|
|
new_lines(numstring);
|
2013-08-17 01:11:09 +08:00
|
|
|
} else if (inpt_command == 'l') {
|
2013-08-11 11:24:11 +08:00
|
|
|
print_friendlist(m);
|
2013-08-17 01:11:09 +08:00
|
|
|
} else if (inpt_command == 's') {
|
2013-08-23 22:26:52 +08:00
|
|
|
uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH];
|
2013-07-19 01:56:50 +08:00
|
|
|
int i = 0;
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(line);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-31 14:15:01 +08:00
|
|
|
for (i = 3; i < len; i++) {
|
2013-07-19 01:56:50 +08:00
|
|
|
if (line[i] == 0 || line[i] == '\n') break;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
status[i - 3] = line[i];
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
status[i - 3] = 0;
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_set_statusmessage(m, status, strlen((char *)status) + 1);
|
2013-07-19 01:56:50 +08:00
|
|
|
char numstring[100];
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(numstring, "[i] changed status to %s", (char *)status);
|
2013-07-19 01:56:50 +08:00
|
|
|
new_lines(numstring);
|
2013-08-17 01:11:09 +08:00
|
|
|
} else if (inpt_command == 'a') {
|
2013-07-28 08:04:56 +08:00
|
|
|
uint8_t numf = atoi(line + 3);
|
|
|
|
char numchar[100];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-04 19:01:20 +08:00
|
|
|
if (numf >= num_requests || pending_requests[numf].accepted) {
|
2013-08-17 01:11:09 +08:00
|
|
|
sprintf(numchar, "[i] you either didn't receive that request or you already accepted it");
|
2013-08-01 00:38:13 +08:00
|
|
|
new_lines(numchar);
|
|
|
|
} else {
|
2013-08-23 22:26:52 +08:00
|
|
|
int num = tox_addfriend_norequest(m, pending_requests[numf].id);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-04 07:46:54 +08:00
|
|
|
if (num != -1) {
|
|
|
|
pending_requests[numf].accepted = 1;
|
|
|
|
sprintf(numchar, "[i] friend request %u accepted", numf);
|
|
|
|
new_lines(numchar);
|
|
|
|
sprintf(numchar, "[i] added friendnumber %d", num);
|
|
|
|
new_lines(numchar);
|
|
|
|
} else {
|
|
|
|
sprintf(numchar, "[i] failed to add friend");
|
|
|
|
new_lines(numchar);
|
|
|
|
}
|
2013-08-01 00:38:13 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-28 08:04:56 +08:00
|
|
|
do_refresh();
|
2013-08-17 01:11:09 +08:00
|
|
|
} else if (inpt_command == 'h') { //help
|
|
|
|
new_lines(help);
|
2013-09-13 07:26:30 +08:00
|
|
|
} else if (inpt_command == 'x') { //info
|
2013-08-17 01:11:09 +08:00
|
|
|
char idstring[200];
|
|
|
|
get_id(m, idstring);
|
|
|
|
new_lines(idstring);
|
2013-09-13 07:26:30 +08:00
|
|
|
} else if (inpt_command == 'g') { //create new group chat
|
|
|
|
char msg[256];
|
2013-09-13 08:29:30 +08:00
|
|
|
sprintf(msg, "[g] Created new group chat with number: %u", tox_add_groupchat(m));
|
2013-09-13 07:26:30 +08:00
|
|
|
new_lines(msg);
|
|
|
|
} else if (inpt_command == 'i') { //invite friendnum to groupnum
|
|
|
|
char *posi[1];
|
|
|
|
int friendnumber = strtoul(line + prompt_offset, posi, 0);
|
|
|
|
int groupnumber = strtoul(*posi + 1, NULL, 0);
|
|
|
|
char msg[256];
|
|
|
|
sprintf(msg, "[g] Invited friend number %u to group number %u, returned: %u (0 means success)", friendnumber,
|
2013-09-13 08:29:30 +08:00
|
|
|
groupnumber, tox_invite_friend(m, friendnumber, groupnumber));
|
2013-09-13 07:26:30 +08:00
|
|
|
new_lines(msg);
|
|
|
|
} else if (inpt_command == 'z') { //send message to groupnum
|
|
|
|
char *posi[1];
|
|
|
|
int groupnumber = strtoul(line + prompt_offset, posi, 0);
|
|
|
|
|
|
|
|
if (**posi != 0) {
|
|
|
|
char msg[256 + 1024];
|
|
|
|
sprintf(msg, "[g] sent message: %s to group num: %u returned: %u (0 means success)", *posi + 1, groupnumber,
|
2013-09-13 08:29:30 +08:00
|
|
|
tox_group_message_send(m, groupnumber, (uint8_t *)*posi + 1, strlen(*posi + 1) + 1));
|
2013-09-13 07:26:30 +08:00
|
|
|
new_lines(msg);
|
|
|
|
}
|
|
|
|
} else if (inpt_command == 'q') { //exit
|
2013-09-12 06:01:46 +08:00
|
|
|
save_data(m);
|
2013-07-24 00:11:07 +08:00
|
|
|
endwin();
|
|
|
|
exit(EXIT_SUCCESS);
|
2013-08-03 19:27:52 +08:00
|
|
|
} else {
|
2013-07-30 17:31:02 +08:00
|
|
|
new_lines("[i] invalid command");
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
|
|
|
} else {
|
2013-07-30 17:31:02 +08:00
|
|
|
new_lines("[i] invalid command");
|
2013-07-14 09:57:09 +08:00
|
|
|
//new_lines(line);
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width)
|
2013-07-14 01:31:16 +08:00
|
|
|
{
|
2013-08-17 01:11:09 +08:00
|
|
|
strcpy(output, input);
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(output);
|
2013-07-31 14:15:01 +08:00
|
|
|
int i = 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
for (i = line_width; i < len; i = i + line_width) {
|
2013-07-14 01:31:16 +08:00
|
|
|
while (output[i] != ' ' && i != 0) {
|
|
|
|
i--;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
if (i > 0) {
|
|
|
|
output[i] = '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
int count_lines(char *string)
|
2013-07-14 01:31:16 +08:00
|
|
|
{
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(string);
|
2013-07-14 01:31:16 +08:00
|
|
|
int count = 1;
|
2013-07-31 14:15:01 +08:00
|
|
|
int i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-31 14:15:01 +08:00
|
|
|
for (i = 0; i < len; i++) {
|
2013-07-27 11:07:25 +08:00
|
|
|
if (string[i] == '\n')
|
2013-07-14 01:31:16 +08:00
|
|
|
count++;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *appender(char *str, const char c)
|
|
|
|
{
|
2013-07-27 04:16:58 +08:00
|
|
|
size_t len = strlen(str);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
if (len < STRING_LENGTH) {
|
2013-08-17 01:11:09 +08:00
|
|
|
str[len + 1] = str[len];
|
2013-07-14 01:31:16 +08:00
|
|
|
str[len] = c;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_refresh()
|
|
|
|
{
|
2013-08-17 01:11:09 +08:00
|
|
|
int count = 0;
|
2013-07-14 01:31:16 +08:00
|
|
|
char wrap_output[STRING_LENGTH];
|
2013-07-31 14:15:01 +08:00
|
|
|
int L;
|
|
|
|
int i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-31 14:15:01 +08:00
|
|
|
for (i = 0; i < HISTORY; i++) {
|
2013-07-14 01:31:16 +08:00
|
|
|
wrap(wrap_output, lines[i], x);
|
2013-07-31 14:15:01 +08:00
|
|
|
L = count_lines(wrap_output);
|
|
|
|
count = count + L;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
if (count < y) {
|
2013-08-17 01:11:09 +08:00
|
|
|
move(y - 1 - count, 0);
|
2013-07-14 01:31:16 +08:00
|
|
|
printw(wrap_output);
|
|
|
|
clrtoeol();
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
move(y - 1, 0);
|
2013-07-14 01:31:16 +08:00
|
|
|
clrtoeol();
|
|
|
|
printw(">> ");
|
|
|
|
printw(line);
|
|
|
|
clrtoeol();
|
|
|
|
refresh();
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
|
2013-07-14 01:31:16 +08:00
|
|
|
{
|
2013-07-28 08:04:56 +08:00
|
|
|
new_lines("[i] received friend request with message:");
|
|
|
|
new_lines((char *)data);
|
|
|
|
char numchar[100];
|
2013-07-30 02:17:30 +08:00
|
|
|
sprintf(numchar, "[i] accept request with /a %u", num_requests);
|
2013-07-28 08:04:56 +08:00
|
|
|
new_lines(numchar);
|
2013-08-23 22:26:52 +08:00
|
|
|
memcpy(pending_requests[num_requests].id, public_key, TOX_CLIENT_ID_SIZE);
|
2013-08-04 07:46:54 +08:00
|
|
|
pending_requests[num_requests].accepted = 0;
|
2013-07-28 08:04:56 +08:00
|
|
|
++num_requests;
|
2013-07-14 01:31:16 +08:00
|
|
|
do_refresh();
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
void print_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
2013-07-14 01:31:16 +08:00
|
|
|
{
|
2013-08-17 01:11:09 +08:00
|
|
|
new_lines(format_message(m, (char *)string, friendnumber));
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
2013-07-31 14:15:01 +08:00
|
|
|
{
|
2013-08-23 22:26:52 +08:00
|
|
|
char name[TOX_MAX_NAME_LENGTH];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) {
|
2013-08-17 01:11:09 +08:00
|
|
|
char msg[100 + length];
|
2013-08-04 06:02:09 +08:00
|
|
|
sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
|
|
|
|
new_lines(msg);
|
|
|
|
}
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
void print_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
2013-07-31 14:15:01 +08:00
|
|
|
{
|
2013-08-23 22:26:52 +08:00
|
|
|
char name[TOX_MAX_NAME_LENGTH];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) {
|
2013-08-17 01:11:09 +08:00
|
|
|
char msg[100 + length + strlen(name) + 1];
|
2013-08-04 06:02:09 +08:00
|
|
|
sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
|
|
|
|
new_lines(msg);
|
|
|
|
}
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
static char *data_file_name = NULL;
|
|
|
|
|
2013-09-13 15:16:48 +08:00
|
|
|
static int load_data(Tox *m)
|
2013-07-31 14:15:01 +08:00
|
|
|
{
|
2013-09-12 06:01:46 +08:00
|
|
|
FILE *data_file = fopen(data_file_name, "r");
|
2013-08-04 05:57:44 +08:00
|
|
|
int size = 0;
|
2013-07-31 14:15:01 +08:00
|
|
|
if (data_file) {
|
2013-07-24 00:11:07 +08:00
|
|
|
fseek(data_file, 0, SEEK_END);
|
2013-08-04 05:57:44 +08:00
|
|
|
size = ftell(data_file);
|
|
|
|
rewind(data_file);
|
|
|
|
|
2013-07-24 00:11:07 +08:00
|
|
|
uint8_t data[size];
|
2013-08-17 01:11:09 +08:00
|
|
|
if (fread(data, sizeof(uint8_t), size, data_file) != size) {
|
2013-09-12 20:19:22 +08:00
|
|
|
fputs("[!] could not read data file!\n", stderr);
|
2013-09-12 06:01:46 +08:00
|
|
|
return 0;
|
2013-07-24 00:11:07 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_load(m, data, size);
|
2013-08-03 12:45:34 +08:00
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
if (fclose(data_file) < 0) {
|
|
|
|
perror("[!] fclose failed");
|
2013-09-12 20:19:22 +08:00
|
|
|
/* we got it open and the expected data read... let it be ok */
|
|
|
|
/* return 0; */
|
2013-08-03 12:45:34 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-13 15:16:48 +08:00
|
|
|
static int save_data(Tox *m)
|
2013-09-12 06:01:46 +08:00
|
|
|
{
|
|
|
|
FILE *data_file = fopen(data_file_name, "w");
|
|
|
|
if (!data_file) {
|
|
|
|
perror("[!] load_key");
|
|
|
|
return 0;
|
2013-07-24 00:11:07 +08:00
|
|
|
}
|
2013-08-04 05:57:44 +08:00
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
int size = tox_size(m);
|
|
|
|
uint8_t data[size];
|
|
|
|
tox_save(m, data);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
if (fwrite(data, sizeof(uint8_t), size, data_file) != size) {
|
|
|
|
fputs("[!] could not write data file (1)!", stderr);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-04 09:59:17 +08:00
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
if (fclose(data_file) < 0) {
|
|
|
|
perror("[!] could not write data file (2)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-13 15:16:48 +08:00
|
|
|
static int load_old_key_or_save_new_one(Tox *m, char *path)
|
2013-09-12 06:01:46 +08:00
|
|
|
{
|
|
|
|
data_file_name = path;
|
|
|
|
if (load_data(m))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (save_data(m))
|
|
|
|
return 1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
return 0;
|
2013-07-23 01:09:30 +08:00
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-08-03 12:45:34 +08:00
|
|
|
void print_help(void)
|
|
|
|
{
|
|
|
|
printf("nTox %.1f - Command-line tox-core client\n", 0.1);
|
|
|
|
puts("Options:");
|
|
|
|
puts("\t-h\t-\tPrint this help and exit.");
|
2013-08-03 13:14:30 +08:00
|
|
|
puts("\t-f\t-\tSpecify a keyfile to read (or write to) from.");
|
2013-08-03 12:45:34 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 08:29:30 +08:00
|
|
|
void print_invite(Tox *m, int friendnumber, uint8_t *group_public_key, void *userdata)
|
2013-09-13 07:26:30 +08:00
|
|
|
{
|
|
|
|
char msg[256];
|
|
|
|
sprintf(msg, "[i] recieved group chat invite from: %u, auto accepting and joining. group number: %u", friendnumber,
|
2013-09-13 08:29:30 +08:00
|
|
|
tox_join_groupchat(m, friendnumber, group_public_key));
|
2013-09-13 07:26:30 +08:00
|
|
|
new_lines(msg);
|
|
|
|
}
|
|
|
|
|
2013-09-13 08:29:30 +08:00
|
|
|
void print_groupmessage(Tox *m, int groupnumber, uint8_t *message, uint16_t length, void *userdata)
|
2013-09-13 07:26:30 +08:00
|
|
|
{
|
|
|
|
char msg[256 + length];
|
|
|
|
sprintf(msg, "[g] %u: %s", groupnumber, message);
|
|
|
|
new_lines(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-08-03 12:45:34 +08:00
|
|
|
int on = 0;
|
|
|
|
int c = 0;
|
|
|
|
int i = 0;
|
|
|
|
char *filename = "data";
|
|
|
|
char idstring[200] = {0};
|
2013-08-23 22:26:52 +08:00
|
|
|
Tox *m;
|
2013-08-03 12:45:34 +08:00
|
|
|
|
2013-07-14 06:27:52 +08:00
|
|
|
if (argc < 4) {
|
2013-08-03 12:45:34 +08:00
|
|
|
printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]);
|
2013-07-14 06:27:52 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
2013-08-03 12:45:34 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (argv[i] == NULL) {
|
|
|
|
break;
|
|
|
|
} else if (argv[i][0] == '-') {
|
|
|
|
if (argv[i][1] == 'h') {
|
2013-08-03 12:45:34 +08:00
|
|
|
print_help();
|
|
|
|
exit(0);
|
2013-08-17 01:11:09 +08:00
|
|
|
} else if (argv[i][1] == 'f') {
|
|
|
|
if (argv[i + 1] != NULL)
|
2013-08-03 12:45:34 +08:00
|
|
|
filename = argv[i + 1];
|
|
|
|
else {
|
|
|
|
fputs("[!] you passed '-f' without giving an argument!\n", stderr);
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 19:26:25 +08:00
|
|
|
}
|
2013-07-22 08:52:02 +08:00
|
|
|
}
|
2013-08-03 12:45:34 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
m = tox_new();
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
if ( !m ) {
|
2013-08-11 11:24:11 +08:00
|
|
|
fputs("Failed to allocate Messenger datastructure", stderr);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
load_old_key_or_save_new_one(m, filename);
|
2013-08-03 12:45:34 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_callback_friendrequest(m, print_request, NULL);
|
|
|
|
tox_callback_friendmessage(m, print_message, NULL);
|
|
|
|
tox_callback_namechange(m, print_nickchange, NULL);
|
|
|
|
tox_callback_statusmessage(m, print_statuschange, NULL);
|
2013-09-13 08:29:30 +08:00
|
|
|
tox_callback_group_invite(m, print_invite, NULL);
|
|
|
|
tox_callback_group_message(m, print_groupmessage, NULL);
|
2013-08-03 07:51:08 +08:00
|
|
|
|
2013-07-14 01:31:16 +08:00
|
|
|
initscr();
|
|
|
|
noecho();
|
|
|
|
raw();
|
2013-07-31 14:15:01 +08:00
|
|
|
getmaxyx(stdscr, y, x);
|
2013-08-03 10:34:45 +08:00
|
|
|
|
|
|
|
new_lines("/h for list of commands");
|
2013-08-13 23:50:33 +08:00
|
|
|
get_id(m, idstring);
|
2013-08-03 07:51:08 +08:00
|
|
|
new_lines(idstring);
|
2013-07-14 01:31:16 +08:00
|
|
|
strcpy(line, "");
|
2013-08-03 10:34:45 +08:00
|
|
|
|
2013-08-23 22:34:10 +08:00
|
|
|
tox_IP_Port bootstrap_ip_port;
|
2013-07-14 06:27:52 +08:00
|
|
|
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
2013-07-21 05:56:18 +08:00
|
|
|
int resolved_address = resolve_addr(argv[1]);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-02 02:52:13 +08:00
|
|
|
if (resolved_address != 0)
|
2013-07-21 02:50:54 +08:00
|
|
|
bootstrap_ip_port.ip.i = resolved_address;
|
2013-08-03 19:27:52 +08:00
|
|
|
else
|
2013-07-21 02:50:54 +08:00
|
|
|
exit(1);
|
2013-08-03 19:27:52 +08:00
|
|
|
|
2013-08-04 17:09:12 +08:00
|
|
|
unsigned char *binary_string = hex_string_to_bin(argv[3]);
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_bootstrap(m, bootstrap_ip_port, binary_string);
|
2013-08-04 17:09:12 +08:00
|
|
|
free(binary_string);
|
2013-07-14 09:57:09 +08:00
|
|
|
nodelay(stdscr, TRUE);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-12 06:01:46 +08:00
|
|
|
new_lines("[i] change username with /n");
|
|
|
|
char name[TOX_MAX_NAME_LENGTH];
|
|
|
|
uint16_t namelen = tox_getselfname(m, name, sizeof(name));
|
|
|
|
if (namelen > 0) {
|
|
|
|
char whoami[128 + TOX_MAX_NAME_LENGTH];
|
|
|
|
snprintf(whoami, sizeof(whoami), "[i] your current username is: %s", name);
|
|
|
|
new_lines(whoami);
|
|
|
|
}
|
|
|
|
|
2013-08-27 19:06:19 +08:00
|
|
|
while (1) {
|
2013-08-23 22:26:52 +08:00
|
|
|
if (on == 0 && tox_isconnected(m)) {
|
2013-09-12 06:01:46 +08:00
|
|
|
new_lines("[i] connected to DHT");
|
2013-07-24 00:11:07 +08:00
|
|
|
on = 1;
|
2013-07-17 07:45:20 +08:00
|
|
|
}
|
2013-07-24 00:11:07 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_do(m);
|
2013-07-17 07:02:44 +08:00
|
|
|
c_sleep(1);
|
2013-07-14 01:31:16 +08:00
|
|
|
do_refresh();
|
2013-07-24 00:11:07 +08:00
|
|
|
|
|
|
|
c = getch();
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-24 00:11:07 +08:00
|
|
|
if (c == ERR || c == 27)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
getmaxyx(stdscr, y, x);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-27 19:06:19 +08:00
|
|
|
if ((c == 0x0d) || (c == 0x0a)) {
|
2013-08-11 11:24:11 +08:00
|
|
|
line_eval(m, line);
|
2013-07-24 00:11:07 +08:00
|
|
|
strcpy(line, "");
|
2013-08-03 19:27:52 +08:00
|
|
|
} else if (c == 8 || c == 127) {
|
2013-08-17 01:11:09 +08:00
|
|
|
line[strlen(line) - 1] = '\0';
|
2013-07-24 00:11:07 +08:00
|
|
|
} else if (isalnum(c) || ispunct(c) || c == ' ') {
|
|
|
|
strcpy(line, appender(line, (char) c));
|
|
|
|
}
|
2013-07-14 01:31:16 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-23 22:26:52 +08:00
|
|
|
tox_kill(m);
|
2013-07-14 01:31:16 +08:00
|
|
|
endwin();
|
|
|
|
return 0;
|
|
|
|
}
|