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.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #define SOURCE_URL "https://code.causal.agency/june/catgirl"
  17. #include <stdarg.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdnoreturn.h>
  22. #include <time.h>
  23. #include <wchar.h>
  24. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  25. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  26. #define err(...) do { uiHide(); err(__VA_ARGS__); } while (0)
  27. #define errx(...) do { uiHide(); errx(__VA_ARGS__); } while (0)
  28. typedef unsigned uint;
  29. typedef unsigned char byte;
  30. struct {
  31. char *host;
  32. char *port;
  33. char *auth;
  34. char *pass;
  35. char *webp;
  36. char *nick;
  37. char *user;
  38. char *real;
  39. char *join;
  40. bool raw;
  41. bool notify;
  42. bool quit;
  43. } self;
  44. void eventWait(const char *argv[static 2]);
  45. void eventPipe(const char *argv[static 2]);
  46. noreturn void eventLoop(void);
  47. struct Tag {
  48. size_t id;
  49. const char *name;
  50. };
  51. enum { TagsLen = 256 };
  52. const struct Tag TagNone;
  53. const struct Tag TagStatus;
  54. const struct Tag TagRaw;
  55. struct Tag tagFind(const char *name);
  56. struct Tag tagFor(const char *name);
  57. enum IRCColor {
  58. IRCWhite,
  59. IRCBlack,
  60. IRCBlue,
  61. IRCGreen,
  62. IRCRed,
  63. IRCBrown,
  64. IRCMagenta,
  65. IRCOrange,
  66. IRCYellow,
  67. IRCLightGreen,
  68. IRCCyan,
  69. IRCLightCyan,
  70. IRCLightBlue,
  71. IRCPink,
  72. IRCGray,
  73. IRCLightGray,
  74. IRCDefault = 99,
  75. };
  76. enum {
  77. IRCBold = 002,
  78. IRCColor = 003,
  79. IRCReverse = 026,
  80. IRCReset = 017,
  81. IRCItalic = 035,
  82. IRCUnderline = 037,
  83. };
  84. struct Format {
  85. const wchar_t *str;
  86. size_t len;
  87. bool split;
  88. bool bold, italic, underline, reverse;
  89. enum IRCColor fg, bg;
  90. };
  91. void formatReset(struct Format *format);
  92. bool formatParse(struct Format *format, const wchar_t *split);
  93. enum IRCColor formatColor(const char *str);
  94. void handle(char *line);
  95. void input(struct Tag tag, char *line);
  96. void inputTab(void);
  97. int ircConnect(void);
  98. void ircRead(void);
  99. void ircWrite(const char *ptr, size_t len);
  100. void ircFmt(const char *format, ...) __attribute__((format(printf, 1, 2)));
  101. void uiInit(void);
  102. void uiShow(void);
  103. void uiHide(void);
  104. void uiExit(void);
  105. void uiDraw(void);
  106. void uiRead(void);
  107. void uiPrompt(void);
  108. void uiViewTag(struct Tag tag);
  109. void uiViewNum(int num);
  110. void uiCloseTag(struct Tag tag);
  111. enum UIHeat {
  112. UICold,
  113. UIWarm,
  114. UIHot,
  115. };
  116. void uiLog(struct Tag tag, enum UIHeat heat, const wchar_t *line);
  117. void uiFmt(struct Tag tag, enum UIHeat heat, const wchar_t *format, ...);
  118. enum TermMode {
  119. TermFocus,
  120. TermPaste,
  121. };
  122. enum TermEvent {
  123. TermNone,
  124. TermFocusIn,
  125. TermFocusOut,
  126. TermPasteStart,
  127. TermPasteEnd,
  128. };
  129. void termInit(void);
  130. void termTitle(const char *title);
  131. void termMode(enum TermMode mode, bool set);
  132. enum TermEvent termEvent(char ch);
  133. enum Edit {
  134. EditLeft,
  135. EditRight,
  136. EditHome,
  137. EditEnd,
  138. EditBackWord,
  139. EditForeWord,
  140. EditInsert,
  141. EditBackspace,
  142. EditDelete,
  143. EditKillBackWord,
  144. EditKillForeWord,
  145. EditKillLine,
  146. EditComplete,
  147. EditEnter,
  148. };
  149. void edit(struct Tag tag, enum Edit op, wchar_t ch);
  150. const wchar_t *editHead(void);
  151. const wchar_t *editTail(void);
  152. void tabTouch(struct Tag tag, const char *word);
  153. void tabAdd(struct Tag tag, const char *word);
  154. void tabRemove(struct Tag tag, const char *word);
  155. void tabReplace(struct Tag tag, const char *prev, const char *next);
  156. void tabClear(struct Tag tag);
  157. struct Tag tabTag(const char *word);
  158. const char *tabNext(struct Tag tag, const char *prefix);
  159. void tabAccept(void);
  160. void tabReject(void);
  161. void urlScan(struct Tag tag, const char *str);
  162. void urlList(struct Tag tag);
  163. void urlOpenMatch(struct Tag tag, const char *substr);
  164. void urlOpenRange(struct Tag tag, size_t at, size_t to);
  165. void logOpen(const char *path);
  166. void logFmt(
  167. struct Tag tag, const time_t *ts, const char *format, ...
  168. ) __attribute__((format(printf, 3, 4)));
  169. void logReplay(struct Tag tag);
  170. wchar_t *wcsnchr(const wchar_t *wcs, size_t len, wchar_t chr);
  171. wchar_t *wcsnrchr(const wchar_t *wcs, size_t len, wchar_t chr);
  172. wchar_t *ambstowcs(const char *src);
  173. char *awcstombs(const wchar_t *src);
  174. char *awcsntombs(const wchar_t *src, size_t nwc);
  175. int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap);
  176. int aswprintf(wchar_t **ret, const wchar_t *format, ...);
  177. char *base64(const byte *src, size_t len);
  178. // HACK: clang won't check wchar_t *format strings.
  179. #ifdef NDEBUG
  180. #define uiFmt(tag, heat, format, ...) uiFmt(tag, heat, L##format, __VA_ARGS__)
  181. #else
  182. #define uiFmt(tag, heat, format, ...) do { \
  183. snprintf(NULL, 0, format, __VA_ARGS__); \
  184. uiFmt(tag, heat, L##format, __VA_ARGS__); \
  185. } while(0)
  186. #endif