this is my patched version of ii - the suckless irc client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

494 lines
14 KiB

9 months ago
  1. /* (C)opyright MMV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
  2. * (C)opyright MMV-MMXI Nico Golde <nico at ngolde dot de>
  3. * See LICENSE file for license details. */
  4. #include <errno.h>
  5. #include <netdb.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/socket.h>
  9. #include <sys/select.h>
  10. #include <netinet/in.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <limits.h>
  14. #include <fcntl.h>
  15. #include <string.h>
  16. #include <pwd.h>
  17. #include <signal.h>
  18. #include <ctype.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #ifndef PIPE_BUF /* FreeBSD don't know PIPE_BUF */
  22. #define PIPE_BUF 4096
  23. #endif
  24. #define PING_TIMEOUT 300
  25. #define SERVER_PORT 6667
  26. enum { TOK_NICKSRV = 0, TOK_USER, TOK_CMD, TOK_CHAN, TOK_ARG, TOK_TEXT, TOK_LAST };
  27. typedef struct Channel Channel;
  28. struct Channel {
  29. int fd;
  30. char *name;
  31. Channel *next;
  32. };
  33. static int irc;
  34. static time_t last_response;
  35. static Channel *channels = NULL;
  36. static char *host = "irc.freenode.net";
  37. static char nick[32]; /* might change while running */
  38. static char path[_POSIX_PATH_MAX];
  39. static char message[PIPE_BUF]; /* message buf used for communication */
  40. static void usage() {
  41. fputs("ii - irc it - " VERSION "\n"
  42. "(C)opyright MMV-MMVI Anselm R. Garbe\n"
  43. "(C)opyright MMV-MMXI Nico Golde\n"
  44. "usage: ii [-i <irc dir>] [-s <host>] [-p <port>]\n"
  45. " [-n <nick>] [-k <password>] [-f <fullname>]\n", stderr);
  46. exit(EXIT_FAILURE);
  47. }
  48. static char *striplower(char *s) {
  49. char *p = NULL;
  50. for(p = s; p && *p; p++) {
  51. if(*p == '/') *p = ',';
  52. *p = tolower(*p);
  53. }
  54. return s;
  55. }
  56. /* creates directories top-down, if necessary */
  57. static void create_dirtree(const char *dir) {
  58. char tmp[256];
  59. char *p = NULL;
  60. size_t len;
  61. snprintf(tmp, sizeof(tmp),"%s",dir);
  62. len = strlen(tmp);
  63. if(tmp[len - 1] == '/')
  64. tmp[len - 1] = 0;
  65. for(p = tmp + 1; *p; p++)
  66. if(*p == '/') {
  67. *p = 0;
  68. mkdir(tmp, S_IRWXU);
  69. *p = '/';
  70. }
  71. mkdir(tmp, S_IRWXU);
  72. }
  73. static int get_filepath(char *filepath, size_t len, char *channel, char *file) {
  74. if(channel) {
  75. if(!snprintf(filepath, len, "%s/%s", path, channel))
  76. return 0;
  77. create_dirtree(filepath);
  78. return snprintf(filepath, len, "%s/%s/%s", path, channel, file);
  79. }
  80. return snprintf(filepath, len, "%s/%s", path, file);
  81. }
  82. static void create_filepath(char *filepath, size_t len, char *channel, char *suffix) {
  83. if(!get_filepath(filepath, len, striplower(channel), suffix)) {
  84. fputs("ii: path to irc directory too long\n", stderr);
  85. exit(EXIT_FAILURE);
  86. }
  87. }
  88. static int open_channel(char *name) {
  89. static char infile[256];
  90. create_filepath(infile, sizeof(infile), name, "in");
  91. if(access(infile, F_OK) == -1)
  92. mkfifo(infile, S_IRWXU);
  93. return open(infile, O_RDONLY | O_NONBLOCK, 0);
  94. }
  95. static void add_channel(char *cname) {
  96. Channel *c;
  97. int fd;
  98. char *name = striplower(cname);
  99. for(c = channels; c; c = c->next)
  100. if(!strcmp(name, c->name))
  101. return; /* already handled */
  102. fd = open_channel(name);
  103. if(fd == -1) {
  104. printf("ii: exiting, cannot create in channel: %s\n", name);
  105. exit(EXIT_FAILURE);
  106. }
  107. c = calloc(1, sizeof(Channel));
  108. if(!c) {
  109. perror("ii: cannot allocate memory");
  110. exit(EXIT_FAILURE);
  111. }
  112. if(!channels) channels = c;
  113. else {
  114. c->next = channels;
  115. channels = c;
  116. }
  117. c->fd = fd;
  118. c->name = strdup(name);
  119. }
  120. static void rm_channel(Channel *c) {
  121. Channel *p;
  122. if(channels == c) channels = channels->next;
  123. else {
  124. for(p = channels; p && p->next != c; p = p->next);
  125. if(p->next == c)
  126. p->next = c->next;
  127. }
  128. free(c->name);
  129. free(c);
  130. }
  131. static void login(char *key, char *fullname) {
  132. if(key) snprintf(message, PIPE_BUF,
  133. "PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n", key,
  134. nick, nick, host, fullname ? fullname : nick);
  135. else snprintf(message, PIPE_BUF, "NICK %s\r\nUSER %s localhost %s :%s\r\n",
  136. nick, nick, host, fullname ? fullname : nick);
  137. write(irc, message, strlen(message)); /* login */
  138. }
  139. static int tcpopen(unsigned short port) {
  140. int fd;
  141. struct sockaddr_in sin;
  142. struct hostent *hp = gethostbyname(host);
  143. memset(&sin, 0, sizeof(struct sockaddr_in));
  144. if(!hp) {
  145. perror("ii: cannot retrieve host information");
  146. exit(EXIT_FAILURE);
  147. }
  148. sin.sin_family = AF_INET;
  149. memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
  150. sin.sin_port = htons(port);
  151. if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  152. perror("ii: cannot create socket");
  153. exit(EXIT_FAILURE);
  154. }
  155. if(connect(fd, (const struct sockaddr *) &sin, sizeof(sin)) < 0) {
  156. perror("ii: cannot connect to host");
  157. exit(EXIT_FAILURE);
  158. }
  159. return fd;
  160. }
  161. static size_t tokenize(char **result, size_t reslen, char *str, char delim) {
  162. char *p = NULL, *n = NULL;
  163. size_t i;
  164. if(!str)
  165. return 0;
  166. for(n = str; *n == ' '; n++);
  167. p = n;
  168. for(i = 0; *n != 0;) {
  169. if(i == reslen)
  170. return 0;
  171. if(i > TOK_CHAN - TOK_CMD && strtol(result[0], NULL, 10) > 0) delim=':'; /* workaround non-RFC compliant messages */
  172. if(*n == delim) {
  173. *n = 0;
  174. result[i++] = p;
  175. p = ++n;
  176. } else
  177. n++;
  178. }
  179. if(i<reslen && p < n && strlen(p))
  180. result[i++] = p;
  181. return i; /* number of tokens */
  182. }
  183. static void print_out(char *channel, char *buf) {
  184. static char outfile[256], server[256], buft[18];
  185. FILE *out = NULL;
  186. time_t t = time(0);
  187. if(channel) snprintf(server, sizeof(server), "-!- %s", channel);
  188. if(strstr(buf, server)) channel="";
  189. create_filepath(outfile, sizeof(outfile), channel, "out");
  190. if(!(out = fopen(outfile, "a"))) return;
  191. if(channel && channel[0]) add_channel(channel);
  192. strftime(buft, sizeof(buft), "%F %R", localtime(&t));
  193. fprintf(out, "%s %s\n", buft, buf);
  194. fclose(out);
  195. }
  196. static void proc_channels_privmsg(char *channel, char *buf) {
  197. snprintf(message, PIPE_BUF, "<%s> %s", nick, buf);
  198. print_out(channel, message);
  199. snprintf(message, PIPE_BUF, "PRIVMSG %s :%s\r\n", channel, buf);
  200. write(irc, message, strlen(message));
  201. }
  202. static void proc_channels_input(Channel *c, char *buf) {
  203. /* static char infile[256]; */
  204. char *p = NULL;
  205. if(buf[0] != '/' && buf[0] != 0) {
  206. proc_channels_privmsg(c->name, buf);
  207. return;
  208. }
  209. message[0] = '\0';
  210. if(buf[2] == ' ' || buf[2] == '\0') switch (buf[1]) {
  211. case 'j':
  212. p = strchr(&buf[3], ' ');
  213. if(p) *p = 0;
  214. if((buf[3]=='#')||(buf[3]=='&')||(buf[3]=='+')||(buf[3]=='!')){
  215. if(p) snprintf(message, PIPE_BUF, "JOIN %s %s\r\n", &buf[3], p + 1); /* password protected channel */
  216. else snprintf(message, PIPE_BUF, "JOIN %s\r\n", &buf[3]);
  217. add_channel(&buf[3]);
  218. }
  219. else if(p){
  220. add_channel(&buf[3]);
  221. proc_channels_privmsg(&buf[3], p + 1);
  222. return;
  223. }
  224. break;
  225. case 't':
  226. if(strlen(buf)>=3) snprintf(message, PIPE_BUF, "TOPIC %s :%s\r\n", c->name, &buf[3]);
  227. break;
  228. case 'a':
  229. if(strlen(buf)>=3){
  230. snprintf(message, PIPE_BUF, "-!- %s is away \"%s\"", nick, &buf[3]);
  231. print_out(c->name, message);
  232. }
  233. if(buf[2] == 0 || strlen(buf)<3) /* or used to make else part safe */
  234. snprintf(message, PIPE_BUF, "AWAY\r\n");
  235. else
  236. snprintf(message, PIPE_BUF, "AWAY :%s\r\n", &buf[3]);
  237. break;
  238. case 'n':
  239. if(strlen(buf)>=3){
  240. snprintf(nick, sizeof(nick),"%s", &buf[3]);
  241. snprintf(message, PIPE_BUF, "NICK %s\r\n", &buf[3]);
  242. }
  243. break;
  244. case 'l':
  245. if(c->name[0] == 0)
  246. return;
  247. if(buf[2] == ' ' && strlen(buf)>=3)
  248. snprintf(message, PIPE_BUF, "PART %s :%s\r\n", c->name, &buf[3]);
  249. else
  250. snprintf(message, PIPE_BUF,
  251. "PART %s :ii - 500 SLOC are too much\r\n", c->name);
  252. write(irc, message, strlen(message));
  253. close(c->fd);
  254. /*create_filepath(infile, sizeof(infile), c->name, "in");
  255. unlink(infile); */
  256. rm_channel(c);
  257. return;
  258. break;
  259. default:
  260. snprintf(message, PIPE_BUF, "%s\r\n", &buf[1]);
  261. break;
  262. }
  263. else
  264. snprintf(message, PIPE_BUF, "%s\r\n", &buf[1]);
  265. if (message[0] != '\0')
  266. write(irc, message, strlen(message));
  267. }
  268. static void proc_server_cmd(char *buf) {
  269. char *argv[TOK_LAST], *cmd = NULL, *p = NULL;
  270. int i;
  271. if(!buf || *buf=='\0')
  272. return;
  273. for(i = 0; i < TOK_LAST; i++)
  274. argv[i] = NULL;
  275. /* <message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
  276. <prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
  277. <command> ::= <letter> { <letter> } | <number> <number> <number>
  278. <SPACE> ::= ' ' { ' ' }
  279. <params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
  280. <middle> ::= <Any *non-empty* sequence of octets not including SPACE
  281. or NUL or CR or LF, the first of which may not be ':'>
  282. <trailing> ::= <Any, possibly *empty*, sequence of octets not including NUL or CR or LF>
  283. <crlf> ::= CR LF */
  284. if(buf[0] == ':') { /* check prefix */
  285. if (!(p = strchr(buf, ' '))) return;
  286. *p = 0;
  287. for(++p; *p == ' '; p++);
  288. cmd = p;
  289. argv[TOK_NICKSRV] = &buf[1];
  290. if((p = strchr(buf, '!'))) {
  291. *p = 0;
  292. argv[TOK_USER] = ++p;
  293. }
  294. } else
  295. cmd = buf;
  296. /* remove CRLFs */
  297. for(p = cmd; p && *p != 0; p++)
  298. if(*p == '\r' || *p == '\n')
  299. *p = 0;
  300. if((p = strchr(cmd, ':'))) {
  301. *p = 0;
  302. argv[TOK_TEXT] = ++p;
  303. }
  304. tokenize(&argv[TOK_CMD], TOK_LAST - TOK_CMD, cmd, ' ');
  305. if(!argv[TOK_CMD] || !strncmp("PONG", argv[TOK_CMD], 5)) {
  306. return;
  307. } else if(!strncmp("PING", argv[TOK_CMD], 5)) {
  308. snprintf(message, PIPE_BUF, "PONG %s\r\n", argv[TOK_TEXT]);
  309. write(irc, message, strlen(message));
  310. return;
  311. } else if(!argv[TOK_NICKSRV] || !argv[TOK_USER]) { /* server command */
  312. snprintf(message, PIPE_BUF, "%s%s", argv[TOK_ARG] ? argv[TOK_ARG] : "", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
  313. print_out(0, message);
  314. return;
  315. } else if(!strncmp("ERROR", argv[TOK_CMD], 6))
  316. snprintf(message, PIPE_BUF, "-!- error %s", argv[TOK_TEXT] ? argv[TOK_TEXT] : "unknown");
  317. else if(!strncmp("JOIN", argv[TOK_CMD], 5)) {
  318. if (argv[TOK_TEXT] != NULL)
  319. argv[TOK_CHAN] = argv[TOK_TEXT];
  320. snprintf(message, PIPE_BUF, "-!- %s(%s) has joined %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]);
  321. } else if(!strncmp("PART", argv[TOK_CMD], 5)) {
  322. snprintf(message, PIPE_BUF, "-!- %s(%s) has left %s", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_CHAN]);
  323. } else if(!strncmp("MODE", argv[TOK_CMD], 5))
  324. snprintf(message, PIPE_BUF, "-!- %s changed mode/%s -> %s %s", argv[TOK_NICKSRV], argv[TOK_CMD + 1] ? argv[TOK_CMD + 1] : "" , argv[TOK_CMD + 2]? argv[TOK_CMD + 2] : "", argv[TOK_CMD + 3] ? argv[TOK_CMD + 3] : "");
  325. else if(!strncmp("QUIT", argv[TOK_CMD], 5))
  326. snprintf(message, PIPE_BUF, "-!- %s(%s) has quit \"%s\"", argv[TOK_NICKSRV], argv[TOK_USER], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
  327. else if(!strncmp("NICK", argv[TOK_CMD], 5))
  328. snprintf(message, PIPE_BUF, "-!- %s changed nick to %s", argv[TOK_NICKSRV], argv[TOK_TEXT]);
  329. else if(!strncmp("TOPIC", argv[TOK_CMD], 6))
  330. snprintf(message, PIPE_BUF, "-!- %s changed topic to \"%s\"", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
  331. else if(!strncmp("KICK", argv[TOK_CMD], 5))
  332. snprintf(message, PIPE_BUF, "-!- %s kicked %s (\"%s\")", argv[TOK_NICKSRV], argv[TOK_ARG], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
  333. else if(!strncmp("NOTICE", argv[TOK_CMD], 7))
  334. snprintf(message, PIPE_BUF, "-!- \"%s\")", argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
  335. else if(!strncmp("PRIVMSG", argv[TOK_CMD], 8))
  336. snprintf(message, PIPE_BUF, "<%s> %s", argv[TOK_NICKSRV], argv[TOK_TEXT] ? argv[TOK_TEXT] : "");
  337. if(!argv[TOK_CHAN] || !strncmp(argv[TOK_CHAN], nick, strlen(nick)))
  338. print_out(argv[TOK_NICKSRV], message);
  339. else
  340. print_out(argv[TOK_CHAN], message);
  341. }
  342. static int read_line(int fd, size_t res_len, char *buf) {
  343. size_t i = 0;
  344. char c = 0;
  345. do {
  346. if(read(fd, &c, sizeof(char)) != sizeof(char))
  347. return -1;
  348. buf[i++] = c;
  349. }
  350. while(c != '\n' && i < res_len);
  351. buf[i - 1] = 0; /* eliminates '\n' */
  352. return 0;
  353. }
  354. static void handle_channels_input(Channel *c) {
  355. static char buf[PIPE_BUF];
  356. if(read_line(c->fd, PIPE_BUF, buf) == -1) {
  357. close(c->fd);
  358. int fd = open_channel(c->name);
  359. if(fd != -1)
  360. c->fd = fd;
  361. else
  362. rm_channel(c);
  363. return;
  364. }
  365. proc_channels_input(c, buf);
  366. }
  367. static void handle_server_output() {
  368. static char buf[PIPE_BUF];
  369. if(read_line(irc, PIPE_BUF, buf) == -1) {
  370. perror("ii: remote host closed connection");
  371. exit(EXIT_FAILURE);
  372. }
  373. proc_server_cmd(buf);
  374. }
  375. static void run() {
  376. Channel *c;
  377. int r, maxfd;
  378. fd_set rd;
  379. struct timeval tv;
  380. char ping_msg[512];
  381. snprintf(ping_msg, sizeof(ping_msg), "PING %s\r\n", host);
  382. for(;;) {
  383. FD_ZERO(&rd);
  384. maxfd = irc;
  385. FD_SET(irc, &rd);
  386. for(c = channels; c; c = c->next) {
  387. if(maxfd < c->fd)
  388. maxfd = c->fd;
  389. FD_SET(c->fd, &rd);
  390. }
  391. tv.tv_sec = 120;
  392. tv.tv_usec = 0;
  393. r = select(maxfd + 1, &rd, 0, 0, &tv);
  394. if(r < 0) {
  395. if(errno == EINTR)
  396. continue;
  397. perror("ii: error on select()");
  398. exit(EXIT_FAILURE);
  399. } else if(r == 0) {
  400. if(time(NULL) - last_response >= PING_TIMEOUT) {
  401. print_out(NULL, "-!- ii shutting down: ping timeout");
  402. exit(EXIT_FAILURE);
  403. }
  404. write(irc, ping_msg, strlen(ping_msg));
  405. continue;
  406. }
  407. if(FD_ISSET(irc, &rd)) {
  408. handle_server_output();
  409. last_response = time(NULL);
  410. }
  411. for(c = channels; c; c = c->next)
  412. if(FD_ISSET(c->fd, &rd))
  413. handle_channels_input(c);
  414. }
  415. }
  416. int main(int argc, char *argv[]) {
  417. int i;
  418. unsigned short port = SERVER_PORT;
  419. struct passwd *spw = getpwuid(getuid());
  420. char *key = NULL, *fullname = NULL;
  421. char prefix[_POSIX_PATH_MAX];
  422. if(!spw) {
  423. fputs("ii: getpwuid() failed\n", stderr);
  424. exit(EXIT_FAILURE);
  425. }
  426. snprintf(nick, sizeof(nick), "%s", spw->pw_name);
  427. snprintf(prefix, sizeof(prefix),"%s/irc", spw->pw_dir);
  428. if (argc <= 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) usage();
  429. for(i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) {
  430. switch (argv[i][1]) {
  431. case 'i': snprintf(prefix,sizeof(prefix),"%s", argv[++i]); break;
  432. case 's': host = argv[++i]; break;
  433. case 'p': port = strtol(argv[++i], NULL, 10); break;
  434. case 'n': snprintf(nick,sizeof(nick),"%s", argv[++i]); break;
  435. case 'k': key = getenv(argv[++i]); break;
  436. case 'f': fullname = argv[++i]; break;
  437. default: usage(); break;
  438. }
  439. }
  440. irc = tcpopen(port);
  441. if(!snprintf(path, sizeof(path), "%s/%s", prefix, host)) {
  442. fputs("ii: path to irc directory too long\n", stderr);
  443. exit(EXIT_FAILURE);
  444. }
  445. create_dirtree(path);
  446. add_channel(""); /* master channel */
  447. login(key, fullname);
  448. run();
  449. return EXIT_SUCCESS;
  450. }