]> Pileus Git - ~andy/lamechat/blob - chat.c
XMPP MUC
[~andy/lamechat] / chat.c
1 /*
2  * Copyright (C) 2017 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdarg.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23
24 #include "util.h"
25 #include "conf.h"
26 #include "chat.h"
27 #include "view.h"
28
29 /* Global data */
30 log_t *chat_log;
31 int    chat_len;
32
33 /* Local data */
34 static buf_t log_buf;
35
36 /* Local functions */
37
38 /* View init */
39 void chat_init(void)
40 {
41         chat_log = (log_t*)log_buf.data;
42         chat_len = 0;
43
44         irc_init();
45         xmpp_init();
46 }
47
48 void chat_config(const char *group, const char *name, const char *key, const char *value)
49 {
50         irc_config(group, name, key, value);
51         xmpp_config(group, name, key, value);
52 }
53
54 void chat_notice(const char *channel, const char *from, const char *fmt, ...)
55 {
56         static char buf[1024];
57
58         va_list ap;
59         va_start(ap, fmt);
60         vsnprintf(buf, sizeof(buf), fmt, ap);
61         va_end(ap);
62
63         chat_recv(channel, from, buf);
64 }
65
66 void chat_recv(const char *channel, const char *from, const char *msg)
67 {
68         append(&log_buf, NULL, sizeof(log_t));
69         chat_log = (log_t*)log_buf.data;
70
71         log_t *log = &chat_log[chat_len];
72         log->when = time(NULL);
73         log->from = strcopy(from);
74         log->channel = strcopy(channel);
75         log->msg  = strcopy(msg);
76
77         chat_len++;
78         view_draw();
79 }
80
81 void chat_send(const char *channel, const char *msg)
82 {
83         append(&log_buf, NULL, sizeof(log_t));
84         chat_log = (log_t*)log_buf.data;
85
86         log_t *log = &chat_log[chat_len];
87         log->when = time(NULL);
88         log->from = "andy";
89         log->channel = strcopy(channel);
90         log->msg  = strcopy(msg);
91
92         chat_len++;
93         irc_send(channel, msg);
94         xmpp_send(channel, msg);
95         view_draw();
96 }
97
98 void chat_exit(void)
99 {
100         irc_exit();
101         xmpp_exit();
102
103         for (int i = 0; i < chat_len; i++)
104                 free((void*)chat_log[i].msg);
105         release(&log_buf);
106 }