示例#1
0
文件: prompt.c 项目: notadecent/toxic
void cmd_status(ToxWindow *self, Messenger *m, char **args)
{
    char *status = args[1];
    char *status_text;

    USERSTATUS status_kind;

    if (!strncmp(status, "online", strlen("online"))) {
        status_kind = USERSTATUS_NONE;
        status_text = "ONLINE";
    } else if (!strncmp(status, "away", strlen("away"))) {
        status_kind = USERSTATUS_AWAY;
        status_text = "AWAY";
    } else if (!strncmp(status, "busy", strlen("busy"))) {
        status_kind = USERSTATUS_BUSY;
        status_text = "BUSY";
    } else {
        wprintw(self->window, "Invalid status.\n");
        return;
    }

    char *msg = args[2];

    if (msg == NULL) {
        m_set_userstatus(m, status_kind);
        wprintw(self->window, "Status set to: %s\n", status_text);
    } else {
        m_set_userstatus(m, status_kind);
        m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
        wprintw(self->window, "Status set to: %s, %s\n", status_text, msg);
    }
}
示例#2
0
void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
{
  if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
    wclear(self->window);
    wclear(ctx->history);
  }

  else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
    print_help(ctx);

  else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
    endwin();
    exit(0);
  }

  else if (!strncmp(cmd, "/status ", strlen("/status "))) {
    char *msg;
    msg = strchr(cmd, ' ');
    if (msg == NULL) {
      wprintw(ctx->history, "Invalid syntax.\n");
      return;
    }
    msg++;
    m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1);
    wprintw(ctx->history, "Status set to: %s\n", msg);
  }

  else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
    char *nick;
    nick = strchr(cmd, ' ');
    if (nick == NULL) {
      wprintw(ctx->history, "Invalid syntax.\n");
      return;
    }
    nick++;
    setname((uint8_t*) nick, strlen(nick)+1);
    wprintw(ctx->history, "Nickname set to: %s\n", nick);
  }

  else if (!strcmp(cmd, "/myid")) {
    char id[32*2 + 1] = {0};
    int i;
    for (i = 0; i < 32; i++) {
      char xx[3];
      snprintf(xx, sizeof(xx), "%02x",  self_public_key[i] & 0xff);
      strcat(id, xx);
    }
    wprintw(ctx->history, "Your ID: %s\n", id);
  }

  else if (strcmp(ctx->line, "/close") == 0) {
    active_window = 0;    // Go to prompt screen
    int f_num = ctx->friendnum;
    delwin(ctx->linewin);
    del_window(self, f_num);
  }

  else
    wprintw(ctx->history, "Invalid command.\n");
}
示例#3
0
void change_status(int savetofile)
{
    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(USERSTATUS_KIND_RETAIN, status, strlen((char*)status));
    char numstring[100];
    sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status);
    printf(numstring);

    if (savetofile == 1) {
        FILE* status_file = NULL;
        status_file = fopen("statusfile.txt", "w");
        fprintf(status_file, "%s", (char*)status);
        fclose(status_file);
    }
}
示例#4
0
/* run this at startup */
int initMessenger()
{
    new_keys();
    m_set_userstatus((uint8_t*)"Online", sizeof("Online"));
    initNetCrypto();
    IP ip;
    ip.i = 0;

    if(init_networking(ip,PORT) == -1)
        return -1;

    return 0;
}
示例#5
0
int main(int argc, char *argv[])
{
    if (argc < 4) {
        printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]);
        exit(0);
    }
    if (initMessenger() == -1) {
        printf("initMessenger failed");
        exit(0);
    }
    if (argc > 4) {
        if(strncmp(argv[4], "nokey", 6) < 0) {
        }
    } else {
        load_key();
    }

    int nameloaded = 0;
    int statusloaded = 0;

    FILE* name_file = NULL;
    name_file = fopen("namefile.txt", "r");
    if(name_file) {
        uint8_t name[MAX_NAME_LENGTH];
        while (fgets(line, MAX_NAME_LENGTH, name_file) != NULL) {
            sscanf(line, "%s", (char*)name);
        }
        setname(name, strlen((char*)name)+1);
        nameloaded = 1;
        printf("%s\n", name);
    }
    fclose(name_file);

    FILE* status_file = NULL;
    status_file = fopen("statusfile.txt", "r");
    if(status_file) {
        uint8_t status[MAX_USERSTATUS_LENGTH];
        while (fgets(line, MAX_USERSTATUS_LENGTH, status_file) != NULL) {
            sscanf(line, "%s", (char*)status);
        }
        m_set_userstatus(status, strlen((char*)status)+1);
        statusloaded = 1;
        printf("%s\n", status);
    }
    fclose(status_file);

    m_callback_friendrequest(print_request);
    m_callback_friendmessage(print_message);
    m_callback_namechange(print_nickchange);
    m_callback_userstatus(print_statuschange);
    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(users_id,"[i] your ID: ");
    int j;
    for (j = 0; j < PUB_KEY_BYTES; j++) {
        strcat(users_id,idstring1[j]);
        strcat(users_id,idstring2[j]);
    }

    do_header();
    
    IP_Port bootstrap_ip_port;
    bootstrap_ip_port.port = htons(atoi(argv[2]));
    int resolved_address = resolve_addr(argv[1]);
    if (resolved_address != 0)
        bootstrap_ip_port.ip.i = resolved_address;
    else 
        exit(1);
    
    DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));

    int c;
    int on = 0;

    _beginthread(get_input, 0, NULL);

    if (nameloaded == 1) {
        printf("\nNickname automatically loaded");
        printf("\n---------------------------------");
    }

    if (statusloaded == 1) {
        printf("\nStatus automatically loaded");
        printf("\n---------------------------------");
    }

    while(1) {
        if (on == 1 && DHT_isconnected() == -1) {
            printf("\n---------------------------------");
            printf("\n[i] Disconnected from the DHT");
            printf("\n---------------------------------\n\n");
            on = 0;
        }
        if (on == 0 && DHT_isconnected()) {
            printf("\n[i] Connected to DHT");
            printf("\n---------------------------------\n\n");
            on = 1;
        }
        doMessenger();
        Sleep(1);
    }
    return 0;
}
示例#6
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
    }
}
示例#7
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
    }
}
示例#8
0
int tox_set_userstatus(void *tox, USERSTATUS status)
{
    Messenger *m = tox;
    return m_set_userstatus(m, status);
}
示例#9
0
文件: tox.c 项目: askielboe/toxcore
int tox_set_user_status(Tox *tox, uint8_t status)
{
    Messenger *m = tox;
    return m_set_userstatus(m, status);
}
示例#10
0
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
{
    if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
        wclear(self->window);
        wclear(ctx->history);
        int x, y;
        getmaxyx(self->window, y, x);
        (void) x;
        wmove(self->window, y - CURS_Y_OFFSET, 0);
    }

    else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
        print_help(ctx);

    else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
        endwin();
        exit(0);
    }

    else if (!strncmp(cmd, "/me ", strlen("/me "))) {
        struct tm *timeinfo = get_time();
        char *action = strchr(cmd, ' ');

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

        action++;

        wattron(ctx->history, COLOR_PAIR(2));
        wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
        wattroff(ctx->history, COLOR_PAIR(2));

        uint8_t selfname[MAX_NAME_LENGTH];
        int len = getself_name(m, selfname, sizeof(selfname));
        char msg[MAX_STR_SIZE - len - 4];
        snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action);

        wattron(ctx->history, COLOR_PAIR(5));
        wprintw(ctx->history, msg);
        wattroff(ctx->history, COLOR_PAIR(5));

        if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) {
            wattron(ctx->history, COLOR_PAIR(3));
            wprintw(ctx->history, " * Failed to send action\n");
            wattroff(ctx->history, COLOR_PAIR(3));
        }
    }

    else if (!strncmp(cmd, "/status ", strlen("/status "))) {
        char *status = strchr(cmd, ' ');
        char *msg;
        char *status_text;

        if (status == NULL) {
            wprintw(ctx->history, "Invalid syntax.\n");
            return;
        }

        status++;
        USERSTATUS status_kind;

        if (!strncmp(status, "online", strlen("online"))) {
            status_kind = USERSTATUS_NONE;
            status_text = "ONLINE";
        }

        else if (!strncmp(status, "away", strlen("away"))) {
            status_kind = USERSTATUS_AWAY;
            status_text = "AWAY";
        }

        else if (!strncmp(status, "busy", strlen("busy"))) {
            status_kind = USERSTATUS_BUSY;
            status_text = "BUSY";
        }

        else {
            wprintw(ctx->history, "Invalid status.\n");
            return;
        }

        msg = strchr(status, ' ');

        if (msg == NULL) {
            m_set_userstatus(m, status_kind);
            wprintw(ctx->history, "Status set to: %s\n", status_text);
        } else {
            msg++;
            m_set_userstatus(m, status_kind);
            m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
            wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
        }
    }

    else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
        char *nick;
        nick = strchr(cmd, ' ');

        if (nick == NULL) {
            wprintw(ctx->history, "Invalid syntax.\n");
            return;
        }

        nick++;
        setname(m, (uint8_t *) nick, strlen(nick) + 1);
        wprintw(ctx->history, "Nickname set to: %s\n", nick);
    }

    else if (!strcmp(cmd, "/myid")) {
        char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
        int i;
        uint8_t address[FRIEND_ADDRESS_SIZE];
        getaddress(m, address);

        for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
            char xx[3];
            snprintf(xx, sizeof(xx), "%02X",  address[i] & 0xff);
            strcat(id, xx);
        }

        wprintw(ctx->history, "%s\n", id);
    }

    else if (strcmp(ctx->line, "/close") == 0) {
        int f_num = ctx->friendnum;
        delwin(ctx->linewin);
        del_window(self);
        disable_chatwin(f_num);
    }

    else
        wprintw(ctx->history, "Invalid command.\n");
}
示例#11
0
int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status)
{
    Messenger *m = tox;
    return m_set_userstatus(m, (USERSTATUS)status);
}
示例#12
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_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) + 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);
    }
}
示例#13
0
文件: chat.c 项目: 1100110/toxic
static void chat_onKey(ToxWindow *self, Messenger *m, wint_t key)
{
    ChatContext *ctx = (ChatContext *) self->x;
    struct tm *timeinfo = get_time();

    int x, y, y2, x2;
    getyx(self->window, y, x);
    getmaxyx(self->window, y2, x2);

    /* Add printable chars to buffer and print on input space */
#if HAVE_WIDECHAR
    if (iswprint(key)) {
#else
    if (isprint(key)) {
#endif
        if (ctx->pos != sizeof(ctx->line) - 1) {
            mvwaddstr(self->window, y, x, wc_to_char(key));
            ctx->line[ctx->pos++] = key;
            ctx->line[ctx->pos] = L'\0';
        }
    }

    /* BACKSPACE key: Remove one character from line */
    else if (key == 0x107 || key == 0x8 || key == 0x7f) {
        if (ctx->pos > 0) {
            ctx->line[--ctx->pos] = L'\0';

            if (x == 0)
                mvwdelch(self->window, y - 1, x2 - 1);
            else
                mvwdelch(self->window, y, x - 1);
        }
    }

    /* RETURN key: Execute command or print line */
    else if (key == '\n') {
        char *line = wcs_to_char(ctx->line);
        wclear(ctx->linewin);
        wmove(self->window, y2 - CURS_Y_OFFSET, 0);
        wclrtobot(self->window);

        if (line[0] == '/')
            execute(self, ctx, m, line);
        else {
            /* make sure the string has at least non-space character */
            if (!string_is_empty(line)) {
                uint8_t selfname[MAX_NAME_LENGTH];
                getself_name(m, selfname, sizeof(selfname));

                wattron(ctx->history, COLOR_PAIR(2));
                wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
                wattroff(ctx->history, COLOR_PAIR(2));
                wattron(ctx->history, COLOR_PAIR(1));
                wprintw(ctx->history, "%s: ", selfname);
                wattroff(ctx->history, COLOR_PAIR(1));
                wprintw(ctx->history, "%s\n", line);

                if (m_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) {
                    wattron(ctx->history, COLOR_PAIR(3));
                    wprintw(ctx->history, " * Failed to send message.\n");
                    wattroff(ctx->history, COLOR_PAIR(3));
                }
            }
        }

        ctx->line[0] = L'\0';
        ctx->pos = 0;
        free(line);
    }
}

void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
{
    if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
        wclear(self->window);
        wclear(ctx->history);
        int x, y;
        getmaxyx(self->window, y, x);
        (void) x;
        wmove(self->window, y - CURS_Y_OFFSET, 0);
    }

    else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
        print_help(ctx);

    else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
        endwin();
        exit(0);
    }

    else if (!strncmp(cmd, "/me ", strlen("/me "))) {
        struct tm *timeinfo = get_time();
        char *action = strchr(cmd, ' ');

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

        action++;

        wattron(ctx->history, COLOR_PAIR(2));
        wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
        wattroff(ctx->history, COLOR_PAIR(2));

        uint8_t selfname[MAX_NAME_LENGTH];
        int len = getself_name(m, selfname, sizeof(selfname));
        char msg[MAX_STR_SIZE - len - 4];
        snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action);

        wattron(ctx->history, COLOR_PAIR(5));
        wprintw(ctx->history, msg);
        wattroff(ctx->history, COLOR_PAIR(5));

        if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) {
            wattron(ctx->history, COLOR_PAIR(3));
            wprintw(ctx->history, " * Failed to send action\n");
            wattroff(ctx->history, COLOR_PAIR(3));
        }
    }

    else if (!strncmp(cmd, "/status ", strlen("/status "))) {
        char *status = strchr(cmd, ' ');
        char *msg;
        char *status_text;

        if (status == NULL) {
            wprintw(ctx->history, "Invalid syntax.\n");
            return;
        }

        status++;
        USERSTATUS status_kind;

        if (!strncmp(status, "online", strlen("online"))) {
            status_kind = USERSTATUS_NONE;
            status_text = "ONLINE";
        }

        else if (!strncmp(status, "away", strlen("away"))) {
            status_kind = USERSTATUS_AWAY;
            status_text = "AWAY";
        }

        else if (!strncmp(status, "busy", strlen("busy"))) {
            status_kind = USERSTATUS_BUSY;
            status_text = "BUSY";
        }

        else {
            wprintw(ctx->history, "Invalid status.\n");
            return;
        }

        msg = strchr(status, ' ');

        if (msg == NULL) {
            m_set_userstatus(m, status_kind);
            wprintw(ctx->history, "Status set to: %s\n", status_text);
        } else {
            msg++;
            m_set_userstatus(m, status_kind);
            m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
            wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
        }
    }

    else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
        char *nick;
        nick = strchr(cmd, ' ');

        if (nick == NULL) {
            wprintw(ctx->history, "Invalid syntax.\n");
            return;
        }

        nick++;
        setname(m, (uint8_t *) nick, strlen(nick) + 1);
        wprintw(ctx->history, "Nickname set to: %s\n", nick);
    }

    else if (!strcmp(cmd, "/myid")) {
        char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0};
        uint8_t address[FRIEND_ADDRESS_SIZE];
        size_t i;
        getaddress(m, address);

        for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
            char xx[3];
            snprintf(xx, sizeof(xx), "%02X",  address[i] & 0xff);
            strcat(id, xx);
        }

        wprintw(ctx->history, "%s\n", id);
    }

    else if (strcmp(cmd, "/close") == 0) {
        int f_num = ctx->friendnum;
        delwin(ctx->linewin);
        del_window(self);
        disable_chatwin(f_num);
    }

    else
        wprintw(ctx->history, "Invalid command.\n");
}

static void chat_onDraw(ToxWindow *self, Messenger *m)
{
    curs_set(1);
    int x, y;
    getmaxyx(self->window, y, x);
    (void) y;
    ChatContext *ctx = (ChatContext *) self->x;
    mvwhline(ctx->linewin, 0, 0, '_', x);
    wrefresh(self->window);
}
示例#14
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;
            int 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);
            m_sendmessage(num, (uint8_t*) message, sizeof(message));
        } else if (line[1] == 'n') {
            uint8_t name[MAX_NAME_LENGTH];
            int i = 0;
            for (i=3; i<strlen(line); 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;
            for (i=3; i<strlen(line); 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] == 'q') { //exit
            endwin();
            exit(EXIT_SUCCESS);
        }
    } else {
        //new_lines(line);
    }
}
示例#15
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);
    }
}
示例#16
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");
    }
  }
}
示例#17
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");
  }
}