Tuesday, August 28, 2007

Converting Host Name To IP Address

This code was found on a Visual Basic web site and converted to LotusScript. It is a LotusScript class that allows you to convert a host name to its IP address. To use, create a new HostName object. Then check the IPAddress property of the new object to get the IP address. The property is a string.

This code is best stored in a script library. Create a new script library, then go to the (Declarations) section. The first part of that section is some constants:

Public Const IP_SUCCESS = 0
Private Const WSADescription_Len = 255 ' 256, 0-based
Private Const WSASYS_Status_Len = 127 ' 128, 0-based
Public Const WS_VERSION_REQD = &H101
Public Const MIN_SOCKETS_REQD = 1
Public Const SOCKET_ERROR = -1

After that, a custom Type is needed, still in the (Declarations) section.

Public Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To WSADescription_Len) As Integer
szSystemStatus(0 To WSASYS_Status_Len) As Integer
wMaxSockets As Long
wMaxUDPDG As Long
dwVendorInfo As Long
End Type

Note: If you are using R6, then you can define szDescription and szSystemStatus as Byte arrays instead of Integer arrays.
Next comes the Windows API calls we will need to convert the host name to the IP address:

Declare Function gethostbyname Lib "wsock32" (Byval hostname As String) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (xDest As Any, xSource As Any, Byval nbytes As Long)
Declare Function lstrlenA Lib "kernel32" (lpString As Any) As Long
Declare Function WSAStartup Lib "wsock32" (Byval wVersionRequired As Long, lpWSADATA As WSADATA) As Long
Declare Function WSACleanup Lib "wsock32" () As Long
Declare Function inet_ntoa Lib "wsock32.dll" (Byval addr As Long) As Long
Declare Function lstrcpyA Lib "kernel32" (Byval RetVal As String, Byval Ptr As Long) As Long

Finally, the actual class definition comes. This class definition is pretty simplified (there aren't a lot of properties/methods).

Class HostName
Private HostNameStr As String
Public IPAddress As String
Public ErrMsg As String
Public Error As Integer
Sub New(host As String)
If SocketsInitialize() Then
Me.IPAddress = GetIPFromHostName(host)
Me.Error = 0
Me.ErrMsg = ""
If Not SocketsCleanup Then
Me.Error = 200
Me.ErrMsg = "Windows Sockets error occurred in Cleanup."
End If
Me.Error = 100
Me.ErrMsg = "Windows Sockets for 32 bit Windows is not successfully responding."
Me.IPAddress = ""
End If
End Sub
End Class

Some additional functions are needed. Those are defined below. They are still part of the script library.

Private Function SocketsInitialize() As Integer
Dim success As Long
SocketsInitialize = (WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS)
End Function

Private Function SocketsCleanup() As Integer
If WSACleanup() <> 0 Then
SocketsCleanup = False
SocketsCleanup = True
End If
End Function

Private Function GetIPFromHostName(Byval sHostName As String) As String
Dim ptrHosent As Long
Dim ptrName As Long
Dim ptrAddress As Long
Dim ptrIPAddress As Long
Dim dwAddress As Long
ptrHosent = gethostbyname(sHostName & Chr(0))
If ptrHosent <> 0 Then
ptrName = ptrHosent
ptrAddress = ptrHosent + 12
CopyMemory ptrAddress, Byval ptrAddress, 4
CopyMemory ptrIPAddress, Byval ptrAddress, 4
CopyMemory dwAddress, Byval ptrIPAddress, 4
GetIPFromHostName = GetIPFromAddress(dwAddress)
End If
End Function

Public Function GetIPFromAddress(Address As Long) As String
Dim ptrString As Long
ptrString = inet_ntoa(Address)
GetIPFromAddress = GetStrFromPtrA(ptrString)
End Function

Public Function GetStrFromPtrA(Byval lpszA As Long) As String
GetStrFromPtrA = String$(lstrlenA(Byval lpszA), 0)
Call lstrcpyA(Byval GetStrFromPtrA, Byval lpszA)
End Function

(author is here http://www.sbacode.com/pageTips.aspx?id=207&)

Document values "changer"

I'm sure that every Lotus Notes developer during debugging has problems with changing values of fields. So I want to show a code which will help you (I hope) in this. Just try it !

(author is here http://www.sbacode.com/pageTips.aspx?id=208&)

REM {Get a listing of all the fields on the current document};
List := @DocFields;

REM {Possible data types to choose from.};
REM {I called Number Integer because use keyboard to select what you want with keyboard quicker.};
DataTypes := "Text" : "Date" : "Integer" : "Password" : "Name" : "Common Name" : "**** Remove Field ****" : "Text Multi Value" : "Date Multi Value" : "Integer Multi Value" : "Name Multi Value";

REM {Prompt for which field needs to be updated.};
EditField := @Prompt([OkCancelList]; "Select Field To Update"; "Select the field you wish to update:"; ""; List : "**** ADD A NEW FIELD ****");

REM {If adding a new field, prompt for the field name};
NewFieldName := @If(EditField = "**** ADD A NEW FIELD ****"; @Prompt([OkCancelEdit]; "Enter Field Name"; "Enter the name of the new field:"; ""); "");
CheckFieldName := @If(@IsMember(NewFieldName; List) & NewFieldName != ""; @Return(@Prompt([Ok]; "Already In List"; "The field " + NewFieldName + " already exists on the document.")); "");
UpdateVariable := @If(NewFieldName = ""; ""; EditField := NewFieldName);

REM {Prompt for which data type you would like the data to be};
REM {This needs to be done before value prompt to determine if the};
REM { Picklist or any prompting needs to be used.};
DataType := @Prompt([OkCancelList] : [NoSort]; "Choose Data Type"; "Please Select the correct data type or action for field: " + EditField; "Text"; DataTypes);

REM {For multi-valued fields, let the user choose the separator to use};
Separator := @If(@Contains(DataType; "Multi Value"); @Prompt([OkCancelList] : [NoSort]; "Choose Separator"; "Choose the separator to split out your multiple values"; ":"; (":" : ";" : "+" : "-" : "*")); "");

REM {Pull out the current value of the field};
CurrValue1 := @Eval(@Text(EditField));
CurrValue2 := @Abstract([TextOnly]; 254; ""; @Text(EditField));
CurrValue := @If(@IsNumber(CurrValue1) | @IsTime(CurrValue1); @Implode(@Text(CurrValue1); Separator); CurrValue2 != ""; CurrValue2; @Implode(@Text(CurrValue1); Separator));

REM {Based on what type of data is being entered different prompts will happen if any at all.};
RawValue := @If(
@Contains(DataType; "Name Multi Value"); @PickList([Name]);
@Contains(DataType; "Name"); @PickList([Name] : [Single]);
DataType = "**** Remove Field ****"; "";
@Contains(DataType; "Multi Value"); @Prompt([OkCancelEdit]; "New Value"; "Please enter the new desired value for: " + EditField + " seperated with " + Separator + " for each value."; CurrValue);
@Prompt([OkCancelEdit]; "New Value"; "Please enter the new desired value for: " + EditField + "."; CurrValue)

REM {If data conversion doesn't work then don't set field.};
DataType = "Date"; @If(@SetField(EditField; @TextToTime(RawValue)));
DataType = "Integer"; @If(@IsError(@TextToNumber(RawValue)); ""; @SetField(EditField; @TextToNumber(RawValue)));
DataType = "Password"; @SetField(EditField; @Password(RawValue));
DataType = "**** Remove Field ****"; @SetField(EditField; @DeleteField);
DataType = "Text Multi Value"; @SetField(EditField; @Explode(RawValue; Separator));
DataType = "Date Multi Value"; @SetField(EditField; @TextToTime(@Explode(RawValue; Separator)));
DataType = "Integer Multi Value"; @If(@IsError(@TextToNumber(@Explode(RawValue; Separator))); ""; @SetField(EditField; @TextToNumber(@Explode(RawValue; Separator))));
DataType = "Name Multi Value"; @SetField(EditField; @Explode(@Name([Canonicalize]; RawValue); Separator));
@SetField(EditField; RawValue)

Wednesday, August 22, 2007

CGI variables (Remote_Addr)

here are some examples of getting the CGI variables from a web-agent.

Sub Initialize
Dim session As NotesSession
Set session = New NotesSession
Dim doc As NotesDocument
Set doc = session.DocumentContext
Print "IP address =" + doc.Remote_Addr(0)
End Sub

public class JavaAgent extends AgentBase {
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Document doc = agentContext.getDocumentContext();
getAgentOutput().println("IP address = " + doc.getItemValueString("Remote_Addr"));
catch(Exception e) {

Thursday, August 09, 2007

office's "moved"

Our office "moved" to another place. now I ride to office usually near 20-30 minutes. Not bad. Really.