hirc

IRC client
Log | Files | Refs

commit 4f2efe992f6edf630c55e16df8f2088132655564
parent 68e7cd5a064c2d65d0e73b050050b32c9bf033ee
Author: hhvn <dev@hhvn.uk>
Date:   Sat, 15 Jan 2022 16:34:43 +0000

Tidy up history

- prefix is now skipped in params automatically
- a history struct is now passed to handle_ functions

Diffstat:
Msrc/commands.c | 2+-
Msrc/handle.c | 287+++++++++++++++++++++++++++++++++++++------------------------------------------
Msrc/hirc.h | 7+++++--
Msrc/hist.c | 34++++++++++++++++++++--------------
Msrc/struct.h | 5+++--
Msrc/ui.c | 20++++++--------------
6 files changed, 171 insertions(+), 184 deletions(-)

diff --git a/src/commands.c b/src/commands.c @@ -1118,7 +1118,7 @@ command_grep(struct Server *server, char *str) { /* TODO: matching ui_format result by default, * option for matching raw */ if (regexec(&re, p->raw, 0, NULL, 0) == 0) - hist_add(selected.history, p->from, p->raw, p->params, p->activity, p->timestamp, p->options | HIST_GREP | HIST_TMP); + hist_addp(selected.history, p, p->activity, p->options | HIST_GREP | HIST_TMP); } hist_format(selected.history, Activity_none, HIST_SHOW|HIST_TMP|HIST_GREP, "SELF_GREP_END :end of /grep command"); diff --git a/src/handle.c b/src/handle.c @@ -23,27 +23,29 @@ #include <stdlib.h> #include "hirc.h" -static void handle_ERROR(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_PING(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_PONG(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_JOIN(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_PART(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_KICK(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_QUIT(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_NICK(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_MODE(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_TOPIC(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_PRIVMSG(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_RPL_WELCOME(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_RPL_ISUPPORT(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_RPL_CHANNELMODEIS(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_RPL_NOTOPIC(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_RPL_TOPIC(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_RPL_TOPICWHOTIME(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_RPL_NAMREPLY(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_RPL_ENDOFNAMES(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_RPL_ENDOFMOTD(char *msg, char **params, struct Server *server, time_t timestamp); -static void handle_ERR_NICKNAMEINUSE(char *msg, char **params, struct Server *server, time_t timestamp); +#define HANDLER(func) static void func(struct Server *server, struct History *msg) +HANDLER(handle_ERROR); +HANDLER(handle_PING); +HANDLER(handle_PONG); +HANDLER(handle_JOIN); +HANDLER(handle_PART); +HANDLER(handle_KICK); +HANDLER(handle_QUIT); +HANDLER(handle_NICK); +HANDLER(handle_MODE); +HANDLER(handle_TOPIC); +HANDLER(handle_PRIVMSG); +HANDLER(handle_RPL_WELCOME); +HANDLER(handle_RPL_ISUPPORT); +HANDLER(handle_RPL_CHANNELMODEIS); +HANDLER(handle_RPL_NOTOPIC); +HANDLER(handle_RPL_TOPIC); +HANDLER(handle_RPL_TOPICWHOTIME); +HANDLER(handle_RPL_NAMREPLY); +HANDLER(handle_RPL_ENDOFNAMES); +HANDLER(handle_RPL_ENDOFMOTD); +HANDLER(handle_ERR_NICKNAMEINUSE); +#undef HANDLER struct Handler handlers[] = { { "ERROR", handle_ERROR }, @@ -76,55 +78,49 @@ struct Handler handlers[] = { }; static void -handle_PING(char *msg, char **params, struct Server *server, time_t timestamp) { - if (**params == ':') - params++; - - if (param_len(params) < 2) +handle_PING(struct Server *server, struct History *msg) { + if (param_len(msg->params) < 2) return; - ircprintf(server, "PONG :%s\r\n", *(params+1)); + ircprintf(server, "PONG :%s\r\n", *(msg->params+1)); } static void -handle_PONG(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_PONG(struct Server *server, struct History *msg) { int len; - if (**params == ':') - params++; - - if ((len = param_len(params)) < 2) + if ((len = param_len(msg->params)) < 2) return; /* RFC1459 says that PONG should have a list of daemons, * but that's not how PONG seems to work in modern IRC. * Therefore, consider the last parameter as the "message" */ - if (strcmp_n(*(params + len - 1), handle_expect_get(server, Expect_pong)) == 0) { - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + if (strcmp_n(*(msg->params + len - 1), handle_expect_get(server, Expect_pong)) == 0) { + hist_addp(server->history, msg, Activity_status, HIST_DFL); handle_expect(server, Expect_pong, NULL); } } static void -handle_JOIN(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_JOIN(struct Server *server, struct History *msg) { struct Channel *chan; struct Nick *nick; char *target; - if (**params != ':' || param_len(params) < 3) + if (!msg->from || param_len(msg->params) < 2) return; - target = *(params+2); + target = *(msg->params+1); if ((chan = chan_get(&server->channels, target, -1)) == NULL) chan = chan_add(server, &server->channels, target, 0); chan_setold(chan, 0); - nick = nick_create(*params, ' ', server); + nick = msg->from; if (nick_get(&chan->nicks, nick->nick) == NULL) - nick_add(&chan->nicks, *params, ' ', server); + nick_add(&chan->nicks, msg->from->prefix, ' ', server); - hist_add(server->history, nick, msg, params, Activity_status, timestamp, HIST_LOG); - hist_add(chan->history, nick, msg, params, Activity_status, timestamp, HIST_SHOW); + hist_addp(server->history, msg, Activity_status, HIST_LOG); + hist_addp(chan->history, msg, Activity_status, HIST_SHOW); if (nick_isself(nick)) { if (strcmp_n(target, handle_expect_get(server, Expect_join)) == 0) @@ -135,24 +131,22 @@ handle_JOIN(char *msg, char **params, struct Server *server, time_t timestamp) { } else if (selected.channel == chan) { windows[Win_nicklist].refresh = 1; } - - nick_free(nick); } static void -handle_PART(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_PART(struct Server *server, struct History *msg) { struct Channel *chan; struct Nick *nick; char *target; - if (**params != ':' || param_len(params) < 3) + if (!msg->from || param_len(msg->params) < 2) return; - target = *(params+2); + target = *(msg->params+1); if ((chan = chan_get(&server->channels, target, -1)) == NULL) return; - nick = nick_create(*params, ' ', server); + nick = msg->from; if (nick_isself(nick)) { chan_setold(chan, 1); nick_free_list(&chan->nicks); @@ -167,25 +161,24 @@ handle_PART(char *msg, char **params, struct Server *server, time_t timestamp) { windows[Win_nicklist].refresh = 1; } - hist_add(server->history, nick, msg, params, Activity_status, timestamp, HIST_LOG); - hist_add(chan->history, nick, msg, params, Activity_status, timestamp, HIST_SHOW); - nick_free(nick); + hist_addp(server->history, msg, Activity_status, HIST_LOG); + hist_addp(chan->history, msg, Activity_status, HIST_SHOW); } static void -handle_KICK(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_KICK(struct Server *server, struct History *msg) { struct Channel *chan; struct Nick *nick; char *target; - if (**params != ':' || param_len(params) < 4) + if (!msg->from || param_len(msg->params) < 3) return; - target = *(params+2); + target = *(msg->params+1); if ((chan = chan_get(&server->channels, target, -1)) == NULL) chan = chan_add(server, &server->channels, target, 0); - nick = nick_create(*(params+3), ' ', server); + nick = nick_create(*(msg->params+2), ' ', server); if (nick_isself(nick)) { chan_setold(chan, 1); nick_free_list(&chan->nicks); @@ -198,118 +191,110 @@ handle_KICK(char *msg, char **params, struct Server *server, time_t timestamp) { windows[Win_nicklist].refresh = 1; } - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_LOG); - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_SHOW); + hist_addp(server->history, msg, Activity_status, HIST_LOG); + hist_addp(chan->history, msg, Activity_status, HIST_SHOW); nick_free(nick); } static void -handle_ERROR(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_ERROR(struct Server *server, struct History *msg) { serv_disconnect(server, 0, NULL); - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(server->history, msg, Activity_status, HIST_DFL); } static void -handle_QUIT(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_QUIT(struct Server *server, struct History *msg) { struct Channel *chan; struct Nick *nick; - if (**params != ':' || param_len(params) < 2) + if (!msg->from || param_len(msg->params) < 1) return; - nick = nick_create(*params, ' ', server); + nick = msg->from; if (nick_isself(nick)) { serv_disconnect(server, 0, NULL); } - hist_add(server->history, nick, msg, params, Activity_status, timestamp, HIST_LOG); + hist_addp(server->history, msg, Activity_status, HIST_LOG); for (chan = server->channels; chan; chan = chan->next) { if (nick_get(&chan->nicks, nick->nick) != NULL) { nick_remove(&chan->nicks, nick->nick); - hist_add(chan->history, nick, msg, params, Activity_status, timestamp, HIST_SHOW); + hist_addp(chan->history, msg, Activity_status, HIST_SHOW); if (chan == selected.channel) windows[Win_nicklist].refresh = 1; } } - - nick_free(nick); } static void -handle_MODE(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_MODE(struct Server *server, struct History *msg) { struct Channel *chan; - if (**params != ':' || param_len(params) < 4) + if (!msg->from || param_len(msg->params) < 3) return; - if (serv_ischannel(server, *(params+2))) { - if ((chan = chan_get(&server->channels, *(params+2), -1)) == NULL) - chan = chan_add(server, &server->channels, *(params+2), 0); + if (serv_ischannel(server, *(msg->params+1))) { + if ((chan = chan_get(&server->channels, *(msg->params+1), -1)) == NULL) + chan = chan_add(server, &server->channels, *(msg->params+1), 0); - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + 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 */ ircprintf(server, "NAMES %s\r\n", chan->name); /* Also get updated priviledges */ } else { - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(server->history, msg, Activity_status, HIST_DFL); } } static void -handle_PRIVMSG(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_PRIVMSG(struct Server *server, struct History *msg) { int act_direct = Activity_hilight, act_regular = Activity_message; struct Channel *chan; struct Channel *priv; struct Nick *nick; char *target; - if (**params != ':' || param_len(params) < 4) + if (!msg->from || param_len(msg->params) < 3) return; - if (strcmp(*params, "NOTICE") == 0) + if (strcmp(*msg->params, "NOTICE") == 0) act_direct = act_regular = Activity_notice; - target = *(params + 2); - nick = nick_create(*params, ' ', server); + target = *(msg->params + 1); + nick = msg->from; if (strchr(nick->nick, '.')) { /* it's a server */ - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(server->history, msg, Activity_status, HIST_DFL); } else if (strcmp(target, server->self->nick) == 0) { /* it's messaging me */ if ((priv = chan_get(&server->privs, nick->nick, -1)) == NULL) priv = chan_add(server, &server->privs, nick->nick, 1); chan_setold(priv, 0); - hist_add(priv->history, nick, msg, params, act_direct, timestamp, HIST_DFL); + hist_addp(priv->history, msg, act_direct, HIST_DFL); } else if (nick_isself(nick) && !chrcmp(*target, "#&!+")) { /* i'm messaging someone */ if ((priv = chan_get(&server->privs, target, -1)) == NULL) priv = chan_add(server, &server->privs, target, 1); chan_setold(priv, 0); - hist_add(priv->history, nick, msg, params, act_regular, timestamp, HIST_DFL); + hist_addp(priv->history, msg, act_regular, HIST_DFL); } else { /* message to a channel */ if ((chan = chan_get(&server->channels, target, -1)) == NULL) chan = chan_add(server, &server->channels, target, 0); - hist_add(chan->history, nick, msg, params, - strstr(*(params+3), server->self->nick) ? act_direct : act_regular, - timestamp, HIST_DFL); + hist_addp(chan->history, msg, nick_isself(nick) ? act_direct : act_regular, HIST_DFL); } - - nick_free(nick); } static void -handle_RPL_ISUPPORT(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_RPL_ISUPPORT(struct Server *server, struct History *msg) { char *key, *value; + char **params = msg->params; - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); - if (**params == ':') - params++; - - if (param_len(params) < 4) + hist_addp(server->history, msg, Activity_status, HIST_DFL); + if (param_len(msg->params) < 4) return; params += 2; @@ -330,38 +315,35 @@ handle_RPL_ISUPPORT(char *msg, char **params, struct Server *server, time_t time } static void -handle_RPL_CHANNELMODEIS(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_RPL_CHANNELMODEIS(struct Server *server, struct History *msg) { struct Channel *chan; - if (**params != ':' && param_len(params) < 5) + if (param_len(msg->params) < 4) return; - if ((chan = chan_get(&server->channels, *(params+3), -1)) == NULL) - chan = chan_add(server, &server->channels, *(params+3), 0); + if ((chan = chan_get(&server->channels, *(msg->params+2), -1)) == NULL) + chan = chan_add(server, &server->channels, *(msg->params+2), 0); free(chan->mode); - chan->mode = strdup(*(params+4)); + chan->mode = strdup(*(msg->params+3)); if (handle_expect_get(server, Expect_channelmodeis)) { - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); handle_expect(server, Expect_channelmodeis, NULL); } else { - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_LOG); + hist_addp(chan->history, msg, Activity_status, HIST_LOG); } } static void -handle_RPL_NAMREPLY(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_RPL_NAMREPLY(struct Server *server, struct History *msg) { struct Channel *chan; struct Nick *oldnick; - char **bparams = params; + char **params = msg->params; char *nick, priv, *target; char **nicks, **nicksref; char *supportedprivs; - if (**params == ':') - params++; - if (param_len(params) < 5) return; @@ -372,9 +354,9 @@ handle_RPL_NAMREPLY(char *msg, char **params, struct Server *server, time_t time chan = chan_add(server, &server->channels, target, 0); if (strcmp_n(target, handle_expect_get(server, Expect_names)) == 0) - hist_add(chan->history, NULL, msg, bparams, Activity_status, timestamp, HIST_DFL); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); else - hist_add(chan->history, NULL, msg, bparams, Activity_status, timestamp, HIST_LOG); + hist_addp(chan->history, msg, Activity_status, HIST_LOG); params++; supportedprivs = strchr(support_get(server, "PREFIX"), ')'); @@ -404,26 +386,23 @@ handle_RPL_NAMREPLY(char *msg, char **params, struct Server *server, time_t time } static void -handle_RPL_ENDOFNAMES(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_RPL_ENDOFNAMES(struct Server *server, struct History *msg) { char *target; - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_LOG); - if (params && *params && **params == ':') - params++; - - if (param_len(params) < 3) + hist_addp(server->history, msg, Activity_status, HIST_LOG); + if (param_len(msg->params) < 3) return; - target = *(params+2); + target = *(msg->params+2); if (strcmp_n(target, handle_expect_get(server, Expect_names)) == 0) handle_expect(server, Expect_names, NULL); } static void -handle_ERR_NICKNAMEINUSE(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_ERR_NICKNAMEINUSE(struct Server *server, struct History *msg) { char nick[64]; /* should be limited to 9 chars, but newer servers *shrug*/ - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(server->history, msg, Activity_status, HIST_DFL); if (handle_expect_get(server, Expect_nicknameinuse) == NULL) { snprintf(nick, sizeof(nick), "%s_", server->self->nick); @@ -437,19 +416,19 @@ handle_ERR_NICKNAMEINUSE(char *msg, char **params, struct Server *server, time_t } static void -handle_NICK(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_NICK(struct Server *server, struct History *msg) { struct Nick *nick, *chnick; struct Channel *chan; char prefix[128]; char *newnick; char priv; - if (**params != ':' || !*(params+1) || !*(params+2)) + if (!msg->from || !*msg->params || !*msg->params+1) return; - nick = nick_create(*params, ' ', server); - hist_add(server->history, nick, msg, params, Activity_status, timestamp, HIST_DFL); - newnick = *(params+2); + nick = msg->from; + hist_addp(server->history, msg, Activity_status, HIST_DFL); + newnick = *(msg->params+1); if (strcmp(nick->nick, newnick) == 0) return; @@ -468,7 +447,7 @@ handle_NICK(char *msg, char **params, struct Server *server, time_t timestamp) { priv = chnick->priv; nick_remove(&chan->nicks, nick->nick); nick_add(&chan->nicks, prefix, priv, server); - hist_add(chan->history, nick, msg, params, Activity_status, timestamp, HIST_SHOW); + hist_addp(chan->history, msg, Activity_status, HIST_SHOW); if (selected.channel == chan) windows[Win_nicklist].refresh = 1; } @@ -476,51 +455,50 @@ handle_NICK(char *msg, char **params, struct Server *server, time_t timestamp) { } static void -handle_TOPIC(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_TOPIC(struct Server *server, struct History *msg) { struct Channel *chan; - if (param_len(params) < 4 || **params != ':') + if (param_len(msg->params) < 3 || !msg->from) return; - - if ((chan = chan_get(&server->channels, *(params+2), -1)) != NULL) { - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + if ((chan = chan_get(&server->channels, *(msg->params+1), -1)) != NULL) { + hist_addp(chan->history, msg, Activity_status, HIST_DFL); free(chan->topic); - chan->topic = *(params+3) ? strdup(*(params+3)) : NULL; + chan->topic = *(msg->params+2) ? strdup(*(msg->params+2)) : NULL; } } static void -handle_RPL_NOTOPIC(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_RPL_NOTOPIC(struct Server *server, struct History *msg) { struct Channel *chan; char *target; - if (param_len(params) < 5 || **params != ':') + if (param_len(msg->params) < 4) return; - target = *(params+3); + target = *(msg->params+2); if ((chan = chan_get(&server->channels, target, -1)) == NULL) return; if (strcmp_n(target, handle_expect_get(server, Expect_topic)) == 0) { - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); handle_expect(server, Expect_topic, NULL); } else { - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_LOG); + hist_addp(chan->history, msg, Activity_status, HIST_LOG); } } static void -handle_RPL_TOPIC(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_RPL_TOPIC(struct Server *server, struct History *msg) { struct Channel *chan; char *target, *topic; - if (param_len(params) < 5 || **params != ':') + if (param_len(msg->params) < 4) return; - target = *(params+3); - topic = *(params+4); + target = *(msg->params+2); + topic = *(msg->params+3); if ((chan = chan_get(&server->channels, target, -1)) == NULL) return; @@ -529,46 +507,46 @@ handle_RPL_TOPIC(char *msg, char **params, struct Server *server, time_t timesta chan->topic = topic ? strdup(topic) : NULL; if (strcmp_n(target, handle_expect_get(server, Expect_topic)) == 0) { - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); handle_expect(server, Expect_topic, NULL); handle_expect(server, Expect_topicwhotime, target); } else { - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_LOG); + hist_addp(chan->history, msg, Activity_status, HIST_LOG); } } static void -handle_RPL_TOPICWHOTIME(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_RPL_TOPICWHOTIME(struct Server *server, struct History *msg) { struct Channel *chan; char *target; - if (param_len(params) < 6 || **params != ':') + if (param_len(msg->params) < 5) return; - target = *(params+3); + target = *(msg->params+2); if ((chan = chan_get(&server->channels, target, -1)) == NULL) return; if (strcmp_n(target, handle_expect_get(server, Expect_topicwhotime)) == 0) { - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(chan->history, msg, Activity_status, HIST_DFL); handle_expect(server, Expect_topicwhotime, NULL); } else { - hist_add(chan->history, NULL, msg, params, Activity_status, timestamp, HIST_LOG); + hist_addp(chan->history, msg, Activity_status, HIST_LOG); } } static void -handle_RPL_WELCOME(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_RPL_WELCOME(struct Server *server, struct History *msg) { server->status = ConnStatus_connected; - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(server->history, msg, Activity_status, HIST_DFL); } static void -handle_RPL_ENDOFMOTD(char *msg, char **params, struct Server *server, time_t timestamp) { +handle_RPL_ENDOFMOTD(struct Server *server, struct History *msg) { /* If server doesn't support RPL_WELCOME, use RPL_ENDOFMOTD to set status */ server->status = ConnStatus_connected; - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_addp(server->history, msg, Activity_status, HIST_DFL); } /* Expect stuff should probably be moved to serv.c. @@ -591,12 +569,13 @@ handle_expect_get(struct Server *server, enum Expect cmd) { } void -handle_logonly(char *msg, char **params, struct Server *server, time_t timestamp) { - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_LOG); +handle_logonly(struct Server *server, struct History *msg) { + hist_addp(server->history, msg, Activity_status, HIST_LOG); } void handle(struct Server *server, char *msg) { + struct History *hist; time_t timestamp; char **params; char *cmd; @@ -628,13 +607,19 @@ handle(struct Server *server, char *msg) { for (i=0; cmd && handlers[i].cmd; i++) { if (strcmp(handlers[i].cmd, cmd) == 0) { - if (handlers[i].func) - handlers[i].func(msg, params, server, timestamp); + if (handlers[i].func) { + /* histinfo set to the server's history + * currently, but not actually appended */ + hist = hist_create(server->history, NULL, msg, 0, timestamp, 0); + handlers[i].func(server, hist); + hist_free(hist); + } /* NULL handlers will stop a message being added to server->history */ return; } } /* add it to server->history if there is no handler */ - hist_add(server->history, NULL, msg, params, Activity_status, timestamp, HIST_DFL); + hist_add(server->history, NULL, msg, Activity_status, timestamp, HIST_DFL); + param_free(params); } diff --git a/src/hirc.h b/src/hirc.h @@ -43,6 +43,7 @@ void cleanup(char *quitmsg); void param_free(char **params); int param_len(char **params); char ** param_create(char *msg); +char ** param_dup(char **p); int read_line(int fd, char *buf, size_t buf_len); int ircgets(struct Server *server, char *buf, size_t buf_len); int ircprintf(struct Server *server, char *format, ...); @@ -81,9 +82,11 @@ void nick_sort(struct Nick **head, struct Server *server); void hist_free(struct History *history); void hist_free_list(struct HistInfo *histinfo); struct History *hist_create(struct HistInfo *histinfo, struct Nick *from, char *msg, - char **params, enum Activity activity, time_t timestamp, enum HistOpt options); + enum Activity activity, time_t timestamp, enum HistOpt options); +struct History *hist_addp(struct HistInfo *histinfo, struct History *p, + enum Activity activity, enum HistOpt options); struct History *hist_add(struct HistInfo *histinfo, - struct Nick *from, char *msg, char **params, enum Activity activity, + struct Nick *from, char *msg, enum Activity activity, time_t timestamp, enum HistOpt options); struct History *hist_format(struct HistInfo *history, enum Activity activity, enum HistOpt options, char *format, ...); diff --git a/src/hist.c b/src/hist.c @@ -30,7 +30,7 @@ void hist_free(struct History *history) { - param_free(history->params); + param_free(history->_params); if (history->from) { free(history->from->prefix); free(history->from); @@ -53,8 +53,7 @@ hist_free_list(struct HistInfo *histinfo) { struct History * hist_create(struct HistInfo *histinfo, struct Nick *from, char *msg, - char **params, enum Activity activity, - time_t timestamp, enum HistOpt options) { + enum Activity activity, time_t timestamp, enum HistOpt options) { struct History *new; new = emalloc(sizeof(struct History)); @@ -62,35 +61,42 @@ hist_create(struct HistInfo *histinfo, struct Nick *from, char *msg, new->timestamp = timestamp ? timestamp : time(NULL); new->activity = activity; new->raw = estrdup(msg); - new->params = params; + new->_params = new->params = param_create(msg); new->options = options; new->origin = histinfo; - if (from) { + if (from) new->from = nick_dup(from, histinfo->server); - } else if (**params == ':') { - new->from = nick_create(*params, ' ', histinfo->server); - } else { + else if (**new->_params == ':') + new->from = nick_create(*new->_params, ' ', histinfo->server); + else new->from = NULL; - } + + if (**new->_params == ':') + new->params++; return new; } struct History * +hist_addp(struct HistInfo *histinfo, struct History *p, enum Activity activity, enum HistOpt options) { + return hist_add(histinfo, p->from, p->raw, activity, p->timestamp, options); +} + +struct History * hist_add(struct HistInfo *histinfo, struct Nick *from, - char *msg, char **params, enum Activity activity, + char *msg, enum Activity activity, time_t timestamp, enum HistOpt options) { struct History *new, *p; int i; if (options & HIST_MAIN) { if (options & HIST_TMP && histinfo == main_buf) { - hist_add(main_buf, from, msg, params, activity, timestamp, HIST_SHOW); + hist_add(main_buf, from, msg, activity, timestamp, HIST_SHOW); new = NULL; goto ui; } else if (histinfo != main_buf) { - hist_add(main_buf, from, msg, params, activity, timestamp, HIST_SHOW); + hist_add(main_buf, from, msg, activity, timestamp, HIST_SHOW); } else { ui_error("HIST_MAIN specified, but history is &main_buf", NULL); } @@ -99,7 +105,7 @@ hist_add(struct HistInfo *histinfo, struct Nick *from, if (options & HIST_SELF && histinfo->server) from = histinfo->server->self; - new = hist_create(histinfo, from, msg, params, activity, timestamp, options); + new = hist_create(histinfo, from, msg, activity, timestamp, options); if (histinfo && options & HIST_SHOW && activity > histinfo->activity && histinfo != selected.history) { histinfo->activity = activity; @@ -181,7 +187,7 @@ hist_format(struct HistInfo *histinfo, enum Activity activity, enum HistOpt opti params = param_create(msg); - return hist_add(histinfo, NULL, msg, params, Activity_status, 0, options); + return hist_add(histinfo, NULL, msg, Activity_status, 0, options); } int diff --git a/src/struct.h b/src/struct.h @@ -65,7 +65,8 @@ struct History { enum Activity activity; enum HistOpt options; char *raw; - char **params; + char **_params; /* contains all params, free from here */ + char **params; /* contains params without perfix, don't free */ struct HistInfo *origin; struct Nick *from; struct History *next; @@ -164,7 +165,7 @@ struct Server { /* messages received from server */ struct Handler { char *cmd; /* or numeric */ - void (*func)(char *msg, char **params, struct Server *server, time_t timestamp); + void (*func)(struct Server *server, struct History *msg); }; /* commands received from user */ diff --git a/src/ui.c b/src/ui.c @@ -1077,15 +1077,9 @@ static char * ui_get_pseudocmd(struct History *hist) { char *cmd, *p1, *p2; - if (**(hist->params) == ':') { - cmd = *(hist->params+1); - p1 = *(hist->params+2); - p2 = *(hist->params+3); - } else { - cmd = *(hist->params); - p1 = *(hist->params+1); - p2 = *(hist->params+2); - } + cmd = *(hist->params); + p1 = *(hist->params+1); + p2 = *(hist->params+2); if (strcmp_n(cmd, "MODE") == 0) { if (p1 && serv_ischannel(hist->origin->server, p1)) @@ -1118,7 +1112,7 @@ ui_hist_print(struct Window *window, int lines, struct History *hist) { if (!hist) return -1; - if (!hist->params || !*(hist->params+1)) + if (!hist->params) goto raw; cmd = ui_get_pseudocmd(hist); @@ -1142,7 +1136,7 @@ ui_hist_len(struct Window *window, struct History *hist, int *lines) { if (!hist) return -1; - if (!hist->params || !*(hist->params+2)) + if (!hist->params) goto raw; cmd = ui_get_pseudocmd(hist); @@ -1309,9 +1303,7 @@ ui_format(struct Window *window, char *format, struct History *hist) { } } - if (**(params = hist->params) == ':') - params++; - + params = hist->params; subs[sub_cmd].val = *params; params++; }