[merge] trunk
[jelmer/ctrlproxy.git] / src / network.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 2 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 #ifndef G_MODULE_EXPORT
24 #define G_MODULE_EXPORT 
25 #endif
26
27 #include "state.h"
28
29 struct global;
30 struct network;
31 struct client;
32 struct line;
33
34 enum network_connection_state { 
35                 NETWORK_CONNECTION_STATE_NOT_CONNECTED = 0, 
36                 NETWORK_CONNECTION_STATE_RECONNECT_PENDING,
37                 NETWORK_CONNECTION_STATE_CONNECTED,
38                 NETWORK_CONNECTION_STATE_LOGIN_SENT, 
39                 NETWORK_CONNECTION_STATE_MOTD_RECVD,
40 };
41
42 struct network_connection {
43         enum network_connection_state state;
44
45         time_t last_line_sent;
46         GQueue *pending_lines;
47         gint queue_send_id;
48
49         union { 
50                 struct {
51                         struct tcp_server_config *current_server;
52                         struct sockaddr *remote_name;
53                         struct sockaddr *local_name;
54                         size_t namelen;
55                         GIOChannel *outgoing;
56                         gint outgoing_id;
57                 } tcp;
58                 
59                 struct {
60                         GIOChannel *outgoing;
61                         gint outgoing_id;
62                 } program;
63
64                 struct {
65                         void *private_data;
66                         struct virtual_network_ops {
67                                 char *name;
68                                 gboolean (*init) (struct network *);
69                                 gboolean (*to_server) (struct network *, struct client *c, struct line *);
70                                 gboolean (*fini) (struct network *);
71                         } *ops;
72                 } virtual;
73         } data;
74 };
75
76 struct network {
77         char *name;
78         struct global *global;
79         struct network_config *config;
80
81         GList *clients;
82         guint reconnect_id;
83
84         struct network_state *state;
85         struct network_info info;
86         struct network_connection connection;
87 };
88
89 /* server.c */
90 G_MODULE_EXPORT struct network *find_network_by_hostname(struct global *global, const char *host, guint16 port, gboolean create);
91 G_MODULE_EXPORT gboolean load_networks(struct global *, struct ctrlproxy_config *cfg);
92 G_MODULE_EXPORT gboolean autoconnect_networks(struct global *);
93 G_MODULE_EXPORT struct network *load_network(struct global *, struct network_config *);
94 G_MODULE_EXPORT void unload_network(struct network *);
95 G_MODULE_EXPORT gboolean connect_network(struct network *);
96 G_MODULE_EXPORT void network_select_next_server(struct network *n);
97 G_MODULE_EXPORT gboolean disconnect_network(struct network *s);
98 G_MODULE_EXPORT void clients_send(struct network *, struct line *, const struct client *exception);
99 G_MODULE_EXPORT gboolean network_send_line(struct network *s, struct client *c, const struct line *);
100 G_MODULE_EXPORT gboolean network_send_args(struct network *s, ...);
101 G_MODULE_EXPORT void register_virtual_network(struct virtual_network_ops *);
102 G_MODULE_EXPORT struct network *find_network(struct global *, const char *);
103 G_MODULE_EXPORT gboolean virtual_network_recv_line(struct network *l, struct line *);
104 G_MODULE_EXPORT gboolean virtual_network_recv_args(struct network *l, const char *origin, ...); 
105 typedef void (*new_network_notify_fn) (struct network *, void *);
106 G_MODULE_EXPORT void register_new_network_notify(struct global *, new_network_notify_fn, void *userdata);
107
108 #endif /* __CTRLPROXY_NETWORK_H__ */