#! /bin/sh /usr/share/dpatch/dpatch-run
## 99-unnamed.dpatch by Loic Minier <lool@dooz.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: No description.

@DPATCH@
--- gtranslator-1.1.5.orig/src/header_stuff.c
+++ gtranslator-1.1.5/src/header_stuff.c
@@ -146,15 +146,43 @@
 		if_key_is("Content-Type") {
 			gchar *prefix;
 			
-			prefix = strstr (pair[1], "text/plain; charset=");
-			if (prefix)
-				ph->charset = g_strdup (prefix + 20);
+			/* check whether we've got "text/plain" */
+			prefix = strstr (pair[1], "text/plain");
+			if ((! prefix) || prefix != pair[1])
+			{
+				g_warning("Content-Type doesn't start with text/plain (got '%s')", pair[1]);
+			}
+			/* search for a "charset=" parameter */
+			/* (note that we're cool since we allow other params
+			 * to be passed too!) */
+			prefix = strstr (pair[1], "; charset=");
+			if (prefix) {
+				/* we should check for allowed charsets --
+				 * a complete list can be found in section 5.2,
+				 * Filling in the Header Entry, in the gettext
+				 * manual -- and default to detecting the
+				 * encoding */
+				ph->charset = g_strdup (prefix + 10);
+			}
 			else
-				ph->charset = g_strdup (pair[1]);
+			{
+				/* no "charset=" parameter
+				 * gtranslator used to default to pair[1].
+				 * The best thing to do would be to detect
+				 * the encoding, but frankly people should
+				 * simply put a charset */
+				g_warning("Content-Type has no charset parameter (got '%s'), defaulting to UTF-8", pair[1]);
+				/* we g_strdup() it so it can be freed
+				 * transparently */
+				ph->charset = g_strdup ("UTF-8");
+			}
 		}
 		else
 		if_key_is("Content-Transfer-Encoding")
-		    ph->encoding = g_strdup(pair[1]);
+		{
+			/* we should warn for anything else than "8-bit" */
+			ph->encoding = g_strdup(pair[1]);
+		}
 		else
 		if_key_is("Plural-Forms")
