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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 ircQuit(const char *mesg);
  102. void uiInit(void);
  103. void uiShow(void);
  104. void uiHide(void);
  105. void uiExit(void);
  106. void uiDraw(void);
  107. void uiRead(void);
  108. void uiPrompt(void);
  109. void uiViewTag(struct Tag tag);
  110. void uiViewNum(int num);
  111. void uiCloseTag(struct Tag tag);
  112. enum UIHeat {
  113. UICold,
  114. UIWarm,
  115. UIHot,
  116. };
  117. void uiLog(struct Tag tag, enum UIHeat heat, const wchar_t *line);
  118. void uiFmt(struct Tag tag, enum UIHeat heat, const wchar_t *format, ...);
  119. enum TermMode {
  120. TermFocus,
  121. TermPaste,
  122. };
  123. enum TermEvent {
  124. TermNone,
  125. TermFocusIn,
  126. TermFocusOut,
  127. TermPasteStart,
  128. TermPasteEnd,
  129. };
  130. void termInit(void);
  131. void termTitle(const char *title);
  132. void termMode(enum TermMode mode, bool set);
  133. enum TermEvent termEvent(char ch);
  134. enum Edit {
  135. EditLeft,
  136. EditRight,
  137. EditHome,
  138. EditEnd,
  139. EditBackWord,
  140. EditForeWord,
  141. EditInsert,
  142. EditBackspace,
  143. EditDelete,
  144. EditKillBackWord,
  145. EditKillForeWord,
  146. EditKillLine,
  147. EditROT13,
  148. EditComplete,
  149. EditEnter,
  150. };
  151. void edit(struct Tag tag, enum Edit op, wchar_t ch);
  152. const wchar_t *editHead(void);
  153. const wchar_t *editTail(void);
  154. void tabTouch(struct Tag tag, const char *word);
  155. void tabAdd(struct Tag tag, const char *word);
  156. void tabRemove(struct Tag tag, const char *word);
  157. void tabReplace(struct Tag tag, const char *prev, const char *next);
  158. void tabClear(struct Tag tag);
  159. struct Tag tabTag(const char *word);
  160. const char *tabNext(struct Tag tag, const char *prefix);
  161. void tabAccept(void);
  162. void tabReject(void);
  163. void urlScan(struct Tag tag, const char *str);
  164. void urlList(struct Tag tag);
  165. void urlOpenMatch(struct Tag tag, const char *substr);
  166. void urlOpenRange(struct Tag tag, size_t at, size_t to);
  167. void logOpen(const char *path);
  168. void logFmt(
  169. struct Tag tag, const time_t *ts, const char *format, ...
  170. ) __attribute__((format(printf, 3, 4)));
  171. void logReplay(struct Tag tag);
  172. wchar_t *wcsnchr(const wchar_t *wcs, size_t len, wchar_t chr);
  173. wchar_t *wcsnrchr(const wchar_t *wcs, size_t len, wchar_t chr);
  174. wchar_t *ambstowcs(const char *src);
  175. char *awcstombs(const wchar_t *src);
  176. char *awcsntombs(const wchar_t *src, size_t nwc);
  177. int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap);
  178. int aswprintf(wchar_t **ret, const wchar_t *format, ...);
  179. size_t base64Size(size_t len);
  180. void base64(char *dst, const byte *src, size_t len);
  181. // HACK: clang won't check wchar_t *format strings.
  182. #ifdef NDEBUG
  183. #define uiFmt(tag, heat, format, ...) uiFmt(tag, heat, L##format, __VA_ARGS__)
  184. #else
  185. #define uiFmt(tag, heat, format, ...) do { \
  186. snprintf(NULL, 0, format, __VA_ARGS__); \
  187. uiFmt(tag, heat, L##format, __VA_ARGS__); \
  188. } while(0)
  189. #endif