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.

url.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (C) 2018 Curtis 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. #include <assert.h>
  17. #include <err.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sysexits.h>
  21. #include <unistd.h>
  22. #include "chat.h"
  23. static const char *Schemes[] = {
  24. "cvs:",
  25. "ftp:",
  26. "git:",
  27. "http:",
  28. "https:",
  29. "irc:",
  30. "ircs:",
  31. "magnet:",
  32. "sftp:",
  33. "ssh:",
  34. "svn:",
  35. "telnet:",
  36. "vnc:",
  37. };
  38. static const size_t SchemesLen = sizeof(Schemes) / sizeof(Schemes[0]);
  39. struct Entry {
  40. size_t tag;
  41. char *url;
  42. };
  43. enum { RingLen = 32 };
  44. static_assert(!(RingLen & (RingLen - 1)), "power of two RingLen");
  45. static struct {
  46. struct Entry buf[RingLen];
  47. size_t end;
  48. } ring;
  49. static void ringPush(struct Tag tag, const char *url, size_t len) {
  50. free(ring.buf[ring.end].url);
  51. ring.buf[ring.end].tag = tag.id;
  52. ring.buf[ring.end].url = strndup(url, len);
  53. if (!ring.buf[ring.end].url) err(EX_OSERR, "strndup");
  54. ring.end = (ring.end + 1) & (RingLen - 1);
  55. }
  56. static struct Entry ringEntry(size_t i) {
  57. return ring.buf[(ring.end + i) & (RingLen - 1)];
  58. }
  59. void urlScan(struct Tag tag, const char *str) {
  60. while (str[0]) {
  61. size_t len = 1;
  62. for (size_t i = 0; i < SchemesLen; ++i) {
  63. if (strncmp(str, Schemes[i], strlen(Schemes[i]))) continue;
  64. len = strcspn(str, " >\"");
  65. ringPush(tag, str, len);
  66. }
  67. str = &str[len];
  68. }
  69. }
  70. void urlList(struct Tag tag) {
  71. uiHide();
  72. for (size_t i = 0; i < RingLen; ++i) {
  73. struct Entry entry = ringEntry(i);
  74. if (!entry.url || entry.tag != tag.id) continue;
  75. printf("%s\n", entry.url);
  76. }
  77. }
  78. void urlOpenMatch(struct Tag tag, const char *substr) {
  79. for (size_t i = RingLen - 1; i < RingLen; --i) {
  80. struct Entry entry = ringEntry(i);
  81. if (!entry.url || entry.tag != tag.id) continue;
  82. if (!strstr(entry.url, substr)) continue;
  83. eventPipe((const char *[]) { "open", entry.url, NULL });
  84. break;
  85. }
  86. }
  87. void urlOpenRange(struct Tag tag, size_t at, size_t to) {
  88. size_t argc = 1;
  89. const char *argv[2 + RingLen] = { "open" };
  90. size_t tagIndex = 0;
  91. for (size_t i = RingLen - 1; i < RingLen; --i) {
  92. struct Entry entry = ringEntry(i);
  93. if (!entry.url || entry.tag != tag.id) continue;
  94. if (tagIndex >= at && tagIndex < to) argv[argc++] = entry.url;
  95. tagIndex++;
  96. }
  97. argv[argc] = NULL;
  98. if (argc > 1) eventPipe(argv);
  99. }