hirc

IRC client
Log | Files | Refs

commit d4371114943c8d4db44c8e9a79826beeb2b316f9
parent a611ee4557078dada54f3bdb77ccf431c92a5b8e
Author: hhvn <dev@hhvn.uk>
Date:   Thu, 21 Apr 2022 20:53:49 +0100

Move ircread/ircprintf to serv.c

Diffstat:
Msrc/commands.c | 74+++++++++++++++++++++++++++++++++++++-------------------------------------
Msrc/handle.c | 10+++++-----
Msrc/hirc.h | 4++--
Msrc/main.c | 114++-----------------------------------------------------------------------------
Msrc/serv.c | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
5 files changed, 160 insertions(+), 159 deletions(-)

diff --git a/src/commands.c b/src/commands.c @@ -87,9 +87,9 @@ command_away) { if (all) { for (sp = servers; sp; sp = sp->next) - ircprintf(sp, format, str); + serv_write(sp, format, str); } else if (server) { - ircprintf(server, format, str); + serv_write(server, format, str); } else { ui_error("-one specified, but no server selected", NULL); } @@ -112,7 +112,7 @@ command_msg) { else chan = chan_get(&server->privs, target, -1); - ircprintf(server, "PRIVMSG %s :%s\r\n", target, message); + serv_write(server, "PRIVMSG %s :%s\r\n", target, message); if (chan) { hist_format(chan->history, Activity_self, HIST_SHOW|HIST_LOG|HIST_SELF, "PRIVMSG %s :%s", target, message); @@ -136,7 +136,7 @@ command_notice) { else chan = chan_get(&server->privs, target, -1); - ircprintf(server, "NOTICE %s :%s\r\n", target, message); + serv_write(server, "NOTICE %s :%s\r\n", target, message); if (chan) { hist_format(chan->history, Activity_self, HIST_SHOW|HIST_LOG|HIST_SELF, "NOTICE %s :%s", target, message); @@ -148,7 +148,7 @@ command_me) { if (!str) str = ""; - ircprintf(server, "PRIVMSG %s :%cACTION %s%c\r\n", channel->name, 1, str, 1); + serv_write(server, "PRIVMSG %s :%cACTION %s%c\r\n", channel->name, 1, str, 1); hist_format(channel->history, Activity_self, HIST_SHOW|HIST_LOG|HIST_SELF, "PRIVMSG %s :%cACTION %s%c", channel->name, 1, str, 1); } @@ -176,7 +176,7 @@ command_ctcp) { /* XXX: if we CTCP a channel, responses should go to that channel. * This requires more than just expect_set, so might never be * implemented. */ - ircprintf(server, "PRIVMSG %s :%c%s%c\r\n", target, 1, ctcp, 1); + serv_write(server, "PRIVMSG %s :%c%s%c\r\n", target, 1, ctcp, 1); if (chan) { hist_format(channel->history, Activity_self, HIST_SHOW|HIST_LOG|HIST_SELF, "PRIVMSG %s :%c%s%c", @@ -231,7 +231,7 @@ command_join) { snprintf(msg, sizeof(msg), "JOIN %c%s\r\n", '#', str); if (server->status == ConnStatus_connected) - ircprintf(server, "%s", msg); + serv_write(server, "%s", msg); else schedule_push(server, "376" /* RPL_ENDOFMOTD */, msg); @@ -267,7 +267,7 @@ command_part) { snprintf(msg, sizeof(msg), "PART %s :%s\r\n", chan, reason ? reason : config_gets("def.partmessage")); - ircprintf(server, "%s", msg); + serv_write(server, "%s", msg); expect_set(server, Expect_part, chan); } @@ -314,9 +314,9 @@ command_kick) { } if (reason) - ircprintf(server, "KICK %s %s :%s\r\n", chan, nick, reason); + serv_write(server, "KICK %s %s :%s\r\n", chan, nick, reason); else - ircprintf(server, "KICK %s %s\r\n", chan, nick); + serv_write(server, "KICK %s %s\r\n", chan, nick); } COMMAND( @@ -345,10 +345,10 @@ command_mode) { if (modes) { if (chan == channel->name) expect_set(server, Expect_nosuchnick, chan); - ircprintf(server, "MODE %s %s\r\n", chan, modes); + serv_write(server, "MODE %s %s\r\n", chan, modes); } else { expect_set(server, Expect_channelmodeis, chan); - ircprintf(server, "MODE %s\r\n", chan); + serv_write(server, "MODE %s\r\n", chan); } } @@ -364,7 +364,7 @@ command_nick) { return; } - ircprintf(server, "NICK %s\r\n", str); + serv_write(server, "NICK %s\r\n", str); expect_set(server, Expect_nicknameinuse, str); } @@ -375,7 +375,7 @@ command_list) { return; } - ircprintf(server, "LIST\r\n", str); + serv_write(server, "LIST\r\n", str); } COMMAND( @@ -394,9 +394,9 @@ command_whois) { } if (tserver) - ircprintf(server, "WHOIS %s :%s\r\n", tserver, nick); + serv_write(server, "WHOIS %s :%s\r\n", tserver, nick); else - ircprintf(server, "WHOIS %s\r\n", nick); + serv_write(server, "WHOIS %s\r\n", nick); } COMMAND( @@ -404,7 +404,7 @@ command_who) { if (!str) str = "*"; /* wildcard */ - ircprintf(server, "WHO %s\r\n", str); + serv_write(server, "WHO %s\r\n", str); } COMMAND( @@ -420,11 +420,11 @@ command_whowas) { } if (tserver) - ircprintf(server, "WHOWAS %s %s :%s\r\n", nick, count, tserver); + serv_write(server, "WHOWAS %s %s :%s\r\n", nick, count, tserver); else if (count) - ircprintf(server, "WHOWAS %s %s\r\n", nick, count); + serv_write(server, "WHOWAS %s %s\r\n", nick, count); else - ircprintf(server, "WHOWAS %s 5\r\n", nick); + serv_write(server, "WHOWAS %s 5\r\n", nick); } COMMAND( @@ -434,7 +434,7 @@ command_ping) { return; } - ircprintf(server, "PING :%s\r\n", str); + serv_write(server, "PING :%s\r\n", str); expect_set(server, Expect_pong, str); } @@ -448,7 +448,7 @@ command_quote) { } if (server->status == ConnStatus_connected) { - ircprintf(server, "%s\r\n", str); + serv_write(server, "%s\r\n", str); } else { snprintf(msg, sizeof(msg), "%s\r\n", str); schedule_push(server, "376" /* RPL_ENDOFMOTD */, msg); @@ -820,7 +820,7 @@ command_names) { return; } - ircprintf(server, "NAMES %s\r\n", chan); + serv_write(server, "NAMES %s\r\n", chan); expect_set(server, Expect_names, chan); } @@ -866,14 +866,14 @@ command_topic) { command_toomany("topic"); return; } - ircprintf(server, "TOPIC %s :\r\n", chan); + serv_write(server, "TOPIC %s :\r\n", chan); return; } if (!topic) { - ircprintf(server, "TOPIC %s\r\n", chan); + serv_write(server, "TOPIC %s\r\n", chan); expect_set(server, Expect_topic, chan); - } else ircprintf(server, "TOPIC %s :%s\r\n", chan, topic); + } else serv_write(server, "TOPIC %s :%s\r\n", chan, topic); } COMMAND( @@ -895,7 +895,7 @@ command_oper) { user = server->self->nick; } - ircprintf(server, "OPER %s %s\r\n", user, pass); + serv_write(server, "OPER %s %s\r\n", user, pass); } static void @@ -903,7 +903,7 @@ command_send0(struct Server *server, char *cmd, char *cmdname, char *str) { if (str) command_toomany(cmdname); else - ircprintf(server, "%s\r\n", cmd); + serv_write(server, "%s\r\n", cmd); } COMMAND( @@ -921,9 +921,9 @@ command_send1(struct Server *server, char *cmd, char *cmdname, char *str) { if (str && strchr(str, ' ')) command_toomany(cmdname); else if (str) - ircprintf(server, "%s %s\r\n", cmd, str); + serv_write(server, "%s %s\r\n", cmd, str); else - ircprintf(server, "%s\r\n", cmd); + serv_write(server, "%s\r\n", cmd); } COMMAND( @@ -941,9 +941,9 @@ command_send2(struct Server *server, char *cmd, char *cmdname, char *str) { if (str && strchr(str, ' ') != strrchr(str, ' ')) command_toomany(cmdname); else if (str) - ircprintf(server, "%s %s\r\n", cmd, str); + serv_write(server, "%s %s\r\n", cmd, str); else - ircprintf(server, "%s\r\n", cmd); + serv_write(server, "%s\r\n", cmd); } COMMAND( @@ -968,7 +968,7 @@ command_kill) { nick = strtok_r(str, " ", &reason); if (!reason) reason = config_gets("def.killmessage"); - ircprintf(server, "KILL %s :%s\r\n", nick, reason); + serv_write(server, "KILL %s :%s\r\n", nick, reason); } COMMAND( @@ -1537,7 +1537,7 @@ command_close) { if (chp) { if (serv_ischannel(sp, chp->name) && !chp->old) { - ircprintf(sp, "PART %s\r\n", chp->name); + serv_write(sp, "PART %s\r\n", chp->name); chan_remove(&sp->channels, chp->name); } else { chan_remove(&sp->privs, chp->name); @@ -1744,7 +1744,7 @@ modelset(char *cmd, struct Server *server, struct Channel *channel, i++; *(modes + i + 1) = '\0'; - ircprintf(server, "MODE %s %s %s\r\n", channel->name, modes, args); + serv_write(server, "MODE %s %s %s\r\n", channel->name, modes, args); args = p; } @@ -1838,7 +1838,7 @@ command_invite) { if (!chan) chan = channel->name; - ircprintf(server, "INVITE %s %s\r\n", nick, chan); + serv_write(server, "INVITE %s %s\r\n", nick, chan); } int @@ -1992,7 +1992,7 @@ command_eval(struct Server *server, char *str) { if (selected.channel && selected.server) { // TODO: message splitting snprintf(msg, sizeof(msg), "PRIVMSG %s :%s", selected.channel->name, s); - ircprintf(selected.server, "%s\r\n", msg); + serv_write(selected.server, "%s\r\n", msg); hist_format(selected.channel->history, Activity_self, HIST_SHOW|HIST_LOG|HIST_SELF, "%s", msg); } else { ui_error("channel not selected, message ignored", NULL); diff --git a/src/handle.c b/src/handle.c @@ -29,7 +29,7 @@ handle_PING) { if (param_len(msg->params) < 2) return; - ircprintf(server, "PONG :%s\r\n", *(msg->params+1)); + serv_write(server, "PONG :%s\r\n", *(msg->params+1)); } HANDLER( @@ -206,9 +206,9 @@ handle_MODE) { expect_set(server, Expect_nosuchnick, NULL); 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 + serv_write(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 */ + serv_write(server, "NAMES %s\r\n", chan->name); /* Also get updated priviledges */ } else { hist_addp(server->history, msg, Activity_status, HIST_DFL); } @@ -439,7 +439,7 @@ handle_ERR_NICKNAMEINUSE) { nick_free(server->self); server->self = nnick; server->self->self = 1; - ircprintf(server, "NICK %s\r\n", nick); + serv_write(server, "NICK %s\r\n", nick); } else { expect_set(server, Expect_nicknameinuse, NULL); } @@ -639,7 +639,7 @@ handle(struct Server *server, char *msg) { /* Fire off any scheduled events for the current cmd */ while ((schmsg = schedule_pull(server, cmd)) != NULL) - ircprintf(server, "%s", schmsg); + serv_write(server, "%s", schmsg); for (i=0; cmd && handlers[i].cmd; i++) { if (strcmp(handlers[i].cmd, cmd) == 0) { diff --git a/src/hirc.h b/src/hirc.h @@ -42,8 +42,6 @@ size_t wcslcpy(wchar_t *, const wchar_t *, size_t); /* main.c */ void cleanup(char *quitmsg); -int ircgets(struct Server *server, char *buf, size_t buf_len); -int ircprintf(struct Server *server, char *format, ...); /* params.c */ void param_free(char **params); @@ -116,6 +114,8 @@ void hist_purgeopt(struct HistInfo *histinfo, enum HistOpt options); /* serv.c */ void serv_free(struct Server *server); void serv_connect(struct Server *server); +void serv_read(struct Server *sp); +int serv_write(struct Server *server, char *format, ...); struct Server * serv_create(char *name, char *host, char *port, char *nick, char *username, char *realname, int tls, int tls_verify); void serv_update(struct Server *sp, char *nick, char *username, diff --git a/src/main.c b/src/main.c @@ -49,116 +49,6 @@ cleanup(char *quitmsg) { ui_deinit(); } -void -ircread(struct Server *sp) { - char *line, *end; - char *err; - char *reason = NULL; - size_t len; - int ret; - - if (!sp) - return; - -#ifdef TLS - if (sp->tls) { - switch (ret = tls_read(sp->tls_ctx, &sp->inputbuf[sp->inputlen], SERVER_INPUT_SIZE - sp->inputlen - 1)) { - case -1: - err = (char *)tls_error(sp->tls_ctx); - len = CONSTLEN("tls_read(): ") + strlen(err) + 1; - reason = emalloc(len); - snprintf(reason, len, "tls_read(): %s", err); - /* fallthrough */ - case 0: - serv_disconnect(sp, 1, "EOF"); - hist_format(sp->history, Activity_error, HIST_SHOW, - "SELF_CONNECTLOST %s %s %s :%s", - sp->name, sp->host, sp->port, reason ? reason : "connection close"); - pfree(&reason); - return; - case TLS_WANT_POLLIN: - case TLS_WANT_POLLOUT: - return; - default: - sp->inputlen += ret; - break; - } - } else { -#endif /* TLS */ - switch (ret = read(sp->rfd, &sp->inputbuf[sp->inputlen], SERVER_INPUT_SIZE - sp->inputlen - 1)) { - case -1: - err = estrdup(strerror(errno)); - len = CONSTLEN("read(): ") + strlen(err) + 1; - reason = emalloc(len); - snprintf(reason, len, "read(): %s", err); - pfree(&err); - /* fallthrough */ - case 0: - serv_disconnect(sp, 1, "EOF"); - hist_format(sp->history, Activity_error, HIST_SHOW, - "SELF_CONNECTLOST %s %s %s :%s", - sp->name, sp->host, sp->port, reason ? reason : "connection closed"); - pfree(&reason); - return; - default: - sp->inputlen += ret; - break; - } -#ifdef TLS - } -#endif /* TLS */ - - sp->inputbuf[SERVER_INPUT_SIZE - 1] = '\0'; - line = sp->inputbuf; - while (end = strstr(line, "\r\n")) { - *end = '\0'; - handle(sp, line); - line = end + 2; - } - - sp->inputlen -= line - sp->inputbuf; - memmove(sp->inputbuf, line, sp->inputlen); -} - -int -ircprintf(struct Server *server, char *format, ...) { - char msg[512]; - va_list ap; - int ret, serrno; - - if (!server || server->status == ConnStatus_notconnected) { - ui_error("Not connected to server '%s'", server ? server->name : ""); - return -1; - } - - va_start(ap, format); - if (vsnprintf(msg, sizeof(msg), format, ap) < 0) { - va_end(ap); - return -1; - } - -#ifdef TLS - if (server->tls) - do { - ret = tls_write(server->tls_ctx, msg, strlen(msg)); - } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT); - else -#endif /* TLS */ - ret = write(server->wfd, msg, strlen(msg)); - - if (ret == -1 && server->status == ConnStatus_connected) { - serv_disconnect(server, 1, NULL); - hist_format(server->history, Activity_error, HIST_SHOW, - "SELF_CONNECTLOST %s %s %s :%s", - server->name, server->host, server->port, strerror(errno)); - } else if (ret == -1 && server->status != ConnStatus_connecting) { - ui_error("Not connected to server '%s'", server->name); - } - - va_end(ap); - return ret; -} - int main(int argc, char *argv[]) { struct Selected oldselected; @@ -226,10 +116,10 @@ main(int argc, char *argv[]) { sp->pingsent = 0; sp->lastrecv = time(NULL); sp->rpollfd->revents = 0; - ircread(sp); + serv_read(sp); } else if (!sp->pingsent && sp->lastrecv && (time(NULL) - sp->lastrecv) >= pinginact) { /* haven't heard from server in pinginact seconds, sending a ping */ - ircprintf(sp, "PING :ground control to Major Tom\r\n"); + serv_write(sp, "PING :ground control to Major Tom\r\n"); sp->pingsent = time(NULL); } else if (sp->pingsent && (time(NULL) - sp->pingsent) >= pinginact) { /* haven't gotten a response in pinginact seconds since diff --git a/src/serv.c b/src/serv.c @@ -321,8 +321,8 @@ serv_connect(struct Server *server) { freeaddrinfo(ai); server->connectfail = 0; - ircprintf(server, "NICK %s\r\n", server->self->nick); - ircprintf(server, "USER %s * * :%s\r\n", + serv_write(server, "NICK %s\r\n", server->self->nick); + serv_write(server, "USER %s * * :%s\r\n", server->username ? server->username : server->self->nick, server->realname ? server->realname : server->self->nick); @@ -336,6 +336,117 @@ fail: freeaddrinfo(ai); } +void +serv_read(struct Server *sp) { + char *line, *end; + char *err; + char *reason = NULL; + size_t len; + int ret; + + if (!sp) + return; + +#ifdef TLS + if (sp->tls) { + switch (ret = tls_read(sp->tls_ctx, &sp->inputbuf[sp->inputlen], SERVER_INPUT_SIZE - sp->inputlen - 1)) { + case -1: + err = (char *)tls_error(sp->tls_ctx); + len = CONSTLEN("tls_read(): ") + strlen(err) + 1; + reason = emalloc(len); + snprintf(reason, len, "tls_read(): %s", err); + /* fallthrough */ + case 0: + serv_disconnect(sp, 1, "EOF"); + hist_format(sp->history, Activity_error, HIST_SHOW, + "SELF_CONNECTLOST %s %s %s :%s", + sp->name, sp->host, sp->port, reason ? reason : "connection close"); + pfree(&reason); + return; + case TLS_WANT_POLLIN: + case TLS_WANT_POLLOUT: + return; + default: + sp->inputlen += ret; + break; + } + } else { +#endif /* TLS */ + switch (ret = read(sp->rfd, &sp->inputbuf[sp->inputlen], SERVER_INPUT_SIZE - sp->inputlen - 1)) { + case -1: + err = estrdup(strerror(errno)); + len = CONSTLEN("read(): ") + strlen(err) + 1; + reason = emalloc(len); + snprintf(reason, len, "read(): %s", err); + pfree(&err); + /* fallthrough */ + case 0: + serv_disconnect(sp, 1, "EOF"); + hist_format(sp->history, Activity_error, HIST_SHOW, + "SELF_CONNECTLOST %s %s %s :%s", + sp->name, sp->host, sp->port, reason ? reason : "connection closed"); + pfree(&reason); + return; + default: + sp->inputlen += ret; + break; + } +#ifdef TLS + } +#endif /* TLS */ + + sp->inputbuf[SERVER_INPUT_SIZE - 1] = '\0'; + line = sp->inputbuf; + while (end = strstr(line, "\r\n")) { + *end = '\0'; + handle(sp, line); + line = end + 2; + } + + sp->inputlen -= line - sp->inputbuf; + memmove(sp->inputbuf, line, sp->inputlen); +} + +int +serv_write(struct Server *server, char *format, ...) { + char msg[512]; + va_list ap; + int ret, serrno; + + if (!server || server->status == ConnStatus_notconnected) { + ui_error("Not connected to server '%s'", server ? server->name : ""); + return -1; + } + + va_start(ap, format); + if (vsnprintf(msg, sizeof(msg), format, ap) < 0) { + va_end(ap); + return -1; + } + +#ifdef TLS + if (server->tls) + do { + ret = tls_write(server->tls_ctx, msg, strlen(msg)); + } while (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT); + else +#endif /* TLS */ + ret = write(server->wfd, msg, strlen(msg)); + + if (ret == -1 && server->status == ConnStatus_connected) { + serv_disconnect(server, 1, NULL); + hist_format(server->history, Activity_error, HIST_SHOW, + "SELF_CONNECTLOST %s %s %s :%s", + server->name, server->host, server->port, strerror(errno)); + } else if (ret == -1 && server->status != ConnStatus_connecting) { + ui_error("Not connected to server '%s'", server->name); + } + + va_end(ap); + return ret; +} + + int serv_len(struct Server **head) { struct Server *p; @@ -377,7 +488,7 @@ serv_disconnect(struct Server *server, int reconnect, char *msg) { int ret; if (msg) - ircprintf(server, "QUIT :%s\r\n", msg); + serv_write(server, "QUIT :%s\r\n", msg); #ifdef TLS if (server->tls) { if (server->tls_ctx) {