2 * Copyright (C) 2017 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
37 static buf_t chan_buf;
40 static const char *proto_map[] = {
48 servers = (server_t*)srv_buf.data;
49 channels = (channel_t*)chan_buf.data;
50 messages = (message_t*)msg_buf.data;
57 void chat_config(const char *group, const char *name, const char *key, const char *value)
60 server_t *server = NULL;
61 channel_t *channel = NULL;
63 if (match(group, "server")) {
64 server = find_server(name);
68 else if (match(key, "protocol")) {
69 protocol = get_enum(value, proto_map, N_ELEMENTS(proto_map));
70 server = add_server(name, protocol);
73 protocol = server->protocol;
76 error("No server found for [%s]", name);
80 if (match(group, "channel")) {
81 channel = find_channel(name);
85 else if (match(key, "server")) {
86 server = find_server(get_string(value));
88 error("server '%s' does not exist", name);
89 channel = add_channel(name, server);
90 protocol = server->protocol;
94 protocol = channel->server->protocol;
97 error("No channel found for [%s]", name);
101 debug("chat_config: [%s-%s \"%s\"] %s = %s",
102 protocol>=0 ? proto_map[protocol] : "none",
103 group, name, key, value,
108 irc_config(server, channel, group, name, key, value);
111 xmpp_config(server, channel, group, name, key, value);
116 void chat_notice(const char *channel, const char *from, const char *fmt, ...)
118 static char buf[1024];
122 vsnprintf(buf, sizeof(buf), fmt, ap);
125 chat_recv(channel, from, buf);
128 void chat_recv(const char *channel, const char *from, const char *text)
130 append(&msg_buf, NULL, sizeof(message_t));
131 messages = (message_t*)msg_buf.data;
133 message_t *msg = &messages[history];
134 msg->channel = find_channel(channel);
135 msg->when = time(NULL);
136 msg->from = strcopy(from);
137 msg->text = strcopy(text);
143 void chat_send(const char *channel_name, const char *text)
145 channel_t *channel = find_channel(channel_name);
147 append(&msg_buf, NULL, sizeof(message_t));
148 messages = (message_t*)msg_buf.data;
150 message_t *msg = &messages[history];
151 msg->channel = channel;
152 msg->when = time(NULL);
153 msg->from = strcopy("andy");
154 msg->text = strcopy(text);
168 for (int i = 0; i < history; i++)
169 free((void*)messages[i].text);
173 /* Server and channel function */
174 server_t *add_server(const char *name, protocol_t protocol)
176 server_t *server = NULL;
177 debug("adding %s server \"%s\"", proto_map[protocol], name);
180 server = irc_server();
183 server = xmpp_server();
186 server->next = servers;
187 server->name = strcopy(name);
188 server->protocol = protocol;
189 return (servers = server);
192 channel_t *add_channel(const char *name, server_t *server)
194 channel_t *channel = NULL;
195 debug("adding %s channel \"%s\"", server->name, name);
196 switch (server->protocol) {
198 channel = irc_channel();
201 channel = xmpp_channel();
204 channel->next = channels;
205 channel->name = strcopy(name);
206 channel->server = server;
207 return (channels = channel);
210 server_t *find_server(const char *name)
212 for (server_t *cur = servers; cur; cur = cur->next)
213 if (match(cur->name, name))
218 channel_t *find_channel(const char *name)
220 for (channel_t *cur = channels; cur; cur = cur->next) {
221 if (match(cur->name, name))