For the past couple of months, I've been demonstrating how you can use Perl with Windows Management Instrumentation (WMI). This month, however, I digress a tad and discuss a technology that's just plain fun: MSN Messenger Service.

Lately, I've found myself using MSN Messenger Service more and more. This service is similar to ICQ's and AOL's instant-messaging programs. These programs let you communicate with friends and colleagues over the Internet. You can type messages to one another and respond immediately. Unlike email, this type of messaging is pretty much instantaneous—the people you're chatting with see your message as soon as you press the Enter key.

Instant messaging is not only convenient but also fun. When friends and colleagues log on, you receive notification so that you can initiate a text-based chat with them if you'd like. Instant messaging is a great way to collaborate because you can chat with several people simultaneously.

MSN Messenger Service lets you modify your user information, or profile, whenever you want. However, to modify your profile, you have to navigate through and interact with the program. Because MSN Messenger Service exposes its COM-based object model for anyone to exploit, you can automate this process to make it even easier and quicker.

Exploiting the Object Model
You can control all aspects of MSN Messenger Service by procuring an instance of Messenger.MsgrObject, the main COM interface object in the object model. Listing 1 contains the code that you can use to obtain an instance of this object. In this code, the first line imports the in() function, which lets you later enumerate COM collections. The second line loads the MSN Messenger Type Library, which provides constant values. The last line creates the $Messenger object, which points to MsgrObject. At this point, you can use $Messenger to query and set properties and initiate actions. For example, you can see who's in your contact list, change your persona, and use events.

Listing Your Pals
Periodically, you might want to check whom you've added to your contact list in MSN Messenger Service. As the script in Listing 2 shows, you can use the List() method to enumerate the user accounts in your contact list.

The %STATE hash at callout A in Listing 2 identifies the possible states (e.g., online, idle, busy) that users can be in. The hash maps a state value to a descriptive string, which the script later prints.

The code at callout B in Listing 2 results in a COM collection object ($UserList) that represents all the contacts in your list. MLIST_CONTACT is a constant from the MSN Messenger Type Library you previously loaded into Perl's main namespace.

With the $UserList object in hand, you can enumerate each user from this collection and examine that user's properties. examines two properties: FriendlyName and LogonName. The FriendlyName property contains a user's friendly name (i.e., display name). If your friends are like mine, you'll end up seeing some pretty wild names, so you won't know which name represents which user. To solve this dilemma, the script retrieves each user's LogonName value, which is usually the user's email address.

The code at callout C in Listing 2 uses the in() function that you previously imported into the script. For each user that the $UserList collection holds, the in() function returns the respective user object and assigns it to the $User variable. This code also sorts the collection by the user's state. That way, when you print the results, all users that are online are in one group, all the users that are idle are in another group, and so on.

You can find and the other scripts I discuss this month in the Code Library on the Windows Scripting Solutions Web site ( To run this script on your machine, type


on a command line.

Changing Your Persona
Instant-messaging users sometimes like to change their friendly name to reflect their location, mood, or another aspect of their life. For example, my friendly name sometimes switches between Dave (home) and Dave (work) to specify whether I'm at home or at work. I've even changed my friendly name to Dave's machine crashed again! so that when people start to chat with me, they already know that I'm seething with frustration.

With the script in Listing 3, you can change your friendly name at any time. The script accepts one parameter: a string that specifies your new friendly name. If you don't pass in a parameter, the script simply displays your current friendly name.'s magic begins in the fourth line. The script first accesses a COM collection object called Services. This object contains a collection of Messenger Service objects that represent the various messenger services (e.g., Voice over IP—VoIP) you can use. Each Messenger Service object contains properties that specify the necessary profile information, such as your friendly name (FriendlyName) and logon name (LogonName).

The script then accesses the Services object's PrimaryService property. This property specifies which Messenger Service object—and hence which profile information—to use when you log on. Through the PrimaryService property, the script accesses the FriendlyName property in the applicable Messenger Service object. The script either prints your current friendly name (the code at callout A in Listing 3) or changes it (the code at callout B in Listing 3), depending on whether you pass in a parameter.

To use to change your friendly name, type

perl NewName

where NewName is your new friendly name. If you just want to display your current friendly name, don't include the NewName parameter.

Monitoring Events
Another way to automate MSN Messenger Service is to write a script that listens for and reacts to events. Events aren't easy to understand. To fully explain what events are and how they work is a column in itself (I'll discuss this topic in a future column), so for now, let's just have fun and look at an example of how to take advantage of instant messaging events.

MSN Messenger Service fires events in response to certain situations, such as when someone sends you a text message. You can write a script that performs actions when specific events occur. Take, for example, the script, which you can find in the Code Library on the Windows Scripting Solutions Web site. This script logs information when MSN Messenger Service fires an event in response to

  • you logging on (OnLogonResult event)
  • someone sending you a text message (OnTextReceived event)
  • a user joining or leaving your conversation (OnUserJoin and OnUserLeave events, respectively)

Listing 4 contains the main body of Callout A in Listing 4 highlights an important line in the script. After creating the $Messenger object, the script uses this code to register a Perl subroutine with the MSN Messenger Service's event interface so that the script can listen for events. If an event occurs, the script calls the ProcessEvents() function (more on this function shortly).

Callout B in Listing 4 highlights another important part of This code calls the SpinMessageLoop() method, which examines all incoming messages, possibly resulting in a call to the script's ProcessEvents() subroutine. SpinMessage-Loop() examines all messages and events that MSN Messenger Service has sent to Perl. The events that this function processes result in Win32::OLE calling back to the ProcessEvents() subroutine. Notice that the script falls asleep for 50 milliseconds (ms). This pause prevents the script from eating up CPU processing time.

Every time MSN Messenger Service fires an event, the script calls ProcessEvents(). This function checks for four particular events (i.e., OnLogonResult, OnTextReceived, OnUserJoin, and OnUserLeave events) and logs information to a file if one of these events occurs. For OnLogonResult events, the function logs the date and time you logged on. For OnTextReceived events, the function logs the sender's friendly name, logon name, and text message. For OnUserJoin and OnUserLeave events, the function logs the person's friendly name and logon name.

In addition to logging event information, the ProcessEvents() function automatically changes your friendly name to reflect the location from which you logged on. For example, I have four machines from which I log on to MSN Messenger Service. The %Location hash near the top of the code in Listing 4 identifies these machines and maps their locations.

When I log on to MSN Messenger Service, it fires the OnLogonResult event, which kicks the ProcessEvents() function into action. ProcessEvents(), in turn, calls the ModifyUserLocation() function to change my current friendly name to a new one based on the name of the computer I logged on to. So, if I logged on to the computer named patella, ModifyUserLocation() changes my friendly name to Dave (work). This feature makes the script handy for anyone who logs on to multiple machines in different locations.

Before you use, you need to customize the code in the %Location hash. You can then run the script by typing


on a command line.

Have a Blast
I hope this quick trip through the MSN Messenger Service object model has shown you that using the MsgrObject properties, methods, and events is pretty simple—and quite fun. But none of this excitement is possible unless you have the appropriate software. You can download MSN Messenger Service for free at Install this software, then have a blast using and automating it.