annotate libnnchat.c @ 354:c01e42fc9adb

More work on SOCKS proxy support, should work now.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 23 Jun 2011 08:26:48 +0300
parents b54c8545dcb0
children ba2aa110755b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Written by Matti 'ccr' Hämäläinen
274
1211b740fbda Update copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 215
diff changeset
4 * (C) Copyright 2008-2011 Tecnic Software productions (TNSP)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 */
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 #include "libnnchat.h"
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8
90
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
9 #ifdef __WIN32
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
10 const char *hstrerror(int err)
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
11 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
12 static char buf[64];
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
13 snprintf(buf, sizeof(buf), "Error #%d", err);
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
14 return buf;
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
15 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
16
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
17 int nn_get_socket_errno(void)
90
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
18 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
19 return WSAGetLastError();
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
20 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
21
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
22 const char *nn_get_socket_errstr(int err)
90
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
23 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
24 static char buf[64];
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
25 switch (err) {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
26 case WSAEADDRINUSE: return "Address already in use";
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
27 case WSAECONNABORTED: return "Software caused connection abort";
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
28 case WSAECONNREFUSED: return "Connection refused";
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
29 case WSAECONNRESET: return "Connection reset by peer";
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
30 case WSAEHOSTUNREACH: return "No route to host";
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
31 case WSAENETDOWN: return "Network is down";
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
32 case WSAETIMEDOUT: return "Connection timed out";
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
33 case WSAHOST_NOT_FOUND: return "Host not found";
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
34 case WSAVERNOTSUPPORTED: return "Wrong WinSock DLL version";
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
35 default:
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
36 snprintf(buf, sizeof(buf), "Error #%d", err);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
37 return buf;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
38 break;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
39 }
90
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
40 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
41 #else
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
42 int nn_get_socket_errno(void)
90
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
43 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
44 return errno;
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
45 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
46
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
47 const char *nn_get_socket_errstr(int err)
90
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
48 {
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
49 return strerror(err);
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
50 }
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
51 #endif
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
52
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
53
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
54 void nn_conn_err(nn_conn_t *conn, const char *fmt, ...)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 {
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
56 if (conn->errfunc) {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
57 va_list ap;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
58 va_start(ap, fmt);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
59 conn->errfunc(conn, fmt, ap);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
60 va_end(ap);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
61 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
62 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
63
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
64
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
65 static void nn_conn_msg(nn_conn_t *conn, const char *fmt, ...)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
66 {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
67 if (conn->msgfunc) {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
68 va_list ap;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
69 va_start(ap, fmt);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
70 conn->msgfunc(conn, fmt, ap);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
71 va_end(ap);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
72 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
73 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
74
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
75 struct hostent *nn_resolve_host(nn_conn_t *conn, const char *name)
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
76 {
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
77 struct hostent *res = gethostbyname(name);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
78 if (res == NULL)
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
79 nn_conn_err(conn, "Could not resolve hostname: %s\n", strerror(h_errno));
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
80 else
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
81 nn_conn_msg(conn, "True hostname for %s is %s\n", name, res->h_name);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
82
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
83 return res;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
84 }
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
85
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
86 static const char *nn_proxy_types[] = {
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
87 "none",
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
88 "SOCKS 4",
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
89 "SOCKS 4a",
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
90 NULL
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
91 };
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
92
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
93
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
94 nn_conn_t * nn_conn_new(
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
95 void (*errfunc)(nn_conn_t *conn, const char *fmt, va_list ap),
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
96 void (*msgfunc)(nn_conn_t *conn, const char *fmt, va_list ap))
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
97 {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
98 nn_conn_t *conn = th_calloc(1, sizeof(nn_conn_t));
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
99
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
100 if (conn == NULL)
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
101 return NULL;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
102
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
103 conn->errfunc = errfunc;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
104 conn->msgfunc = msgfunc;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
105
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
106 return conn;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
107 }
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
108
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
109 int nn_conn_set_proxy(nn_conn_t *conn, int type, int port, const char *host)
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
110 {
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
111 if (conn == NULL)
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
112 return -1;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
113
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
114 conn->proxy.type = type;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
115 conn->proxy.port = port;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
116 conn->proxy.host = th_strdup(host);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
117
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
118 if (host != NULL) {
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
119 conn->proxy.hst = nn_resolve_host(conn, host);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
120 if (conn->proxy.hst != NULL)
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
121 conn->proxy.addr = *(struct in_addr *) (conn->proxy.hst->h_addr);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
122 else {
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
123 conn->proxy.addr.s_addr = 0;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
124 }
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
125 } else
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
126 return -2;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
127
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
128 return 0;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
129 }
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
130
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
131 int nn_conn_open(nn_conn_t *conn, const int port, const char *host)
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
132 {
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
133 struct sockaddr_in dest;
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
134 static const char *userid = "James Bond";
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
135
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
136 if (conn == NULL)
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
137 return -1;
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
138
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
139 conn->port = port;
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
140 if (host != NULL) {
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
141 conn->host = th_strdup(host);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
142 conn->hst = nn_resolve_host(conn, host);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
143 }
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
144
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
145 if (conn->hst != NULL)
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
146 conn->addr = *(struct in_addr *) (conn->hst->h_addr);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
147 else {
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
148 conn->addr.s_addr = 0;
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
149 }
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
151
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
152 /* Prepare for connection */
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
153 if (conn->proxy.type > NN_PROXY_NONE && conn->proxy.type < NN_PROXY_LAST) {
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
154 dest.sin_family = AF_INET;
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
155 dest.sin_port = htons(conn->proxy.port);
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
156 dest.sin_addr = conn->proxy.addr;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
157
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
158 nn_conn_msg(conn, "Connecting to %s proxy %s:%d ...\n",
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
159 nn_proxy_types[conn->proxy.type],
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
160 inet_ntoa(dest.sin_addr), port);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
161 } else {
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
162 dest.sin_family = AF_INET;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
163 dest.sin_port = htons(port);
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
164 dest.sin_addr = conn->addr;
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
165
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
166 nn_conn_msg(conn, "Connecting to %s:%d ...\n",
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
167 inet_ntoa(dest.sin_addr), port);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
168 }
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
170 if ((conn->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
171 conn->err = nn_get_socket_errno();
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
172 nn_conn_err(conn, "Could not open socket: %s\n", nn_get_socket_errstr(conn->err));
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
173 goto error;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
174 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
175
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
176 nn_conn_msg(conn, "Using socket %d.\n", conn->socket);
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
177
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
178 if (connect(conn->socket, (struct sockaddr *) &dest, sizeof(dest)) == -1) {
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
179 conn->err = nn_get_socket_errno();
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
180 nn_conn_err(conn, "Could not connect: %s\n", nn_get_socket_errstr(conn->err));
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
181 goto error;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
182 }
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
183
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
184 FD_ZERO(&(conn->sockfds));
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
185 FD_SET(conn->socket, &(conn->sockfds));
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
186
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
187 /* Proxy-specific setup */
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
188 if (conn->proxy.type == NN_PROXY_SOCKS4 || conn->proxy.type == NN_PROXY_SOCKS4A) {
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
189 struct nn_socks_t *socksh;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
190 size_t bufsiz = sizeof(struct nn_socks_t) + strlen(userid) + 1 + strlen(conn->host) + 1;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
191 char *ptr, *buf;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
192 int tries, status = -1;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
193
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
194 ptr = buf = th_malloc(bufsiz);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
195 if (buf == NULL) {
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
196 conn->err = -1;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
197 nn_conn_err(conn, "Could not allocate memory for SOCKS negotiation buffer, %d bytes.\n", bufsiz);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
198 goto error;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
199 }
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
200
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
201 /* Create SOCKS 4/4A request */
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
202 nn_conn_msg(conn, "Initializing proxy negotiation.\n");
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
203 socksh = (struct nn_socks_t *) buf;
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
204 socksh->version = 4;
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
205 socksh->command = SOCKS_CMD_CONNECT;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
206 socksh->port = htons(port);
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
207 if (conn->proxy.type == NN_PROXY_SOCKS4A)
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
208 socksh->addr = htonl(0x00000032);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
209 else
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
210 socksh->addr = conn->addr.s_addr;
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
211 ptr += sizeof(struct nn_socks_t);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
212
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
213 strcpy(ptr, userid);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
214 ptr += strlen(userid) + 1;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
215
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
216 strcpy(ptr, conn->host);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
217
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
218 /* Send request */
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
219 if (!nn_conn_send_buf(conn, buf, bufsiz)) {
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
220 th_free(buf);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
221 nn_conn_err(conn, "Error sending SOCKS proxy request.\n");
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
222 goto error;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
223 }
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
224 th_free(buf);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
225
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
226 /* Wait for SOCKS server to reply */
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
227 for (tries = 0; tries < 20; tries++) {
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
228 status = nn_conn_pull(conn);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
229 if (status <= 0) break;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
230 }
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
231
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
232 /* Check results */
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
233 if (status == 0) {
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
234 struct nn_socks_res_t *res = (struct nn_socks_res_t *) &(conn->buf);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
235 if (res->nb != 0) {
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
236 nn_conn_err(conn, "Invalid SOCKS server reply, does not begin with NUL byte (%d).\n", res->nb);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
237 goto error;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
238 }
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
239 if (res->result != 0x5a) {
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
240 char *s = NULL;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
241 switch (res->result) {
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
242 case 0x5b: s = "Request rejected or failed"; break;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
243 case 0x5c: s = "Request failed because client is not running identd (or not reachable from the server)"; break;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
244 case 0x5d: s = "Request failed because client's identd could not confirm the user ID string in the request"; break;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
245 default: s = "Unknown SOCKS error response"; break;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
246 }
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
247 nn_conn_err(conn, "SOCKS setup failed, 0x%02x: %s.\n", res->result, s);
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
248 goto error;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
249 }
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
250 }
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
251 else
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
252 goto error;
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
253 }
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
254
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
255 conn->status = NN_CONN_OPEN;
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
256 return 0;
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
257
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
258 error:
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
259 conn->status = NN_CONN_CLOSED;
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
260 return -2;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
264 void nn_conn_close(nn_conn_t *conn)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 {
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
266 if (conn == NULL)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
267 return;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
268
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
269 if (conn->socket >= 0) {
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
270 #ifdef __WIN32
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
271 closesocket(conn->socket);
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
272 #else
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
273 close(conn->socket);
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
274 #endif
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
275 conn->socket = -1;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
276 }
354
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
277
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
278 th_free(conn->host);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
279 th_free(conn->proxy.host);
c01e42fc9adb More work on SOCKS proxy support, should work now.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
280
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
281 conn->status = NN_CONN_CLOSED;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
282
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
283 th_free(conn);
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
287 BOOL nn_conn_send_buf(nn_conn_t *conn, const char *buf, const size_t len)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
288 {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
289 size_t bufLeft = len;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
290 const char *bufPtr = buf;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
291
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
292 while (bufLeft > 0) {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
293 ssize_t bufSent;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
294 bufSent = send(conn->socket, bufPtr, bufLeft, 0);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
295 if (bufSent < 0) {
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
296 conn->err = nn_get_socket_errno();
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
297 nn_conn_err(conn, "nn_conn_send_buf() failed: %s", nn_get_socket_errstr(conn->err));
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
298 return FALSE;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
299 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
300 bufLeft -= bufSent;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
301 bufPtr += bufSent;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
302 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
303
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
304 return TRUE;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
305 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
306
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
307
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
308 int nn_conn_pull(nn_conn_t *conn)
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
309 {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
310 int result;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
311 struct timeval socktv;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
312 fd_set tmpfds;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
313
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
314 if (conn == NULL)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
315 return -1;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
316
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
317 conn->ptr = conn->buf;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
318
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
319 /* Check for incoming data */
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
320 socktv.tv_sec = 0;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
321 socktv.tv_usec = NN_DELAY_USEC;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
322 tmpfds = conn->sockfds;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
323
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
324 if ((result = select(conn->socket + 1, &tmpfds, NULL, NULL, &socktv)) == -1) {
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
325 conn->err = nn_get_socket_errno();
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
326 if (conn->err != EINTR) {
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
327 nn_conn_err(conn, "Error occured in select(%d, sockfds): %d, %s\n",
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
328 socket, conn->err, nn_get_socket_errstr(conn->err));
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
329 return -1;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
330 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
331 } else
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
332 if (FD_ISSET(conn->socket, &tmpfds)) {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
333 conn->got = recv(conn->socket, conn->ptr, NN_CONNBUF_SIZE, 0);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
334 if (conn->got < 0) {
352
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
335 conn->err = nn_get_socket_errno();
b54c8545dcb0 Overhaul network code a bit, add initial implementation of SOCKS4/4A proxy support -- which may not work yet, it is untested.
Matti Hamalainen <ccr@tnsp.org>
parents: 330
diff changeset
336 nn_conn_err(conn, "Error in recv: %d, %s\n", conn->err, nn_get_socket_errstr(conn->err));
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
337 return -2;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
338 } else if (conn->got == 0) {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
339 nn_conn_err(conn, "Server closed connection.\n");
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
340 conn->status = NN_CONN_CLOSED;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
341 return -3;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
342 } else {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
343 /* Handle protocol data */
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
344 conn->buf[conn->got] = 0;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
345 return 0;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
346 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
347 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
348
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
349 return 1;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
350 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
351
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
352 BOOL nn_conn_check(nn_conn_t *conn)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
353 {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
354 if (conn == NULL)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
355 return FALSE;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
356
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
357 return conn->err == 0 && conn->status == NN_CONN_OPEN;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
358 }
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
359
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
360
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
361
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
362 BOOL nn_network_init(void)
91
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
363 {
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
364 #ifdef __WIN32
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
365 /* Initialize WinSock, if needed */
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
366 WSADATA wsaData;
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
367 int err = WSAStartup(0x0101, &wsaData);
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
368 if (err != 0) {
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
369 THERR("Could not initialize WinSock library (err=%d).\n", err);
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
370 return FALSE;
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
371 }
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
372 #endif
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
373 return TRUE;
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
374 }
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
375
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
376
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
377 void nn_network_close(void)
91
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
378 {
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
379 #ifdef __WIN32
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
380 WSACleanup();
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
381 #endif
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
382 }
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
383
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
384
304
61884ce9db41 Use th_vputch() and th_vputs() instead of similar routines in libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
385 #define PUSHCHAR(x) th_vputch(&result, &resSize, &resPos, x)
61884ce9db41 Use th_vputch() and th_vputs() instead of similar routines in libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 285
diff changeset
386 #define PUSHSTR(x) th_vputs(&result, &resSize, &resPos, x)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
388 char *nn_encode_str1(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
390 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
391 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
392 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
393
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
394 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
395
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
396 resSize = strlen(str) + NN_ALLOC_SIZE;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
397 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
398 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
399
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
400 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
401 switch (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
402 case 32:
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
403 PUSHCHAR('+');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
404 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
405
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
406 default:
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
407 if (th_isalnum(*s))
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
408 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
409 else {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
410 char tmpStr[4];
215
659b8229d015 Silence some warnings on OpenBSD.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
411 snprintf(tmpStr, sizeof(tmpStr), "%2X", (unsigned char) *s);
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
412 PUSHCHAR('%');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
413 PUSHSTR(tmpStr);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
414 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
415 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
416 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
417 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
418 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
419 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
420
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
421 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 static int getHexDigit(const int c, const int shift)
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
427 int i;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
428
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
429 if (c >= 'A' && c <= 'F')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
430 i = c - 'A' + 10;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
431 else if (c >= 'a' && c <= 'f')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
432 i = c - 'a' + 10;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
433 else if (c >= '0' && c <= '9')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
434 i = c - '0';
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
435 else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
436 return -1;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
437
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
438 return i << shift;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
442 char *nn_decode_str1(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
444 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
445 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
446 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
447 int c;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
448
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
449 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
450
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
451 resSize = strlen(str) + NN_ALLOC_SIZE;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
452 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
453 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
454
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
455 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
456 switch (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
457 case '+':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
458 PUSHCHAR(' ');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
459 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
460 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
461
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
462 case '½':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
463 /* Escape these .. */
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
464 PUSHCHAR('½');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
465 PUSHCHAR('½');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
466 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
467 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
468
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
469 case '\r':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
470 PUSHCHAR(' ');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
471 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
472 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
473
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
474 case '%':
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
475 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
476 if (*s == '%')
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
477 PUSHCHAR('%');
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
478 else if ((c = getHexDigit(*s, 4)) >= 0) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
479 int i = getHexDigit(*(++s), 0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
480 if (i >= 0) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
481 PUSHCHAR(c | i);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
482 } else {
72
fe5fc76c0806 Fix the fuckups of previous commit.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
483 PUSHCHAR('§');
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
484 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
485 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
486 } else {
72
fe5fc76c0806 Fix the fuckups of previous commit.
Matti Hamalainen <ccr@tnsp.org>
parents: 71
diff changeset
487 PUSHCHAR('§');
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
488 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
489 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
490 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
491 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
492
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
493 default:
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
494 PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
495 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
496 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
497 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
498 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
499
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
500 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
504 char *nn_strip_tags(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
506 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
507 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
508 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
509
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
510 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
511
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
512 resSize = strlen(str) + NN_ALLOC_SIZE;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
513 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
514 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
515
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
516 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
517 if (*s == '<') {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
518 while (*s && *s != '>') s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
519 if (*s == '>') s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
520 } else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
521 PUSHCHAR(*s++);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
522 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
523 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
524
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
525 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
529 typedef struct {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
530 char c;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
531 char *ent;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
532 } html_entity_t;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
533
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
534
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
535 static const html_entity_t HTMLEntities[] = {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
536 { '<', "&lt;" },
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
537 { '>', "&gt;" },
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
538 };
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
539
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
540 static const int numHTMLEntities = sizeof(HTMLEntities) / sizeof(HTMLEntities[0]);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
541
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
542
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
543 char *nn_encode_str2(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
545 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
546 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
547 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
548
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
549 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
550
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
551 resSize = strlen(str) + NN_ALLOC_SIZE;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
552 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
553 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
554
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
555 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
556 int i;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
557 BOOL found = FALSE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
558 for (i = 0; i < numHTMLEntities; i++)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
559 if (HTMLEntities[i].c == *s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
560 PUSHSTR(HTMLEntities[i].ent);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
561 found = TRUE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
562 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
563 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
564 if (!found) PUSHCHAR(*s);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
565
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
566 s++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
567 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
568 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
569
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
570 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
574 char *nn_decode_str2(const char *str)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
576 const char *s = str;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
577 char *result;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
578 size_t resSize, resPos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
579
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
580 if (str == NULL) return NULL;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
581
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
582 resSize = strlen(str);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
583 if ((result = th_malloc(resSize)) == NULL)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
584 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
585
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
586 while (*s) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
587 if (*s == '&') {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
588 int i;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
589 BOOL found = FALSE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
590 for (i = 0; i < numHTMLEntities; i++) {
95
69f9c46e4a94 Constify.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
591 const html_entity_t *ent = &HTMLEntities[i];
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
592 int len = strlen(ent->ent);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
593 if (!strncmp(s, ent->ent, len)) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
594 PUSHCHAR(ent->c);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
595 s += len;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
596 found = TRUE;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
597 break;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
598 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
599 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
600 if (!found) PUSHCHAR(*s++);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
601 } else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
602 PUSHCHAR(*s++);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
603 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
604 PUSHCHAR(0);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
605
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
606 return result;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
609
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
610 char *nn_dbldecode_str(const char *str)
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
611 {
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
612 char *res, *tmp;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
613
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
614 if ((tmp = nn_decode_str1(str)) == NULL)
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
615 return NULL;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
616
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
617 res = nn_decode_str2(tmp);
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
618 th_free(tmp);
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
619
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
620 return res;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
621 }
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
622
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
623
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
624 char *nn_dblencode_str(const char *str)
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
625 {
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
626 char *res, *tmp;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
627
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
628 if ((tmp = nn_encode_str2(str)) == NULL)
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
629 return NULL;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
630
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
631 res = nn_encode_str1(tmp);
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
632 th_free(tmp);
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
633
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
634 return res;
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
635 }
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
636
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 72
diff changeset
637
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
638 BOOL nn_conn_send_msg(nn_conn_t *conn, const char *user, const char *fmt, ...)
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639 {
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
640 char *tmp, *msg;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
641 va_list ap;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
642
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
643 va_start(ap, fmt);
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
644 tmp = th_strdup_vprintf(fmt, ap);
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
645 va_end(ap);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
646
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
647 if (tmp == NULL)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
648 return FALSE;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
649
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
650 msg = th_strdup_printf("<USER>%s</USER><MESSAGE>%s</MESSAGE>", user, tmp);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
651 th_free(tmp);
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
652
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
653 if (msg != NULL) {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
654 BOOL ret = nn_conn_send_buf(conn, msg, strlen(msg) + 1);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
655 th_free(msg);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
656 return ret;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
657 } else
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
658 return FALSE;
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659 }
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
660
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
661
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
662 int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
663 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
664 if (buf->len+1 >= buf->size) return -3;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
665
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
666 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
667 return -1;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
668 else if (pos >= buf->len) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
669 buf->data[buf->len++] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
670 } else {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
671 buf->data[pos] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
672 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
673 return 0;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
674 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
675
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
676
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
677 int nn_editbuf_insert(nn_editbuf_t *buf, ssize_t pos, int ch)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
678 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
679 if (buf->len+1 >= buf->size) return -3;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
680
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
681 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
682 return -1;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
683 else if (pos >= buf->len) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
684 buf->data[buf->len] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
685 } else {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
686 memmove(&(buf->data[pos+1]), &(buf->data[pos]), buf->len - pos + 1);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
687 buf->data[pos] = ch;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
688 }
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
689 buf->len++;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
690 return 0;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
691 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
692
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
693
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
694 int nn_editbuf_delete(nn_editbuf_t *buf, ssize_t pos)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
695 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
696 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
697 return -1;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
698 else if (pos < buf->len) {
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
699 memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
700 buf->len--;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
701 return 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
702 } else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
703 return -2;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
704 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
705
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
706
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
707 void nn_editbuf_clear(nn_editbuf_t *buf)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
708 {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
709 buf->len = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
710 buf->pos = 0;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
711 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
712
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
713
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
714 nn_editbuf_t * nn_editbuf_new(ssize_t n)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
715 {
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
716 nn_editbuf_t *res = th_calloc(1, sizeof(nn_editbuf_t));
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
717
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
718 res->data = (char *) th_malloc(n);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
719 res->size = n;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
720
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
721 return res;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
722 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
723
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
724
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
725 void nn_editbuf_free(nn_editbuf_t *buf)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
726 {
312
a0c001672b44 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 304
diff changeset
727 if (buf != NULL) {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
728 th_free(buf->data);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
729 th_free(buf);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
730 }
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
731 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
732
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
733
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
734 nn_editbuf_t * nn_editbuf_copy(nn_editbuf_t *src)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
735 {
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
736 nn_editbuf_t *res;
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
737
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
738 assert(src != NULL);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
739
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
740 if (src == NULL) return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
741
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
742 if ((res = nn_editbuf_new(src->size)) == NULL)
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
743 return NULL;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
744
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
745 memcpy(res->data, src->data, src->size);
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
746 res->pos = res->len = src->len;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
747
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
748 return res;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
749 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
750
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
751
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
752 char * nn_editbuf_get_string(nn_editbuf_t *buf, ssize_t start, ssize_t end)
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
753 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
754 char *str;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
755 ssize_t siz;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
756
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
757 if (buf == NULL)
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
758 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
759
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
760 if (start < 0 || end > buf->len || start >= buf->len)
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
761 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
762
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
763 if (end < 0) {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
764 siz = buf->len - start + 1;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
765 } else if (start <= end) {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
766 siz = end - start + 1;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
767 } else
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
768 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
769
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
770 if ((str = th_malloc(siz + 1)) == NULL)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
771 return NULL;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
772
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
773 memcpy(str, buf->data + start, siz);
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
774 str[siz] = 0;
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 167
diff changeset
775
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
776 return str;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
777 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
778
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
779
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 95
diff changeset
780 void nn_editbuf_setpos(nn_editbuf_t *buf, ssize_t pos)
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
781 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
782 assert(buf != NULL);
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
783
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
784 if (pos < 0)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
785 buf->pos = 0;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
786 else if (pos >= buf->len)
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
787 buf->pos = buf->len;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
788 else
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
789 buf->pos = pos;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
790 }
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
791
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
792
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
793 static uint8_t nn_hash_user(const char *name)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
794 {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
795 /*
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
796 int n = 0;
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
797 const uint8_t *c = (uint8_t *)name;
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
798 uint8_t hash = 0xff;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
799
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
800 while (*c && n < 4) {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
801 hash = (hash << 1) ^ tolower(*c);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
802 c++; n++;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
803 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
804
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
805 return (hash & 0xff);
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
806 */
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
807 return tolower(name[0]);
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
808 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
809
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
810
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
811 static void nn_user_insert(nn_user_t **list, nn_user_t *node)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
812 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
813 node->next = *list;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
814 *list = node;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
815 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
816
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
817
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
818 nn_user_t *nn_userhash_foreach(const nn_userhash_t *list, int (*func)(const nn_user_t *))
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
819 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
820 int i;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
821
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
822 if (list == NULL) return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
823
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
824 for (i = 0; i < NN_NUM_BUCKETS; i++)
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
825 if (list->buckets[i] != NULL) {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
826 nn_user_t *curr = list->buckets[i];
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
827 while (curr != NULL) {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
828 if (func(curr) != 0)
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
829 return curr;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
830 curr = curr->next;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
831 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
832 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
833
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
834 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
835 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
836
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
837
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
838 nn_user_t *nn_user_find(const nn_userhash_t *list, const char *name)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
839 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
840 uint8_t hash;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
841
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
842 if (list == NULL) return NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
843
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
844 hash = nn_hash_user(name);
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
845 if (list->buckets[hash] != NULL) {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
846 nn_user_t *curr = list->buckets[hash];
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
847 while (curr != NULL) {
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
848 if (strcasecmp(curr->name, name) == 0)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
849 return curr;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
850 curr = curr->next;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
851 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
852 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
853
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
854 return NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
855 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
856
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
857
164
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
858 static nn_user_t *nn_user_match_do(nn_user_t *list, const char *pattern, size_t len)
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
859 {
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
860 nn_user_t *curr = list;
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
861 while (curr != NULL) {
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
862 if (len <= strlen(curr->name) && strncasecmp(curr->name, pattern, len) == 0)
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
863 return curr;
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
864 curr = curr->next;
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
865 }
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
866 return NULL;
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
867 }
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
868
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
869
116
741e45592522 Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents: 108
diff changeset
870 nn_user_t *nn_user_match(const nn_userhash_t *list, const char *pattern, const char *current, BOOL again)
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
871 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
872 uint8_t hash;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
873
108
f323b137ca08 Tab-completion now works with cycling, but bugs with names that have whitespaces.
Matti Hamalainen <ccr@tnsp.org>
parents: 106
diff changeset
874 if (list == NULL || pattern == NULL) return NULL;
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
875
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
876 hash = nn_hash_user(pattern);
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
877 if (list->buckets[hash] != NULL) {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
878 nn_user_t *curr = list->buckets[hash];
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
879 size_t len = strlen(pattern);
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
880
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
881 if (current != NULL) {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
882 nn_user_t *found = NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
883 while (curr != NULL) {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
884 if (strcasecmp(curr->name, current) == 0) {
116
741e45592522 Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents: 108
diff changeset
885 if (again)
741e45592522 Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents: 108
diff changeset
886 return curr;
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
887 found = curr->next;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
888 break;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
889 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
890 curr = curr->next;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
891 }
164
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
892
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
893 if (found != NULL && (found = nn_user_match_do(found, pattern, len)) != NULL)
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
894 return found;
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
895 }
116
741e45592522 Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents: 108
diff changeset
896
164
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
897 if ((curr = nn_user_match_do(list->buckets[hash], pattern, len)) != NULL)
701c54a45466 Fix matching of usernames against hash. In some cases after the last match the function would not go back to the first match.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
898 return curr;
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
899 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
900
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
901 return NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
902 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
903
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
904
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
905 nn_userhash_t *nn_userhash_new(void)
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
906 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
907 return th_calloc(1, sizeof(nn_userhash_t));
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
908 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
909
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
910
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
911 int nn_userhash_insert(nn_userhash_t *list, const char *name)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
912 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
913 uint8_t hash;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
914 nn_user_t *user;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
915
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
916 /* Check arguments */
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
917 if (list == NULL || name == NULL)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
918 return -1;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
919
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
920 /* Check if username is already there */
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
921 if (nn_user_find(list, name) != NULL)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
922 return 1;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
923
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
924 /* No, we'll add it */
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
925 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
926 return -3;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
927
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
928 user->name = th_strdup(name);
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
929 if (user->name == NULL)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
930 return -4;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
931
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
932 hash = nn_hash_user(name);
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
933 nn_user_insert(&(list->buckets[hash]), user);
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
934
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
935 return 0;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
936 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
937
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
938
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
939 int nn_userhash_delete(nn_userhash_t *list, const char *name)
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
940 {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
941 uint8_t hash;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
942
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
943 /* Check arguments */
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
944 if (list == NULL || name == NULL)
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
945 return -1;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
946
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
947 /* Check if username is already there */
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
948 hash = nn_hash_user(name);
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
949 if (list->buckets[hash] != NULL) {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
950 nn_user_t *curr, *prev;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
951 curr = list->buckets[hash];
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
952 prev = NULL;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
953 while (curr != NULL) {
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
954 if (strcasecmp(curr->name, name) == 0) {
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
955 if (prev)
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
956 prev->next = curr->next;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
957 else
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
958 list->buckets[hash] = curr->next;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
959
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
960 nn_user_free(curr);
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
961
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
962 return 0;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
963 } else {
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
964 prev = curr;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
965 curr = curr->next;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
966 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
967 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
968 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
969
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
970 return 1;
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
971 }
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
972
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
973
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
974 nn_user_t *nn_user_copy(const nn_user_t *src)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
975 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
976 nn_user_t *user;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
977
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
978 if (src == NULL) return NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
979
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
980 if ((user = th_calloc(1, sizeof(nn_user_t))) == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
981 return NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
982
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
983 /* Copy relevant data */
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
984 user->name = th_strdup(src->name);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
985 user->lastspoke = src->lastspoke;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
986 user->joined = src->joined;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
987
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
988 return user;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
989 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
990
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
991
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
992 void nn_user_free(nn_user_t *user)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
993 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
994 th_free(user->name);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
995 th_free(user);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
996 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
997
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
998
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
999 void nn_user_free_list(nn_user_t *list)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1000 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1001 nn_user_t *next, *curr = list;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1002 while (curr != NULL) {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1003 next = curr->next;
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
1004 nn_user_free(curr);
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1005 curr = next;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1006 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1007 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1008
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
1009 void nn_userhash_free(nn_userhash_t *hash)
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1010 {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1011 int i;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1012 if (hash == NULL)
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1013 return;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1014
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1015 for (i = 0; i < NN_NUM_BUCKETS; i++) {
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
1016 nn_user_free_list(hash->buckets[i]);
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1017 hash->buckets[i] = NULL;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1018 }
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1019
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1020 th_free(hash);
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
1021 }
285
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1022
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1023
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1024 char *nn_username_encode(char *str)
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1025 {
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1026 unsigned char *c = (unsigned char *) str;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1027 if (str == NULL) return NULL;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1028 for (; *c ; c++)
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1029 if (*c == ' ') *c = 255;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1030 return str;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1031 }
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1032
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1033
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1034 char *nn_username_decode(char *str)
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1035 {
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1036 unsigned char *c = (unsigned char *) str;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1037 if (str == NULL) return NULL;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1038 for (; *c ; c++)
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1039 if (*c == 255) *c = ' ';
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1040 return str;
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1041 }
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1042
b765f15f9895 Move nn_username_{de,en}code() to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 277
diff changeset
1043
325
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1044 nn_window_t *nn_window_new(const char *id)
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1045 {
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1046 nn_window_t *res = th_calloc(1, sizeof(nn_window_t));
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1047
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1048 if (res == NULL) return NULL;
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1049
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1050 res->data = th_ringbuf_new(NN_BACKBUF_LEN, th_free);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1051 if (res->data == NULL) {
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1052 th_free(res);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1053 return NULL;
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1054 }
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1055
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1056 res->id = th_strdup(id);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1057
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1058 return res;
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1059 }
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1060
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1061
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1062 void nn_window_free(nn_window_t *win)
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1063 {
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1064 if (win != NULL) {
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1065 th_ringbuf_free(win->data);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1066 th_free(win->id);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1067 th_free(win);
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1068 }
c086345d176b Move some functions to libnnchat and rename nn_find_window to findWindow()
Matti Hamalainen <ccr@tnsp.org>
parents: 312
diff changeset
1069 }
330
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1070
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1071
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1072 nn_strtuple_t *nn_strtuple_new(size_t len, char *str)
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1073 {
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1074 nn_strtuple_t *tuple = th_calloc(1, sizeof(nn_strtuple_t));
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1075 tuple->len = len;
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1076 tuple->str = str;
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1077 return tuple;
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1078 }
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1079
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1080
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1081 void nn_strtuple_free(nn_strtuple_t *tuple)
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1082 {
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1083 th_free(tuple->str);
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1084 th_free(tuple);
8e509d6546d3 Move nn_strtuple_* functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
1085 }