about summary refs log tree commit diff stats
path: root/profanity.c
blob: 732b83040d2e2057d43d2af521cb27ab1fa646c5 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ncurses.h>
#include <string.h>
#include <strophe/strophe.h>

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

// refernce to log
extern FILE *logp;

// area for log message in profanity
static const char *prof = "prof";

// window references
extern WINDOW *title_bar;
extern WINDOW *cmd_bar;
extern WINDOW *cmd_win;
extern WINDOW *main_win;

// message handlers
int in_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, 
    void * const userdata);

// connection handler
void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, 
          const int error, xmpp_stream_error_t * const stream_error,
          void * const userdata);

// curses functions
void init(void);
void event_loop(xmpp_ctx_t *ctx, xmpp_conn_t *conn);

int main(void)
{   
    char cmd[50];

    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;

    logp = fopen("profanity.log", "a");
    logmsg(prof, "Starting Profanity...");

    // initialise curses
    init();
    refresh();
    
    // create windows
    create_title_bar();
    wrefresh(title_bar);
    create_command_bar();
    wrefresh(cmd_bar);
    create_command_window();
    wrefresh(cmd_win);
    create_main_window();
    wrefresh(main_win);

    // set cursor in command window and loop in input
    wmove(cmd_win, 0, 0);
    while (TRUE) {
        wgetstr(cmd_win, cmd);
        
        // handle quit
        if (strcmp(cmd, "/quit") == 0) {
            break;
        // handle connect
        } else if (strncmp(cmd, "/connect ", 9) == 0) {
            char *user;
            char passwd[20];

            user = strndup(cmd+9, strlen(cmd)-9);
            
            mvwprintw(cmd_bar, 0, 0, "Enter password:");
            wrefresh(cmd_bar);
            
            wclear(cmd_win);
            noecho();
            mvwgetstr(cmd_win, 0, 0, passwd);
            echo();

            mvwprintw(cmd_bar, 0, 0, "%s", user);
            wrefresh(cmd_bar);
            
            wclear(cmd_win);
            wmove(cmd_win, 0, 0);
            wrefresh(cmd_win);

            char loginmsg[100];
            sprintf(loginmsg, "User <%s> logged in", user);
            logmsg(prof, loginmsg);
           
            xmpp_initialize();
            
            log = xmpp_get_file_logger(); 
            ctx = xmpp_ctx_new(NULL, log);
            conn = xmpp_conn_new(ctx);

            xmpp_conn_set_jid(conn, user);
            xmpp_conn_set_pass(conn, passwd);
            xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);

            event_loop(ctx, conn);

            break;
        } else {
            // echo ignore
            wclear(cmd_win);
            wmove(cmd_win, 0, 0);
        }
    }
    
    endwin();
    fclose(logp);

    return 0;
}

void event_loop(xmpp_ctx_t *ctx, xmpp_conn_t *conn)
{
    int cmd_y = 0;
    int cmd_x = 0;

    wtimeout(cmd_win, 0);

    while(TRUE) {
        char ch = 0;
        char command[100];
        int size = 0;
        
        // while not enter
        while(ch != '\n') {
            usleep(1);
            // handle incoming messages
            xmpp_run_once(ctx, 10);

            // move cursor back to cmd_win
            getyx(cmd_win, cmd_y, cmd_x);
            wmove(cmd_win, cmd_y, cmd_x);
            
            // get some more input
            ch = wgetch(cmd_win);
            if (ch > 0 && ch != '\n') {
                command[size++] = ch;
            }
        }

        command[size++] = '\0';

        // newline was hit, check if /quit command issued
        if (strcmp(command, "/quit") == 0) {
            break;
        } else {
            // send the message
            xmpp_stanza_t *reply, *body, *text;

            reply = xmpp_stanza_new(ctx);
            xmpp_stanza_set_name(reply, "message");
            xmpp_stanza_set_type(reply, "chat");
            xmpp_stanza_set_attribute(reply, "to", "boothj5@localhost");

            body = xmpp_stanza_new(ctx);
            xmpp_stanza_set_name(body, "body");

            text = xmpp_stanza_new(ctx);
            xmpp_stanza_set_text(text, command);
            xmpp_stanza_add_child(body, text);
            xmpp_stanza_add_child(reply, body);

            xmpp_send(conn, reply);
            xmpp_stanza_release(reply);

            // show it in the main window
            wprintw(main_win, "me: %s\n", command);
            wrefresh(main_win);

            // move back to the command window and clear it
            wclear(cmd_win);
            wmove(cmd_win, 0, 0);
            wrefresh(cmd_win);
        }
    }

    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    xmpp_shutdown();

}

void wait_command(void)
{
    wclear(cmd_win);
    wmove(cmd_win, 0, 0);
}

int in_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, 
    void * const userdata)
{
    char *intext;

    if(!xmpp_stanza_get_child_by_name(stanza, "body")) 
        return 1;
    if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "error")) 
        return 1;

    intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));

    char in_line[200];
    char *from = xmpp_stanza_get_attribute(stanza, "from");

    char *short_from = strtok(from, "@");

    sprintf(in_line, "%s: %s\n", short_from, intext);

    wprintw(main_win, in_line);
    wrefresh(main_win);

    return 1;
}

void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, 
          const int error, xmpp_stream_error_t * const stream_error,
          void * const userdata)
{
    xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;

    if (status == XMPP_CONN_CONNECT) {
        xmpp_stanza_t* pres;
        fprintf(logp, "DEBUG: connected\n");
        xmpp_handler_add(conn,in_message_handler, NULL, "message", NULL, ctx);

        pres = xmpp_stanza_new(ctx);
        xmpp_stanza_set_name(pres, "presence");
        xmpp_send(conn, pres);
        xmpp_stanza_release(pres);
    }
    else {
        fprintf(logp, "DEBUG: disconnected\n");
        xmpp_stop(ctx);
    }
}

void init(void)
{
    initscr();
    cbreak();
    keypad(stdscr, TRUE);
    start_color();

    init_color(COLOR_WHITE, 1000, 1000, 1000);
    init_pair(1, COLOR_WHITE, COLOR_BLACK);
    init_pair(2, COLOR_GREEN, COLOR_BLACK);

    init_color(COLOR_BLUE, 0, 0, 250);
    init_pair(3, COLOR_WHITE, COLOR_BLUE);

    attron(A_BOLD);
    attron(COLOR_PAIR(1));
}