1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-10 03:27:56 +02:00
This commit is contained in:
Jfreegman 2013-08-04 02:40:57 -04:00
commit 67e76d397d
2 changed files with 71 additions and 19 deletions

35
chat.c
View File

@ -7,6 +7,7 @@
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <ctype.h> #include <ctype.h>
#include <time.h>
#include "../../core/Messenger.h" #include "../../core/Messenger.h"
#include "../../core/network.h" #include "../../core/network.h"
@ -26,11 +27,15 @@ typedef struct {
extern void fix_name(uint8_t* name); extern void fix_name(uint8_t* name);
static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) {
ChatContext* ctx = (ChatContext*) self->x; ChatContext* ctx = (ChatContext*) self->x;
uint8_t nick[MAX_NAME_LENGTH] = {0}; 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; return;
@ -42,10 +47,11 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len)
fix_name(msg); fix_name(msg);
fix_name(nick); fix_name(nick);
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattron(ctx->history, COLOR_PAIR(4)); wattron(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s: ", nick); wprintw(ctx->history, "%s: ", nick);
wattroff(ctx->history, COLOR_PAIR(4)); wattroff(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s\n", msg); wprintw(ctx->history, "%s\n", msg);
self->blink = true; self->blink = true;
@ -71,9 +77,26 @@ static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint1
} }
/* check that the string has one non-space character */
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) { static void chat_onKey(ToxWindow* self, int key) {
ChatContext* ctx = (ChatContext*) self->x; ChatContext* ctx = (ChatContext*) self->x;
time_t now;
time(&now);
struct tm * timeinfo;
timeinfo = localtime(&now);
if(isprint(key)) { if(isprint(key)) {
if(ctx->pos != sizeof(ctx->line)-1) { if(ctx->pos != sizeof(ctx->line)-1) {
@ -81,11 +104,15 @@ static void chat_onKey(ToxWindow* self, int key) {
ctx->line[ctx->pos] = '\0'; ctx->line[ctx->pos] = '\0';
} }
} }
else if(key == '\n') { else if(key == '\n') {
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);
wattron(ctx->history, COLOR_PAIR(1)); wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "you: ", ctx->line); wprintw(ctx->history, "you: ", ctx->line);
wattroff(ctx->history, COLOR_PAIR(1)); wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", ctx->line); 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) {
@ -97,6 +124,8 @@ static void chat_onKey(ToxWindow* self, int key) {
ctx->line[0] = '\0'; ctx->line[0] = '\0';
ctx->pos = 0; ctx->pos = 0;
} }
}
else if(key == 0x107 || key == 0x8 || key == 0x7f) { else if(key == 0x107 || key == 0x8 || key == 0x7f) {
if(ctx->pos != 0) { if(ctx->pos != 0) {
ctx->line[--ctx->pos] = '\0'; ctx->line[--ctx->pos] = '\0';

31
main.c
View File

@ -170,12 +170,12 @@ static void do_tox() {
doMessenger(); doMessenger();
} }
static void load_data() { static void load_data(char *path) {
FILE* fd; FILE* fd;
size_t len; size_t len;
uint8_t* buf; uint8_t* buf;
if((fd = fopen("data", "r")) != NULL) { if((fd = fopen(path, "r")) != NULL) {
fseek(fd, 0, SEEK_END); fseek(fd, 0, SEEK_END);
len = ftell(fd); len = ftell(fd);
fseek(fd, 0, SEEK_SET); fseek(fd, 0, SEEK_SET);
@ -213,7 +213,7 @@ static void load_data() {
Messenger_save(buf); Messenger_save(buf);
fd = fopen("data", "w"); fd = fopen(path, "w");
if(fd == NULL) { if(fd == NULL) {
fprintf(stderr, "fopen() failed.\n"); fprintf(stderr, "fopen() failed.\n");
@ -282,13 +282,36 @@ void prepare_window(WINDOW* w) {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
int ch; int ch;
int i = 0;
int f_flag = 0;
char *filename = "data";
ToxWindow* a; ToxWindow* a;
for(i = 0; i < argc; i++) {
if(argv[i][0] == '-') {
if(argv[i][1] == 'f') {
if(argv[i + 1] != NULL)
filename = argv[i + 1];
else {
f_flag = -1;
}
}
}
}
init_term(); init_term();
init_tox(); init_tox();
load_data(); load_data(filename);
init_windows(); init_windows();
if(f_flag == -1) {
attron(COLOR_PAIR(3) | A_BOLD);
wprintw(prompt->window, "You passed '-f' without giving an argument!\n"
"defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD);
}
while(true) { while(true) {
// Update tox. // Update tox.
do_tox(); do_tox();