MessageSending Pop-Up Messages

Send a network message from your own application

There is a nice little pop-up program in the NetMail e-mail program informing you about new e-mails, dates or server errors. With the freeware CapiDog, it is even able to align the time on all workstations if an ISDN adapter is installed in one, using the time which the public switch sends each time when an outgoing connection is made. But even better, it is not too complicated to send pop-up messages from your own application to the screen of any workstation.

'Visual Basic snippet for sending a mailslot msg.
Option Explicit: DefLng A-Z
Const PopAdr$ = "*" 'To all workstations

'Windows API: File open/close functions
Private Declare Function CreateFile Lib"kernel32" _
 Alias "CreateFileA" (ByVal lpFileName As String, _
 ByVal dwDesiredAccess As Long, ByVal dwShareMode _
 As Long, ByVal lpSecurityAttributes As Long, _
 ByVal dwCreationDisposition As Long, _
 ByVal dwFlagsAndAttributes As Long, _
 ByVal hTemplateFile As Long) As Long
Private Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, ByVal lpBuffer As Any, _
 ByVal nNumberOfBytesToWrite As Long, _
 lpNumberOfBytesWritten As Long, _
 ByVal lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib _
 "kernel32" (ByVal hHandle As Long) As Long

'File open constants
Private Const OPEN_EXISTING = 3
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_ATTRIBUTE_NORMAL = &H80

Private Sub SendPopup(e$)
'Sends message text e$ to destination PopAdr$
 Dim f$, i, h
 f$="_MyApp" &vbNullChar & PopAdr$ & vbNullChar &e$
 h = CreateFile("\\*\mailslot\shamrock", _
  GENERIC_WRITE, FILE_SHARE_READ, 0&, _
  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
 If h Then
  WriteFile h, f$, Len(f$), i, 0: CloseHandle h
 End If
End Sub

Mailslots: How they work

Mailslots were already used in the first MS-DOS network clients and made their way into even the newest Windows incarnations. They offer a simple way for interprocess communication, either within one PC, or between distinct workstations, whatever network protocol is used, such as TCP/IP, NetBEUI or IPX/SPX. Using TCP/IP, mailslot messages are sent as UDP broadcast packets within the local network segment (defined by the IP address/mask) with a maximum length of about 450 bytes.

To send a mailslot message from within a program, one has to open e.g. \\*\mailslot\shamrock just as a file, where * means a broadcast, and shamrock is the mailslot name. (By the way, the Microsoft Winpopup client uses messngr instead of shamrock in the mailslot path, but of course we find it much less comfortable, and you will agree if you compare both.)

A simple sample

The sample program is written in Visual Basic 4.0. It is not a complete application but only a code snippet showing the required Windows API declarations and a short subroutine which actually sends a text message, in this case to all workstations. You could change the asterisk behind PopAdr to a computer name, or a NetMail user name, and then only this workstation will see the message. The sender address shown in the pop-up window is simply "MyApp". The underscore before MyApp means that this is an automatically (not manually) sent message so that the popup program will not copy the sender's address into its own addressing field and also uses a different color to display it.

Unfortunately, the normal Open command of VB does not work for mailslots since it cannot handle such odd path names. This is why the file open/write/close functions of the Windows API must be used for this purpose. If you are not using VB, it should not be a big affair to translate this little program since it mostly consists of API calls.

Pop-up messages worldwide

NetMail 2.21 and later extends the range of pop-up messages to the Internet. Popup users of distant NetMail servers can exchange instant messages by adding an @ symbol and the domain of the destination system behind the remote username, e.g. *(at)example.dyndns.org. The Mailer module of NetMail receives pop-ups in HTTP format and relays them as mailslot messages into the local network.

This can also be used to send pop-up messages from a web server anywhere in the world to a NetMail system, e.g. status information. Address information and text are given as parameters in a HTTP GET request. NetMail returns "204 No content" as HTTP status. The request syntax is:
http://<domain>/sys/?f=*popup&t=<From>,<To>,<Password>,<URL_encoded_text>
A typical sample looks like this:
http://example.dyndns.org/sys/?f=*popup&t=Webserver,*,Secret,This+is+a+test

#!/usr/bin/perl
# Perl script: Pop-up message to a NetMail server
use LWP::Simple qw($ua get);
$dom='http://example.dyndns.org'; # NetMail domain
$path='/sys/?f=*popup&t=_Server,*,,';
$t = 'Hello world!'; # The text to send
for (0..255) # Prepare array for URL escaping
{ $esc{chr($_)} = sprintf("%%%02X", $_)
}
$ua->timeout(5); # 5 s timeout
get $dom.$path.urlencode($t); # Send message
 
# Encode special characters to %xx for URL
sub urlencode
{ my ($s)=$_[0];
  $s=~s/([^;\/:,A-Za-z0-9\-_.!~*'()])/$esc{$1}/g;
  return $s
}

The Perl sample code on the left shows how to send the text "Hello world!" to all workstations of the NetMail server with the domain example.dyndns.org. Using the LWP module with its simple HTTP functions makes the script quite straightforward. (Note: You cannot use this script to send messages to the common Winpopup program. It designed for a NetMail recipient only.)

In the path, the underscore in _Server means that the message is not created manually and an answer is not possible; the NetMail popup program does not update its address field if such a message is received.

The asterisk behind it means "to all workstations"; instead, a single username or computername might be used. A password can be used between to two following commas, if one is configured in NetMail as a protection against unwanted alien pop-ups.


05/2006 Shamrock Software GmbH