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