summaryrefslogtreecommitdiff
path: root/net/centericq/files/patch-msn_commands.cc
blob: 00838c3e63cb5ccfcb36bf37b9c42bd50ddaaa9f (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
--- libmsn-0.1/msn_commands.cc.orig	Wed Oct 16 18:20:59 2002
+++ libmsn-0.1/msn_commands.cc	Wed Oct 30 16:44:55 2002
@@ -30,7 +30,10 @@
 #include <sys/socket.h>
 #include <errno.h>
 #include <netdb.h>
-
+#define HAVE_ICONV_H 1
+#if HAVE_ICONV_H
+#include <iconv.h>
+#endif
 #include "libmsn.h"
 #include "msn_commands.h"
 #include "parse_utils.h"
@@ -426,6 +429,128 @@
     return 0;
 }
 
+#if HAVE_ICONV_H
+/* 
+** Name:    safe_iconv
+** Purpose: 'Fault-tolerant' version if iconv. Replaces invalid seq with '?'
+** Input:   see iconv manpage
+*/
+static int safe_iconv( iconv_t handle,
+	     const char **inbuf, size_t *inbytesleft,
+	     char **outbuf, size_t *outbytesleft)
+{
+    int ret;
+    while (*inbytesleft) {
+	 ret = iconv( handle, inbuf, inbytesleft,
+		outbuf, outbytesleft);
+	if (!*inbytesleft || !*outbytesleft)
+	    return ret;
+	/*got invalid seq - so replace it with '?' */
+	**outbuf = '?'; (*outbuf)++; (*outbytesleft)--;
+	(*inbuf)++; (*inbytesleft)--;
+    }
+    return ret;
+}
+
+/* charset name cache buffer */
+char loc_charset[32];
+
+#define DEFAULT_CHARSET "ISO-8859-1"
+
+/*
+** Name:    guess_current_locale_charset
+** Purpose: Try to guess default charset for the current locale
+** Output:  charset name
+** FIXME:   is there more right method for guessing charset
+	    than scanning $LANG ?
+*/
+static char* guess_current_locale_charset()
+{
+    char *lang, *ch;
+    /* Return previously learned charset */
+    return "big5";
+    if (loc_charset[0])
+	return loc_charset;
+
+    lang = getenv("LANG");
+    ch = strrchr( lang, '.' ) + 1;
+    if (!ch)
+	strcpy( loc_charset, DEFAULT_CHARSET );
+    else {
+	iconv_t pt;
+	strncpy( loc_charset, ch, sizeof(loc_charset) );
+	/* try to open iconv handle using guessed charset */
+	if ( (pt = iconv_open( loc_charset, loc_charset )) == (iconv_t)(-1) )
+	{
+	    strcpy( loc_charset, DEFAULT_CHARSET );
+	} else {
+	    iconv_close(pt);
+	};
+	
+    }
+
+    return loc_charset;
+}
+#endif /* HAVE_ICONV_H */
+
+/*
+** Name:    Str2Utf8
+** Purpose: convert a string in UTF-8 format
+** Input:   inbuf     - the string to convert
+** Output:  a new string in UTF-8 format
+*/
+static char *StrToUtf8( const char *inbuf )
+{
+#if HAVE_ICONV_H
+    size_t length = strlen( inbuf );
+    size_t outmaxlength = length * 4; /* FIXME: Is x4 multiplier enoght? */
+    char *outbuf = (char*) malloc( outmaxlength + 1 );
+    char *outbuf_save = outbuf;
+    int ret;
+
+    iconv_t handle = iconv_open( "utf-8", guess_current_locale_charset() );
+    ret = safe_iconv( handle, (const char **) &inbuf, &length, &outbuf, &outmaxlength );
+    
+    iconv_close( handle );
+
+    return outbuf_save;
+#else
+    char *outbuf = strdup( inbuf );
+    char *outbuf_save = outbuf;
+    /* Clear eight bit */
+    for (; *outbuf; ++outbuf)
+        *outbuf &= 0x7F;
+    return outbuf_save;
+#endif
+}
+ 
+/*
+** Name:    Utf8ToStr
+** Purpose: revert UTF-8 string conversion
+** Input:   inbuf  - the string to decode
+** Output:  a new decoded string
+*/
+static char *Utf8ToStr( char *inbuf )
+{
+#if HAVE_ICONV_H
+    size_t length = strlen( inbuf );
+    size_t outmaxlength = length;
+    char *outbuf = (char*) malloc( outmaxlength + 1 );
+    char *outbuf_save = outbuf;
+    int ret;
+
+    iconv_t handle = iconv_open( guess_current_locale_charset(), "utf-8" );
+
+    ret = safe_iconv( handle, (const char **) &inbuf, &length, &outbuf, &outmaxlength );
+    
+    iconv_close( handle );
+
+    return outbuf_save;
+#else
+    return strdup( inbuf );
+#endif
+}
+
 /*
 ** Name:    HandleMessage
 ** Purpose: This function handles an instant message from either the server
@@ -440,6 +565,7 @@
 {
     MSN_InstantMessage newIm;
     char *message;
+    char *decodedIm;
     char *mimeInfo, *im;
     int  length, nread; 
     struct timeval t;
@@ -448,6 +574,8 @@
     message = NULL;
     mimeInfo = NULL;
     im = NULL; 
+    decodedIm = NULL;
+
 	
     if (numOfArgs != 4)
 	return -1;
@@ -470,13 +598,15 @@
  
     if (mimeInfo != NULL) {
 	if (strstr(mimeInfo, "text/plain") != NULL) {
+
+	    decodedIm = Utf8ToStr( im );
 	    newIm.year = 0;
 	    newIm.month = 0;
 	    newIm.day = 0;
 	    newIm.hour = 0;
 	    newIm.minute = 0;
 	    newIm.sec = 0;
-	    newIm.msg = im;
+	    newIm.msg = decodedIm;
 	    RemoveHotmail(args[1], &newIm.sender);
 	    newIm.friendlyhandle = args[2];
 	    newIm.fd = conn->fd;
@@ -571,15 +701,18 @@
 int SendMessage(MSN_Conn *conn, const char *message)
 {
     char   *commandLine;
+    char   *UTFmessage;
     int    length;
 
     if (!message)
 	return -1;
+    UTFmessage = NULL;
 
-    commandLine = (char *)malloc(strlen(MIME_HEADER)+strlen(message)+25);
+    UTFmessage = StrToUtf8( message );
+    commandLine = (char *)malloc(strlen(MIME_HEADER)+strlen(UTFmessage)+25);
     length = sprintf(commandLine, "%s %lu N %d\r\n%s%s", CommandString[MSG],
-		     TrID++, strlen(message)+strlen(MIME_HEADER), MIME_HEADER, 
-		     message);
+		     TrID++, strlen(UTFmessage)+strlen(MIME_HEADER), MIME_HEADER, 
+		     UTFmessage);
 
     write(conn->fd, commandLine, length);
     free(commandLine);