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.

chat.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Copyright (C) 2018 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define _WITH_GETLINE
  17. #include <err.h>
  18. #include <locale.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sysexits.h>
  23. #include <unistd.h>
  24. #include "chat.h"
  25. static char *dupe(const char *str) {
  26. char *dup = strdup(str);
  27. if (!dup) err(EX_OSERR, "strdup");
  28. return dup;
  29. }
  30. static char *prompt(const char *prompt) {
  31. char *line = NULL;
  32. size_t cap;
  33. for (;;) {
  34. printf("%s", prompt);
  35. fflush(stdout);
  36. ssize_t len = getline(&line, &cap, stdin);
  37. if (ferror(stdin)) err(EX_IOERR, "getline");
  38. if (feof(stdin)) exit(EX_OK);
  39. if (len < 2) continue;
  40. line[len - 1] = '\0';
  41. return line;
  42. }
  43. }
  44. int main(int argc, char *argv[]) {
  45. setlocale(LC_CTYPE, "");
  46. int opt;
  47. while (0 < (opt = getopt(argc, argv, "NPRa:h:j:k:l:n:p:r:u:vw:"))) {
  48. switch (opt) {
  49. break; case 'N': self.notify = true;
  50. break; case 'P': self.nick = prompt("Name: ");
  51. break; case 'R': self.limit = true;
  52. break; case 'a': self.auth = dupe(optarg);
  53. break; case 'h': self.host = dupe(optarg);
  54. break; case 'j': self.join = dupe(optarg);
  55. break; case 'k': self.keys = dupe(optarg);
  56. break; case 'l': logOpen(optarg);
  57. break; case 'n': self.nick = dupe(optarg);
  58. break; case 'p': self.port = dupe(optarg);
  59. break; case 'r': self.real = dupe(optarg);
  60. break; case 'u': self.user = dupe(optarg);
  61. break; case 'v': self.raw = true;
  62. break; case 'w': self.pass = dupe(optarg);
  63. break; default: return EX_USAGE;
  64. }
  65. }
  66. if (!self.nick) {
  67. const char *user = getenv("USER");
  68. if (!user) errx(EX_USAGE, "USER unset");
  69. self.nick = dupe(user);
  70. }
  71. if (!self.host) self.host = prompt("Host: ");
  72. if (!self.port) self.port = dupe("6697");
  73. if (!self.user) self.user = dupe(self.nick);
  74. if (!self.real) self.real = dupe(self.nick);
  75. inputTab();
  76. uiInit();
  77. eventLoop();
  78. }