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

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