ii-1.8-ucspi.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       ii-1.8-ucspi.diff (9092B)
       ---
            1 diff -up ii-1.8/ii.c ii-1.8-mod/ii.c
            2 --- ii-1.8/ii.c        Sun Feb  4 14:36:09 2018
            3 +++ ii-1.8-mod/ii.c        Mon Feb 26 22:24:36 2018
            4 @@ -1,16 +1,13 @@
            5  /* See LICENSE file for license details. */
            6  #include <sys/select.h>
            7 -#include <sys/socket.h>
            8  #include <sys/stat.h>
            9  #include <sys/types.h>
           10 -#include <sys/un.h>
           11  
           12  #include <ctype.h>
           13  #include <errno.h>
           14  #include <fcntl.h>
           15  #include <limits.h>
           16  #include <netdb.h>
           17 -#include <netinet/in.h>
           18  #include <pwd.h>
           19  #include <signal.h>
           20  #include <stdarg.h>
           21 @@ -20,6 +17,9 @@
           22  #include <time.h>
           23  #include <unistd.h>
           24  
           25 +#define READ_FD 6
           26 +#define WRITE_FD 7
           27 +
           28  char *argv0;
           29  
           30  #include "arg.h"
           31 @@ -56,16 +56,16 @@ static void      channel_rm(Channel *);
           32  static void      create_dirtree(const char *);
           33  static void      create_filepath(char *, size_t, const char *, const char *, const char *);
           34  static void      ewritestr(int, const char *);
           35 -static void      handle_channels_input(int, Channel *);
           36 -static void      handle_server_output(int);
           37 +static void      handle_channels_input(Channel *);
           38 +static void      handle_server_output(void);
           39  static int       isnumeric(const char *);
           40 -static void      loginkey(int, const char *);
           41 -static void      loginuser(int, const char *, const char *);
           42 -static void      proc_channels_input(int, Channel *, char *);
           43 -static void      proc_channels_privmsg(int, Channel *, char *);
           44 -static void      proc_server_cmd(int, char *);
           45 +static void      loginkey(const char *);
           46 +static void      loginuser(const char *, const char *);
           47 +static void      proc_channels_input(Channel *, char *);
           48 +static void      proc_channels_privmsg(Channel *, char *);
           49 +static void      proc_server_cmd(char *);
           50  static int       read_line(int, char *, size_t);
           51 -static void      run(int, const char *);
           52 +static void      run(const char *);
           53  static void      setup(void);
           54  static void      sighandler(int);
           55  static int       tcpopen(const char *, const char *);
           56 @@ -319,84 +319,22 @@ channel_leave(Channel *c)
           57  }
           58  
           59  static void
           60 -loginkey(int ircfd, const char *key)
           61 +loginkey(const char *key)
           62  {
           63          snprintf(msg, sizeof(msg), "PASS %s\r\n", key);
           64 -        ewritestr(ircfd, msg);
           65 +        ewritestr(WRITE_FD, msg);
           66  }
           67  
           68  static void
           69 -loginuser(int ircfd, const char *host, const char *fullname)
           70 +loginuser(const char *host, const char *fullname)
           71  {
           72          snprintf(msg, sizeof(msg), "NICK %s\r\nUSER %s localhost %s :%s\r\n",
           73                   nick, nick, host, fullname);
           74          puts(msg);
           75 -        ewritestr(ircfd, msg);
           76 +        ewritestr(WRITE_FD, msg);
           77  }
           78  
           79  static int
           80 -udsopen(const char *uds)
           81 -{
           82 -        struct sockaddr_un sun;
           83 -        size_t len;
           84 -        int fd;
           85 -
           86 -        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
           87 -                fprintf(stderr, "%s: socket: %s\n", argv0, strerror(errno));
           88 -                exit(1);
           89 -        }
           90 -
           91 -        sun.sun_family = AF_UNIX;
           92 -        if (strlcpy(sun.sun_path, uds, sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
           93 -                fprintf(stderr, "%s: UNIX domain socket path truncation\n", argv0);
           94 -                exit(1);
           95 -        }
           96 -        len = strlen(sun.sun_path) + 1 + sizeof(sun.sun_family);
           97 -        if (connect(fd, (struct sockaddr *)&sun, len) == -1) {
           98 -                fprintf(stderr, "%s: connect: %s\n", argv0, strerror(errno));
           99 -                exit(1);
          100 -        }
          101 -        return fd;
          102 -}
          103 -
          104 -static int
          105 -tcpopen(const char *host, const char *service)
          106 -{
          107 -        struct addrinfo hints, *res = NULL, *rp;
          108 -        int fd = -1, e;
          109 -
          110 -        memset(&hints, 0, sizeof(hints));
          111 -        hints.ai_family = AF_UNSPEC; /* allow IPv4 or IPv6 */
          112 -        hints.ai_flags = AI_NUMERICSERV; /* avoid name lookup for port */
          113 -        hints.ai_socktype = SOCK_STREAM;
          114 -
          115 -        if ((e = getaddrinfo(host, service, &hints, &res))) {
          116 -                fprintf(stderr, "%s: getaddrinfo: %s\n", argv0, gai_strerror(e));
          117 -                exit(1);
          118 -        }
          119 -
          120 -        for (rp = res; rp; rp = rp->ai_next) {
          121 -                fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
          122 -                if (fd == -1)
          123 -                        continue;
          124 -                if (connect(fd, res->ai_addr, res->ai_addrlen) == -1) {
          125 -                        close(fd);
          126 -                        fd = -1;
          127 -                        continue;
          128 -                }
          129 -                break; /* success */
          130 -        }
          131 -        if (fd == -1) {
          132 -                fprintf(stderr, "%s: could not connect to %s:%s: %s\n",
          133 -                        argv0, host, service, strerror(errno));
          134 -                exit(1);
          135 -        }
          136 -
          137 -        freeaddrinfo(res);
          138 -        return fd;
          139 -}
          140 -
          141 -static int
          142  isnumeric(const char *s)
          143  {
          144          errno = 0;
          145 @@ -445,22 +383,22 @@ channel_print(Channel *c, const char *buf)
          146  }
          147  
          148  static void
          149 -proc_channels_privmsg(int ircfd, Channel *c, char *buf)
          150 +proc_channels_privmsg(Channel *c, char *buf)
          151  {
          152          snprintf(msg, sizeof(msg), "<%s> %s", nick, buf);
          153          channel_print(c, msg);
          154          snprintf(msg, sizeof(msg), "PRIVMSG %s :%s\r\n", c->name, buf);
          155 -        ewritestr(ircfd, msg);
          156 +        ewritestr(WRITE_FD, msg);
          157  }
          158  
          159  static void
          160 -proc_channels_input(int ircfd, Channel *c, char *buf)
          161 +proc_channels_input(Channel *c, char *buf)
          162  {
          163          char *p = NULL;
          164          size_t buflen;
          165  
          166          if (buf[0] != '/' && buf[0] != '\0') {
          167 -                proc_channels_privmsg(ircfd, c, buf);
          168 +                proc_channels_privmsg(c, buf);
          169                  return;
          170          }
          171          msg[0] = '\0';
          172 @@ -481,7 +419,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf)
          173                                  channel_join(&buf[3]);
          174                          } else if (p) {
          175                                  if ((c = channel_join(&buf[3])))
          176 -                                        proc_channels_privmsg(ircfd, c, p + 1);
          177 +                                        proc_channels_privmsg(c, p + 1);
          178                                  return;
          179                          }
          180                          break;
          181 @@ -513,7 +451,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf)
          182                          else
          183                                  snprintf(msg, sizeof(msg),
          184                                           "PART %s :leaving\r\n", c->name);
          185 -                        ewritestr(ircfd, msg);
          186 +                        ewritestr(WRITE_FD, msg);
          187                          channel_leave(c);
          188                          return;
          189                          break;
          190 @@ -523,7 +461,7 @@ proc_channels_input(int ircfd, Channel *c, char *buf)
          191                          else
          192                                  snprintf(msg, sizeof(msg),
          193                                           "QUIT %s\r\n", "bye");
          194 -                        ewritestr(ircfd, msg);
          195 +                        ewritestr(WRITE_FD, msg);
          196                          isrunning = 0;
          197                          return;
          198                          break;
          199 @@ -536,11 +474,11 @@ proc_channels_input(int ircfd, Channel *c, char *buf)
          200                  snprintf(msg, sizeof(msg), "%s\r\n", &buf[1]);
          201          }
          202          if (msg[0] != '\0')
          203 -                ewritestr(ircfd, msg);
          204 +                ewritestr(WRITE_FD, msg);
          205  }
          206  
          207  static void
          208 -proc_server_cmd(int fd, char *buf)
          209 +proc_server_cmd(char *buf)
          210  {
          211          Channel *c;
          212          const char *channel;
          213 @@ -588,7 +526,7 @@ proc_server_cmd(int fd, char *buf)
          214                  return;
          215          } else if (!strcmp("PING", argv[TOK_CMD])) {
          216                  snprintf(msg, sizeof(msg), "PONG %s\r\n", argv[TOK_TEXT]);
          217 -                ewritestr(fd, msg);
          218 +                ewritestr(WRITE_FD, msg);
          219                  return;
          220          } else if (!argv[TOK_NICKSRV] || !argv[TOK_USER]) {
          221                  /* server command */
          222 @@ -675,7 +613,7 @@ read_line(int fd, char *buf, size_t bufsiz)
          223  }
          224  
          225  static void
          226 -handle_channels_input(int ircfd, Channel *c)
          227 +handle_channels_input(Channel *c)
          228  {
          229          char buf[IRC_MSG_MAX];
          230  
          231 @@ -684,22 +622,22 @@ handle_channels_input(int ircfd, Channel *c)
          232                          channel_rm(c);
          233                  return;
          234          }
          235 -        proc_channels_input(ircfd, c, buf);
          236 +        proc_channels_input(c, buf);
          237  }
          238  
          239  static void
          240 -handle_server_output(int ircfd)
          241 +handle_server_output(void)
          242  {
          243          char buf[IRC_MSG_MAX];
          244  
          245 -        if (read_line(ircfd, buf, sizeof(buf)) == -1) {
          246 +        if (read_line(READ_FD, buf, sizeof(buf)) == -1) {
          247                  fprintf(stderr, "%s: remote host closed connection: %s\n",
          248                          argv0, strerror(errno));
          249                  exit(1);
          250          }
          251          fprintf(stdout, "%lu %s\n", (unsigned long)time(NULL), buf);
          252          fflush(stdout);
          253 -        proc_server_cmd(ircfd, buf);
          254 +        proc_server_cmd(buf);
          255  }
          256  
          257  static void
          258 @@ -721,7 +659,7 @@ setup(void)
          259  }
          260  
          261  static void
          262 -run(int ircfd, const char *host)
          263 +run(const char *host)
          264  {
          265          Channel *c, *tmp;
          266          fd_set rdset;
          267 @@ -731,9 +669,9 @@ run(int ircfd, const char *host)
          268  
          269          snprintf(ping_msg, sizeof(ping_msg), "PING %s\r\n", host);
          270          while (isrunning) {
          271 -                maxfd = ircfd;
          272 +                maxfd = READ_FD;
          273                  FD_ZERO(&rdset);
          274 -                FD_SET(ircfd, &rdset);
          275 +                FD_SET(READ_FD, &rdset);
          276                  for (c = channels; c; c = c->next) {
          277                          if (c->fdin > maxfd)
          278                                  maxfd = c->fdin;
          279 @@ -752,17 +690,17 @@ run(int ircfd, const char *host)
          280                                  channel_print(channelmaster, "-!- ii shutting down: ping timeout");
          281                                  exit(2); /* status code 2 for timeout */
          282                          }
          283 -                        ewritestr(ircfd, ping_msg);
          284 +                        ewritestr(WRITE_FD, ping_msg);
          285                          continue;
          286                  }
          287 -                if (FD_ISSET(ircfd, &rdset)) {
          288 -                        handle_server_output(ircfd);
          289 +                if (FD_ISSET(READ_FD, &rdset)) {
          290 +                        handle_server_output();
          291                          last_response = time(NULL);
          292                  }
          293                  for (c = channels; c; c = tmp) {
          294                          tmp = c->next;
          295                          if (FD_ISSET(c->fdin, &rdset))
          296 -                                handle_channels_input(ircfd, c);
          297 +                                handle_channels_input(c);
          298                  }
          299          }
          300  }
          301 @@ -775,7 +713,7 @@ main(int argc, char *argv[])
          302          const char *key = NULL, *fullname = NULL, *host = "";
          303          const char *uds = NULL, *service = "6667";
          304          char prefix[PATH_MAX];
          305 -        int ircfd, r;
          306 +        int r;
          307  
          308          /* use nickname and home dir of user by default */
          309          if (!(spw = getpwuid(getuid()))) {
          310 @@ -815,11 +753,6 @@ main(int argc, char *argv[])
          311          if (!*host)
          312                  usage();
          313  
          314 -        if (uds)
          315 -                ircfd = udsopen(uds);
          316 -        else
          317 -                ircfd = tcpopen(host, service);
          318 -
          319  #ifdef __OpenBSD__
          320          /* OpenBSD pledge(2) support */
          321          if (pledge("stdio rpath wpath cpath dpath", NULL) == -1) {
          322 @@ -837,10 +770,10 @@ main(int argc, char *argv[])
          323  
          324          channelmaster = channel_add(""); /* master channel */
          325          if (key)
          326 -                loginkey(ircfd, key);
          327 -        loginuser(ircfd, host, fullname && *fullname ? fullname : nick);
          328 +                loginkey(key);
          329 +        loginuser(host, fullname && *fullname ? fullname : nick);
          330          setup();
          331 -        run(ircfd, host);
          332 +        run(host);
          333          if (channelmaster)
          334                  channel_leave(channelmaster);
          335