mirror of
https://github.com/Tha14/toxic.git
synced 2025-07-10 04:06:45 +02:00
code format/clean up
This commit is contained in:
110
chat.c
110
chat.c
@ -16,38 +16,34 @@
|
||||
|
||||
typedef struct {
|
||||
int friendnum;
|
||||
|
||||
char line[256];
|
||||
size_t pos;
|
||||
|
||||
WINDOW* history;
|
||||
WINDOW* linewin;
|
||||
|
||||
} ChatContext;
|
||||
|
||||
extern int w_active;
|
||||
extern void del_window(ToxWindow *w, int f_num);
|
||||
extern void fix_name(uint8_t* name);
|
||||
void print_help(ChatContext* self);
|
||||
void execute(ToxWindow* self, ChatContext* ctx, char* cmd);
|
||||
extern int active_window;
|
||||
|
||||
static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
extern void del_window(ToxWindow *w, int f_num);
|
||||
extern void fix_name(uint8_t *name);
|
||||
void print_help(ChatContext *self);
|
||||
void execute(ToxWindow *self, ChatContext *ctx, char *cmd);
|
||||
|
||||
static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
||||
|
||||
time_t now;
|
||||
time(&now);
|
||||
struct tm * timeinfo;
|
||||
timeinfo = localtime(&now);
|
||||
|
||||
if(ctx->friendnum != num)
|
||||
if (ctx->friendnum != num)
|
||||
return;
|
||||
|
||||
getname(num, (uint8_t*) &nick);
|
||||
|
||||
msg[len-1] = '\0';
|
||||
nick[MAX_NAME_LENGTH-1] = '\0';
|
||||
|
||||
fix_name(msg);
|
||||
fix_name(nick);
|
||||
|
||||
@ -64,15 +60,14 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len)
|
||||
flash();
|
||||
}
|
||||
|
||||
static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) {
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
|
||||
if(ctx->friendnum != num)
|
||||
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
if (ctx->friendnum != num)
|
||||
return;
|
||||
|
||||
nick[len-1] = '\0';
|
||||
fix_name(nick);
|
||||
|
||||
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
|
||||
|
||||
wattron(ctx->history, COLOR_PAIR(3));
|
||||
@ -80,7 +75,8 @@ static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t
|
||||
wattroff(ctx->history, COLOR_PAIR(3));
|
||||
}
|
||||
|
||||
static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) {
|
||||
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@ -89,35 +85,33 @@ int string_is_empty(char *string)
|
||||
{
|
||||
int rc = 0;
|
||||
char *copy = strdup(string);
|
||||
|
||||
rc = ((strtok(copy, " ") == NULL) ? 1:0);
|
||||
free(copy);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void chat_onKey(ToxWindow* self, int key) {
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
|
||||
static void chat_onKey(ToxWindow *self, int key)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
time_t now;
|
||||
time(&now);
|
||||
struct tm * timeinfo;
|
||||
timeinfo = localtime(&now);
|
||||
|
||||
/* PRINTABLE characters: Add to line */
|
||||
if(isprint(key)) {
|
||||
if(ctx->pos != sizeof(ctx->line)-1) {
|
||||
/* Add printable characters to line */
|
||||
if (isprint(key)) {
|
||||
if (ctx->pos != sizeof(ctx->line)-1) {
|
||||
ctx->line[ctx->pos++] = key;
|
||||
ctx->line[ctx->pos] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/* RETURN key: Execute command or print line */
|
||||
else if(key == '\n') {
|
||||
else if (key == '\n') {
|
||||
if (ctx->line[0] == '/')
|
||||
execute(self, ctx, ctx->line);
|
||||
else {
|
||||
if(!string_is_empty(ctx->line)) {
|
||||
if (!string_is_empty(ctx->line)) {
|
||||
/* make sure the string has at least non-space character */
|
||||
wattron(ctx->history, COLOR_PAIR(2));
|
||||
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
||||
@ -127,7 +121,7 @@ static void chat_onKey(ToxWindow* self, int key) {
|
||||
wattroff(ctx->history, COLOR_PAIR(1));
|
||||
wprintw(ctx->history, "%s\n", ctx->line);
|
||||
}
|
||||
if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) {
|
||||
if (m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) {
|
||||
wattron(ctx->history, COLOR_PAIR(3));
|
||||
wprintw(ctx->history, " * Failed to send message.\n");
|
||||
wattroff(ctx->history, COLOR_PAIR(3));
|
||||
@ -138,29 +132,32 @@ static void chat_onKey(ToxWindow* self, int key) {
|
||||
}
|
||||
|
||||
/* BACKSPACE key: Remove one character from line */
|
||||
else if(key == 0x107 || key == 0x8 || key == 0x7f) {
|
||||
if(ctx->pos != 0) {
|
||||
else if (key == 0x107 || key == 0x8 || key == 0x7f) {
|
||||
if (ctx->pos != 0) {
|
||||
ctx->line[--ctx->pos] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
|
||||
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;
|
||||
char *msg;
|
||||
msg = strchr(cmd, ' ');
|
||||
if(msg == NULL) {
|
||||
if (msg == NULL) {
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
@ -168,10 +165,11 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
|
||||
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;
|
||||
char *nick;
|
||||
nick = strchr(cmd, ' ');
|
||||
if(nick == NULL) {
|
||||
if (nick == NULL) {
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
@ -179,7 +177,8 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
|
||||
setname((uint8_t*) nick, strlen(nick)+1);
|
||||
wprintw(ctx->history, "Nickname set to: %s\n", nick);
|
||||
}
|
||||
else if(!strcmp(cmd, "/myid")) {
|
||||
|
||||
else if (!strcmp(cmd, "/myid")) {
|
||||
char id[32*2 + 1] = {0};
|
||||
int i;
|
||||
for (i = 0; i < 32; i++) {
|
||||
@ -189,48 +188,46 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
|
||||
}
|
||||
wprintw(ctx->history, "Your ID: %s\n", id);
|
||||
}
|
||||
|
||||
else if (strcmp(ctx->line, "/close") == 0) {
|
||||
w_active = 0; // Go to prompt screen
|
||||
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");
|
||||
}
|
||||
|
||||
static void chat_onDraw(ToxWindow* self) {
|
||||
static void chat_onDraw(ToxWindow *self)
|
||||
{
|
||||
curs_set(1);
|
||||
int x, y;
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
getmaxyx(self->window, y, x);
|
||||
|
||||
(void) x;
|
||||
if(y < 3)
|
||||
return;
|
||||
if (y < 3) return;
|
||||
|
||||
wclear(ctx->linewin);
|
||||
mvwhline(ctx->linewin, 0, 0, '_', COLS);
|
||||
mvwprintw(self->window, y-1, 0, "%s\n", ctx->line);
|
||||
|
||||
wrefresh(self->window);
|
||||
}
|
||||
|
||||
static void chat_onInit(ToxWindow* self) {
|
||||
static void chat_onInit(ToxWindow *self)
|
||||
{
|
||||
int x, y;
|
||||
ChatContext* ctx = (ChatContext*) self->x;
|
||||
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
getmaxyx(self->window, y, x);
|
||||
|
||||
ctx->history = subwin(self->window, y - 4, x, 0, 0);
|
||||
scrollok(ctx->history, 1);
|
||||
|
||||
ctx->linewin = subwin(self->window, 2, x, y - 3, 0);
|
||||
print_help(ctx);
|
||||
}
|
||||
|
||||
void print_help(ChatContext* self) {
|
||||
void print_help(ChatContext *self)
|
||||
{
|
||||
wattron(self->history, COLOR_PAIR(2) | A_BOLD);
|
||||
wprintw(self->history, "Commands:\n");
|
||||
wattroff(self->history, A_BOLD);
|
||||
@ -246,9 +243,9 @@ void print_help(ChatContext* self) {
|
||||
wattroff(self->history, COLOR_PAIR(2));
|
||||
}
|
||||
|
||||
ToxWindow new_chat(int friendnum) {
|
||||
ToxWindow new_chat(int friendnum)
|
||||
{
|
||||
ToxWindow ret;
|
||||
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
|
||||
ret.onKey = &chat_onKey;
|
||||
@ -264,9 +261,8 @@ ToxWindow new_chat(int friendnum) {
|
||||
|
||||
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
|
||||
|
||||
ChatContext* x = calloc(1, sizeof(ChatContext));
|
||||
ChatContext *x = calloc(1, sizeof(ChatContext));
|
||||
x->friendnum = friendnum;
|
||||
|
||||
ret.x = (void*) x;
|
||||
return ret;
|
||||
}
|
||||
|
Reference in New Issue
Block a user