~ctrlproxy/ctrlproxy/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
	ctrlproxy: A modular IRC proxy
	(c) 2002-2008 Jelmer Vernooij <jelmer@nl.linux.org>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "internals.h"
#include "transport.h"
#include "line.h"
#include "util.h"
#include <glib.h>
#include <sys/socket.h>
#include <ctype.h>
#include <fcntl.h>
#include <netdb.h>

void irc_transport_disconnect(struct irc_transport *transport)
{
	if (!transport->backend_ops->is_connected(transport->backend_data))
		return; /* We're already disconnected */

	transport->backend_ops->disconnect(transport->backend_data);
	transport->callbacks->disconnect(transport);
}

void free_irc_transport(struct irc_transport *transport)
{
	transport->backend_ops->free_data(transport->backend_data);
	transport->backend_data = NULL;

	g_free(transport->charset);

	g_free(transport);
}

/**
 * Change the character set used to send data to a client
 * @param c client to change the character set for
 * @param name name of the character set to change to
 * @return whether changing the character set succeeded
 */
gboolean transport_set_charset(struct irc_transport *transport, const char *name)
{
	gboolean ret;

	ret = transport->backend_ops->set_charset(transport, name);
	if (ret == FALSE)
		return ret;

	g_free(transport->charset);
	transport->charset = g_strdup(name);

	return TRUE;
}

gboolean transport_send_line(struct irc_transport *transport, 
							 const struct irc_line *l, GError **error)
{
	if (!transport->backend_ops->is_connected(transport->backend_data)) {
		g_set_error_literal(error, IRC_TRANSPORT_ERROR, IRC_TRANSPORT_ERROR_DISCONNECTED,
							"Transport is disconnected");
		return FALSE;
	}

	return transport->backend_ops->send_line(transport, l, error);
}

gboolean transport_send_response(struct irc_transport *transport, GError **error, const char *from, const char *to, int response, ...) 
{
	struct irc_line *l;
	gboolean ret;
	va_list ap;

	g_assert(transport != NULL);

	va_start(ap, response);
	l = virc_parse_response(from, to, response, ap);
	va_end(ap);

	ret = transport_send_line(transport, l, error);

	free_line(l); 

	return ret;
}

gboolean transport_send_args(struct irc_transport *transport, GError **error, ...) 
{
	struct irc_line *l;
	gboolean ret;
	va_list ap;

	g_assert(transport != NULL);

	va_start(ap, error);
	l = virc_parse_line(NULL, ap);
	va_end(ap);

	ret = transport_send_line(transport, l, error);

	free_line(l); 

	return ret;
}

char *transport_get_peer_hostname(struct irc_transport *transport)
{
	return transport->backend_ops->get_peer_name(transport->backend_data);
}

void irc_transport_set_callbacks(struct irc_transport *transport, const struct irc_transport_callbacks *callbacks, void *userdata)
{
	transport->userdata = userdata;
	transport->callbacks = callbacks;

	transport->backend_ops->activate(transport);
}

GQuark irc_transport_error_quark(void)
{
	return g_quark_from_static_string("irc-transport-error-quark");
}