CVS: tmda-cgi CgiUtil.py,1.13,1.14 ChangeLog,1.25,1.26 PendList.py,1.11,1.12 TODO,1.13,1.14 View.py,1.16,1.17 defaults.ini,1.17,1.18 tmda-cgi.py,1.27,1.28

Gre7g Luterman <[email protected]>
Newsgroups gmane.mail.spam.tmda.cvs
Message-ID <[email protected]>
Update of /cvsroot/tmda/tmda-cgi
In directory sc8-pr-cvs1:/tmp/cvs-serv18821

Modified Files:
	CgiUtil.py ChangeLog PendList.py TODO View.py defaults.ini 
	tmda-cgi.py 
Log Message:
Added new defaults.ini variable for controlling the default character set.

Added two new util functions, one for looking up the alias of a character set,
and one that translates characters into UTF-8.

Encode the character sets found so they display properly.


Index: CgiUtil.py
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/CgiUtil.py,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- CgiUtil.py	4 May 2003 20:26:25 -0000	1.13
+++ CgiUtil.py	6 May 2003 21:50:39 -0000	1.14
@@ -22,6 +22,7 @@
 "Utilities for tmda-cgi."
 
 import cgi
+import codecs
 import os
 import re
 import sys
@@ -38,6 +39,7 @@
 UnquotedString = re.compile(r"^(\S+)\s*")
 HomeDirSearch  = re.compile("^~/")
 HTMLTagSearch  = re.compile("</?([^\s>]*).*?>", re.S)
+UTF8           = codecs.getencoder("utf-8")
 
 # CGI exception classes
 class NotInstalled(Errors.TMDAError):
@@ -147,7 +149,7 @@
 
 def ReportToSpamCop(MsgObj):
   "Report a given message to SpamCop."
-  
+
   if PVars[("NoOverride", "Sendmail")]:
     Sendmail = "-s %s" % PVars[("NoOverride", "Sendmail")]
   else:
@@ -157,3 +159,70 @@
   P = os.popen(Command, "w")
   P.write(MsgObj.msgobj.as_string(1))
   P.close()
+
+def AliasCharSet(CharSet):
+  "Recognize a charset's alias."
+  if CharSet in ("csgb2312", "gb_2312-80", "iso-ir-58", "chinese",
+    "csiso58gb231280"):
+    CharSet = "gb2312"
+  # Is it GBK?
+  elif CharSet in ("cp936", "ms936", "windows-936"):
+    CharSet = "gbk"
+  # Is it Big5?
+  elif CharSet in ("big5-hkscs", "csbig5", "chinesebig5"):
+    CharSet = "big5"
+  return CharSet
+
+def TranslateToUTF8(CharSet, Str, Errors):
+  "Represent a string in UTF-8."
+
+  if not CharSet:
+    return Str
+  CharSet = AliasCharSet(CharSet)
+
+  # Find appropriate decoder
+  try:
+    Decoder = codecs.getdecoder(CharSet)
+  except LookupError:
+    try:
+      # Is it GB2312?
+      if CharSet == "gb2312":
+        import chinese.gb2312
+        Lib = chinese.gb2312
+      # Is it GBK?
+      elif CharSet == "gbk":
+        import chinese.gbk
+        Lib = chinese.gbk
+      # Is it Big5?
+      elif CharSet == "big5":
+        import chinese.big5
+        Lib = chinese.big5
+      # Don't recognize it.  Was it our fallback?
+      elif CharSet == PVars[("General", "CSEncoding")]:
+        # It was our fallback!  Give up now!
+        return Str
+      # Mark it and use the fallback
+      else:
+        return "(%s) %s" % (CharSet,
+          TranslateToUTF8(PVars[("General", "CSEncoding")], Str, Errors))
+      Decoder = Lib.Codec().decode
+    except ImportError:
+      # We know what it was, but we don't have the library installed.
+      return "(%s) %s" % (CharSet, Str)
+
+  # Decode string to Unicode
+  try:
+    Uni = Decoder(Str, errors = Errors)[0]
+  except TypeError:
+    Uni = Decoder(Str)[0]
+
+  # Encode for UTF-8
+  return UTF8(Uni)[0]
+
+def FindCharSet(MsgObj):
+  "Find the character set in an e-mail."
+  RetVal = None
+  for Part in MsgObj.msgobj.walk():
+    CS = Part.get_content_charset()
+    if CS: RetVal = CS
+  return RetVal

Index: ChangeLog
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/ChangeLog,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- ChangeLog	4 May 2003 23:06:41 -0000	1.25
+++ ChangeLog	6 May 2003 21:50:40 -0000	1.26
@@ -1,3 +1,7 @@
+2003-05-06  Gre7g Luterman  <[email protected]>
+
+	* Display international characters in both pending lists and e-mails.
+
 2003-05-04  Gre7g Luterman  <[email protected]>
 
 	* Replaced compile with new, improved configure.  Makes better guesses

Index: PendList.py
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/PendList.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- PendList.py	6 May 2003 14:49:41 -0000	1.11
+++ PendList.py	6 May 2003 21:50:40 -0000	1.12
@@ -93,6 +93,7 @@
 
   # Load the display template
   T = Template.Template("pending.html")
+  T["CharSet"] = "utf-8"
 
   # Find the message numbers we'll display
   FirstMsg = PVars["Pager"]
@@ -216,6 +217,9 @@
       )
       T["Date"] = ZeroSearch.sub(ZeroSub, Date)
 
+      # Character set
+      CharSet = CgiUtil.FindCharSet(MsgObj)
+
       # Subject:
       if not MsgObj.msgobj["subject"]:
         Subject = "None"
@@ -224,8 +228,12 @@
         value = ""
         for decoded in email.Header.decode_header( MsgObj.msgobj["subject"] ):
           if decoded[1]:
-            value += "(" + decoded[1] + ") " 
-          value += decoded[0] + " "
+            try:
+              value += CgiUtil.TranslateToUTF8(decoded[1], decoded[0], "strict")
+            except UnicodeError:
+              value += CgiUtil.TranslateToUTF8(CharSet, decoded[0], "ignore")
+          else:
+            value += CgiUtil.TranslateToUTF8(CharSet, decoded[0], "ignore")
         Subject = value
         if len(Subject) > int(PVars[("PendingList", "CropSubject")]):
           Subject = \
@@ -243,8 +251,12 @@
         value = ""
         for decoded in email.Header.decode_header( MsgObj.msgobj["from"] ):
           if decoded[1]:
-            value += "(" + decoded[1] + ") " 
-          value += decoded[0] + " "
+            try:
+              value += CgiUtil.TranslateToUTF8(decoded[1], decoded[0], "strict")
+            except UnicodeError:
+              value += CgiUtil.TranslateToUTF8(CharSet, decoded[0], "ignore")
+          else:
+            value += CgiUtil.TranslateToUTF8(CharSet, decoded[0], "ignore")
         From = value
         Temp = Address.search(From)
         if Temp:

Index: TODO
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/TODO,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- TODO	4 May 2003 23:06:41 -0000	1.13
+++ TODO	6 May 2003 21:50:40 -0000	1.14
@@ -6,10 +6,6 @@
 
     * Filter Viewer -- graphic view of your filters
 
-    * Language Support -- code to recognize the language an e-mail is in and
-                          include the appropriate language identifiers in the
-                          viewer
-
     * Tutorial -- finish section on mailing lists
 
     * URL Confirmation -- doesn't work yet for virtual users

Index: View.py
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/View.py,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- View.py	6 May 2003 14:49:41 -0000	1.16
+++ View.py	6 May 2003 21:50:40 -0000	1.17
@@ -51,8 +51,6 @@
 def AddIcon(Part):
   "Add an appropriate attachment."
   
-  global Attachment
-  
   Filename = Part.get_filename("")
   Icon = "exe"
   if ImageType1.search(Filename): Icon = "image"
@@ -73,7 +71,7 @@
 def Show():
   "Show an e-mail in HTML."
 
-  global messageCharset, Allow, Remove, Attachment, Divider, PartTemplate, T
+  global Allow, Remove, Attachment, Divider, PartTemplate, T
 
   # Deal with a particular message?
   if Form.has_key("msgid"):
@@ -227,6 +225,8 @@
       # Decode internationalized headers
       for decoded in email.Header.decode_header( Line ):
         Headers += decoded[0] + " "
+        if decoded[1]:
+          T["CharSet"] = CgiUtil.AliasCharSet(decoded[1])
       Headers += "\n"
     T["Headers"] = '<pre class="Headers">%s</pre>' % Headers
   else:
@@ -240,6 +240,8 @@
       # Decode internationalazed headers
       for decoded in email.Header.decode_header( MsgObj.msgobj[Header] ):
         value += decoded[0] + " "
+        if decoded[1]:
+          T["CharSet"] = CgiUtil.AliasCharSet(decoded[1])
       T["Value"] = CgiUtil.Escape(value)
       HeaderRow.Add()
 
@@ -250,12 +252,6 @@
   Divider      = T["Divider"]
   PartTemplate = T["Part"]
 
-  # Check if there's a charset defined.
-  messageCharset = None
-  T["charset"] = "us-ascii" # default charset
-  if MsgObj.msgobj.get_content_charset():
-    messageCharset = MsgObj.msgobj.get_content_charset()
-    T["charset"] = messageCharset
   ShowPart(MsgObj.msgobj)
 
   # Remove unneeded bits?
@@ -282,8 +278,6 @@
 def ShowPart(Part):
   "Analyze message part and display it as best possible."
 
-  global Allow, Remove, Divider, PartTemplate, T
-
   # Each part is one of five things and must be handled accordingly
   # multipart/alternative - pick one and display it
   # message or multipart  - recurse through each
@@ -293,8 +287,7 @@
 
   # Check if there's a character set for this part.
   if Part.get_content_charset():
-    messageCharset = Part.get_content_charset()
-    T["charset"] = messageCharset
+    T["CharSet"] = CgiUtil.AliasCharSet(Part.get_content_charset())
 
   # Display this part
   if Part.is_multipart():

Index: defaults.ini
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/defaults.ini,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- defaults.ini	4 May 2003 03:26:48 -0000	1.17
+++ defaults.ini	6 May 2003 21:50:40 -0000	1.18
@@ -44,6 +44,7 @@
 ;;                                                  ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 [General]
+CSEncoding   = iso-8859-1
 SpamCopAddr  =
 Theme        = Blue
 UseJSConfirm = Yes

Index: tmda-cgi.py
===================================================================
RCS file: /cvsroot/tmda/tmda-cgi/tmda-cgi.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -r1.27 -r1.28
--- tmda-cgi.py	3 May 2003 17:29:39 -0000	1.27
+++ tmda-cgi.py	6 May 2003 21:50:40 -0000	1.28
@@ -58,6 +58,7 @@
 # Make some global stuff available to all
 Template.Template.BaseDir = "%s/display/themes/Blue/template" % \
   os.path.abspath(os.path.split(sys.argv[0])[0])
+Template.Template.Dict["CharSet"]  = "iso-8859-1"
 Template.Template.Dict["Script"]   = os.environ["SCRIPT_NAME"]
 Template.Template.Dict["SID"]      = ""
 Template.Template.Dict["DispDir"]  = os.environ["TMDA_CGI_DISP_DIR"]
@@ -81,6 +82,7 @@
   try:
     PVars = Session.Session(Form)
     CgiUtil.ErrTemplate = "error.html"
+    Template.Template.Dict["CharSet"] = PVars[("General", "CSEncoding")]
   except CgiUtil.NotInstalled, (ErrStr, PVars):
     Template.Template.Dict["ErrMsg"] = ErrStr
     # Can log in but TMDA is not installed correctly

_______________________________________
tmda-cvs mailing list
http://tmda.net/lists/listinfo/tmda-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.