1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-06-30 21:55:24 +00:00
profanity/profanity.c

172 lines
4.0 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2012-02-02 01:12:50 +00:00
#include <ncurses.h>
#include <string.h>
2012-02-02 20:35:49 +00:00
#include <strophe/strophe.h>
2012-02-02 01:12:50 +00:00
2012-02-05 15:10:10 +00:00
#include "log.h"
2012-02-05 23:08:15 +00:00
#include "windows.h"
2012-02-05 23:29:09 +00:00
#include "jabber.h"
2012-02-05 15:10:10 +00:00
// refernce to log
extern FILE *logp;
2012-02-05 15:22:02 +00:00
// area for log message in profanity
static const char *prof = "prof";
2012-02-05 23:08:15 +00:00
// window references
extern WINDOW *title_bar;
extern WINDOW *cmd_bar;
extern WINDOW *cmd_win;
extern WINDOW *main_win;
2012-02-05 15:10:10 +00:00
// curses functions
void event_loop(xmpp_ctx_t *ctx, xmpp_conn_t *conn);
2012-02-05 21:11:43 +00:00
int main(void)
{
2012-02-05 17:53:57 +00:00
char cmd[50];
xmpp_ctx_t *ctx;
xmpp_conn_t *conn;
xmpp_log_t *log;
logp = fopen("profanity.log", "a");
2012-02-05 15:22:02 +00:00
logmsg(prof, "Starting Profanity...");
2012-02-05 23:29:09 +00:00
// initialise curses, and create windows
initgui();
// set cursor in command window and loop on input
2012-02-05 17:53:57 +00:00
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);
2012-02-05 17:53:57 +00:00
wclear(cmd_win);
wmove(cmd_win, 0, 0);
wrefresh(cmd_win);
char loginmsg[100];
sprintf(loginmsg, "User <%s> logged in", user);
logmsg(prof, loginmsg);
2012-02-05 21:11:43 +00:00
2012-02-05 17:53:57 +00:00
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);
2012-02-05 17:53:57 +00:00
break;
2012-02-05 17:53:57 +00:00
} else {
2012-02-05 21:11:43 +00:00
// echo ignore
2012-02-05 17:53:57 +00:00
wclear(cmd_win);
wmove(cmd_win, 0, 0);
}
}
2012-02-05 17:53:57 +00:00
endwin();
fclose(logp);
return 0;
}
void event_loop(xmpp_ctx_t *ctx, xmpp_conn_t *conn)
2012-02-05 21:11:43 +00:00
{
int cmd_y = 0;
int cmd_x = 0;
wtimeout(cmd_win, 0);
while(TRUE) {
2012-02-06 22:04:30 +00:00
int ch = ERR;
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);
2012-02-06 22:04:30 +00:00
// echo off, and get some more input
noecho();
ch = wgetch(cmd_win);
2012-02-06 22:04:30 +00:00
// if delete pressed, go back and delete it
if (ch == 127) {
2012-02-06 22:07:50 +00:00
if (size > 0) {
getyx(cmd_win, cmd_y, cmd_x);
wmove(cmd_win, cmd_y, cmd_x-1);
wdelch(cmd_win);
size--;
}
2012-02-06 22:04:30 +00:00
}
// else if not error or newline, show it and store it
else if (ch != ERR && ch != '\n') {
waddch(cmd_win, ch);
command[size++] = ch;
}
2012-02-06 22:04:30 +00:00
echo();
}
command[size++] = '\0';
// newline was hit, check if /quit command issued
if (strcmp(command, "/quit") == 0) {
break;
} else {
// send the message
2012-02-05 23:38:35 +00:00
prof_send(command, ctx, conn);
// 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);
}
}
2012-02-05 21:11:43 +00:00
xmpp_conn_release(conn);
xmpp_ctx_free(ctx);
2012-02-05 21:11:43 +00:00
xmpp_shutdown();
}