Morfik Indy package provides an interface to clients, servers, TCP, UDP, and raw sockets, as well as over 100 higher level protocols implementations such as SMTP, POP3, NNTP, and HTTP. Indy includes support for OpenSSL and Zlib in the protocol implementations.


package-view.png
package-down.png


A Quick Walkthrough

To use Morfik Indy Package, add the package to the project by using the ‘Used Packages’ command on the project ribbon (see Figure 1), or drag the package file onto the application main client area.


security-fig1.png
Figure 1: Used Packages Command


To use this package in your code, first you have to add mfk_SystemInternetIndy to the ‘Uses’ section of your module. After that you will gain access to all the functionality of the Indy library. The Indy library documentation can be found here.


Code Examples for Morfik's Ararat Synapse Package

Below are four examples of Morfik Indy Package being implemented in a package demonstration.


Receiveng an email
FX Code

Procedure wm_POP3.mfk_Pop3Send();
Var
    pop3     : TIdPOP3;
    nrOfMsgs : Integer;
    i        : Integer;
    Msg      : TIdMessage;
    from     : String;
    subject  : String;
    body     : String;
Begin
    pop3          := TIdPOP3.Create(Nil);
    pop3.Username := fLogin;
    pop3.Password := fPass;
    pop3.Host     := fHost;
    pop3.Port     := StrToIntDef(fPort,110);
    pop3.Connect;
 
    nrOfMsgs := pop3.CheckMessages;
 
    If nrOfMsgs > 0 Then
    Begin
        For i := 1 To nrOfMsgs do
        Begin
            Msg     := TIdMessage.Create(Nil);
            pop3.Retrieve(i, msg);
            from    := msg.From.Address;
            subject := msg.Subject;
            body    := '';
 
            If (msg.MessageParts.Count = 0) Then
                body := msg.Body.GetText();
 
            MailContent := MailContent + 'From: ' +  from + '<br> Subject: ' + subject + ' <br> Body: ' + body + '<br><br>';
        End;
    End
    Else
        MailContent := 'Mailbox is empty';
 
    pop3.Disconnect();
End;


Sending an email
FX Code

Function SendEmail(Const SMTPServer, EMailAddress, From, Subject, MailText, UserName, Password : String;
    Var ErrorMsg : String; UseSSL : Boolean = False; SSLPort : Integer = 465; UseTLS : TIdUseTLS = utUseExplicitTLS) : Integer;
Var
   SMTP        : TIdSMTP;
   Msg         : TIdMessage;
   k           : Integer;
   FromName    : String;
   FromAddress : String;
Begin
    ErrorMsg := '';
    Try
        //Create SMTP object
        SMTP := TIdSMTP.Create(Nil);
        Try
            //Get FromName and FromAddress
            k := Pos('[',From);
            If k = 0 Then
            Begin
                FromName    := Copy(From,1,pos('@',From)-1);
                FromAddress := From;
            End
            Else
            Begin
                FromName    := Trim(Copy(From,1,k-1));
                FromAddress := Trim(Copy(From,k+1,Pos(']',From)-k-1));
            End;
 
            If UserName <> '' Then
            Begin
                SMTP.AuthType := atDefault;
                //If need authorization, then fill username
                SMTP.UserName           := UserName;
                //Specify user's password
                SMTP.Password           := Password;
            End;
 
            //Create Message object
            Msg := TIdMessage.Create(SMTP);
 
            //Fill future mail's header
            Msg.Subject := Subject;
            With Msg.Recipients.Add do
            Begin
                Address := EMailAddress;
                Name    := EMailAddress;
            End;
            Msg.From.Address := FromAddress;
            Msg.From.Name    := FromName;
            with Msg.ReplyTo.Add do
            Begin
                Address := FromAddress;
                Name    := FromName;
            End;
 
            //Fill future mail's body
            Msg.Body.Text := MailText;
            Msg.ContentType := Format('text/plain; charset="%s"', [Catalog.XAppBrowserCharSet]);
 
            //Specify target server IP (or symbolic name)
            SMTP.Host := SMTPServer;
 
            //Enable SSL|TLS protocols
            If UseSSL Then
            Begin
                SMTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
                SMTP.AuthType := atDefault;
                //Specify target server port
                SMTP.Port := SSLPort;
                SMTP.UseTLS := UseTLS;
            End;
 
            //Connect to SMTP server
            SMTP.Connect;
            Try
                //Transmit message data.
                SMTP.Send(Msg);
                Result:= 0;
            Finally
                //Close SMTP session (QUIT command) and disconnect from SMTP server.
                SMTP.Disconnect(true);
            End;
        Finally
          SMTP.Free;
        End;
    Except
        Result:= 1;
        ErrorMsg := Exception(ExceptObject).Message;
    End;
End;


Translating text using Google translate API
FX Code

Procedure wm_translate.Execute;
Var
    Http : TIdHttp;
    ResponseStream : TMemoryStream;
    str  : TStringList;
Begin
    //Create TIdHTTP object
    Http           := TIdHTTP.Create(Nil);
    ResponseStream := TMemoryStream.Create;
 
    //Connects to host define in URL and access to resource defined in URL by method.
    Http.GET('http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='+encodeURIComponent(fInput)+'&langpair=en%7Ces', ResponseStream);
    ResponseStream.Seek(0, 0);
    str := TStringList.Create;
 
    //Save stream with document received from HTTP server
    str.loadFromStream(ResponseStream);
    fOutput := str.text;
End;


Getting resources
FX Code

Procedure wm_GetResource.GetRes;
Const
    SQLINSERT = 'INSERT INTO "tblResources" ("FileName", "FileContent") VALUES (:"FileName", :"FileContent")';
    SQLSELECT = 'SELECT COUNT(t."Id") as countid FROM "tblResources" t Where t."FileName" = :"FileName"';
    SQLDELETE = 'DELETE FROM "tblResources" t Where t."FileName" = :"FileName"';
Var
    Http           : TIdHttp;
    ResponseStream : TMemoryStream;
    Command        : TSQLCommand;
    HTTPServer     : THTTPServer;
    DBParam        : TDBParam;
    ColumnData     : TMemoryStream;
    DBFileBlob     : TDBFileBlob;
    DBFieldInfo    : TDBFieldInfo;
Begin
    //Initialization of Database connction and datafield access
    DBFieldInfo := Catalog.GetTableFieldByName('tblResources', 'FileContent');
    If DBFieldInfo = Nil Then Exit;
 
    HTTPServer     := THTTPServer.Create(Nil, Nil);
    Command        := HTTPServer.DefaultDBConnection.CreateSQLCommand('');
    ColumnData     := TMemoryStream.Create;
    DBFileBlob     := TDBFileBlob.Create(ColumnData, DBFieldInfo, False, False);
    //Create TIdHTTP object
    Http           := TIdHTTP.Create(Nil);
    ResponseStream := TMemoryStream.Create;
 
    //Connects to host define in URL and access to resource defined in URL by method.
    Http.Get(flURL, ResponseStream);
    ResponseStream.Seek(0, 0);
 
    Try
        //Searching for resource in database with the same name
        Command.SQL.Add(SQLSELECT);
        Command.Prepare;
        Command.ParamByName('"FileName"').AsString  := flName;
        Command.Execute;
 
        //If resource with the same name was founded then
        If Command.fieldByName('countid').asInteger > 0 Then
        Begin
        //Delete resource with the same name
            Command.Free;
            Command := HTTPServer.DefaultDBConnection.CreateSQLCommand('');
            Command.SQL.Add(SQLDELETE);
            Command.Prepare;
            Command.ParamByName('"FileName"').AsString  := flName;
            Command.Execute;
        End;
 
        //Specify SQL query for adding new resource in database
        Command.Free;
        Command := HTTPServer.DefaultDBConnection.CreateSQLCommand('');
        Command.SQL.Add(SQLINSERT);
        Command.Prepare;
        Command.ParamByName('"FileName"').AsString  := flName;
 
        Try
            DBFileBlob.Clear(True);
            //Get resource stream into blob field
            DBFileBlob.LoadFromFile(flName, ResponseStream);
            DBFileBlob.SaveToDBField(Nil);
            DBParam := Command.ParamByName('"FileContent"');
 
            If DBParam Is TDBFieldBlob Then
                TDBFieldBlob(DBParam).LoadFromStream(ColumnData);
        Except
        End;
 
        //Add new resource in database
        Command.Execute;
        flContent := flName;
    Except
        ShowMessage('Error. Cannot display resource');
    End;
 
    Http.Free;
    ResponseStream.Free;
 
    HTTPServer.DefaultDBConnection.DestroySQLCommand(Command);
End;

See Also

Back to top