flashmob 6 years ago
parent
commit
723b34f47e
1 changed files with 11 additions and 15 deletions
  1. 11 15
      backends/s_transformer.go

+ 11 - 15
backends/s_transformer.go

@@ -17,9 +17,12 @@ import (
 // ----------------------------------------------------------------------------------
 // ----------------------------------------------------------------------------------
 // Config Options:
 // Config Options:
 // --------------:-------------------------------------------------------------------
 // --------------:-------------------------------------------------------------------
-// Input         :
+// Input         : envelope.Values["MimeParts"]
 // ----------------------------------------------------------------------------------
 // ----------------------------------------------------------------------------------
-// Output        : 8bit mime message
+// Output        : 8bit mime message, with charsets decoded to UTF-8
+//               : Note that this processor changes the body counts. Therefore, it makes
+//               : a new instance of envelope.Values["MimeParts"] which is then populated
+//               : by parsing the new re-written message
 // ----------------------------------------------------------------------------------
 // ----------------------------------------------------------------------------------
 
 
 func init() {
 func init() {
@@ -41,13 +44,11 @@ type Transform struct {
 	decoder             io.Reader
 	decoder             io.Reader
 	pr                  *io.PipeReader
 	pr                  *io.PipeReader
 	pw                  *io.PipeWriter
 	pw                  *io.PipeWriter
-	parts               *mime.Parts
 	partsCachedOriginal *mime.Parts
 	partsCachedOriginal *mime.Parts
 	envelope            *mail.Envelope
 	envelope            *mail.Envelope
 
 
-	state   int
-	matched int
-
+	// we re-parse the output since the counts have changed
+	// parser implements the io.Writer interface, here output will be sent to it and then forwarded to the next processor
 	parser *mime.Parser
 	parser *mime.Parser
 }
 }
 
 
@@ -74,6 +75,8 @@ func (t *Transform) unswap() {
 
 
 func (t *Transform) ReWrite(b []byte) (count int, err error) {
 func (t *Transform) ReWrite(b []byte) (count int, err error) {
 	if !t.isBody {
 	if !t.isBody {
+		// we place the partial header's bytes on a buffer from which we can read one line at a time
+		// then we match and replace the lines we want
 		count = len(b)
 		count = len(b)
 		if i, err := io.Copy(&t.buf, bytes.NewReader(b)); err != nil {
 		if i, err := io.Copy(&t.buf, bytes.NewReader(b)); err != nil {
 			return int(i), err
 			return int(i), err
@@ -93,7 +96,6 @@ func (t *Transform) ReWrite(b []byte) (count int, err error) {
 				if err != nil {
 				if err != nil {
 					return
 					return
 				}
 				}
-
 			} else {
 			} else {
 				break
 				break
 			}
 			}
@@ -167,7 +169,6 @@ func Transformer() *StreamDecorator {
 			}
 			}
 
 
 			sd.Open = func(e *mail.Envelope) error {
 			sd.Open = func(e *mail.Envelope) error {
-				//charset_pos = 0
 				envelope = e
 				envelope = e
 				_ = envelope
 				_ = envelope
 				if reWriter.parser == nil {
 				if reWriter.parser == nil {
@@ -184,7 +185,6 @@ func Transformer() *StreamDecorator {
 
 
 					// we are going to change envelope.Values["MimeParts"] to our own copy with our own counts
 					// we are going to change envelope.Values["MimeParts"] to our own copy with our own counts
 					envelope.Values["MimeParts"] = reWriter.swap()
 					envelope.Values["MimeParts"] = reWriter.swap()
-
 					defer reWriter.unswap()
 					defer reWriter.unswap()
 					var pos int
 					var pos int
 
 
@@ -203,15 +203,12 @@ func Transformer() *StreamDecorator {
 								break
 								break
 							}
 							}
 							reWriter.current = part
 							reWriter.current = part
-
 							pos += count
 							pos += count
 							msgPos = part.StartingPos
 							msgPos = part.StartingPos
 						}
 						}
 						// break chunk on header (found the body)
 						// break chunk on header (found the body)
 						if part.StartingPosBody > 0 && part.StartingPosBody >= msgPos {
 						if part.StartingPosBody > 0 && part.StartingPosBody >= msgPos {
-							//reWriter.recalculate()
 							count, err = reWriter.ReWrite(p[pos : part.StartingPosBody-offset])
 							count, err = reWriter.ReWrite(p[pos : part.StartingPosBody-offset])
-
 							total += count
 							total += count
 							if err != nil {
 							if err != nil {
 								break
 								break
@@ -219,7 +216,6 @@ func Transformer() *StreamDecorator {
 							_, _ = reWriter.parser.Write([]byte{'\n'}) // send an end of header to the parser
 							_, _ = reWriter.parser.Write([]byte{'\n'}) // send an end of header to the parser
 							reWriter.isBody = true
 							reWriter.isBody = true
 							reWriter.current = part
 							reWriter.current = part
-
 							pos += count
 							pos += count
 							msgPos = part.StartingPosBody
 							msgPos = part.StartingPosBody
 						}
 						}
@@ -242,10 +238,10 @@ func Transformer() *StreamDecorator {
 						progress = len(*parts) - 2 // skip to 2nd last part, assume previous parts are already processed
 						progress = len(*parts) - 2 // skip to 2nd last part, assume previous parts are already processed
 					}
 					}
 				}
 				}
-
+				// note that in this case, ReWrite method will output the stream to further processors down the line
+				// here we just return back with the result
 				return total, err
 				return total, err
 			})
 			})
 		}
 		}
-
 	return sd
 	return sd
 }
 }