Welcome to

The Conxsys Blog

Thoughts on Conxsys, Notes/Domino, LotusScript, & Technology

SnTT: Function to Determine if Addressee is Internal or External

October 23rd, 2008 by Corey Davis

Show-n-Tell ThursdayHow familiar are you with LotusScript’s Like operator? If not very, then you are missing out because Like packs a lot of punch. To demonstrate that I am going to outline a function that I use in my applications to determine if a particular e-mail address is internal to my domain.

This function requires that you provide the addressee’s domain, be it the Domino domain or the Internet domain. In a previous SnTT post I provided a function that will output this very information, so take a look at that if necessary.  And, just like that last SnTT, this one is less than 30 lines of code so I will annotate once again.

First, you will need a list of your internal domains.  I store mine in a constant:

Const INTERNAL_DOMAINS = “Empire;empire.gov;sith.org?

You will notice that the INTERNAL_DOMAINS constant contains both the Domino domain (Empire) as well as Internet domains (empire.gov and sith.org). These constants would be found in the Declarations subroutine.

1:    Function IsInInternalDomain(domain As String) As Boolean

Our function, named IsInInternalDomain, requires for input the adressee’s domain. The domain would be the domain as returned by the AddresseeDomain function.  For example, if the addressee were in a Domino domain such as “Count Dooku/Sith/Empire@Serenno” the domain would be “Serenno?.  If the addressee were an Internet address such as “customer-service@trade-federation.org? the domain would be “trade-federation.org”. The output of this function will be the Boolean value of True if the domain is internal or False if the domain is external. Keep in mind here that internal versus external is not the same as Domino domain versus Internet domain. In our case, an Internet domain will be considered internal if it is one of our internet domains.

2:    Dim internalDomains As Variant

Here is our friend Split again doing its powerful work of splitting our domain constant into a finely chopped array:

internalDomains(0) = “Empire?
internalDomains(1) = “empire.gov?
internalDomains(2) = “sith.org?

5:    If Instr(domain, “.”) Then

Depending on whether the addressee’s domain that was passed into the function is a Domino domain or an Internet domain will determine which type of test we perform.  Checking for the existence of a period is a quick way to determine if the domain is Domino or Internet.  Domino domains do not contain periods but Internet domains always do.  Note that the only exception to this rule about Internet domains containing a period is if you own a top level domain (TLD) but I seriously doubt that you do unless you are some type of galactic overlord

6:        ‘This is an internet address so we need to do a likewise comparison against internal domains
7:        Forall d In internalDomains
8:            If (“.” & Lcase(domain)) Like (“*.” & Lcase(d)) Then        ‘We are adding periods in here because
9:                                            ‘if we don’t we will get false positives
10:                                          ‘on similar domains.
11:                Let IsInInternalDomain = True
12:                Exit Forall
13:            End If
14:        End Forall

If we determine that we have an Internet domain then this is where we will see just how wonderful Like can be.  As you can see in line 7 we are going to loop through each of our internal domains one at a time.  On line 8 we check our current internal domain against the addressee’s domain that was passed into the function.  We could do the check without Like in this manner:

If Lcase(domain) = Lcase(d) Then

But there is a problem with this approach that may not be readily apparent: sub-domains.  If you currently use or in the future decide to use sub-domains such as deathstar.empire.gov or recruiting.sith.org and e-mail comes from these domains then the more straightforward If/Then above would generate false negatives.   Afterall, “deathstar.empire.gov? does not equal “empire.gov”, but we can clearly see that “deathstar.empire.gov? is actually part of “empire.gov? and should be considered internal.  We could populate our INTERNAL_DOMAINS constant with all the sub-domains we are currently using, but this is very narrow thinking and does not take into consideration future additions to our sub-domain architecture.  This is where Like comes to the rescue!

If Lcase(domain) Like (“*? & Lcase(d)) Then

By performing a likewise comparison between “empire.gov? to “*empire.gov? we get a match.  More importantly, if the addressee’s domain is “deathstar.empire.gov? we would also get a match.  This is a very simplistic Like operation, but a good start if you have never used it.  I highly recommend reading the Designer Help on the Like operator because you can do some very sophisticated comparisons, including regular expression-like comparisons (e.g. California zip codes range from 90000-96999 which can be expressed “If zip Like “9[0-6]###?) that could save you many lines of code and hours of frustration. Finally, note that we used Lcase to force both ‘domain’ and ‘d’ variables to lower-case representations because Like does perform case-sensitive comparisons.

However, “If Lcase(domain) Like (“*? & Lcase(d)) Then? was not the statement we used in our code.  We used:

If (“.” & Lcase(domain)) Like (“*.” & Lcase(d)) Then

So, what’s up with adding those periods?  They are actually pretty darn important.  By prepending the periods we are ensuring that we do not generate a false positive for a similar domain.  For example, if some rebel scum created the domain “WeHateTheSith.org? then our comparison of “Lcase(domain) Like (“*? & Lcase(d))? would generate a match because “WeHateTheSith.org? DOES match “*sith.org?.  But, if compared using “.sith.org? we would ensure that we are only matching our own domains and no other similarly named domains because “.WeHateTheSith.org? does not match “*.sith.org?.

If the Like comparison is True then the addressee is internal and we exit out of the domain loop.  If it is False then we continue to loop through our domains until we have either found a match or looped through them all.  Once they have all been checked and all come up False then we know that we have an external addressee.

Hopefully, now, the value of using Like is coming into focus.  No doubt you will be able to find many uses for it in your own code.

15:    Else
16:        ‘This is a Notes name
17:        If Isnull(Arraygetindex(internalDomains, domain, 5)) Then
18:            Let IsInInternalDomain = False
19:        Else
20:            Let IsInInternalDomain = True
21:        End If
22:    End If
23:    End Function

This is the block of code that we drop into if we have been passed a Domino domain.  Because we do not have to worry about all these crazy sub-domains as we did with Internet domains, we can do a very simple check of our internal domain array looking for any occurrence of the addressee’s domain that was passed into the function using ArrayGetIndex.  By using “5″ for the comparison method we are forcing a comparison that is not case-sensitive and therefore do not need to use Lcase here.  If the comparison comes back Null that means that the addressee’s domain we were given does not match any of our internal domains and therefore is external.  If it does match, then it is internal.

We can now be certain that our code has properly validated the addressee’s domain as being either internal or external.  What is done with that information is up to you and your needs.  Even if you do not have a need for such a function, you should be able to find plenty of ways to use Like.  Keep it in mind next time you are refactoring code and you just may realize that you can flush out a lot of slow comparison code by employing the power of Like.


Tags: , , ,
Posted in Domino, SnTT, 3,863 views, 2 Comments
Digg This Submit to del.icio.us Submit to Technorati  

2 Responses

  1. Dennis van Remortel Says:

    Very nice. Thanks!

  2. Kali Says:

    Veuillez nous accorder 1 à two jours afin que cette régularisation soit productive et visible sur votre compte de trading.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

CC-GNU GPL Creative Commons License
The Conxsys Blog by Corey Davis is licensed under a Creative Commons Attribution 3.0 Unported License unless otherwise specified. Based on a work at conxsys.com/blog. All code on this blog is licensed under the CC-GNU GPL version 2.0 or later unless otherwise specified.

Copyright © 2006-2010 by Conxsys | Login | Powered by Wordpress | Template based on a design by Design4