/* * Copyright (C) 2017 Andy Spencer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include "util.h" #include "conf.h" #include "chat.h" #include "view.h" /* Global data */ log_t *chat_log; int chat_len; /* Local data */ static buf_t log_buf; /* Local functions */ /* View init */ void chat_init(void) { chat_log = (log_t*)log_buf.data; chat_len = 0; irc_init(); xmpp_init(); } void chat_config(const char *group, const char *name, const char *key, const char *value) { irc_config(group, name, key, value); xmpp_config(group, name, key, value); } void chat_notice(const char *channel, const char *from, const char *fmt, ...) { static char buf[1024]; va_list ap; va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); chat_recv(channel, from, buf); } void chat_recv(const char *channel, const char *from, const char *msg) { append(&log_buf, NULL, sizeof(log_t)); chat_log = (log_t*)log_buf.data; log_t *log = &chat_log[chat_len]; log->when = time(NULL); log->from = strcopy(from); log->channel = strcopy(channel); log->msg = strcopy(msg); chat_len++; view_draw(); } void chat_send(const char *channel, const char *msg) { append(&log_buf, NULL, sizeof(log_t)); chat_log = (log_t*)log_buf.data; log_t *log = &chat_log[chat_len]; log->when = time(NULL); log->from = "andy"; log->channel = strcopy(channel); log->msg = strcopy(msg); chat_len++; irc_send(channel, msg); xmpp_send(channel, msg); view_draw(); } void chat_exit(void) { irc_exit(); xmpp_exit(); for (int i = 0; i < chat_len; i++) free((void*)chat_log[i].msg); release(&log_buf); }