Provide access to more Network attributes.
[jelmer/ctrlproxy.git] / libirc / connection.h
1 /*
2         ctrlproxy: A modular IRC proxy
3         (c) 2002-2005 Jelmer Vernooij <jelmer@nl.linux.org>
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 3 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the Free Software
17         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __CTRLPROXY_NETWORK_H__
21 #define __CTRLPROXY_NETWORK_H__
22
23 /**
24  * @file
25  * @brief Network functions
26  */
27
28 #include <glib.h>
29
30 #ifndef G_MODULE_EXPORT
31 #define G_MODULE_EXPORT 
32 #endif
33
34 #include "state.h"
35 #include "isupport.h"
36 #include <sys/socket.h>
37
38 #define DEFAULT_NETWORK_CHARSET NULL
39
40 struct global;
41 struct irc_network;
42 struct irc_client;
43 struct irc_line;
44 struct linestack_context;
45
46 enum irc_network_connection_state { 
47                 NETWORK_CONNECTION_STATE_NOT_CONNECTED = 0, 
48                 NETWORK_CONNECTION_STATE_RECONNECT_PENDING,
49                 NETWORK_CONNECTION_STATE_CONNECTING,
50                 NETWORK_CONNECTION_STATE_CONNECTED,
51                 NETWORK_CONNECTION_STATE_LOGIN_SENT, 
52                 NETWORK_CONNECTION_STATE_MOTD_RECVD,
53 };
54
55 /**
56  * Information about the connection to a network.
57  */
58 struct irc_network_connection {
59         enum irc_network_connection_state state;
60
61         /** Time the last line was sent to this network. */
62         time_t last_line_sent;
63         /** Time the last line was received from this network. */
64         time_t last_line_recvd;
65
66         struct irc_transport *transport;
67         union { 
68                 struct {
69                         /** Configuration for TCP/IP server currently connected to. */
70                         struct tcp_server_config *current_server;
71                         /** Name of remote server. */
72                         struct sockaddr *remote_name;
73                         /** Name of local host used for connection. */
74                         struct sockaddr *local_name;
75                         /** Socket name length for remote_name and local_name. */
76                         socklen_t namelen;
77                         /** Last reason for disconnect. */
78                         char *last_disconnect_reason;
79                         /** Source ID for function that regularly pings the network. */
80                         gint ping_id;
81                         /** Source ID for function that finishes connect. */
82                         gint connect_id;
83                 } tcp;
84                 
85                 struct {
86                         void *private_data;
87                         struct virtual_network_ops {
88                                 char *name;
89                                 gboolean not_disconnectable;
90                                 gboolean (*init) (struct irc_network *);
91                                 gboolean (*to_server) (struct irc_network *, struct irc_client *c, const struct irc_line *);
92                                 void (*fini) (struct irc_network *);
93                         } *ops;
94                 } virtual;
95         } data;
96 };
97
98 typedef void (*network_log_fn) (enum log_level, const struct irc_network *, const char *);
99
100 struct irc_network_callbacks {
101         network_log_fn log;
102         struct irc_login_details *(*get_login_details) (struct irc_network *);
103         gboolean (*process_from_server) (struct irc_network *, const struct irc_line *);
104         gboolean (*process_to_server) (struct irc_network *, const struct irc_line *);
105         void (*disconnect) (struct irc_network *);
106         void (*state_set) (struct irc_network *);
107 };
108
109 /**
110  * An IRC network
111  */
112 struct irc_network {
113         char *name;
114
115         /** Global data. */
116         struct global *global;
117
118         /** Private data */
119         void *private_data;
120
121         /** Number of references that exist to this network. */
122         int references;
123
124         /** Clients connected to this network.*/
125         GList *clients;
126         guint reconnect_id;
127
128         gpointer ssl_credentials;
129
130         int reconnect_interval;
131
132         /** External network state, when connected. */
133         struct irc_network_state *external_state;
134
135         /** Internal network state, as used by clients */
136         struct irc_network_state *internal_state;
137
138         /** Network information. */
139         struct irc_network_info *info;
140
141         struct irc_network_connection connection;
142         
143         /** Linestack context. */
144         struct linestack_context *linestack;
145
146         /** How many linestack errors have occurred so far */
147         guint linestack_errors;
148
149         const struct irc_network_callbacks *callbacks;
150
151         struct query_stack *queries;
152 };
153
154 /* server.c */
155 G_MODULE_EXPORT gboolean network_set_charset(struct irc_network *n, const char *name);
156 G_MODULE_EXPORT gboolean autoconnect_networks(GList *);
157 G_MODULE_EXPORT struct irc_network *irc_network_new(const struct irc_network_callbacks *callbacks, void *private_data);
158 G_MODULE_EXPORT gboolean connect_network(struct irc_network *);
159 G_MODULE_EXPORT void irc_network_select_next_server(struct irc_network *n);
160 G_MODULE_EXPORT gboolean disconnect_network(struct irc_network *s);
161 G_MODULE_EXPORT gboolean network_send_line(struct irc_network *s, struct irc_client *c, const struct irc_line *);
162 G_MODULE_EXPORT gboolean network_send_args(struct irc_network *s, ...);
163 G_MODULE_EXPORT void register_virtual_network(struct virtual_network_ops *);
164 G_MODULE_EXPORT struct virtual_network_ops *find_virtual_network(const char *name);
165 G_MODULE_EXPORT struct irc_network *find_network(GList *gl, const char *);
166 G_MODULE_EXPORT gboolean virtual_network_recv_line(struct irc_network *l, struct irc_line *);
167 G_MODULE_EXPORT gboolean virtual_network_recv_args(struct irc_network *l, const char *origin, ...); 
168 G_MODULE_EXPORT gboolean virtual_network_recv_response(struct irc_network *n, int num, ...);
169 struct ctrlproxy_config;
170 G_MODULE_EXPORT G_GNUC_MALLOC struct linestack_context *new_linestack(struct irc_network *network, struct ctrlproxy_config *settings);
171 G_MODULE_EXPORT G_GNUC_MALLOC char *network_generate_feature_string(struct irc_network *n);
172 G_MODULE_EXPORT struct irc_network *irc_network_ref(struct irc_network *);
173 G_MODULE_EXPORT void irc_network_unref(struct irc_network *);
174 G_MODULE_EXPORT void unregister_virtual_networks(void);
175
176 #endif /* __CTRLPROXY_NETWORK_H__ */