Merge upstream.
[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 /**
24  * @file
25  * @brief Network functions
26  */
27
28 #ifndef G_MODULE_EXPORT
29 #define G_MODULE_EXPORT 
30 #endif
31
32 #include "state.h"
33 #include "isupport.h"
34 #include <sys/socket.h>
35
36 struct global;
37 struct network;
38 struct client;
39 struct line;
40 struct linestack_context;
41
42 enum network_connection_state { 
43                 NETWORK_CONNECTION_STATE_NOT_CONNECTED = 0, 
44                 NETWORK_CONNECTION_STATE_RECONNECT_PENDING,
45                 NETWORK_CONNECTION_STATE_LOGIN_SENT, 
46                 NETWORK_CONNECTION_STATE_MOTD_RECVD,
47 };
48
49 /**
50  * Information about the connection to a network.
51  */
52 struct network_connection {
53         enum network_connection_state state;
54
55         time_t last_line_sent;
56         time_t last_line_recvd;
57         GQueue *pending_lines;
58
59         GIOChannel *outgoing;
60         gint outgoing_id;
61         gint incoming_id;
62         GIConv outgoing_iconv;
63         GIConv incoming_iconv;
64
65         union { 
66                 struct {
67                         struct tcp_server_config *current_server;
68                         struct sockaddr *remote_name;
69                         struct sockaddr *local_name;
70                         socklen_t namelen;
71                         char *last_disconnect_reason;
72                         gint ping_id;
73                 } tcp;
74                 
75                 struct {
76                         void *private_data;
77                         struct virtual_network_ops {
78                                 char *name;
79                                 gboolean (*init) (struct network *);
80                                 gboolean (*to_server) (struct network *, struct client *c, const struct line *);
81                                 gboolean (*fini) (struct network *);
82                         } *ops;
83                 } virtual;
84         } data;
85 };
86
87 /**
88  * An IRC network
89  */
90 struct network {
91         char *name;
92         struct global *global;
93         struct network_config *config;
94
95         GList *clients;
96         guint reconnect_id;
97
98         gpointer ssl_credentials;
99
100         struct network_state *state;
101         struct network_info info;
102         struct network_connection connection;
103         struct linestack_context *linestack;
104 };
105
106 /* server.c */
107 G_MODULE_EXPORT gboolean network_set_charset(struct network *n, const char *name);
108 G_MODULE_EXPORT struct network *find_network_by_hostname(struct global *global, const char *host, guint16 port, gboolean create);
109 G_MODULE_EXPORT gboolean load_networks(struct global *, struct ctrlproxy_config *cfg);
110 G_MODULE_EXPORT gboolean autoconnect_networks(struct global *);
111 G_MODULE_EXPORT struct network *load_network(struct global *, struct network_config *);
112 G_MODULE_EXPORT void unload_network(struct network *);
113 G_MODULE_EXPORT gboolean connect_network(struct network *);
114 G_MODULE_EXPORT void network_select_next_server(struct network *n);
115 G_MODULE_EXPORT gboolean disconnect_network(struct network *s);
116 G_MODULE_EXPORT void clients_send(GList *clients, struct line *, const struct client *exception);
117 G_MODULE_EXPORT void clients_send_args_ex(GList *clients, const char *hostmask, ...);
118 G_MODULE_EXPORT gboolean network_send_line(struct network *s, struct client *c, const struct line *, gboolean);
119 G_MODULE_EXPORT gboolean network_send_args(struct network *s, ...);
120 G_MODULE_EXPORT void register_virtual_network(struct virtual_network_ops *);
121 G_MODULE_EXPORT struct network *find_network(struct global *, const char *);
122 G_MODULE_EXPORT gboolean virtual_network_recv_line(struct network *l, struct line *);
123 G_MODULE_EXPORT gboolean virtual_network_recv_args(struct network *l, const char *origin, ...); 
124 G_MODULE_EXPORT gboolean virtual_network_recv_response(struct network *n, int num, ...);
125 typedef void (*new_network_notify_fn) (struct network *, void *);
126 G_MODULE_EXPORT void register_new_network_notify(struct global *, new_network_notify_fn, void *userdata);
127 G_MODULE_EXPORT struct linestack_context *new_linestack(struct network *network);
128 char *network_generate_feature_string(struct network *n);
129
130 #endif /* __CTRLPROXY_NETWORK_H__ */