Exemplo n.º 1
0
void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length)
{
    fputs("OK\nsending status to parent", stdout);
    fflush(stdout);
    m_addfriend_norequest(public_key);
    request_flags |= FIRST_FLAG;
}
Exemplo n.º 2
0
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
{
    printf("Friend request received from: \n");
    printf("ClientID: ");
    uint32_t j;

    for (j = 0; j < 32; j++) {
        if (public_key[j] < 16)
            printf("0");

        printf("%hhX", public_key[j]);
    }

    printf("\nOf length: %u with data: %s \n", length, data);

    if (length != sizeof("Install Gentoo")) {
        return;
    }

    if (memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 )
        //if the request contained the message of peace the person is obviously a friend so we add him.
    {
        printf("Friend request accepted.\n");
        m_addfriend_norequest(m, public_key);
    }
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    Suite *messenger = messenger_suite();
    SRunner *test_runner = srunner_create(messenger);
    int number_failed = 0;

    friend_id = hex_string_to_bin(friend_id_str);
    good_id_a = hex_string_to_bin(good_id_a_str);
    good_id_b = hex_string_to_bin(good_id_b_str);
    bad_id    = hex_string_to_bin(bad_id_str);

    /* setup a default friend and friendnum */
    if(m_addfriend_norequest((uint8_t *)friend_id) < 0)
        fputs("m_addfriend_norequest() failed on a valid ID!\n"
              "this was CRITICAL to the test, and the build WILL fail.\n"
              "the tests will continue now...\n\n", stderr);

    if((friend_id_num = getfriend_id((uint8_t *)friend_id)) < 0)
        fputs("getfriend_id() failed on a valid ID!\n"
              "this was CRITICAL to the test, and the build WILL fail.\n"
              "the tests will continue now...\n\n", stderr);

    srunner_run_all(test_runner, CK_NORMAL);
    number_failed = srunner_ntests_failed(test_runner);

    srunner_free(test_runner);
    free(friend_id);
    free(good_id_a);
    free(good_id_b);
    free(bad_id);

    return number_failed;
}
Exemplo n.º 4
0
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
{
    new_lines("[i] received friend request");
    do_refresh();
    if(memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 )
    //if the request contained the message of peace the person is obviously a friend so we add him.
    {
        new_lines("[i] friend request accepted");
        do_refresh();
        int num = m_addfriend_norequest(public_key);
        char numchar[100];
        sprintf(numchar, "[i] added friendnumber %d", num);
        new_lines(numchar);
    }
}
Exemplo n.º 5
0
void cmd_accept(ToxWindow *self, Messenger *m, char **args)
{
  int num = atoi(args[1]);
  if (num >= num_requests) {
    wprintw(self->window, "Invalid syntax.\n");
    return;
  }

  num = m_addfriend_norequest(m, pending_requests[num]);
  if (num == -1)
    wprintw(self->window, "Failed to add friend.\n");
  else {
    wprintw(self->window, "Friend accepted as: %d.\n", num);
    on_friendadded(num);
  }
}
Exemplo n.º 6
0
void accept_friend_request()
{
    friend_request_received = 0;
    uint8_t numf = atoi(line + 3);
    char numchar[100];
    if (numf >= num_requests || pending_requests[numf].accepted) {
        sprintf(numchar, "\n[i] you either didn't receive that request or you already accepted it");
        printf(numchar);
    } else {
        int num = m_addfriend_norequest(pending_requests[numf].id);
        if (num != -1) {
            pending_requests[numf].accepted = 1;
            sprintf(numchar, "\n[i] Added friendnumber: %d\n\n", num);
            printf(numchar);
            ++maxnumfriends;
        } else {
            sprintf(numchar, "[i] failed to add friend");
            printf(numchar);
        }
    }
}
Exemplo n.º 7
0
int main(int argc, char *argv[])
{
    Suite *messenger = messenger_suite();
    SRunner *test_runner = srunner_create(messenger);
    int number_failed = 0;

    friend_id = hex_string_to_bin(friend_id_str);
    good_id_a = hex_string_to_bin(good_id_a_str);
    good_id_b = hex_string_to_bin(good_id_b_str);
    bad_id    = hex_string_to_bin(bad_id_str);

    /* IPv6 status from global define */
    Messenger_Options options = {0};
    options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
    m = new_messenger(&options);

    /* setup a default friend and friendnum */
    if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0)
        fputs("m_addfriend_norequest() failed on a valid ID!\n"
              "this was CRITICAL to the test, and the build WILL fail.\n"
              "the tests will continue now...\n\n", stderr);

    if ((friend_id_num = getfriend_id(m, (uint8_t *)friend_id)) < 0)
        fputs("getfriend_id() failed on a valid ID!\n"
              "this was CRITICAL to the test, and the build WILL fail.\n"
              "the tests will continue now...\n\n", stderr);

    srunner_run_all(test_runner, CK_NORMAL);
    number_failed = srunner_ntests_failed(test_runner);

    srunner_free(test_runner);
    free(friend_id);
    free(good_id_a);
    free(good_id_b);
    free(bad_id);

    kill_messenger(m);

    return number_failed;
}
Exemplo n.º 8
0
/* load the messenger from data of size length. */
int Messenger_load(uint8_t * data, uint32_t length)
{
    if (length == ~0)
        return -1;
    if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2)
        return -1;
    length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2;
    load_keys(data);
    data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
    uint32_t size;
    memcpy(&size, data, sizeof(size));
    data += sizeof(size);

    if (length < size)
        return -1;
    length -= size;
    if (DHT_load(data, size) == -1)
        return -1;
    data += size;
    memcpy(&size, data, sizeof(size));
    data += sizeof(size);
    if (length != size || length % sizeof(Friend) != 0)
        return -1;

    Friend * temp = malloc(size);
    memcpy(temp, data, size);

    uint16_t num = size / sizeof(Friend);

    uint32_t i;
    for (i = 0; i < num; ++i) {
        if(temp[i].status != 0) {
            int fnum = m_addfriend_norequest(temp[i].client_id);
            setfriendname(fnum, temp[i].name);
            /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
        }
    }
    free(temp);
    return 0;
}
Exemplo n.º 9
0
void line_eval(char* line)
{
    if(line[0] == '/') {
        /* Add friend */
        if(line[1] == 'f') {
            int i;
            char temp_id[128];
            for (i=0; i<128; i++) 
                temp_id[i] = line[i+3];
            int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
            char numstring[100];
            sprintf(numstring, "\n[i] added friend %d\n\n", num);
            printf(numstring);
        }

        else if (line[1] == 'd') {
            doMessenger();
        }
        /* Send message to friend */
        else if (line[1] == 'm') {
            int i;
            size_t len = strlen(line);
            char numstring[len-3];
            char message[len-3];
            for (i=0; i<len; i++) {
                if (line[i+3] != ' ') {
                    numstring[i] = line[i+3];
                } else {
                    int j;
                    for (j=i+1; j<len; j++)
                        message[j-i-1] = line[j+3];
                    break;
                }
            }
            int num = atoi(numstring);
            if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
                printf("\n[i] could not send message: %s\n\n", message);
            }
        }

        else if (line[1] == 'n') {
            uint8_t name[MAX_NAME_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i=3; i<len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                name[i - 3] = line[i];
            }
            name[i - 3] = 0;
            setname(name, i);
            char numstring[100];
            sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name);
            printf(numstring);
        }

        else if (line[1] == 's') {
            uint8_t status[MAX_USERSTATUS_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i=3; i<len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                status[i - 3] = line[i];
            }
            status[i - 3] = 0;
            m_set_userstatus(status, strlen((char*)status));
            char numstring[100];
            sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status);
            printf(numstring);
        }

        else if (line[1] == 'a') {
            uint8_t numf = atoi(line + 3);
            char numchar[100];
            sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf);
            printf(numchar);
            int num = m_addfriend_norequest(pending_requests[numf]);
            sprintf(numchar, "\n[i] added friendnumber %d\n\n", num);
            printf(numchar);
        }
        /* EXIT */
        else if (line[1] == 'q') { 
            exit(EXIT_SUCCESS);
        }
    } 
    
    else {
        //nothing atm
    }
}
Exemplo n.º 10
0
/* Add a friend without sending a friendrequest.
 *
 *  return the friend number if success.
 *  return -1 if failure.
 */
int tox_addfriend_norequest(void *tox, uint8_t *client_id)
{
    Messenger *m = tox;
    return m_addfriend_norequest(m, client_id);
}
Exemplo n.º 11
0
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
{
    if (line[0] == '/') {
        char command[STRING_LENGTH + 2] = "> ";
        strcat(command, line);
        new_lines(command);
        if (line[1] == 'f') { // add friend command: /f ID
            int i;
            char temp_id[128];
            for (i=0; i<128; i++) 
                temp_id[i] = line[i+3];
            int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
            char numstring[100];
            sprintf(numstring, "[i] added friend %d", num);
            new_lines(numstring);
            do_refresh();
        }
        else if (line[1] == 'd') {
            doMessenger();
        }
        else if (line[1] == 'm') { //message command: /m friendnumber messsage
            int i;
            size_t len = strlen(line);
            char numstring[len-3];
            char message[len-3];
            for (i=0; i<len; i++) {
                if (line[i+3] != ' ') {
                    numstring[i] = line[i+3];
                } else {
                    int j;
                    for (j=i+1; j<len; j++)
                        message[j-i-1] = line[j+3];
                    break;
                }
            }
            int num = atoi(numstring);
            if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
                new_lines("Error sending message.");
            }
        }
        else if (line[1] == 'n') {
            uint8_t name[MAX_NAME_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i=3; i<len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                name[i - 3] = line[i];
            }
            name[i - 3] = 0;
            setname(name, i);
            char numstring[100];
            sprintf(numstring, "[i] changed nick to %s", (char*)name);
            new_lines(numstring);
        }
        else if (line[1] == 's') {
            uint8_t status[MAX_USERSTATUS_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i=3; i<len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                status[i - 3] = line[i];
            }
            status[i - 3] = 0;
            m_set_userstatus(status, strlen((char*)status));
            char numstring[100];
            sprintf(numstring, "[i] changed status to %s", (char*)status);
            new_lines(numstring);
        }
        else if (line[1] == 'a') {
            uint8_t numf = atoi(line + 3);
            char numchar[100];
            sprintf(numchar, "[i] friend request %u accepted", numf);
            new_lines(numchar);
            int num = m_addfriend_norequest(pending_requests[numf]);
            sprintf(numchar, "[i] added friendnumber %d", num);
            new_lines(numchar);
            do_refresh();
            
        }
        else if (line[1] == 'q') { //exit
            endwin();
            exit(EXIT_SUCCESS);
        }
    } else {
        //new_lines(line);
    }
}
Exemplo n.º 12
0
static void execute(ToxWindow* self, char* cmd) {

  // quit/exit: Exit program.
  if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) {
    endwin();
    exit(0);
  }
  else if(!strncmp(cmd, "connect ", strlen("connect "))) {
    char* ip;
    char* port;
    char* key;
    IP_Port dht;

    ip = strchr(cmd, ' ');
    if(ip == NULL) {
      return;
    }

    ip++;

    port = strchr(ip, ' ');
    if(port == NULL) {
      return;
    }

    port[0] = 0;
    port++;

    key = strchr(port, ' ');
    if(key == NULL) {
      return;
    }

    key[0] = 0;
    key++;

    if(atoi(port) == 0) {
      return;
    }

    wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key);

    dht.port = htons(atoi(port));

    int resolved_address = resolve_addr(ip);
    if (resolved_address == -1) {
      return;
    }

    dht.ip.i = resolved_address;
    DHT_bootstrap(dht, hex_string_to_bin(key));
  }
  else if(!strncmp(cmd, "add ", strlen("add "))) {
    char* id;
    char* msg;
    int num;

    id = strchr(cmd, ' ');

    if(id == NULL) {
      return;
    }

    id++;

    msg = strchr(id, ' ');
    if(msg == NULL) {
      return;
    }

    msg[0] = 0;
    msg++;

    num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1);
    wprintw(self->window, "Friend added as %d.\n", num);
  }
  else if(!strncmp(cmd, "status ", strlen("status "))) {
    char* msg;

    msg = strchr(cmd, ' ');
    if(msg == NULL) {
      return;
    }

    msg++;
    m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
    wprintw(self->window, "Status set to: %s.\n", msg);
  }
  else if(!strncmp(cmd, "nick ", strlen("nick "))) {
    char* nick;

    nick = strchr(cmd, ' ');
    if(nick == NULL) {
      return;
    }

    nick++;
    setname((uint8_t*) nick, strlen(nick)+1);
    wprintw(self->window, "Nickname set to: %s.\n", nick);
  }
  else if(!strcmp(cmd, "myid")) {
    // XXX: Clean this up
    char idstring0[200];
    char idstring1[32][5];
    char idstring2[32][5];
    uint32_t i;

    for(i = 0; i < 32; i++) {
      if(self_public_key[i] < 16)
	strcpy(idstring1[i], "0");
      else 
	strcpy(idstring1[i], "");

      sprintf(idstring2[i], "%hhX", self_public_key[i]);
    }
    
    for (i=0; i<32; i++) {
      strcat(idstring0, idstring1[i]);
      strcat(idstring0, idstring2[i]);
    }

    wprintw(self->window, "%s\n", idstring0);
  }
  else if(!strncmp(cmd, "accept ", strlen("accept "))) {
    char* id;
    int num;

    id = strchr(cmd, ' ');
    if(id == NULL) {
      return;
    }
    id++;
   
    num = atoi(id);
    if(num >= num_requests) {
      return;
    }

    num = m_addfriend_norequest(pending_requests[num]);
    wprintw(self->window, "Friend accepted as: %d.\n", num);
  }
  else if(!strncmp(cmd, "msg ", strlen("msg "))) {
    char* id;
    char* msg;

    id = strchr(cmd, ' ');

    if(id == NULL) {
      return;
    }

    id++;

    msg = strchr(id, ' ');
    if(msg == NULL) {
      return;
    }

    msg[0] = 0;
    msg++;

    if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) {
      wprintw(self->window, "Error occurred while sending message.\n");
    }
    else {
      wprintw(self->window, "Message successfully sent.\n");
    }
  }
}
Exemplo n.º 13
0
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
{
    if (line[0] == '/') {
        char inpt_command = line[1];
        char prompt[STRING_LENGTH + 2] = "> ";
        strcat(prompt, line);
        new_lines(prompt);
        if (inpt_command == 'f') { // add friend command: /f ID
            int i;
            char temp_id[128];
            for (i = 0; i < 128; i++) 
                temp_id[i] = line[i+3];
            int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
            char numstring[100];
            sprintf(numstring, "[i] added friend %d", num);
            new_lines(numstring);
            do_refresh();
        }
        else if (inpt_command == 'd') {
            doMessenger();
        }
        else if (inpt_command == 'm') { //message command: /m friendnumber messsage
            size_t len = strlen(line);
            char numstring[len-3];
            char message[len-3];
            int i;
            for (i = 0; i < len; i++) {
                if (line[i+3] != ' ') {
                    numstring[i] = line[i+3];
                } else {
                    int j;
                    for (j = (i+1); j < len; j++)
                        message[j-i-1] = line[j+3];
                    break;
                }
            }
            int num = atoi(numstring);
            if (m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
                new_lines("[i] could not send message");
            } else {
                new_lines(format_message(message, -1));
            }
        }
        else if (inpt_command == 'n') {
            uint8_t name[MAX_NAME_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i = 3; i < len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                name[i-3] = line[i];
            }
            name[i-3] = 0;
            setname(name, i);
            char numstring[100];
            sprintf(numstring, "[i] changed nick to %s", (char*)name);
            new_lines(numstring);
        }
        else if (inpt_command == 'l') {
            print_friendlist();
        }
        else if (inpt_command == 's') {
            uint8_t status[MAX_USERSTATUS_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i = 3; i < len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                status[i-3] = line[i];
            }
            status[i-3] = 0;
            m_set_userstatus(status, strlen((char*)status));
            char numstring[100];
            sprintf(numstring, "[i] changed status to %s", (char*)status);
            new_lines(numstring);
        }
        else if (inpt_command == 'a') {
            uint8_t numf = atoi(line + 3);
            char numchar[100];
            int num = m_addfriend_norequest(pending_requests[numf]);
            if (num != -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);
            }
            do_refresh();
        }
       else if (inpt_command == 'h') { //help
           new_lines("[i] commands: /f ID (to add friend), /m friendnumber message  (to send message), /s status (to change status)");
           new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)");
        }
       else if (inpt_command == 'i') { //info
           char idstring0[200];
           char idstring1[PUB_KEY_BYTES][5];
           char idstring2[PUB_KEY_BYTES][5];
           int i;
           for (i = 0; i < PUB_KEY_BYTES; i++)
           {
               if (self_public_key[i] < (PUB_KEY_BYTES/2))
                   strcpy(idstring1[i],"0");
               else 
                   strcpy(idstring1[i], "");
               sprintf(idstring2[i], "%hhX", self_public_key[i]);
           }
           //
           strcpy(idstring0,"[i] ID: ");
           int j;
           for (j = 0; j < PUB_KEY_BYTES; j++) {
               strcat(idstring0,idstring1[j]);
               strcat(idstring0,idstring2[j]);
           }    
                  new_lines(idstring0);
       }

        else if (inpt_command == 'q') { //exit
            endwin();
            exit(EXIT_SUCCESS);
        } else { 
            new_lines("[i] invalid command");
        }
    } else {
        new_lines("[i] invalid command");
        //new_lines(line);
    }
}
Exemplo n.º 14
0
/* Add a friend without sending a friendrequest.
 *
 *  return the friend number if success.
 *  return -1 if failure.
 */
int tox_add_friend_norequest(Tox *tox, size_t *client_id)
{
    Messenger *m = tox;
    return m_addfriend_norequest(m, client_id);
}
Exemplo n.º 15
0
void line_eval(char* line)
{
    if(line[0] == '/') {
        char inpt_command = line[1];
        /* Add friend */
        if(inpt_command == 'f') {
            int i;
            char temp_id[128];
            for (i = 0; i < 128; i++) 
                temp_id[i] = line[i+3];
            int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
            if (num >= 0) {
                char numstring[100];
                sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num);
                printf(numstring);
            }
            else if (num == -1) 
                printf("\n[i] Message is too long.\n\n");
            else if (num == -2)
                printf("\n[i] Please add a message to your friend request.\n\n");
            else if (num == -3)
                printf("\n[i] That appears to be your own ID.\n\n");
            else if (num == -4)
                printf("\n[i] Friend request already sent.\n\n");
            else if (num == -5)
                printf("\n[i] Undefined error when adding friend\n\n");
        }

        else if (inpt_command == 'r') {
            do_header();
            printf("\n\n");
        }

        else if (inpt_command == 'l') {
            int activefriends = 0;
            int i;

            for (i = 0; i <= getnumfriends(); i++)
            {
                if (m_friendstatus(i) == 4)
                    activefriends++;
            }

            printf("\n[i] Friend List | Total: %d\n\n", activefriends);

            for (i = 0; i <= getnumfriends(); i++) {
                char name[MAX_NAME_LENGTH];
                getname(i, (uint8_t*)name);
                if (m_friendstatus(i) == 4)    
                    printf("[%d] %s\n\n", i, (uint8_t*)name);
            }
        }

        else if (inpt_command == 'd') {
            size_t len = strlen(line);
            char numstring[len-3];
            int i;
            for (i = 0; i < len; i++) {
                if (line[i+3] != ' ') {
                    numstring[i] = line[i+3];
                }
            }
            int num = atoi(numstring);
            m_delfriend(num);
            printf("\n\n");
        }
        /* Send message to friend */
        else if (inpt_command == 'm') {
            size_t len = strlen(line);
            char numstring[len-3];
            char message[len-3];
            int i;
            for (i = 0; i < len; i++) {
                if (line[i+3] != ' ') {
                    numstring[i] = line[i+3];
                } else {
                    int j;
                    for (j = (i+1); j < len; j++)
                        message[j-i-1] = line[j+3];
                    break;
                }
            }
            int num = atoi(numstring);
            if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) {
                printf("\n[i] could not send message (they may be offline): %s\n", message);
            } else {
                //simply for aesthetics
                printf("\n");
            }
        }

        else if (inpt_command == 'n') {
            uint8_t name[MAX_NAME_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i = 3; i < len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                name[i-3] = line[i];
            }
            name[i-3] = 0;
            setname(name, i);
            char numstring[100];
            sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name);
            printf(numstring);

            FILE *name_file = NULL;
            name_file = fopen("namefile.txt", "w");
            fprintf(name_file, "%s", (char*)name);
            fclose(name_file);
        }

        else if (inpt_command == 's') {
            uint8_t status[MAX_USERSTATUS_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i = 3; i < len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                status[i-3] = line[i];
            }
            status[i-3] = 0;
            m_set_userstatus(status, strlen((char*)status));
            char numstring[100];
            sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status);
            printf(numstring);

            FILE* status_file = NULL;
            status_file = fopen("statusfile.txt", "w");
            fprintf(status_file, "%s", (char*)status);
            fclose(status_file);
        }

        else if (inpt_command == 'a') {
            uint8_t numf = atoi(line + 3);
            char numchar[100];
            sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf);
            printf(numchar);
            int num = m_addfriend_norequest(pending_requests[numf]);
            sprintf(numchar, "\n[i] added friendnumber %d\n\n", num);
            printf(numchar);
        }
        /* EXIT */
        else if (inpt_command == 'q') { 
            uint8_t status[MAX_USERSTATUS_LENGTH] = "Offline";
            m_set_userstatus(status, strlen((char*)status));
            exit(EXIT_SUCCESS);
        }
    } else {
        //nothing atm
    }
}
Exemplo n.º 16
0
/* load the messenger from data of size length. */
int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
{
    if (length == ~0)
        return -1;

    if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3)
        return -1;

    length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3;
    load_keys(data);
    data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
    uint32_t nospam;
    memcpy(&nospam, data, sizeof(nospam));
    set_nospam(nospam);
    data += sizeof(nospam);
    uint32_t size;
    memcpy(&size, data, sizeof(size));
    data += sizeof(size);

    if (length < size)
        return -1;

    length -= size;

    if (DHT_load(data, size) == -1)
        return -1;

    data += size;
    memcpy(&size, data, sizeof(size));
    data += sizeof(size);

    if (length < size || size % sizeof(Friend) != 0)
        return -1;

    Friend *temp = malloc(size);
    memcpy(temp, data, size);

    uint16_t num = size / sizeof(Friend);

    uint32_t i;

    for (i = 0; i < num; ++i) {
        if (temp[i].status >= 3) {
            int fnum = m_addfriend_norequest(m, temp[i].client_id);
            setfriendname(m, fnum, temp[i].name);
            /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
        } else if (temp[i].status != 0) {
            /* TODO: this is not a good way to do this. */
            uint8_t address[FRIEND_ADDRESS_SIZE];
            memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES);
            memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t));
            uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
            memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum));
            m_addfriend(m, address, temp[i].info, temp[i].info_size);
        }
    }

    free(temp);
    data += size;
    length -= size;

    uint16_t small_size;

    if (length < sizeof(small_size))
        return -1;

    memcpy(&small_size, data, sizeof(small_size));
    data += sizeof(small_size);
    length -= sizeof(small_size);

    if (length != small_size)
        return -1;

    setname(m, data, small_size);

    return 0;
}
Exemplo n.º 17
0
/* Add a friend without sending a friendrequest.
 *
 *  return the friend number if success.
 *  return -1 if failure.
 */
int32_t tox_add_friend_norequest(Tox *tox, const uint8_t *client_id)
{
    Messenger *m = tox;
    return m_addfriend_norequest(m, client_id);
}
Exemplo n.º 18
0
void line_eval(char *line)
{
    if (line[0] == '/') {
        char inpt_command = line[1];
        char prompt[STRING_LENGTH+2] = "> ";
        int prompt_offset = 3;
        strcat(prompt, line);
        new_lines(prompt);
        if (inpt_command == 'f') { // add friend command: /f ID
            int i;
            char temp_id[128];
            for (i = 0; i < 128; i++)
                temp_id[i] = line[i+prompt_offset];

            unsigned char *bin_string = hex_string_to_bin(temp_id);
            int num = m_addfriend(bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
            free(bin_string);
            char numstring[100];
            switch (num) {
            case FAERR_TOOLONG:
                sprintf(numstring, "[i] Message is too long.");
                break;
            case FAERR_NOMESSAGE:
                sprintf(numstring, "[i] Please add a message to your request.");
                break;
            case FAERR_OWNKEY:
                sprintf(numstring, "[i] That appears to be your own ID.");
                break;
            case FAERR_ALREADYSENT:
                sprintf(numstring, "[i] Friend request already sent.");
                break;
            case FAERR_UNKNOWN:
                sprintf(numstring, "[i] Undefined error when adding friend.");
                break;
            default:
                sprintf(numstring, "[i] Added friend as %d.", num);
                break;
            }
            new_lines(numstring);
            do_refresh();
        }
        else if (inpt_command == 'd') {
            doMessenger();
        }
        else if (inpt_command == 'm') { //message command: /m friendnumber messsage
            size_t len = strlen(line);
            if(len < 3)
                return;

            char numstring[len-3];
            char message[len-3];
            int i;
            for (i = 0; i < len; i++) {
                if (line[i+3] != ' ') {
                    numstring[i] = line[i+3];
                } else {
                    int j;
                    for (j = (i+1); j < (len+1); j++)
                        message[j-i-1] = line[j+3];
                    break;
                }
            }
            int num = atoi(numstring);
            if (m_sendmessage(num, (uint8_t*) message, strlen(message) + 1) != 1) {
                new_lines("[i] could not send message");
            } else {
                new_lines(format_message(message, -1));
            }
        }
        else if (inpt_command == 'n') {
            uint8_t name[MAX_NAME_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i = 3; i < len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                name[i-3] = line[i];
            }
            name[i-3] = 0;
            setname(name, i - 2);
            char numstring[100];
            sprintf(numstring, "[i] changed nick to %s", (char*)name);
            new_lines(numstring);
        }
        else if (inpt_command == 'l') {
            print_friendlist();
        }
        else if (inpt_command == 's') {
            uint8_t status[MAX_STATUSMESSAGE_LENGTH];
            int i = 0;
            size_t len = strlen(line);
            for (i = 3; i < len; i++) {
                if (line[i] == 0 || line[i] == '\n') break;
                status[i-3] = line[i];
            }
            status[i-3] = 0;
            m_set_statusmessage(status, strlen((char*)status) + 1);
            char numstring[100];
            sprintf(numstring, "[i] changed status to %s", (char*)status);
            new_lines(numstring);
        }
        else if (inpt_command == 'a') {
            uint8_t numf = atoi(line + 3);
            char numchar[100];
            if (numf >= num_requests || pending_requests[numf].accepted) {
                sprintf(numchar,"[i] you either didn't receive that request or you already accepted it");
                new_lines(numchar);
            } else {
                int num = m_addfriend_norequest(pending_requests[numf].id);
                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);
                }
            }
            do_refresh();
        }
       else if (inpt_command == 'h') { //help
           new_lines(help);
        }
       else if (inpt_command == 'i') { //info
           char idstring[200];
           get_id(idstring);
           new_lines(idstring);
       }

        else if (inpt_command == 'q') { //exit
            endwin();
            exit(EXIT_SUCCESS);
        } else {
            new_lines("[i] invalid command");
        }
    } else {
        new_lines("[i] invalid command");
        //new_lines(line);
    }
}
Exemplo n.º 19
0
static void execute(ToxWindow* self, char* cmd) {

  if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) {
    endwin();
    exit(0);
  }
  else if(!strncmp(cmd, "connect ", strlen("connect "))) {
    char* ip;
    char* port;
    char* key;
    IP_Port dht;

    ip = strchr(cmd, ' ');
    if(ip == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    ip++;

    port = strchr(ip, ' ');
    if(port == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    port[0] = 0;
    port++;

    key = strchr(port, ' ');
    if(key == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    key[0] = 0;
    key++;

    if(atoi(port) == 0) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }

    dht.port = htons(atoi(port));

    uint32_t resolved_address = resolve_addr(ip);
    if (resolved_address == 0) {
      return;
    }

    dht.ip.i = resolved_address;
    DHT_bootstrap(dht, hex_string_to_bin(key));
  }
  else if(!strncmp(cmd, "add ", strlen("add "))) {
    uint8_t id_bin[32];
    size_t i;
    char xx[3];
    uint32_t x;

    char* id;
    char* msg;
    int num;

    id = strchr(cmd, ' ');
    if(id == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    id++;

    msg = strchr(id, ' ');
    if(msg != NULL) {
      msg[0] = 0;
      msg++;
    }
    else msg = "";

    if(strlen(id) != 2*32) {
      wprintw(self->window, "Invalid ID length.\n");
      return;
    }

    for(i=0; i<32; i++) {
      xx[0] = id[2*i];
      xx[1] = id[2*i+1];
      xx[2] = '\0';

      if(sscanf(xx, "%02x", &x) != 1) {
        wprintw(self->window, "Invalid ID.\n");
        return;
      }

      id_bin[i] = x;
    }

    num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
    switch (num) {
    case -1: 
      wprintw(self->window, "Message is too long.\n");
      break;
    case -2:
      wprintw(self->window, "Please add a message to your request.\n");
      break;
    case -3:
      wprintw(self->window, "That appears to be your own ID.\n");
      break;
    case -4:
      wprintw(self->window, "Friend request already sent.\n");
      break;
    case -5:
      wprintw(self->window, "[i] Undefined error when adding friend.\n");
      break; 
    default:
      wprintw(self->window, "Friend added as %d.\n", num);
      on_friendadded(num);
      break;
    }
  }

  else if(!strcmp(cmd, "help")) {
	  print_usage(self);
  }
  else if(!strncmp(cmd, "status ", strlen("status "))) {
    char* msg;

    msg = strchr(cmd, ' ');
    if(msg == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    msg++;

    m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
    wprintw(self->window, "Status set to: %s\n", msg);
  }
  else if(!strncmp(cmd, "nick ", strlen("nick "))) {
    char* nick;

    nick = strchr(cmd, ' ');
    if(nick == NULL) {
      return;
    }
    nick++;

    setname((uint8_t*) nick, strlen(nick)+1);
    wprintw(self->window, "Nickname set to: %s.\n", nick);
  }
  else if(!strcmp(cmd, "myid")) {
    char id[32*2 + 1] = {0};
    size_t i;

    for(i=0; i<32; i++) {
      char xx[3];
      snprintf(xx, sizeof(xx), "%02x",  self_public_key[i] & 0xff);
      strcat(id, xx);
    }
    
    wprintw(self->window, "%s\n", id);
  }
  else if(!strncmp(cmd, "accept ", strlen("accept "))) {
    char* id;
    int num;

    id = strchr(cmd, ' ');
    if(id == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    id++;

    num = atoi(id);
    if(num >= num_requests) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }

    num = m_addfriend_norequest(pending_requests[num]);

    if(num == -1) {
      wprintw(self->window, "Failed to add friend.\n");
    }
    else {
      wprintw(self->window, "Friend accepted as: %d.\n", num);
      on_friendadded(num);
    }
  }
  else if(!strncmp(cmd, "msg ", strlen("msg "))) {
    char* id;
    char* msg;

    id = strchr(cmd, ' ');

    if(id == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    id++;

    msg = strchr(id, ' ');
    if(msg == NULL) {
      wprintw(self->window, "Invalid syntax.\n");
      return;
    }
    msg[0] = 0;
    msg++;

    if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) {
      wprintw(self->window, "Error occurred while sending message.\n");
    }
    else {
      wprintw(self->window, "Message successfully sent.\n");
    }
  }
  else {
    wprintw(self->window, "Invalid syntax.\n");
  }
}