about summary refs log tree commit diff stats
path: root/app.c
blob: c1c0cc9946e78a571e422d4f3c0ddbf00ce4079f (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#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) {
        inp_get_command_str(cmd);

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

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

            bar_print_message(user);
            jabber_connect(user, passwd);
            main_event_loop();
            break;
        } else {
            cons_bad_command(cmd);
            inp_clear();
        }
    }
}

static void main_event_loop(void)
{
    inp_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();

            // determine if they changed windows
            if (ch == KEY_F(1)) {
                switch_to(0);
            } else if (ch == KEY_F(2)) {
                switch_to(1);
            } else if (ch == KEY_F(3)) {
                switch_to(2);
            } else if (ch == KEY_F(4)) {
                switch_to(3);
            } else if (ch == KEY_F(5)) {
                switch_to(4);
            } else if (ch == KEY_F(6)) {
                switch_to(5);
            } else if (ch == KEY_F(7)) {
                switch_to(6);
            } else if (ch == KEY_F(8)) {
                switch_to(7);
            } else if (ch == KEY_F(9)) {
                switch_to(8);
            } else if (ch == KEY_F(10)) {
                switch_to(9);
            }

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

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

        // deal with input

        // /quit command -> exit
        if (strcmp(command, "/quit") == 0) {
            break;

        // /help -> print help to console
        } else if (strncmp(command, "/help", 5) == 0) {
            cons_help();
            inp_clear();

        // /close -> close the current chat window, if in chat
        } else if (strncmp(command, "/close", 6) == 0) {
            if (in_chat()) {
                close_win();
            } else {
                cons_bad_command(command);
            }
            inp_clear();

        // send message to recipient, if in chat
        } else {
            if (in_chat()) {
                char recipient[100] = "";
                get_recipient(recipient);
                jabber_send(command, recipient);
                show_outgoing_msg("me", command);
            } else {
                cons_bad_command(command);
            }
            inp_clear();
        }
    }

    jabber_disconnect();
}