ScriptXBody

From 40tude Dialog Wiki

program OnBeforeSavingMessage;

// by Vladimir Panteleev
// tested on 2.0.14.1 (beta 37)
// last update: 28-11-2004

// Usage: 
// Plug in this script in the appropriate script slot. 
// Use header scoring, do not include non-alpha-numeric characters in keywords.
// Example:
// +5 Header "40tude"
// This will match "40tude", "40 tude", "40-tude", as well as "40", new line, "tude".
// If this does not suit you, edit BodyChars below.

const
  // Only the following characters are copied into the header field, all other
  // are skipped. Useful if you want to monitor compound words, like Adom-Bot.
  BodyChars='abcdefghijklmnopqrstuvwxyz'+
            'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+
            '0123456789';

  // Dialog crashes when headers contain long lines. This is the limit of how
  // many characters to allow per one X-Bodyxxx header.
  MaxBodyLength=900;
  
  // Force to break on very long lines
  MaxLineLength=100;

var
  I, J, BodyStart, BodyHeaders: LongInt;
  Body, S: String;

procedure AddXBody(var Message: TStringList);
begin
  Message.Insert(BodyStart-1+BodyHeaders, 'X-Body'+IntToStr(BodyHeaders+1)+': '+Body);
  BodyHeaders:=BodyHeaders+1;
  Body:='';
end;

procedure OnBeforeSavingMessage(var Message: TStringList; ServerName: String; IsEmail: Boolean);
begin
  //Message.SaveToFile('message.txt');    // for debugging
  BodyStart:=10000000;
  for I:=0 to Message.Count-1 do
    if Message[I]='' then
      begin
      BodyStart:=I+1;
      Break
      end;
  Body:='';
  BodyHeaders:=0;
  for I:=BodyStart to Message.Count-1 do
   if Copy(Message[I],1,1)<>'>' then          // skip quoted lines
    begin
    S:=Message[I]+' ';
    for J:=1 to Length(S) do
      if Pos(S[J],BodyChars)>0 then
        begin
        Body:=Body+S[J]
        if Length(Body)>MaxBodyLength+MaxLineLength then
          AddXBody(Message);
        end
      else
        if Length(Body)>MaxBodyLength then
          AddXBody(Message);
    end;
  AddXBody(Message);
  //Message.SaveToFile('message.new');    // for debugging
end;

begin
end.