Akxl Labs C# ASP.NET Articles and Tutorials Akxl Labs Web Apps and Tools for Your Website

ASP.NET C# Instant Messenger Status Determination

Tags tagged as   Code: ASP.NET, C#, Web
A dotnet pluugin to display the status of an IM user on your website. Determine or Display your Status on AIM, Yahoo, MSN, ICQ, Jabber, or Skype.

Posted November 23, 2006    Viewed 7627 times    Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

The Source Code

First, we need some functions to determine the status of an AIM user. I do this using the http://www.imwrapper.com/ service to get the status, based on the protocol.

The first function figures out if the user is online:

1 protected bool GetIMStatus(string screenName, string protocol) 2 { 3 protocol = protocol.ToLower(); 4 5 string imStatusUrl = "http://www.imwrapper.com/" + protocol + "/" 6 + screenName + "/standard"; 7 8 System.Net.HttpWebRequest imStatusRequest = 9 (System.Net.HttpWebRequest) 10 System.Net.WebRequest.Create(imStatusUrl); 11 12 imStatusRequest.Accept="image/*"; 13 imStatusRequest.AllowAutoRedirect = true; 14 15 System.Net.HttpWebResponse imStatusResponse = 16 (System.Net.HttpWebResponse) imStatusRequest.GetResponse(); 17 18 return imStatusResponse.Headers["Content-Disposition"]. 19 Contains("online.png"); 20 }

GetIMStatus takes the following parameters, and returns a boolean, where true indicates that the user is online.
a screen name:
The screen name of the user to check. This value is generally case-insensitive, but this depends on the protocol.
a protocol:
The protocol to check on. Valid values are:
• AIM
• Yahoo
• MSN
• ICQ
• Jabber
• Skype
Note that these values are case-insensitive.

If that's all you want to do, then you only need that function. But I wanted a snap-in, linked status indicator.

So, we need a function to display something based on this information. A big part of this will be the link location. Most protocols support a way to launch a message window to a user via a link, so we'll need to construct a link for each protocol.

1 protected string GetIMStatusString(string screenName, string protocol) 2 { 3 return GetIMStatusString(screenName, protocol, "standard"); 4 } 5 6 protected string GetIMStatusString(string screenName, 7 string protocol, 8 string iconSet) 9 { 10 string html = ""; 11 12 try 13 { 14 protocol = protocol.ToLower(); 15 iconSet = iconSet.ToLower(); 16 17 string imStatusUrl = "http://www.imwrapper.com/" 18 + protocol + "/" 19 + screenName + "/" 20 + iconSet; 21 22 string statusString = 23 ((GetIMStatus(screenName, protocol)) ? "Online" : "Offline"); 24 string linkStart = ""; 25 string linkEnd = ""; 26 27 // jabber : xmpp:<sn> 28 // skype : skype:<sn>?chat 29 // yahoo : ymsgr:sendIM?<sn> 30 // aim : aim:goim?screenname=<sn> 31 32 switch (protocol) 33 { 34 case "aim": 35 linkStart = "<a href=\"aim:goim?screenname=" 36 + screenName 37 + "\">"; 38 linkEnd = "</a>"; 39 break; 40 case "yahoo": 41 linkStart = "<a href=\"ymsgr:sendIM?" 42 + screenName 43 + "\">"; 44 linkEnd = "</a>"; 45 break; 46 case "jabber": 47 linkStart = "<a href=\"xmpp:" 48 + screenName 49 + "\">"; 50 linkEnd = "</a>"; 51 break; 52 case "skype": 53 linkStart = "<a href=\"skype:" 54 + screenName 55 + "?chat\">"; 56 linkEnd = "</a>"; 57 break; 58 } 59 60 html = String.Format("<img src=\"{1}\" alt=\"{0}\" " 61 + "align=\"absmiddle\" /> {3}{0} as {2}{4}" 62 ,statusString 63 ,imStatusUrl 64 ,screenName 65 ,linkStart 66 ,linkEnd 67 ); 68 } 69 catch 70 { 71 // ... 72 } 73 74 return html; 75 }

GetIMStatusString takes the following parameters, and returns the HTML for an icon for the client and status, a string describing the screen name and status, and (where possible) a link that will launch a message window to contact the screen name.
a screen name:
The screen name of the user to check. This value is generally case-insensitive, but this depends on the protocol.
a protocol:
The protocol to check on. Valid values are:
• AIM
• Yahoo
• MSN
• ICQ
• Jabber
• Skype
Note that these values are case-insensitive.
optionally, an icon set:
Check http://www.imwrapper.com/ for icon set names. The default is 'standard', which is the first icon set on each list. All protocols also have 'default', which is the icon set actually used by the canonical clients. To find the icon set name, check the URL of the images in the set. For example, if an image's URL is 'http://www.imwrapper.com/images/icq/bevelled/offline.png', the icon set is 'bevelled'.

The string returned will render something like this:
IM: Unknown - Service Down

Articles and downloads sponsored by:
Thanks! Amazon commissions help me pay for textbooks.

The Custom Control

To make this code reusable, I made a custom control that can be snapped into a page. The control will render as the above status string. The source code that turns the above functions into a custom control is below. The control can be downloaded at the bottom of the page.

1 <%@ Control Language="C#" %> 2 3 <script language="C#" runat="server"> 4 private string _screenName = ""; 5 private string _protocol = "aim"; 6 private string _iconSet = "standard"; 7 8 public string ScreenName 9 { 10 get { return _screenName; } 11 set { _screenName = value; } 12 } 13 14 public string Protocol 15 { 16 get { return _protocol; } 17 set { _protocol = value; } 18 } 19 20 public string IconSet 21 { 22 get { return _iconSet; } 23 set { _iconSet = value; } 24 } 25 26 // [ THREE FUNCTIONS FROM ABOVE HERE ] // 27 </script> 28 29 <%= GetIMStatusString(ScreenName, Protocol, IconSet) %>

To use the control, you need two things:

The following code needs to be at the top of your page, to register the control.

1 <%@ Register tagprefix="akxl" 2 Tagname="IMStatus" 3 src="/controls/IMStatus.ascx">

The next code places the control into the page output. You can also add another attribute, IconSet="<string>" to specify the optional icon set parameter. All attributes directly map to the parameters described above.

1 <akxl:IMStatus id="akxlIMStatus" 2 runat="server" 3 ScreenName="ImTooSmartForMe" 4 Protocol="AIM" />

Here's a live example of the control:

IM: Unknown - Service Down

Download

You can download the control here:

IMStatus.zip (1K)

Comments & Feedback


Najmul Hoda says:
Sunday, August 24, 2008 @ 11:54 PM
Hi,
I have an urgent requirement to know the IM status of the employees working in my company.
For that I am using the code you have written above. But when I used this code I got Exception that object reference not found. The exception is because of the HttpWebResponse.Header does not has any ["Containt-Disposition"] property.
Can you please help me out as soon as possible.

Thanks
Leave this field blank:
Comment on this Entry
This work is licensed under a Creative Commons Attribution 3.0 United States License.
Please link to this article in your source code comments if you use this content.

Labs

Blog

The blog has moved.
Non-technical articles are now on a seperate site.
Contact me for the new address.

Apps

Real-Time Coffee Counter
add it to your website!
Golden Ratio Visualizer
a tool for design

Coffee Counter

Current Count:
Akxl Coffee Meter
Current Coffee:
 Peet's Malawi Songwe River

The Real-Time Coffee Meter is a free Website App from Akxl Labs. Text-only and badge versions available.