Writing a Bandwidth Monitor plugin for mIRC

In this tutorial, you will learn how to import data from Bandwidth Monitor 2 (Professional Edition) into a mIRC script. In order to begin, we must first configure Bandwidth Monitor's broadcast settings to tell it we are are intending to use live data in another application.

In Bandwidth Monitor, right click anywhere and select the 'Broadcasts' option from the menu. This opens the broadcast manager. It is from here that we will make all of our alterations.

Broadcast Manager

If "Enable broadcasts to operate on this system" is not already checked, check it now. This enables the use of Bandwidth Monitor broadcasts on your machine.

Click 'New..' to access the new broadcast properties.

New Broadcast

Ensure that the selected broadcast type is 'Data', and select a port to use for your broadcast. It is recommended that you use a port which will not interfere with other programs. If you wish to follow this tutorial exactly, we have used port 7001. Remember to close/secure this port on your firewall unless you plan to access broadcast data externally. Click OK to finish configuring this broadcast, and click OK again in the Broadcast Manager to dismiss it.

Bandwidth Monitor is now fully configured to communicate with mIRC. Now, we can begin to write the mIRC script which will access the data that Bandwidth Monitor has exposed for us.

Note: This tutorial assumes reasonably advanced knowledge of mIRC scripting. The mechanics of the methods we will be using will not be explained in detail.


Open your mIRC script editor and create a new script.

The first piece of code we will write is an alias to return data tags. The reason for doing this is that it is illegal to use the '%' symbol in a mIRC script unless it is with reference to a variable.

alias bmtag {
     return $chr(37) $+ $1 $+ $chr(37)
}

Upon calling $bmtag( Adapter:Name ), for example, we will be returned %Adapter:Name% (the same, with added % symbols). Next, we must define the main alias we will use to call our bandwidth data. Here, we have used the word 'bmstats' as our alias;

alias bmstats {
%bmstring = ECHO $bmtag(Adapter:Name) is downloading at $bmtag(Adapter:MbitInPerSec) $+ Mbit/sec
.sockclose bmsock
.sockopen bmsock 127.0.0.1 7001
}

Here, we are compiling a string of the same format that we would like to be returned - with all the data tags placed as appropriate (this is the same concept as found in the design of Bandwidth Monitor's themes). We begin the string with the command 'ECHO', which instructs the Bandwidth Monitor broadcast that it should echo this string with all tags processed as we want.

Any existing socket we might be using is closed, and the socket is re-opened - connecting to the local machine on port 7001, or whichever port you chose your broadcast to reside on earlier in this tutorial.

The next piece of code must handle the successful connection of the socket, this is simple;

on 1:sockopen:bmsock: {
sockwrite bmsock %bmstring
}

Here we simply deliver the command string to Bandwidth Monitor, and await its response. Handling the response from Bandwidth Monitor is also simple;

on 1:sockread:bmsock: {
sockread -f %bmstring
msg $active $gettok(%bmstring, 2-, 58);
.sockclose bmsock
}

This code reads the entire response into the same variable we used to send our data. The response from Bandwidth Monitor is of the following format:

ECHO:Data with all tags replaced

To use this, we must remove the ECHO: portion of the response - leaving us with our desired result, ready to deliver to the active window. Finally, we close the socket that we have now finished with. Save the script and call /bmstats in a channel to test.

Script Test

This concludes the tutorial.




Back to Tutorials