]> Pileus Git - ~andy/lamechat/blobdiff - chat.c
Make logfile optional.
[~andy/lamechat] / chat.c
diff --git a/chat.c b/chat.c
index 9d2d0059231463481afb8b6887d86c333db3cd91..b0d008604a221e19e14adae217bc6c87ce858681 100644 (file)
--- a/chat.c
+++ b/chat.c
@@ -34,6 +34,7 @@
 /* Global data */
 server_t  *servers;
 channel_t *channels;
+user_t    *users;
 message_t *messages;
 int        history;
 
@@ -81,6 +82,9 @@ static void write_log(message_t *msg)
 {
        static char log_path[4096];
 
+       if (!log_dir)
+               return;
+
        channel_t *chan = msg->channel;
        server_t  *srv  = chan->server;
 
@@ -110,8 +114,8 @@ static void write_log(message_t *msg)
        fprintf(chan->log, "(%04d-%02d-%02d %02d:%02d:%02d) %s%s %s\n",
                        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
                        tm->tm_hour, tm->tm_min, tm->tm_sec,
-                       msg->from ?: "***",
-                       msg->from ? ":" : "",
+                       msg->from ? msg->from->name : "***",
+                       msg->from ? ":"             : "",
                        msg->text);
        fflush(chan->log);
 }
@@ -127,72 +131,70 @@ void chat_init(void)
        xmpp_init();
 }
 
-void chat_config(const char *group, const char *name, const char *key, const char *value)
+void proto_config(const char *pname, const char *sname, const char *cname,
+                  const char *group, const char *name,
+                  const char *key, const char *value)
 {
-       int        protocol = -1;
-       server_t  *server   = NULL;
-       channel_t *channel  = NULL;
+       protocol_t proto = -1;
+       server_t  *srv   = NULL;
+       channel_t *chan  = NULL;
+
+       for (srv = servers; srv; srv = srv->next)
+               if (match(sname, srv->name))
+                       break;
+       for (chan = channels; chan; chan = chan->next)
+               if (match(cname, chan->name))
+                       break;
+
+       if      (pname) proto = get_map(pname, proto_map);
+       else if (srv)   proto = srv->protocol;
+       else if (chan)  proto = chan->server->protocol;
+
+       debug("chat_config: [%s-%s \"%s\"] %s = %s",
+                       proto>=0 ? proto_map[proto] : "none",
+                       group, name, key, value);
 
+       switch (proto) {
+               case IRC:
+                       irc_config(srv, chan, group, name, key, value);
+                       break;
+               case XMPP:
+                       xmpp_config(srv, chan, group, name, key, value);
+                       break;
+       }
+}
+
+void chat_config(const char *group, const char *name, const char *key, const char *value)
+{
        if (match(group, "general")) {
-               if (match(key, "logdir")) {
+               if (match(key, "logdir"))
                        log_dir = strcopy(get_string(value));
-               }
        }
-
        if (match(group, "server")) {
-               server = find_server(name);
-               if (match(key, "")) {
+               if (match(key, ""))
                        get_name(name);
-               }
-               else if (match(key, "protocol")) {
-                       protocol = get_enum(value, proto_map, N_ELEMENTS(proto_map));
-                       server = add_server(name, protocol);
-               }
-               else if (server) {
-                       protocol = server->protocol;
-               }
-               else {
-                       error("No server found for [%s]", name);
-               }
+               else if (match(key, "protocol"))
+                       proto_config(value, NULL, NULL, group, name, key, value);
+               else
+                       proto_config(NULL, name, NULL, group, name, key, value);
        }
-
        if (match(group, "channel")) {
-               channel = find_channel(name);
-               if (match(key, "")) {
+               if (match(key, ""))
                        get_name(name);
-               }
-               else if (match(key, "server")) {
-                       server = find_server(get_string(value));
-                       if (!server)
-                               error("server '%s' does not exist", name);
-                       channel = add_channel(name, server);
-                       protocol = server->protocol;
-                       server = NULL;
-               }
-               else if (channel) {
-                       protocol = channel->server->protocol;
-               }
-               else {
-                       error("No channel found for [%s]", name);
-               }
+               else if (match(key, "server"))
+                       proto_config(NULL, value, NULL, group, name, key, value);
+               else
+                       proto_config(NULL, NULL, name, group, name, key, value);
        }
-
-       debug("chat_config: [%s-%s \"%s\"] %s = %s",
-                       protocol>=0 ? proto_map[protocol] : "none",
-                       group, name, key, value,
-                       server, channel);
-
-       switch (protocol) {
-               case IRC:
-                       irc_config(server, channel, group, name, key, value);
-                       break;
-               case XMPP:
-                       xmpp_config(server, channel, group, name, key, value);
-                       break;
+       if (match(group, "autojoin")) {
+               if (match(key, ""))
+                       get_name(name);
+               else
+                       proto_config(NULL, name, key, group, name, key, value);
        }
 }
 
-void chat_recv(channel_t *channel, const char *from, const char *text)
+void chat_recv(channel_t *channel, user_t *from, const char *text)
 {
        append(&msg_buf, NULL, sizeof(message_t));
        messages = (message_t*)msg_buf.data;
@@ -200,7 +202,7 @@ void chat_recv(channel_t *channel, const char *from, const char *text)
        message_t *msg = &messages[history];
        msg->channel = channel;
        msg->when = time(NULL);
-       msg->from = strcopy(from);
+       msg->from = from;
        msg->text = strcopy(text);
 
        history++;
@@ -208,6 +210,18 @@ void chat_recv(channel_t *channel, const char *from, const char *text)
        view_draw();
 }
 
+void chat_complete(channel_t *channel, const char *text)
+{
+       switch (channel->server->protocol) {
+               case IRC:
+                       irc_complete(channel, text);
+                       break;
+               case XMPP:
+                       xmpp_complete(channel, text);
+                       break;
+       }
+}
+
 void chat_send(channel_t *channel, const char *text)
 {
        switch (channel->server->protocol) {
@@ -220,73 +234,46 @@ void chat_send(channel_t *channel, const char *text)
        }
 }
 
+void chat_update(void)
+{
+       view_draw();
+}
+
 void chat_exit(void)
 {
        irc_exit();
        xmpp_exit();
 
-       for (int i = 0; i < history; i++) {
-               free((void*)messages[i].from);
+       for (int i = 0; i < history; i++)
                free((void*)messages[i].text);
-       }
        release(&msg_buf);
 }
 
 /* Server and channel function */
-server_t *add_server(const char *name, protocol_t protocol)
+void add_server(server_t *server)
 {
-       server_t *server = NULL;
+       protocol_t proto = server->protocol;
+       debug("adding %s server \"%s\"", proto_map[proto], server->name);
        server_t **last = &servers;
-       debug("adding %s server \"%s\"", proto_map[protocol], name);
-       switch (protocol) {
-               case IRC:
-                       server = irc_server();
-                       break;
-               case XMPP:
-                       server = xmpp_server();
-                       break;
-       }
-       server->name = strcopy(name);
-       server->protocol = protocol;
        while (*last)
                last = &(*last)->next;
-       return (*last = server);
+       *last = server;
 }
 
-channel_t *add_channel(const char *name, server_t *server)
+void add_channel(channel_t *channel)
 {
-       channel_t *channel = NULL;
+       debug("adding %s channel \"%s\"", channel->server->name, channel->name);
        channel_t **last = &channels;
-       debug("adding %s channel \"%s\"", server->name, name);
-       switch (server->protocol) {
-               case IRC:
-                       channel = irc_channel();
-                       break;
-               case XMPP:
-                       channel = xmpp_channel();
-                       break;
-       }
-       channel->name = strcopy(name);
-       channel->server = server;
        while (*last)
                last = &(*last)->next;
-       return (*last = channel);
-}
-
-server_t *find_server(const char *name)
-{
-       for (server_t *cur = servers; cur; cur = cur->next)
-               if (match(cur->name, name))
-                       return cur;
-       return NULL;
+       *last = channel;
 }
 
-channel_t *find_channel(const char *name)
+void add_user(user_t *user)
 {
-       for (channel_t *cur = channels; cur; cur = cur->next) {
-               if (match(cur->name, name))
-                       return cur;
-       }
-       return NULL;
+       debug("adding %s user \"%s\"", user->server->name, user->name);
+       user_t **last = &users;
+       while (*last)
+               last = &(*last)->next;
+       *last = user;
 }
-