Mercurial > wcc
comparison wcc.c @ 0:aa723e3948a4 default tip
*: initial commit
awesome
| author | Paper <paper@tflc.us> |
|---|---|
| date | Tue, 09 Sep 2025 00:29:57 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:aa723e3948a4 |
|---|---|
| 1 /* | |
| 2 * wcc -- a shitty sockchat client | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; either version 2 of the License, or | |
| 7 * (at your option) any later version. | |
| 8 * | |
| 9 * This program is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with this program; if not, write to the Free Software | |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 17 */ | |
| 18 | |
| 19 #include <unistd.h> | |
| 20 #include <stdio.h> | |
| 21 #include <string.h> | |
| 22 #include <stdlib.h> | |
| 23 #include <time.h> | |
| 24 #include <curses.h> | |
| 25 #include <locale.h> | |
| 26 | |
| 27 #include "flashii.h" | |
| 28 | |
| 29 /* ------------------------------------------------------------------------ */ | |
| 30 | |
| 31 /* creates a set (only unique IDs allowed) | |
| 32 * | |
| 33 * the ID must be a string! */ | |
| 34 #define DYNAMIC_SET(type, name, uid) \ | |
| 35 struct name { \ | |
| 36 type *arr; \ | |
| 37 size_t size; \ | |
| 38 size_t alloc; \ | |
| 39 }; \ | |
| 40 \ | |
| 41 static type *name##_lookup(struct name *arr, const char *id) \ | |
| 42 { \ | |
| 43 size_t i; \ | |
| 44 \ | |
| 45 for (i = 0; i < arr->size; i++) \ | |
| 46 if (!strcmp(arr->arr[i].uid, id)) \ | |
| 47 return arr->arr + i; \ | |
| 48 \ | |
| 49 return NULL; \ | |
| 50 } \ | |
| 51 \ | |
| 52 static int name##_insert(struct name *x, type *u) \ | |
| 53 { \ | |
| 54 type *uu; \ | |
| 55 \ | |
| 56 uu = name##_lookup(x, u->uid); \ | |
| 57 if (uu) { \ | |
| 58 /* overwrite it with our own data */ \ | |
| 59 memcpy(uu, u, sizeof(*u)); \ | |
| 60 return 0; \ | |
| 61 } /* else... */ \ | |
| 62 \ | |
| 63 if (x->size + 1 >= x->alloc) { \ | |
| 64 x->alloc = (x->alloc) ? (x->alloc * 2) : 8; \ | |
| 65 x->arr = realloc(x->arr, x->alloc * sizeof(*u)); \ | |
| 66 if (!x->arr) \ | |
| 67 return -1; \ | |
| 68 } \ | |
| 69 \ | |
| 70 memcpy(x->arr + x->size, u, sizeof(*u)); \ | |
| 71 x->size++; \ | |
| 72 \ | |
| 73 return 1; \ | |
| 74 } \ | |
| 75 \ | |
| 76 static int name##_remove(struct name *s, type *u) \ | |
| 77 { \ | |
| 78 /* TODO */ \ | |
| 79 return -1; \ | |
| 80 } | |
| 81 | |
| 82 DYNAMIC_SET(struct flashii_user, flashii_user_set, id) | |
| 83 DYNAMIC_SET(struct flashii_msg, flashii_msg_set, msg_id) | |
| 84 | |
| 85 /* ------------------------------------------------------------------------ */ | |
| 86 | |
| 87 static struct flashii_user_set users = {0}; | |
| 88 static struct flashii_msg_set msgs = {0}; | |
| 89 | |
| 90 static void msg_recv(struct flashii *fls, struct flashii_msg *msg) | |
| 91 { | |
| 92 flashii_msg_set_insert(&msgs, msg); | |
| 93 } | |
| 94 | |
| 95 static void user_recv(struct flashii *fls, struct flashii_user *user) | |
| 96 { | |
| 97 flashii_user_set_insert(&users, user); | |
| 98 } | |
| 99 | |
| 100 /* pending message ... */ | |
| 101 static wchar_t pending_msg[65536]; | |
| 102 static size_t pending_msg_len = 0; | |
| 103 | |
| 104 int main(void) | |
| 105 { | |
| 106 int height, width; | |
| 107 struct flashii *fls; | |
| 108 WINDOW *cw, *iw; | |
| 109 | |
| 110 /* stupid C shit */ | |
| 111 setlocale(LC_ALL, "en_US.UTF-8"); | |
| 112 | |
| 113 initscr(); | |
| 114 cbreak(); | |
| 115 noecho(); | |
| 116 curs_set(1); | |
| 117 | |
| 118 getmaxyx(stdscr, height, width); | |
| 119 | |
| 120 #define INPUT_HEIGHT (3) | |
| 121 cw = newwin(height - INPUT_HEIGHT, width, 0, 0); | |
| 122 iw = newwin(INPUT_HEIGHT, width, height - INPUT_HEIGHT, 0); | |
| 123 | |
| 124 scrollok(cw, TRUE); | |
| 125 box(iw, 0, 0); | |
| 126 nodelay(iw, TRUE); /* no blocking! */ | |
| 127 keypad(iw, TRUE); | |
| 128 | |
| 129 fls = flashii_init("wss", "chatsrv-neru.flashii.net", 443, | |
| 130 msg_recv, user_recv); | |
| 131 if (!fls) | |
| 132 return 1; | |
| 133 | |
| 134 for (;;) { | |
| 135 size_t i; | |
| 136 | |
| 137 /* IM GONNA TALK TO DA POWER */ | |
| 138 flashii_work(fls, 1); | |
| 139 | |
| 140 /* chat window */ | |
| 141 werase(cw); | |
| 142 | |
| 143 /* draw msgs to the screen */ | |
| 144 for (i = 0; i < msgs.size; i++) { | |
| 145 const char *name; | |
| 146 struct flashii_user *user; | |
| 147 | |
| 148 user = flashii_user_set_lookup(&users, msgs.arr[i].id); | |
| 149 name = (user && user->name) ? user->name : msgs.arr[i].id; | |
| 150 | |
| 151 wprintw(cw, "<%s> %s\n", name, msgs.arr[i].body); | |
| 152 } | |
| 153 | |
| 154 wrefresh(cw); | |
| 155 | |
| 156 werase(iw); | |
| 157 | |
| 158 /* handle input */ | |
| 159 box(iw, 0, 0); | |
| 160 for (;;) { | |
| 161 wint_t c; | |
| 162 int r = wget_wch(iw, &c); | |
| 163 if (r == KEY_CODE_YES) { | |
| 164 /* keycode, KEY_DOWN, KEY_UP, etc */ | |
| 165 if (c == KEY_BACKSPACE) { /* backspace */ | |
| 166 pending_msg[--pending_msg_len] = '\0'; | |
| 167 } else { | |
| 168 printf("UNKNOWN KEYCODE: %d\n", c); | |
| 169 } | |
| 170 } else if (r == OK) { | |
| 171 /* unicode */ | |
| 172 if (pending_msg_len >= width - 7) /* or something like that */ | |
| 173 continue; | |
| 174 | |
| 175 if (c == '\r' || c == '\n') { | |
| 176 static char pending_msg_conv[65536]; | |
| 177 wcstombs(pending_msg_conv, pending_msg, pending_msg_len); | |
| 178 flashii_send_message(fls, pending_msg_conv); | |
| 179 memset(pending_msg, 0, pending_msg_len); | |
| 180 pending_msg_len = 0; | |
| 181 } else if (c == '\b' || c == 127) { | |
| 182 pending_msg[--pending_msg_len] = '\0'; | |
| 183 } else { | |
| 184 pending_msg[pending_msg_len++] = c; | |
| 185 } | |
| 186 } else/*if (r == ERR)*/ { | |
| 187 break; | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 mvwprintw(iw, 1, 1, "> %ls", pending_msg); | |
| 192 | |
| 193 wrefresh(iw); | |
| 194 | |
| 195 usleep(10000); | |
| 196 } | |
| 197 | |
| 198 endwin(); | |
| 199 | |
| 200 return 0; | |
| 201 } |
