project BLF > class Mail > method IPAdressGetter

Description

This methods returns a temp table with all possible mail servers for the receiving domain. The Connect statement should use these IP adresses (order: tiPreference, ascending)


Parameters


icDomaininputcharacterThis is the domain, you want to retrieve all possible mailserver IP adresses
oiReturnStatusoutputintegerReturn status of the method.


Internal usage


BLF
method Mail.Connect


program code (program1/mail.p)

assign oiReturnStatus = -98.

/* =========================================================== */
/* First try: read the server.xml for the local mail server    */
/* =========================================================== */


empty temp-table tMailservers.

assign vcIniPath      = search ("server.xml":U).
if vcIniPath = ?
then do:
    <M-6 run SetMessage
       (input  #T-10'The server.xml file was not found.':100(5014)T-10# (icMessage), 
        input  '' (icArguments), 
        input  '' (icFieldName), 
        input  '' (icFieldValue), 
        input  'D':U (icType), 
        input  2 (iiSeverity), 
        input  '' (icRowid), 
        input  'BLF-340':U (icFcMsgNumber), 
        input  '' (icFcExplanation), 
        input  '' (icFcIdentification), 
        input  '' (icFcContext), 
        output viFcReturnSuper (oiReturnStatus)) in Mail>
    assign oiReturnStatus = -5.
    return.
end.

assign vhFcComponent = ?.
<M-13 run MainBlock
     () in XML>
assign vhXMLhandle = vhFcComponent.

<M-14 run ReadXML
   (input  vcIniPath (icXMLFile), 
    input  'mail':U (icStartTag), 
    output tNodesMail (tNodes), 
    output viFcReturnSuper (oiReturnStatus)) in XML>
run gipr_DeleteProcedure in vhXMLhandle.
delete procedure vhXMLHandle.
if viFcReturnSuper <> 0
then assign oiReturnStatus = viFcReturnSuper.
if viFcReturnSuper < 0
then return.

For Each tNodesMail:    
    If tNodesMail.tcNodeName = 'SmtpServer':U
    Then Assign vcSmtpServer = tNodesMail.tcNodeValue.
    If tNodesMail.tcNodeName = 'SmtpPort':U
    Then Assign vcSmtpPort = tNodesMail.tcNodeValue.
End. /*For Each tNodesCall:*/

assign vcSmtpPort = (if vcSmtpPort = "" then "25":U else vcSmtpPort).
if vcSmtpServer <> "" and vcSmtpServer <> ?
then do:
    input stream sStream through value("nslookup ":U + vcSmtpServer).
    assign vcDebug = "In:nslookup ":U + vcSmtpServer + chr(10) + "Out:":U + chr(10).
    assign vlParse = false.
    repeat:
        import stream sStream unformatted vcWork.
        vcDebug = vcDebug + vcWork + chr(10).
        if index(vcWork,"Name:":U) <> 0
        then do:

            vlParse = true.
            vcServerName = trim(substring(vcWork,index(vcWork,":":U) + 1,-1,"CHARACTER":U)).
            create tMailServers.
            assign tMailservers.tcServerName = vcServerName.
            
            next.
        end.
        if index(vcWork,"Address:":U) <> 0 and
           vlParse
        then do:
           vcIP = trim(substring(vcWork,index(vcWork,":":U) + 1,-1,"CHARACTER":U)).
           
           assign tMailServers.tiPreference = 1
                  tMailServers.tcIP         = vcIP
                  tMailServers.tcSmtpPort    = vcSmtpPort.
        end.
    end.
    input stream sStream close.

    if not can-find (first tMailServers where
                           tMailServers.tcIP <> "":U)
    then do:
        create tMailServers.
        assign tMailservers.tcServerName = vcSmtpServer
               tMailServers.tiPreference = 1
               tMailServers.tcIP         = vcSmtpServer
               tMailServers.tcSmtpPort   = vcSmtpPort.
    end.

    /* debugging */
    publish "Logging.BusinessCode" (vcDebug, "").
end.


/* =========================================================== */
/* Second try: find the IP address of the SMTP mail server,    */
/* using nslookup                                              */
/* =========================================================== */
if not can-find (first tMailServers where
                       tMailServers.tcIP <> "":U)
then do:
    input stream sStream through 
    value("nslookup -querytype=MX -domain=":U + icDomain + " ":u + icDomain).
    assign vcDebug = "In:nslookup -querytype=MX -domain=":U + icDomain + " ":u + icDomain
                   + chr(10) + "Out:":U + chr(10).
    repeat:
      import stream sStream unformatted vcWork.
      assign vcDebug = vcDebug + vcWork + chr(10).
      assign viParse1 = index(vcWork,"mail exchanger":U)
             viParse2 = index(vcWork,"internet address = ":U).
      if viParse1 <> 0
        then do:        
            assign viPreference = ?
                   vcServerName = substring(vcWork,viParse1 + 17,-1,"CHARACTER":U)
                   viPreference = integer(substring(vcWork,index(vcWork,"preference = ":U) + 13,2,"CHARACTER":U)) no-error.
            if vcServerName <> '' and vcServerName <> ? and
               viPreference <> ?
            then do:
                find first tMailServers where
                           tMailServers.tcServerName = vcServerName
                           no-error.
                if not available tMailServers 
                then do:
                    create tMailServers.
                    assign tMailservers.tcServerName = vcServerName.
                end.
                assign tMailServers.tiPreference = viPreference.
            end.
        end.
    
        if viParse2 <> 0
        then do:        
            assign vcServerName = trim(substring(vcWork,1,viParse2 - 1,"CHARACTER":U))
                   vcIP = trim(substring(vcWork,viParse2 + 19,-1,"CHARACTER":U)) no-error.
            if vcServerName <> '' and vcServerName <> ? and
               vcIP <> '' and vcIP <> ?
            then do:
                find first tMailServers where
                           tMailServers.tcServerName = vcServerName
                           no-error.
                if not available tMailServers 
                then do:
                    create tMailServers.
                    assign tMailservers.tcServerName = vcServerName
                           tMailservers.tiPreference = 99.
                end.
                assign tMailServers.tcIP = vcIP
                       tMailServers.tcSmtpPort    = "25":U.
            end.
        end. 
    end.
    input stream sStream close.
    
    /* debugging */
    publish "Logging.BusinessCode" (vcDebug, "").
end.

/* =========================================================== */
/* Check availability of the mail server information           */
/* =========================================================== */

if not can-find (first tMailServers where
                       tMailServers.tcIP <> "":U)
then do:
    <M-5 run SetMessage
          (input  #T-12'Unable to send e-mail: server not found':100(16)t-12# (icMessage), 
           input  '' (icArguments), 
           input  '' (icFieldName), 
           input  '' (icFieldValue), 
           input  'D':U (icType), 
           input  3 (iiSeverity), 
           input  '' (icRowid), 
           input  'BLF-339':U (icFcMsgNumber), 
           input  '' (icFcExplanation), 
           input  '' (icFcIdentification), 
           input  '' (icFcContext), 
           output viFcReturnSuper (oiReturnStatus)) in Mail>
    assign oiReturnStatus = -3.
    return.
end.

assign oiReturnStatus = 0.