annotate nnchat.c @ 68:3ab7751fdad1

MingW compatibility, with one ugly kludge.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 14 Nov 2008 07:15:57 +0200
parents 4086088e95dc
children 79982b0aad97
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
1 /*
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
3 * Written by Matti 'ccr' Hämäläinen
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
4 * (C) Copyright 2008 Tecnic Software productions (TNSP)
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
5 */
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
6 #include "libnnchat.h"
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include <stdlib.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include "th_args.h"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <string.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include <errno.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include <time.h>
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
12 #ifdef __WIN32
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
13 #define usleep(t) Sleep((t) / 1000)
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
14 /* Undefine because both windows.h and curses.h #define it */
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
15 #undef MOUSE_MOVED
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
16 #endif
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
17 #include <curses.h>
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
19
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
20 #define SET_MAX_BACKBUF (1024)
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
21 #define SET_MAX_HISTORY (16)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
22 #define SET_DELAY (15)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
23 #define SET_DELAY_USEC (SET_DELAY * 1000)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
25
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 /* Options
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 */
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
28 int optPort = 8005;
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
29 int optUserColor = 0x006080;
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
30 char *optServer = "www11.servemedata.com",
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
31 *optUserName = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
32 *optUserName2 = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
33 *optPassword = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
34 *optLogFilename = NULL,
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
35 *setTarget = NULL,
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
36 *optSite = "NN";
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
37 BOOL optDaemon = FALSE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 FILE *optLogFile = NULL;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
39 WINDOW *mainWin = NULL,
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
40 *statusWin = NULL,
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
41 *editWin = NULL;
59
d57a8acf92bf Remove morse code mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 58
diff changeset
42 BOOL setPrvMode = FALSE;
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
43
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 /* Arguments
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 optarg_t optList[] = {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
47 { 0, '?', "help", "Show this help", OPT_NONE },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
48 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
49 { 2, 'p', "port", "Connect to port", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
50 { 3, 's', "server", "Server to connect to", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
51 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
52 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
53 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE },
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
54 { 7, 'S', "site", "Site (default: NN)", OPT_ARGREQ },
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
57 const int optListN = (sizeof(optList) / sizeof(optList[0]));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
60 int getColor(char *str)
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
61 {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
62 char *p = str;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
63 int len, val = 0;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
64
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
65 for (len = 0; *p && len < 6; p++, len++) {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
66 if (*p >= '0' && *p <= '9') {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
67 val *= 16; val += (*p - '0');
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
68 } else if (*p >= 'A' && *p <= 'F') {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
69 val *= 16; val += (*p - 'A') + 10;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
70 } else if (*p >= 'a' && *p <= 'f') {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
71 val *= 16; val += (*p - 'a') + 10;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
72 } else
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
73 return -1;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
74 }
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
75
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
76 return (len == 6) ? val : -1;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
77 }
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
78
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
79
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
80 void argShowHelp()
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
81 {
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
82 th_args_help(stdout, optList, optListN, th_prog_name,
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
83 "[options] <username> <password>");
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
84 }
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
85
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
86
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 switch (optN) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 case 0:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 argShowHelp();
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 exit(0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 case 1:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 th_verbosityLevel++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 case 2:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 optPort = atoi(optArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 case 3:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 optServer = optArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 case 4:
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
108 if ((optUserColor = getColor(optArg)) < 0) {
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 optArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 THMSG(1, "Using color #%06x\n", optUserColor);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 case 5:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 optLogFilename = optArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
120 case 7:
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
121 optSite = optArg;
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
122 break;
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
123
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
124 case 6:
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
125 optDaemon = TRUE;
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
126 THMSG(1, "Running in pseudo-daemon mode.\n");
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
127 break;
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
128
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 default:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 THERR("Unknown option '%s'.\n", currArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 BOOL argHandleFile(char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 if (!optUserName)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 optUserName = currArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 else if (!optPassword)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 optPassword = currArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 THERR("Username '%s' already specified on commandline!\n", optUserName);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
152 #ifdef __WIN32
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
153 const char *hstrerror(int err)
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
154 {
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
155 static char buf[64];
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
156 snprintf(buf, sizeof(buf), "Error #%d", err);
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
157 return buf;
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
158 }
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
159
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
160 int getSocketErrno(void)
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
161 {
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
162 return WSAGetLastError();
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
163 }
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
164
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
165 const char *getSocketErrStr(int err)
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
166 {
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
167 static char buf[64];
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
168 snprintf(buf, sizeof(buf), "Error #%d", err);
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
169 return buf;
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
170 }
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
171 #else
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
172 int getSocketErrno(void)
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
173 {
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
174 return errno;
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
175 }
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
176
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
177 const char *getSocketErrStr(int err)
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
178 {
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
179 return strerror(err);
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
180 }
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
181 #endif
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
182
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
183 void updateStatus(BOOL insertMode)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
184 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
185 char tmpStr[128] = "";
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
186 time_t timeStamp;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
187 struct tm *tmpTime;;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
188
32
67ec4073e38c Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
189 if (statusWin == NULL) return;
67ec4073e38c Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
190
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
191 timeStamp = time(NULL);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
192 if ((tmpTime = localtime(&timeStamp)) != NULL) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
193 strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
194 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
195
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
196 wbkgdset(statusWin, 0x0d00);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
197 werase(statusWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
198
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
199 wattrset(statusWin, A_BOLD);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
200 mvwaddstr(statusWin, 0, 1, tmpStr);
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
201
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
202 waddstr(statusWin, " | ");
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
203 wattrset(statusWin, A_BOLD | COLOR_PAIR(16));
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
204 waddstr(statusWin, optUserName);
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
205 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
206
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
207 waddstr(statusWin, " | ");
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
208 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
209 waddstr(statusWin, insertMode ? "INS" : "DEL");
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
210
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
211 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
212 waddstr(statusWin, " | Prv: ");
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
213
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
214 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
215 waddstr(statusWin, setTarget != NULL ? setTarget : "-");
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
216
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
217 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
218 waddstr(statusWin, " | P/C: ");
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
219 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
220 snprintf(tmpStr, sizeof(tmpStr), "%d / #%06x", optPort, optUserColor);
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
221 waddstr(statusWin, tmpStr);
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
222
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
223 wrefresh(statusWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
224 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
225
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
226 void printEditBuf(char *str, editbuf_t *buf)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
227 {
32
67ec4073e38c Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
228 if (statusWin == NULL || buf == NULL) return;
67ec4073e38c Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
229
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
230 buf->data[buf->len] = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
231 werase(editWin);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
232
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
233 wattrset(editWin, A_BOLD);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
234 mvwaddstr(editWin, 0, 0, str);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
235 waddstr(editWin, "> ");
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
236 wattrset(editWin, A_NORMAL);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
237
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
238 if (buf->pos < buf->len) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
239 waddnstr(editWin, buf->data, buf->pos);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
240 wattrset(editWin, A_REVERSE);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
241 waddch(editWin, buf->data[buf->pos]);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
242 wattrset(editWin, A_NORMAL);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
243 waddnstr(editWin, buf->data + buf->pos + 1, buf->len - buf->pos - 1);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
244 } else {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
245 waddnstr(editWin, buf->data, buf->len);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
246 wattrset(editWin, A_REVERSE);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
247 waddch(editWin, ' ');
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
248 wattrset(editWin, A_NORMAL);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
249 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
250 wrefresh(editWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
251 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
252
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
253 int printWin(WINDOW *win, const char *fmt)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
254 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
255 const char *s = fmt;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
256 int col = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
257
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
258 while (*s) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
259 if (*s == '½') {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
260 int val = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
261 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
262 if (*s == '½') {
55
5ec3b7d14715 Add typecasts to fix printing of 8-bit characters.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
263 waddch(win, ((unsigned char) *s) | col);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
264 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
265 } else {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
266 while (*s && isdigit(*s)) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
267 val *= 10;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
268 val += (*s - '0');
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
269 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
270 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
271 if (*s != '½') return -1;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
272 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
273
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
274 if (val < 9) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
275 col = A_DIM | COLOR_PAIR(val);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
276 } else if (val < 30) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
277 col = A_BOLD | COLOR_PAIR(val - 9);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
278 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
279 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
280 } else {
55
5ec3b7d14715 Add typecasts to fix printing of 8-bit characters.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
281 waddch(win, ((unsigned char) *s) | col);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
282 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
283 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
284 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
285 return 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
286 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
288
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
289 int printFile(FILE *outFile, const char *fmt)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
291 const char *s = fmt;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
292
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
293 while (*s) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
294 if (*s == '½') {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
295 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
296 if (*s == '½') {
58
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
297 fputc((unsigned char) *s, outFile);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
298 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
299 } else {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
300 while (*s && isdigit(*s)) s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
301 if (*s != '½') return -1;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
302 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
303 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
304 } else {
58
5455d078bedd Misc. fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 57
diff changeset
305 fputc((unsigned char) *s, outFile);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
306 s++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
307 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
308 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
309
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
310 return 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
311 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
312
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
313
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
314 void printMsg(char *fmt, ...)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
315 {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
316 char tmpStr[128] = "", buf[8192];
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 time_t timeStamp;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 struct tm *tmpTime;;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 timeStamp = time(NULL);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 if ((tmpTime = localtime(&timeStamp)) != NULL) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
323 strftime(tmpStr, sizeof(tmpStr), "½17½[½11½%H:%M:%S½17½]½0½ ", tmpTime);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
326 va_start(ap, fmt);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
327 vsnprintf(buf, sizeof(buf), fmt, ap);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
328 va_end(ap);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
329
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 if (optLogFile) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
331 printFile(optLogFile, tmpStr);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
332 printFile(optLogFile, buf);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 fflush(optLogFile);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
336 if (!optDaemon) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
337 printWin(mainWin, tmpStr);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
338 printWin(mainWin, buf);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
339 wrefresh(mainWin);
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
340 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 int handleUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 const char *msg = "</USER><MESSAGE>";
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
347 char *p = str, *q, *s, *t, *h;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 s = strstr(str, msg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 *s = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 s += strlen(msg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 q = strstr(s, "</MESSAGE>");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 if (!q) return 3;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 *q = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 s = decodeStr1(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 if (!s) return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 p = decodeStr1(p);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 if (!p) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 th_free(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 if (*s == '/') {
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
371 t = stripXMLTags(s + 1);
40
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
372 if (!strncmp(t, "BPRV", 4)) {
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
373 h = decodeStr2(t + 1);
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
374 printMsg("½11½%s½0½\n", h);
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
375 } else {
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
376 h = decodeStr2(t);
40
3e2548c3eb51 Fix PRV handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
377 printMsg("½9½* %s½0½\n", h);
19
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
378 }
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
379 th_free(h);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 th_free(t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 } else {
63
afd90bbb3af6 Colour changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
382 BOOL isMine = strcmp(p, optUserName) == 0;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
383 t = stripXMLTags(s);
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
384 h = decodeStr2(t);
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
385 printMsg("½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, p, h);
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
386 th_free(h);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 th_free(t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 th_free(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 th_free(p);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 int handleLogin(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 {
19
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
398 char tmpStr[256] = "";
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
399 time_t timeStamp;
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
400 struct tm *tmpTime;;
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
401
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
402 timeStamp = time(NULL);
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
403 if ((tmpTime = localtime(&timeStamp)) != NULL) {
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
404 strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime);
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
405 }
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
406
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 if (!strncmp(str, "FAILURE", 7)) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
408 printMsg("½1½Login failure½0½ - ½3½%s½0½\n", tmpStr);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 } else if (!strncmp(str, "SUCCESS", 7)) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
411 printMsg("½2½Login success½0½ - ½3½%s½0½\n", tmpStr);
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
412 sendUserMsg(sock, optUserName2, "%%2FRequestUserList");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 } else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 int handleAddUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
421 char *p, *s = strstr(str, "</ADD_USER>");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 *s = 0;
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
427
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
428 p = decodeStr1(str);
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
429 if (!p) return -1;
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
430
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
431 printMsg("! ½3½%s½0½ ½2½ADDED.½0½\n", p);
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
432 th_free(p);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 int handleDeleteUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
439 char *p, *s = strstr(str, "</DELETE_USER>");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 *s = 0;
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
445
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
446 p = decodeStr1(str);
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
447 if (!p) return -1;
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
448
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
449 printMsg("! ½3½%s½0½ ½1½DELETED.½0½\n", p);
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
450 th_free(p);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 int handleFoo(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 (void) sock; (void) str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463 typedef struct {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 char *cmd;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 int (*handler)(int, char *);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 } protocmd_t;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 protocmd_t protoCmds[] = {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470 { "<USER>", handleUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 { "<LOGIN_", handleLogin },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472 { "<DELETE_USER>", handleDeleteUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 { "<ADD_USER>", handleAddUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474 { "<NUMCLIENTS>", handleFoo },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
477 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protoCmds[0]));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
480 int handleProtocol(const int sock, char *buf, size_t bufLen)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484 for (i = 0; i < nprotoCmds; i++) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485 size_t cmdLen = strlen(protoCmds[i].cmd);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486 if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487 return protoCmds[i].handler(sock, buf + cmdLen);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
495 int handleUserInput(const int sock, char *buf, size_t bufLen)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 {
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
497 char *tmpStr, *tmpStr2, tmpBuf[4096];
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 BOOL result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 /* Trim right */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 buf[--bufLen] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502 while (bufLen > 0 && (buf[bufLen] == '\n' || buf[bufLen] == '\r' || th_isspace(buf[bufLen])))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 buf[bufLen--] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 /* Check command */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 if (*buf == 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 return 1;
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
508 } else if (!strncmp(buf, "/color ", 7)) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
509 int tmpInt;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
510 if ((tmpInt = getColor(buf+7)) < 0) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
511 printMsg("Invalid color value '%s'\n", buf+7);
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
512 return 1;
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
513 }
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
514 optUserColor = tmpInt;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
515 printMsg("Setting color to #%06x\n", optUserColor);
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
516 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
517 return 0;
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
518 } else if (!strncmp(buf, "/flood ", 7)) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
519 int i;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
520
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
521 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg . .",
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
522 buf+7);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
523
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
524 tmpStr = encodeStr2(tmpBuf);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
525 if (!tmpStr) return -2;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
526 tmpStr2 = encodeStr1(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
527 if (!tmpStr2) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
528 th_free(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
529 return -3;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
530 }
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
531
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
532 result = TRUE;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
533 for (i = 0; i < 50 && result; i++) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
534 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
535 usleep(250);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
536 }
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
537
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
538 th_free(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
539 th_free(tmpStr2);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
540 return 0;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
541 } else if (!strncmp(buf, "/to ", 4)) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
542 th_free(setTarget);
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
543 setTarget = strdup(buf + 4);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
544 printMsg("Set prv target to '%s'\n", setTarget);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
545 return 0;
67
4086088e95dc Alias '/who' to '/listallusers'.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
546 } else if (!strncmp(buf, "/who", 4)) {
4086088e95dc Alias '/who' to '/listallusers'.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
547 snprintf(tmpBuf, sizeof(tmpBuf), "/listallusers");
4086088e95dc Alias '/who' to '/listallusers'.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
548 buf = tmpBuf;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
549 } else if (setPrvMode) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
550 if (setTarget != NULL) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
551 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
552 buf = tmpBuf;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
553 } else {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
554 printMsg("No target set, exiting prv mode.\n");
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
555 setPrvMode = FALSE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
556 return 1;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
557 }
24
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
558 }
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
559
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
560
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
561 /* Send double-encoded */
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
562 tmpStr = encodeStr2(buf);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
563 if (!tmpStr) return -2;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
564 tmpStr2 = encodeStr1(tmpStr);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
565 if (!tmpStr2) {
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 th_free(tmpStr);
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
567 return -3;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 }
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
569
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
570 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
571 th_free(tmpStr);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
572 th_free(tmpStr2);
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
573 if (result)
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
574 return 0;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
575 else
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
576 return -1;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
580 BOOL initializeWindows(void)
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
581 {
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
582 if (mainWin) delwin(mainWin);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
583 if (statusWin) delwin(statusWin);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
584 if (editWin) delwin(editWin);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
585
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
586 mainWin = newwin(LINES - 4, COLS, 0, 0);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
587 statusWin = newwin(1, COLS, LINES - 4, 0);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
588 editWin = newwin(3, COLS, LINES - 3, 0);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
589
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
590 if (mainWin == NULL || statusWin == NULL || editWin == NULL) {
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
591 THERR("Could not create ncurses windows!\n");
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
592 return FALSE;
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
593 }
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
594 scrollok(mainWin, 1);
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
595
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
596 return TRUE;
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
597 }
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
598
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
599
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600 int main(int argc, char *argv[])
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601 {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
602 int tmpSocket, curVis, updateCount = 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603 struct hostent *tmpHost;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
604 BOOL argsOK, isError = FALSE,
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
605 exitProg = FALSE,
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
606 colorSet = FALSE,
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
607 cursesInit = FALSE,
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
608 #if __WIN32
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
609 networkInit = FALSE,
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
610 #endif
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
611 insertMode = TRUE;
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
612 struct timeval tv;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
613 fd_set sockfds;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
614 char *tmpStr;
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
615 editbuf_t *editBuf = newBuf(SET_BUFSIZE);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
616 editbuf_t *histBuf[SET_MAX_HISTORY+2];
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
617 int histPos = 0, histMax = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
618
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
619 memset(histBuf, 0, sizeof(histBuf));
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
620
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
621 /* Initialize */
61
b802a799c31a v0.6.5.
Matti Hamalainen <ccr@tnsp.org>
parents: 59
diff changeset
622 th_init("NNChat", "Newbie Nudes chat client", "0.6.5",
6
526ba3b578d7 Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
623 "Written and designed by Anonymous Finnish Guy (C) 2008",
526ba3b578d7 Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
624 "This software is freeware, use and distribute as you wish.");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625 th_verbosityLevel = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 /* Parse arguments */
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
628 argsOK = th_args_process(argc, argv, optList, optListN,
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629 argHandleOpt, argHandleFile, FALSE);
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
630
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 /* Check the mode and arguments */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 if (optUserName == NULL || optPassword == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 THERR("User/pass not specified, get some --help\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
637 if (!argsOK)
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
638 return -2;
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
639
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
640
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641 /* Open logfile */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642 if (optLogFilename) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643 THMSG(1, "Opening logfile '%s'\n", optLogFilename);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 if ((optLogFile = fopen(optLogFilename, "a")) == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646 THERR("Could not open logfile for appending!\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647 return -9;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
649 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
651 #ifdef __WIN32
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
652 {
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
653 /* Initialize WinSock, if needed */
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
654 WSADATA wsaData;
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
655 int err = WSAStartup(0x0101, &wsaData);
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
656 if (err != 0) {
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
657 THERR("Could not initialize WinSock DLL: %d\n", err);
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
658 goto err_exit;
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
659 }
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
660 networkInit = TRUE;
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
661 #endif
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
662
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
663 /* Okay ... */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
664 THMSG(1, "Trying to resolve host '%s' ...\n", optServer);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 tmpHost = gethostbyname(optServer);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
666 if (tmpHost == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667 THERR("Could not resolve hostname: %s.\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
668 hstrerror(h_errno));
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
669 goto err_exit;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
670 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
671 THMSG(2, "True hostname: %s\n", tmpHost->h_name);
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
672
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
673
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
674 #if 0
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
675 /* To emulate the official client, we first make a request for
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
676 * policy file, even though we don't use it for anything...
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
677 */
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
678 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) {
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
679 THERR("Policy file request connection setup failed!\n");
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
680 goto err_exit;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
683 tmpStr = "<policy-file-request/>";
23
40fecbab1dc1 Check for error condition properly.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
684 if (sendToSocket(tmpSocket, tmpStr, strlen(tmpStr) + 1) == FALSE) {
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
685 THERR("Failed to send fakeprobe.\n");
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
686 goto err_exit;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
687 } else {
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
688 ssize_t gotBuf;
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
689 char tmpBuf[SET_BUFSIZE];
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
690 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
691 tmpBuf[gotBuf-1] = 0;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
692 THMSG(2, "Probe got: %s\n", tmpBuf);
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
693 closeConnection(tmpSocket);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
694 }
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
695 #endif
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
696
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
697 /* Okay, now do the proper connection ... */
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
698 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) {
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
699 THERR("Main connection setup failed!\n");
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
700 goto err_exit;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
701 }
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
702
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
703 THMSG(1, "Connected, logging in as '%s', site '%s'.\n", optUserName, optSite);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704 optUserName2 = encodeStr1(optUserName);
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
705 tmpStr = encodeStr1(optSite);
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
706 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword);
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
707 th_free(tmpStr);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
709 /* Initialize NCurses */
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
710 if (!optDaemon) {
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
711 if (LINES < 0 || LINES > 1000) LINES = 24;
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
712 if (COLS < 0 || COLS > 1000) COLS = 80;
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
713 LINES=24;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
714 initscr();
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
715 raw();
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
716 keypad(stdscr, TRUE);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
717 noecho();
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
718 meta(stdscr, TRUE);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
719 timeout(SET_DELAY);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
720 curVis = curs_set(0);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
721 if (has_colors()) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
722 start_color();
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
723
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
724 init_pair(1, COLOR_RED, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
725 init_pair(2, COLOR_GREEN, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
726 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
727 init_pair(4, COLOR_BLUE, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
728 init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
729 init_pair(6, COLOR_CYAN, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
730 init_pair(7, COLOR_WHITE, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
731 init_pair(8, COLOR_BLACK, COLOR_BLACK);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
732
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
733 init_pair(10, COLOR_BLACK, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
734 init_pair(11, COLOR_WHITE, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
735 init_pair(12, COLOR_GREEN, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
736 init_pair(13, COLOR_YELLOW, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
737 init_pair(14, COLOR_BLUE, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
738 init_pair(15, COLOR_MAGENTA, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
739 init_pair(16, COLOR_CYAN, COLOR_RED);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
740
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
741 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
742
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
743 cursesInit = TRUE;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
744
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
745 if (!initializeWindows())
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
746 goto err_exit;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
747
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
748 clearBuf(editBuf);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
749 printEditBuf("", editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
750 updateStatus(insertMode);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
751 }
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
752
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
753 /* Enter mainloop */
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
754 FD_ZERO(&sockfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755 FD_SET(tmpSocket, &sockfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
756
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
757 while (!isError && !exitProg) {
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
758 int result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759 fd_set tmpfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
760
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
761 /* Check for incoming data from the server */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
762 tv.tv_sec = 0;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
763 tv.tv_usec = SET_DELAY_USEC;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 tmpfds = sockfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
765 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) {
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
766 int res = getSocketErrno();
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
767 if (res != EINTR) {
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
768 printMsg("Error occured in select(sockfds): %d, %s\n",
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
769 res, getSocketErrStr(res));
53
7b98da167a0b Improved error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
770 isError = TRUE;
7b98da167a0b Improved error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
771 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
772 } else if (FD_ISSET(tmpSocket, &tmpfds)) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
773 ssize_t gotBuf;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
774 char tmpBuf[8192];
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
775 char *bufPtr = tmpBuf;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
776 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
777
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
778 if (gotBuf < 0) {
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
779 int res = getSocketErrno();
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
780 printMsg("Error in recv: %d, %s\n", res, getSocketErrStr(res));
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
781 isError = TRUE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
782 } else if (gotBuf == 0) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
783 printMsg("Server closed connection.\n");
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
784 isError = TRUE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
785 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
786 /* Handle protocol data */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
787 tmpBuf[gotBuf] = 0;
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
788 do {
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
789 size_t bufLen = strlen(bufPtr) + 1;
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
790 result = handleProtocol(tmpSocket, bufPtr, bufLen);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
792 if (result > 0) {
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
793 /* Couldn't handle the message for some reason */
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
794 printMsg("Could not handle: %s\n", tmpBuf);
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
795 } else if (result < 0) {
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
796 /* Fatal error, quit */
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
797 printMsg("Fatal error with message: %s\n", tmpBuf);
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
798 isError = TRUE;
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
799 }
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
800
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
801 gotBuf -= bufLen;
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
802 bufPtr += bufLen;
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
803 } while (gotBuf > 0 && !isError);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
804 updateStatus(insertMode);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
805 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
806 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
807
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
808 /* Handle user input */
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
809 if (!optDaemon) {
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
810 int c, cnt = 0;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
811 BOOL update = FALSE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
812
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
813 /* Handle several buffered keypresses at once */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
814 do {
50
d08bf3b6561d Use strdup; Added function to decode morse code.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
815 c = wgetch(stdscr);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
816 switch (c) {
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
817 case KEY_RESIZE:
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
818 if (!initializeWindows()) {
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
819 THERR("Error resizing ncurses windows\n");
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
820 isError = TRUE;
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
821 }
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
822 break;
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
823
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
824 case KEY_ENTER:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
825 case '\n':
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
826 case '\r':
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
827 /* Call the user input handler */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
828 if (editBuf->len > 0) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
829
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
830 if (histMax > 0) {
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
831 freeBuf(histBuf[SET_MAX_HISTORY+1]);
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
832 histBuf[SET_MAX_HISTORY+1] = NULL;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
833 memmove(&histBuf[2], &histBuf[1], histMax * sizeof(histBuf[0]));
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
834 }
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
835
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
836 histPos = 0;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
837 histBuf[1] = copyBuf(editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
838 if (histMax < SET_MAX_HISTORY) histMax++;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
839
34
f5d335771b16 Fix a bug with mixed editBuf->pos vs editBuf->len.
Matti Hamalainen <ccr@tnsp.org>
parents: 33
diff changeset
840 insertBuf(editBuf, editBuf->len, 0);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
841 result = handleUserInput(tmpSocket, editBuf->data, editBuf->len);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
842
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
843 clearBuf(editBuf);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
844
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
845 if (result < 0) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
846 printMsg("Fatal error handling user input: %s\n", editBuf->data);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
847 isError = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
848 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
849
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
850 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
851 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
852 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
853
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
854 case 0x09: /* Tab = switch between PRV */
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
855 if (setPrvMode)
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
856 setPrvMode = FALSE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
857 else {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
858 if (setTarget != NULL)
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
859 setPrvMode = TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
860 }
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
861 update = TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
862 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
863
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
864 case KEY_UP: /* Backwards in input history */
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
865 if (histPos == 0) {
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
866 freeBuf(histBuf[0]);
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
867 histBuf[0] = copyBuf(editBuf);
31
9b429b283786 Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
868 }
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
869 if (histPos < histMax) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
870 histPos++;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
871 freeBuf(editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
872 editBuf = copyBuf(histBuf[histPos]);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
873 update = TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
874 }
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
875 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
876
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
877 case KEY_DOWN: /* Forwards in input history */
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
878 if (histPos > 0) {
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
879 histPos--;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
880 freeBuf(editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
881 editBuf = copyBuf(histBuf[histPos]);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
882 update = TRUE;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
883 }
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
884 break;
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
885
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
886 case 0x204: /* ctrl+left = Skip words left */
45
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
887 while (editBuf->pos > 0 && isspace(editBuf->data[editBuf->pos - 1]))
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
888 editBuf->pos--;
45
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
889 while (editBuf->pos > 0 && !isspace(editBuf->data[editBuf->pos - 1]))
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
890 editBuf->pos--;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
891 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
892 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
893
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
894 case 0x206: /* ctrl+right = Skip words right */
45
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
895 while (editBuf->pos < editBuf->len && isspace(editBuf->data[editBuf->pos]))
ceec75116aa7 Improved editing commands ctrl+<left/right>
Matti Hamalainen <ccr@tnsp.org>
parents: 43
diff changeset
896 editBuf->pos++;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
897 while (editBuf->pos < editBuf->len && !isspace(editBuf->data[editBuf->pos]))
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
898 editBuf->pos++;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
899 if (editBuf->pos > editBuf->len)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
900 editBuf->pos = editBuf->len;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
901 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
902 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
903
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
904 case 0x111: /* F9 = Quit */
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
905 printMsg("Quitting per user request.\n");
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
906 exitProg = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
907 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
908
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
909 case 0x109: /* F1 = Toggle insert / overwrite mode */
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
910 insertMode = !insertMode;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
911 update = TRUE;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
912 break;
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
913
54
f6666e7c1a48 More comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
914 case 0x10a: /* F2 = Clear editbuffer */
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
915 clearBuf(editBuf);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
916 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
917 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
918
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
919 case KEY_HOME: setBufPos(editBuf, 0); update = TRUE; break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
920 case KEY_END: setBufPos(editBuf, editBuf->len); update = TRUE; break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
921 case KEY_LEFT: setBufPos(editBuf, editBuf->pos - 1); update = TRUE; break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
922 case KEY_RIGHT: setBufPos(editBuf, editBuf->pos + 1); update = TRUE; break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
923
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
924 case KEY_BACKSPACE:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
925 deleteBuf(editBuf, editBuf->pos - 1);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
926 setBufPos(editBuf, editBuf->pos - 1);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
927 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
928 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
929
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
930 case 0x14a:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
931 /* Delete */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
932 deleteBuf(editBuf, editBuf->pos);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
933 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
934 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
935
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
936 case 0x0c:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
937 /* ctrl+l */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
938 redrawwin(mainWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
939 redrawwin(statusWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
940 redrawwin(editWin);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
941 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
942
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
943 case ERR:
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
944 /* Ignore */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
945 break;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
946
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
947 default:
36
34864d021d7e Accept 'äöå' in input.
Matti Hamalainen <ccr@tnsp.org>
parents: 35
diff changeset
948 if (isprint(c) || c == 0xe4 || c == 0xf6 || c == 0xc4 || c == 0xd6) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
949 if (insertMode)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
950 insertBuf(editBuf, editBuf->pos, c);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
951 else
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
952 writeBuf(editBuf, editBuf->pos, c);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
953 setBufPos(editBuf, editBuf->pos + 1);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
954 update = TRUE;
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
955 } else {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
956 printMsg("Unhandled key: %02x\n", c);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
957 }
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
958 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
959 }
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
960 } while (c != ERR && !exitProg && ++cnt < 10);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
961
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
962 if (update) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
963 /* Update edit line */
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
964 printEditBuf(setPrvMode ? setTarget : "", editBuf);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
965 updateStatus(insertMode);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
966 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
967 } /* !optDaemon */
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
968
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
969 if (++updateCount > 10) {
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
970 updateStatus(insertMode);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
971 updateCount = 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
972 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
973
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
974 if (!colorSet) {
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
975 colorSet = TRUE;
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
976 printMsg("%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_fullname);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
977 printMsg("%s\n", th_prog_author);
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
978 printMsg("%s\n", th_prog_license);
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
979 sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
980 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
981 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
982
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
983 /* Shutdown */
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
984 err_exit:
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
985 if (cursesInit) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
986 if (curVis != ERR)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
987 curs_set(curVis);
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
988 endwin();
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
989 THMSG(1, "NCurses deinitialized.\n");
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
990 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
991
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
992 if (isError) {
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
993 THMSG(1, "Error exit.\n");
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
994 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
995
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
996 th_free(optUserName2);
10
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
997
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
998 closeConnection(tmpSocket);
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
999
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1000 #ifdef __WIN32
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1001 if (networkInit)
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1002 WSACleanup();
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1003 #endif
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1004
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
1005 THMSG(1, "Connection terminated.\n");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1006
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1007 if (optLogFile) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1008 THMSG(1, "Closing logfile.\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1009 fclose(optLogFile);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1010 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1011
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1012 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1013 }
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1014
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1015 #ifdef __WIN32
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1016 /* This is a really weird kludge ... */
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1017 }
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1018 #endif