about summary refs log tree commit diff stats
path: root/app.c
blob: e7cf758907a88a7325058a00eb4e7498f5c391a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <string.h>
#include <unistd.h>

#include <ncurses.h>

#include "log.h"
#include "windows.h"
#include "jabber.h"

static void main_event_loop(void);

void start_profanity(void)
{
    char cmd[50];
    while (TRUE) {
        cmd_get_command_str(cmd);

        if (strcmp(cmd, "/quit") == 0) {
            break;
        } else if (strncmp(cmd, "/connect ", 9) == 0) {
            char *user;
            user = strndup(cmd+9, strlen(cmd)-9);

            bar_print_message("Enter password:");
            char passwd[20];
            cmd_get_password(passwd);

            bar_print_message(user);
            jabber_connect(user, passwd);
            main_event_loop();
            break;
        } else {
            cmd_clear();
        }
    }
}

static void main_event_loop(void)
{
    cmd_non_block();

    while(TRUE) {
        int ch = ERR;
        char command[100];
        int size = 0;

        // while not enter, process all events, and try to get another char
        while(ch != '\n') {
            usleep(1);
        
            // handle incoming messages
            jabber_process_events();

            // get another character from the command box
            cmd_poll_char(&ch, command, &size);
        }

        // null terminate the input    
        command[size++] = '\0';

        // newline was hit, check if /quit command issued
        if (strcmp(command, "/quit") == 0) {
            break;
        } else {
            jabber_send(command);
            show_outgoing_msg("me", command);
            cmd_clear();
        }
    }

    jabber_disconnect();
}