From d6d351869ebb144ec812d6d838e9dd7fe24f43a3 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Wed, 16 Nov 2011 17:51:42 +0100
Subject: [PATCH] use proxy if needed in google translation plugin. Fixes #7042

---
 plugins/google_translation/plugin.py | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)

diff --git a/plugins/google_translation/plugin.py b/plugins/google_translation/plugin.py
index d00d1b993a..4ff001e78c 100644
--- a/plugins/google_translation/plugin.py
+++ b/plugins/google_translation/plugin.py
@@ -116,11 +116,7 @@ class GoogleTranslationPlugin(GajimPlugin):
 
         self.config_default_values = {
             'per_jid_config': ({}, ''),
-            'user_agent' :
-                (u'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) '
-                'Gecko/20080213 Firefox/2.0.0.11',
-                u'User Agent data to be used with urllib2 '
-                'when connecting to Google Translate service')}
+        }
 
         self.events_handlers = {'decrypted-message-received': (ged.PREGUI,
             self._nec_decrypted_message_received)}
@@ -136,25 +132,20 @@ class GoogleTranslationPlugin(GajimPlugin):
             '{"translatedText":"(?P<text>[^"]*)"}, 200, null, 200\)')
 
     @log_calls('GoogleTranslationPlugin')
-    def translate_text(self, text, from_lang, to_lang):
+    def translate_text(self, account, text, from_lang, to_lang):
         # Converts text so it can be used within URL as query to Google
         # Translate.
         quoted_text = urllib2.quote(text.encode(getfilesystemencoding()))
         # prepare url
-        headers = { 'User-Agent' : self.config['user_agent'] }
         translation_url = u'http://www.google.com/uds/Gtranslate?callback='\
             'google.language.callbacks.id100&context=22&q=%(quoted_text)s&'\
             'langpair=%(from_lang)s%%7C%(to_lang)s&key=notsupplied&v=1.0' % \
             locals()
-        request = urllib2.Request(translation_url, headers=headers)
 
-        try:
-            response = urllib2.urlopen(request)
-        except urllib2.URLError, e:
-            # print e
+        results = helpers.download_image(account, {'src': translation_url})[0]
+        if not results:
             return text
 
-        results = response.read()
         translated_text = self.translated_text_re.search(results).group('text')
 
         if translated_text:
@@ -177,7 +168,8 @@ class GoogleTranslationPlugin(GajimPlugin):
             return
         from_lang = self.config['per_jid_config'][obj.jid]['from']
         to_lang = self.config['per_jid_config'][obj.jid]['to']
-        translated_text = self.translate_text(obj.msgtxt, from_lang, to_lang)
+        translated_text = self.translate_text(obj.conn.name, obj.msgtxt,
+            from_lang, to_lang)
         if translated_text:
             obj.msgtxt = translated_text + '\n/' + _('Original text:') + '/ ' +\
                 obj.msgtxt
-- 
GitLab