hirc

IRC client
Log | Files | Refs

commit 1d0762a214430910daaf493d7f25dbe024de58ba
parent 3a4eb45429c95eed49cecbf454ee674e93375ff3
Author: hhvn <dev@hhvn.uk>
Date:   Sun,  6 Mar 2022 16:49:07 +0000

Log cooked data.

Diffstat:
Msrc/handle.c | 30++++++++++++++++++++----------
Msrc/hirc.h | 2+-
Msrc/hist.c | 81++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Msrc/serv.c | 2+-
Msrc/struct.h | 1-
5 files changed, 75 insertions(+), 41 deletions(-)

diff --git a/src/handle.c b/src/handle.c @@ -121,7 +121,7 @@ handle_JOIN) { nick_add(&chan->nicks, msg->from->prefix, ' ', server); hist_addp(server->history, msg, Activity_status, HIST_LOG); - hist_addp(chan->history, msg, Activity_status, HIST_SHOW); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); if (nick_isself(nick)) { if (strcmp_n(target, expect_get(server, Expect_join)) == 0) @@ -163,7 +163,7 @@ handle_PART) { } hist_addp(server->history, msg, Activity_status, HIST_LOG); - hist_addp(chan->history, msg, Activity_status, HIST_SHOW); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); } HANDLER( @@ -193,7 +193,7 @@ handle_KICK) { } hist_addp(server->history, msg, Activity_status, HIST_LOG); - hist_addp(chan->history, msg, Activity_status, HIST_SHOW); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); nick_free(nick); } @@ -220,7 +220,7 @@ handle_QUIT) { for (chan = server->channels; chan; chan = chan->next) { if (nick_get(&chan->nicks, nick->nick) != NULL) { nick_remove(&chan->nicks, nick->nick); - hist_addp(chan->history, msg, Activity_status, HIST_SHOW); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); if (chan == selected.channel) windows[Win_nicklist].refresh = 1; } @@ -238,6 +238,7 @@ handle_MODE) { if ((chan = chan_get(&server->channels, *(msg->params+1), -1)) == NULL) chan = chan_add(server, &server->channels, *(msg->params+1), 0); + hist_addp(server->history, msg, Activity_status, HIST_LOG); hist_addp(chan->history, msg, Activity_status, HIST_DFL); ircprintf(server, "MODE %s\r\n", chan->name); /* Get full mode via RPL_CHANNELMODEIS * instead of concatenating manually */ @@ -320,11 +321,12 @@ handle_RPL_AWAY) { struct Channel *priv; struct HistInfo *history; - if ((priv = chan_get(&server->privs, *(msg->params+2), -1)) != NULL) - history = priv->history; - else - history = server->history; - hist_addp(history, msg, Activity_status, HIST_DFL); + if ((priv = chan_get(&server->privs, *(msg->params+2), -1)) != NULL) { + hist_addp(priv->history, msg, Activity_status, HIST_DFL); + hist_addp(server->history, msg, Activity_status, HIST_LOG); + } else { + hist_addp(server->history, msg, Activity_status, HIST_DFL); + } } HANDLER( @@ -340,6 +342,7 @@ handle_RPL_CHANNELMODEIS) { free(chan->mode); chan->mode = estrdup(*(msg->params+3)); + hist_addp(server->history, msg, Activity_status, HIST_LOG); if (expect_get(server, Expect_channelmodeis)) { hist_addp(chan->history, msg, Activity_status, HIST_DFL); expect_set(server, Expect_channelmodeis, NULL); @@ -360,6 +363,8 @@ handle_RPL_NAMREPLY) { if (param_len(params) < 5) return; + hist_addp(server->history, msg, Activity_status, HIST_LOG); + params += 3; target = *params; @@ -462,7 +467,7 @@ handle_NICK) { priv = chnick->priv; nick_remove(&chan->nicks, nick->nick); nick_add(&chan->nicks, prefix, priv, server); - hist_addp(chan->history, msg, Activity_status, HIST_SHOW); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); if (selected.channel == chan) windows[Win_nicklist].refresh = 1; } @@ -493,6 +498,7 @@ handle_RPL_NOTOPIC) { target = *(msg->params+2); + hist_addp(server->history, msg, Activity_status, HIST_LOG); if ((chan = chan_get(&server->channels, target, -1)) == NULL) return; @@ -512,6 +518,8 @@ handle_RPL_TOPIC) { if (param_len(msg->params) < 4) return; + hist_addp(server->history, msg, Activity_status, HIST_LOG); + target = *(msg->params+2); topic = *(msg->params+3); @@ -538,6 +546,8 @@ handle_RPL_TOPICWHOTIME) { if (param_len(msg->params) < 5) return; + hist_addp(server->history, msg, Activity_status, HIST_LOG); + target = *(msg->params+2); if ((chan = chan_get(&server->channels, target, -1)) == NULL) diff --git a/src/hirc.h b/src/hirc.h @@ -94,7 +94,7 @@ struct History *hist_add(struct HistInfo *histinfo, struct History *hist_format(struct HistInfo *history, enum Activity activity, enum HistOpt options, char *format, ...); int hist_len(struct History **history); -int hist_log(char *msg, struct Nick *from, time_t timestamp, struct Server *server); +int hist_log(struct History *hist); void hist_purgeopt(struct HistInfo *histinfo, enum HistOpt options); /* serv.c */ diff --git a/src/hist.c b/src/hist.c @@ -148,14 +148,14 @@ hist_add(struct HistInfo *histinfo, new->next->prev = new; histinfo->history = new; +ui: if (options & HIST_LOG) { if (histinfo->server) - hist_log(new->raw, new->from, new->timestamp, histinfo->server); + hist_log(new); else ui_error("HIST_LOG specified, but server is NULL", NULL); } -ui: /* TODO: this triggers way too often, need to have some sort of delay */ if (selected.history == histinfo) { if (options & HIST_SELF) @@ -210,10 +210,13 @@ hist_format(struct HistInfo *histinfo, enum Activity activity, enum HistOpt opti } int -hist_log(char *msg, struct Nick *from, time_t timestamp, struct Server *server) { +hist_log(struct History *hist) { char filename[2048]; + FILE *f; char *logdir; int ret, serrno; + struct stat st; + char *nick, *ident, *host, *raw; if (!config_getl("log.toggle")) return -2; @@ -221,35 +224,57 @@ hist_log(char *msg, struct Nick *from, time_t timestamp, struct Server *server) if ((logdir = config_gets("log.dir")) == NULL) return -3; - if (*msg == ':' && strchr(msg, ' ')) - msg = strchr(msg, ' ') + 1; - - if (dprintf(server->logfd, "!%lld :%s %s\n", (long long)timestamp, from ? from->prefix : server->name, msg) < 0) { - /* Can't write, try to open the file */ - snprintf(filename, sizeof(filename), "%s/%s.log", homepath(logdir), server->name); - ret = open(filename, O_CREAT|O_APPEND|O_WRONLY); - serrno = errno; - if (ret == -1 && serrno == ENOENT) { - /* No such directory: attempt to create logdir */ - if (mkdir(homepath(logdir), 0700) == -1) { - ui_error("Could not create '%s' directory for logging: %s", logdir, strerror(errno)); - return -1; - } - } else if (ret == -1) { - ui_error("Could not open '%s' for logging: %s", filename, strerror(serrno)); - return -1; - } else { - server->logfd = ret; + logdir = homepath(logdir); + + if (!hist || !hist->origin || !hist->origin->server) + return -4; + + if (stat(logdir, &st) == -1) { + if (mkdir(logdir, 0700) == -1) { + ui_error("Could not create dir '%s': %s", logdir, strerror(errno)); + return -5; } - } else return 1; + } + + if (hist->origin->channel) + snprintf(filename, sizeof(filename), "%s/%s,%s.log", logdir, hist->origin->server->name, hist->origin->channel->name); + else + snprintf(filename, sizeof(filename), "%s/%s.log", logdir, hist->origin->server->name); + + if (!(f = fopen(filename, "a"))) { + ui_error("Could not open '%s': %s", filename, strerror(errno)); + return -6; + } + + if (hist->from) { + nick = hist->from->nick ? hist->from->nick : " "; + ident = hist->from->ident ? hist->from->ident : " "; + host = hist->from->host ? hist->from->host : " "; + } else { + nick = ident = host = " "; + } - /* retry */ - if (dprintf(server->logfd, "!%lld :%s %s\n", (long long)timestamp, from ? from->prefix : server->name, msg) < 0) { - ui_error("Failed to write to log of server '%s': %s", server->name, strerror(errno)); - return -1; + if (*hist->raw == ':' && strchr(hist->raw, ' ')) + raw = strchr(hist->raw, ' ') + 1; + else + raw = hist->raw; + + ret = fprintf(f, + "%lld\t%d\t%d\t%d\t%c\t%s\t%s\t%s\t%s\n", + (long long)hist->timestamp, + hist->activity, + (hist->options & HIST_SHOW) ? 1 : 0, + hist->from ? hist->from->self : 0, /* If from does not exist, it's probably not from us */ + hist->from ? hist->from->priv : ' ', + nick, ident, host, raw); + + if (ret < 0) { + ui_error("Could not write to '%s': %s", filename, strerror(errno)); + fclose(f); + return -7; } - return 1; + fclose(f); } int diff --git a/src/serv.c b/src/serv.c @@ -72,7 +72,7 @@ serv_create(char *name, char *host, char *port, char *nick, server = emalloc(sizeof(struct Server)); server->prev = server->next = NULL; - server->wfd = server->rfd = server->logfd = -1; + server->wfd = server->rfd = -1; server->inputlen = 0; server->rpollfd = emalloc(sizeof(struct pollfd)); server->rpollfd->fd = -1; diff --git a/src/struct.h b/src/struct.h @@ -134,7 +134,6 @@ struct Server { char inputbuf[SERVER_INPUT_SIZE]; int inputlen; struct pollfd *rpollfd; - int logfd; enum ConnStatus status; char *name; char *username;