2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
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 * Please read the file COPYING, README and AUTHORS for more information.
12 #define __conn_encoding_c__
20 * Functions to deal with character encodings and conversions
33 #include "conn-encoding.h"
37 char Encoding_Buffer[COMMAND_LEN];
39 char *Convert_Message PARAMS((iconv_t Handle, char *Message));
42 * Set client character encoding on a connection.
44 * @param Conn Connection identifier.
45 * @param ClientEnc Client encoding (for example "ASCII", "MacRoman", ...).
46 * @return true on success, false otherwise.
49 Conn_SetEncoding(CONN_ID Conn, const char *ClientEnc)
51 char client_enc[25], server_enc[25];
54 assert(ClientEnc != NULL);
56 Conn_UnsetEncoding(Conn);
58 /* Is the client character set identical to server character set? */
59 if (strcasecmp(ClientEnc, "UTF-8") == 0)
62 snprintf(client_enc, sizeof(client_enc), "%s//TRANSLIT", ClientEnc);
63 snprintf(server_enc, sizeof(server_enc), "%s//TRANSLIT", "UTF-8");
65 My_Connections[Conn].iconv_from = iconv_open(server_enc, client_enc);
66 if (My_Connections[Conn].iconv_from == (iconv_t)(-1)) {
67 Conn_UnsetEncoding(Conn);
70 My_Connections[Conn].iconv_to = iconv_open(client_enc, server_enc);
71 if (My_Connections[Conn].iconv_to == (iconv_t)(-1)) {
72 Conn_UnsetEncoding(Conn);
76 LogDebug("Set client character set of connection \"%d\" to \"%s\".",
82 * Remove client character encoding conversion on a connection.
84 * @param Conn Connection identifier.
87 Conn_UnsetEncoding(CONN_ID Conn)
91 if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
92 iconv_close(My_Connections[Conn].iconv_from);
93 if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
94 iconv_close(My_Connections[Conn].iconv_to);
96 My_Connections[Conn].iconv_from = (iconv_t)(-1);
97 My_Connections[Conn].iconv_to = (iconv_t)(-1);
99 LogDebug("Unset character conversion of connection %d.", Conn);
103 * Convert the encoding of a given message.
105 * This function uses a static buffer for the result of the encoding
106 * conversion which is overwritten by subsequent calls to this function!
108 * @param Handle libiconv handle.
109 * @param Message The message to convert.
110 * @return Pointer to the result.
113 Convert_Message(iconv_t Handle, char *Message)
115 size_t in_left, out_left;
116 char *out = Encoding_Buffer;
118 assert (Handle != (iconv_t)(-1));
119 assert (Message != NULL);
121 in_left = strlen(Message);
122 out_left = sizeof(Encoding_Buffer) - 1;
124 if (iconv(Handle, &Message, &in_left, &out, &out_left) == (size_t)(-1)) {
125 /* An error occurred! */
126 LogDebug("Error converting message encoding!");
127 strlcpy(out, Message, sizeof(Encoding_Buffer));
128 iconv(Handle, NULL, NULL, NULL, NULL);
132 return Encoding_Buffer;
138 * Convert encoding of a message received from a connection.
140 * Note 1: If no conversion is required, this function returns the original
141 * pointer to the message.
143 * Note 2: This function uses Convert_Message(), so subsequent calls to this
144 * function will overwrite the earlier results.
146 * @param Conn Connection identifier.
147 * @param Message The message to convert.
148 * @return Pointer to the result.
149 * @see Convert_Message
152 Conn_EncodingFrom(UNUSED CONN_ID Conn, char *Message)
155 assert (Message != NULL);
158 if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
159 return Convert_Message(My_Connections[Conn].iconv_from, Message);
165 * Convert encoding of a message for sending on a connection.
167 * Note 1: If no conversion is required, this function returns the original
168 * pointer to the message.
170 * Note 2: This function uses Convert_Message(), so subsequent calls to this
171 * function will overwrite the earlier results.
173 * @param Conn Connection identifier.
174 * @param Message The message to convert.
175 * @return Pointer to the result.
176 * @see Convert_Message
179 Conn_EncodingTo(UNUSED CONN_ID Conn, char *Message)
182 assert (Message != NULL);
185 if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
186 return Convert_Message(My_Connections[Conn].iconv_to, Message);