using System;
using System.Collections.Generic;
using System.Text;
using FTGate.Framework;
using System.IO;
public class SpoolerExtension : SpoolBehaviour
{
private const string TAG = "[ext]";
private bool prepend = true;
///
/// Called by a the spooler when it starts processing a new message
///
public ProcessMessageReturnType OnPreProcessMessage(MailMessage message)
{
string filePath = Path.GetDirectoryName(message.textPath);
string fileName = Path.GetFileNameWithoutExtension(message.textPath);
string fullSrcPath = filePath + @"\" + fileName + ".txt";
string fullDstPath = filePath + @"\" + fileName + ".tmp";
Log log=new Log();
try
{
// open the file and read lines
// find the header line we want and alter it, then process all the other lines
bool isTagged = false;
bool isInbound = false;
FileStream fin = File.Open(fullSrcPath, FileMode.Open);
FileStream fout = File.Open(fullDstPath, FileMode.Create);
StreamReader sr = new StreamReader(fin);
StreamWriter sw = new StreamWriter(fout);
string line="";
while ((line = sr.ReadLine()) != null)
{
if (!isInbound && line.Contains("by FTGate SmartPop;"))
isInbound = true;
if (!isTagged && isInbound && line.StartsWith("subject:", StringComparison.OrdinalIgnoreCase))
{
isTagged = true;
if (!line.Contains(TAG))
{
if (prepend)
line = "Subject: " + TAG + line.Substring(8);
else
line = line + " " + TAG;
}
}
sw.WriteLine(line);
}
fin.Close();
fout.Close();
// now delete the origial
File.Delete(fullSrcPath);
// and rename the new
File.Move(fullDstPath, fullSrcPath);
log.Write(LogType.Normal, "Tag: Success " + fileName);
}
catch(Exception e)
{
log.Write(LogType.Normal, "Tag error: " + fileName + ": " + e.Message);
return ProcessMessageReturnType.Error; // also same as Continue (doesnt delete message)
}
return ProcessMessageReturnType.Continue;
}
}