annotate libnnchat.h @ 266:9bf3e5620eb5

Include sys/select.h
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 03 Jun 2011 11:24:36 +0300
parents 7ae0719f7763
children 1211b740fbda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Written by Matti 'ccr' Hämäläinen
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
4 * (C) Copyright 2008-2010 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 #ifndef LIBNNCHAT_H
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #define LIBNNCHAT_H
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <stdio.h>
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include <unistd.h>
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
11 #ifdef __WIN32
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 65
diff changeset
12 #include <windows.h>
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
13 #include <winsock.h>
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
14 #else
266
9bf3e5620eb5 Include sys/select.h
Matti Hamalainen <ccr@tnsp.org>
parents: 224
diff changeset
15 #include <sys/select.h>
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 #include <sys/socket.h>
217
7ba4e371e9a6 Add Makefile for OpenBSD, and specific #ifdef'd includes for the platform.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
17 #ifdef HAVE_NETINET_IN_H
7ba4e371e9a6 Add Makefile for OpenBSD, and specific #ifdef'd includes for the platform.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
18 #include <netinet/in.h>
7ba4e371e9a6 Add Makefile for OpenBSD, and specific #ifdef'd includes for the platform.
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
19 #endif
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 #include <arpa/inet.h>
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 #include <netdb.h>
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
22 #endif
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 62
diff changeset
23 #include <sys/types.h>
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
24 #include <time.h>
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 #include <errno.h>
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 #include "th_string.h"
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
28 #define NN_TMPBUF_SIZE (4096)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
29 #define NN_ALLOC_SIZE (128)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
30 #define NN_CONNBUF_SIZE (64 * 1024)
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
31 #define NN_NUM_BUCKETS (256)
224
7ae0719f7763 Adjust select() timeout.
Matti Hamalainen <ccr@tnsp.org>
parents: 217
diff changeset
32 #define NN_DELAY_USEC (15 * 1000)
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
33
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
34 typedef struct {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
35 char **data;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
36 size_t n, size;
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
37 } nn_ringbuf_t;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
38
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
39
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
40 typedef struct {
71
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
41 ssize_t pos, len, size;
3a23c2adfb78 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 68
diff changeset
42 char *data;
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
43 } nn_editbuf_t;
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
44
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
45
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
46 typedef struct _nn_user_t {
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
47 char *name;
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
48 time_t lastspoke, joined;
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
49 struct _nn_user_t *next;
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
50 } nn_user_t;
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
51
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
52
94
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
53
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
54 typedef struct {
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
55 nn_user_t *buckets[NN_NUM_BUCKETS];
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
56 } nn_userhash_t;
6e47426efb6a Add preliminary userlist data handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 91
diff changeset
57
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
58 enum {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
59 NN_CONN_UNINIT = 0,
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
60 NN_CONN_OPEN,
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
61 NN_CONN_CLOSED
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
62 };
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
63
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
64 typedef struct _nn_conn_t {
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
65 int socket;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
66 int port;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
67 struct in_addr address;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
68 fd_set sockfds;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
69
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
70 void (*errfunc)(struct _nn_conn_t *conn, const char *fmt, va_list ap);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
71 void (*msgfunc)(struct _nn_conn_t *conn, const char *fmt, va_list ap);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
72
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
73 int err;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
74 int status;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
75
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
76 char buf[NN_CONNBUF_SIZE + 16];
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
77 char *ptr;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
78 ssize_t got;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
79 } nn_conn_t;
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
80
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
81
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
82 const char *nn_get_errstr(int err);
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
83 BOOL nn_network_init(void);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
84 void nn_network_close(void);
90
1e0bf7b4fd41 Move socket error handling functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 89
diff changeset
85
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
86 nn_conn_t * nn_conn_open(struct in_addr *addr, const int port);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
87 void nn_conn_close(nn_conn_t *);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
88 BOOL nn_conn_pull(nn_conn_t *);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
89 BOOL nn_conn_send_buf(nn_conn_t *, const char *buf, const size_t len);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
90 BOOL nn_conn_send_msg(nn_conn_t *, const char *user, const char *fmt, ...);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
91 BOOL nn_conn_check(nn_conn_t *);
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
92
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
94 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
95 nn_user_t * nn_userhash_foreach(const nn_userhash_t *, int (*func)(const nn_user_t *));
116
741e45592522 Add simple "prediction" into tab-completion based on previously gotten last
Matti Hamalainen <ccr@tnsp.org>
parents: 106
diff changeset
96 nn_user_t * nn_user_match(const nn_userhash_t *list, const char *str, const char *current, BOOL again);
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
97 int nn_userhash_insert(nn_userhash_t *, const char *name);
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
98 int nn_userhash_delete(nn_userhash_t *, 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
99 void nn_userhash_free(nn_userhash_t *);
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
100 void nn_user_free(nn_user_t *);
102
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
101 void nn_user_free_list(nn_user_t *);
b096ae97fc7d Renamed functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 97
diff changeset
102 nn_user_t * nn_user_copy(const nn_user_t *src);
106
c587a99e2096 Drop internal use and storage of encoded usernames.
Matti Hamalainen <ccr@tnsp.org>
parents: 103
diff changeset
103 nn_user_t * nn_user_find(const nn_userhash_t *list, const char *name);
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
105
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
106 char * nn_encode_str1(const char *str);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
107 char * nn_decode_str1(const char *str);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
108 char * nn_encode_str2(const char *str);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
109 char * nn_decode_str2(const char *str);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
110 char * nn_strip_tags(const char *str);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
111 char * nn_dbldecode_str(const char *str);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
112 char * nn_dblencode_str(const char *str);
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
113
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
114
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
115 nn_ringbuf_t * nn_ringbuf_new(const size_t size);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
116 void nn_ringbuf_free(nn_ringbuf_t *buf);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
117 void nn_ringbuf_add(nn_ringbuf_t *buf, const char *str);
65
e763ef5cfd53 Move more functions to libnnchat.
Matti Hamalainen <ccr@tnsp.org>
parents: 64
diff changeset
118
168
2e4850ece456 Partially re-factor connection handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 116
diff changeset
119
97
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
120 int nn_editbuf_write(nn_editbuf_t *buf, ssize_t pos, int ch);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
121 int nn_editbuf_insert(nn_editbuf_t *buf, ssize_t pos, int ch);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
122 int nn_editbuf_delete(nn_editbuf_t *buf, ssize_t pos);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
123 void nn_editbuf_clear(nn_editbuf_t *buf);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
124 nn_editbuf_t * nn_editbuf_new(ssize_t n);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
125 void nn_editbuf_free(nn_editbuf_t *buf);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
126 nn_editbuf_t * nn_editbuf_copy(nn_editbuf_t *src);
218efd2f0641 Rename functions for clarity.
Matti Hamalainen <ccr@tnsp.org>
parents: 94
diff changeset
127 void nn_editbuf_setpos(nn_editbuf_t *buf, ssize_t pos);
103
eaa524e153f9 Initial implementation of functions for implementing tab-completion for user names.
Matti Hamalainen <ccr@tnsp.org>
parents: 102
diff changeset
128 char * nn_editbuf_get_string(nn_editbuf_t *buf, ssize_t start, ssize_t end);
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 #endif