Kulverstukas

EmailExtractor

May 15th, 2010
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.76 KB | None | 0 0
  1. { Started writing on 2010.05.07 by Kulverstukas || last update: 2010.05.15 }
  2. unit emailextracter;
  3.  
  4. interface
  5.  
  6. uses
  7.   Classes,SysUtils,FileUtil,LResources,Forms,Controls,Graphics,Dialogs,StdCtrls,
  8.   Menus, greetz;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     emailText:TLabel;
  16.     emailCount:TLabel;
  17.     ListBox1:TListBox;
  18.     Select:TButton;
  19.     Extract:TButton;
  20.     Edit1:TEdit;
  21.     MainMenu1:TMainMenu;
  22.     MenuItem1:TMenuItem;
  23.     procedure ExtractClick(Sender:TObject);
  24.     procedure SelectClick(Sender:TObject);
  25.     procedure MenuItem1Click(Sender:TObject);
  26.  
  27.   private
  28.     { private declarations }
  29.   public
  30.     { public declarations }
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.   OldPath, NewPath : string;
  36.   OldEmailDump, NewEmailDump : text;
  37.   saveDialog :TSaveDialog;
  38. implementation
  39.  
  40. { TForm1 }
  41. {$I-}
  42. //======================================= // program procedures start here
  43. function FindMail (Line : ansistring) : ansistring;
  44. var i, i1, i2 : longint;
  45. const delimiter = ' !@#$%^&*()[];,"''<>|.   ';
  46.  begin
  47.   result := '';           // null the variable
  48.    i := Pos('@',Line);    // determine exact position of @ in string
  49. //========
  50.  if i = 0 then exit;  // Search down for any delimiter that could be in front of e-mail address
  51.   i1 := i;
  52.    repeat
  53.     Dec(i1);  // decrease i1 by 1 bit
  54.    until (i1 < 1) or (Pos(Copy(Line,i1,1),Delimiter) <> 0); // until i1 is less then 1
  55.     Inc(i1);   // increase i1 by 1 bit if it is less then 1
  56. //========
  57.   i2 := i;   // Search until we find a dot
  58.    repeat
  59.     Inc(i2);
  60.    until (i2 > Length(Line)) or (Pos(Copy(Line,i2,1),Delimiter) <> 0);
  61. //========
  62.  if (i2 > Length(Line)) or (Copy(Line,i2,1) <> '.') then   // If we didn't find a dot, then it's not an e-mail address
  63.   begin
  64.    result := '';
  65.    exit;
  66.   end;
  67. //========
  68.   repeat     // We continue to find another delimiter that would end the address
  69.    Inc(i2);
  70.   until (i2 > Length(Line)) or (Pos(Copy(Line,i2,1),Delimiter) <> 0);
  71.    Dec(i2);
  72. //========
  73.   result := Copy(Line,i1,i2 - i1 + 1);
  74. end;
  75. //=======================================
  76. procedure ExtractMail;
  77.  var email, line : AnsiString;
  78.      mailCount : integer;
  79. begin
  80.  Form1.Extract.Enabled := false;
  81.  Form1.ListBox1.Clear;
  82.  mailCount := 0;
  83.  OldPath := Form1.Edit1.Text;
  84.  NewPath := saveDialog.FileName;
  85.  Assign(OldEmailDump, OldPath);
  86.  Reset(OldEmailDump);
  87.  Assign(NewEmailDump, NewPath);
  88.  ReWrite(NewEmailDump);
  89.   Repeat
  90.    ReadLn(OldEmailDump, line);
  91.     email := FindMail(line);
  92.      if email <> '' then
  93.       begin
  94.        Form1.Refresh;
  95.        mailCount := mailCount + 1;
  96.        Form1.emailCount.Caption := IntToStr(mailCount);
  97.        Form1.ListBox1.Items.Add('Found email: '+email);
  98.        Form1.ListBox1.TopIndex := -1 + Form1.ListBox1.Items.Count;  // scroll down the list to the last item automaticaly
  99.        WriteLn(NewEmailDump, email);
  100.       end;
  101.   until EoF(OldEmailDump);
  102.  Close(OldEmailDump);
  103.  Close(NewEmailDump);
  104. end;
  105.  
  106. //======================================= // object procedures start here
  107. procedure TForm1.SelectClick(Sender:TObject);
  108. var
  109.   openDialog : TOpenDialog;    // Open dialog variable. Used as Record.
  110. begin
  111. //=======
  112.   openDialog := TOpenDialog.Create(self);  // assign the dialog to the variable
  113.   openDialog.InitialDir := GetCurrentDir;  // open directory where exe was launched from
  114.   openDialog.Options := [ofFileMustExist];  // only allow existing files
  115.   openDialog.Filter := 'Any file|*.*|Text files - *.txt|*.txt|SQL dumps - *.sql|*.sql';
  116.   openDialog.FilterIndex := 1;  // define what option is default defined in Filter
  117.   openDialog.Title := 'Select a dump';
  118. //=======
  119.    if openDialog.Execute  // continue if a user has selected a file and clicked OK
  120.     then
  121.      begin
  122.       Edit1.Text := openDialog.FileName;
  123.      end;
  124. end;
  125. //=======================================
  126. procedure TForm1.MenuItem1Click(Sender:TObject);
  127. begin
  128.   Form4.ShowModal;
  129. end;
  130. //=======================================
  131. procedure TForm1.ExtractClick(Sender:TObject);
  132. begin
  133. //==========
  134.   if FileExists(Form1.Edit1.Text) then
  135.    begin
  136. //==========
  137.   saveDialog := TSaveDialog.Create(self); // assign Save file dialog to saveDialog variable
  138.   saveDialog.Title := 'Save your Email dump';  // title of the save dialog
  139.   saveDialog.InitialDir := GetCurrentDir;  // get dir the extracter is in
  140.   saveDialog.Filter := 'Any file|*.*';     // allow any file
  141.   saveDialog.DefaultExt := 'txt';         // default extension. If no extension is provided the it's gonna be txt anyway
  142.    if saveDialog.Execute then
  143.     begin
  144.      ExtractMail;
  145.     end;
  146. //==========
  147.    end
  148.   else
  149.    begin
  150.     ShowMessage('File doesn''t exist. Feed me with existing file!');
  151.    end;
  152. //==========
  153. end;
  154.  
  155. //=======================================
  156. initialization
  157.   {$I emailextracter.lrs}
  158.  
  159. end.
Add Comment
Please, Sign In to add comment