mScriptBox Tutorial
The mIRC Connection Identifier (CID)

Written by by Cobra^
Published with permission.

Table Of Contents


1. Introduction  Back to Top

CID or (Connection ID) is a unique number given to a connection. Each connection has its own unique number associated to it. CID values are assigned when the status window for a connection is created, and they are freed when the status window for that connection is closed (NOT when it simply gets disconnected).
So the "Connection ID" is not strictly for Connected connections, but instead assigned to all open status windows.

The CID Value for a given connection does not change once the status window for that connection has been opened. If you close a connection the CID value for that connection is no longer in use, and wont be used by any future connections without resetting mIRC.

Important Note: Anything and Everything in mIRC is now bound to a specific CID value. Remotes when triggered are Temporarily bound to the CID of the connection which called it. Same for aliases, popups, timers, commands, identifiers, windows ... everything :P The only way to reference commands and identifiers from other connections besides the one your currently bound to is with the use of the commands and identifiers explained below.

Here is a simple example to help you better see how CID's are assigned and used.

on 1:CONNECT: { echo -s *** Hello, This Connection is using the CID: $cid }

The on CONNECT event will run each time you connect to a server. So try connecting to 1 server, then using mIRC's multi server feature and connect to others, like 3 or 4 just to get the idea.
As you can see from this, we did not need any special $scon or $scid identifiers, because this remote is bound to the CID that called it. So anytime a script runs, the code in it will run as though the connection that called it was the only server your connected to (this way you DON'T have to go though changing your entire script for the new 6.x :)


2. Basic Identifiers   Back to Top

mIRC has Provided the following identifiers to help you work with the CID:
$cid - Returns the CID value for the 'Active Connection'.
$activecid - Returns the CID value for the Active Window.
$lactivecid - Returns the CID value for the previous Active Window.

$cid is a reference identifier used to tell you which connection mIRC is looking at for the currently running script. For example, if you check $cid in a remote event, it will return the CID value for the connection that triggered that event. If used in an alias it will return the CID value for the connection that the alias was called from. If used in a windows edit box it will return the CID value for the active window.

$activecid is an extremely useful identifier. This identifier can be called from anywhere, and it will always return the CID value for the active window. This way if your like me, and have a timer update your titlebar with active connection info, the timer although bound to a connection, can check $activecid to get information for the connection that the active window is bound to :)

$lactivecid is used the exact same as $activecid, except it returns the CID for the previously active window. This would be used in scripts that would need to detect a change in the connection your currently viewing.


3. Scripting with the CID   Back to Top

As shown above $cid refers to the connection id of the connection used to call the given script, be it remote, alias, or popup.
You may however want to be able to call an alias to perform operations on many different connections, rather than just the active one.

To do this we use $scon(), $scid(), /scon, and /scid.

'SCON' stands for 'Server Connection', where as 'SCID' stands for 'Server Connection ID'

According to the mIRC help file:

	$scid(N)[.id] - Returns the connection id, where N is a $cid value.

	$scon(N)[.id] - Returns the connection id, where N is the Nth connection.

	If N = 0, returns total number of open server windows.

	If you specify a property which is an identifier, it returns the value of
	that identifier for that connection. This also works for custom identifiers.

	Note: The property cannot use brackets.

These identifiers work basically the same way except $scid() allows you to refer to a connection by its CID, and $scon() allows you to refer to your connections in order starting with 1, and going till the last.
As just a quick example, lets say for whatever reason you needed a script to check to see what nick your using on each connection. Your first though, "simple just use an alias to check $me .. ahh but $me for which connection? Remember everything you do now is bound to a single connection. The .id property was added as a universal way to allow any identifier to be called bound to a connection you specify.


me.loop {
   VAR %ctr = 0
   VAR %tot = $scon(0)
   WHILE (%ctr < %tot) {
     INC %ctr
     echo -a * %ctr - $scon(%ctr).me
   }
}

The above example alias will echo all your nicknames for all your connections regardless of where it is called from. It gives u the value of "$me" for every connection :) simple huh?
Here is another example which displays a bit more information.


alias connection.info {
   echo -a *** Listing Connection Info ***
   VAR %ctr = 0
   VAR %tot = $scon(0)
   WHILE (%ctr < %tot) {
      INC %ctr
      echo -a * Connection %ctr - CID: $scon(%ctr).cid Status: $scon(%ctr).status Nick: $scon(%ctr).me
   }
}
Below you will learn an easier way to do this by using /scon.
We have the following commands for working with mIRC's CID value:
	/scon [-r|-a|-tM] N [command]  - Where N is the N'th connection
	/scid [-r|-a|-tM] N [command]  - Where N is a CID value

Note: The [ ]'s signify an optional argument. Both are used to force mIRC to be bound to a given connection until the script, or command completes, or until the -r switch is used.
/scon is used when working with the N'th connection, this allows you to reference connections 1-? instead of having to know the exact CID value for each one.

/scid is used when working with specific CID values.

These commands are used to change the "Active Connection" to the location you specify until the script completes or until you use the -r switch is used.

	-r  is Used to reset the connection back to the default connection for that running script.
	-a  Performs the command on all Connection ID's
	-tM Where M =
	    1 - Perform on Connected Servers
	    2 - Perform on Servers that are Not Connected
	    4 - Perform on Servers that are Connecting
	    8 - Perform on Servers that are Not Connecting (either connected or not connected) 
If you specify a "Command" argument the active connection will only be changed while running that single command.
Note: Just like when using timers, identifiers are evaluated before they are sent to an alias.

If you have the command "scon 1 echo -a * $cid" in a script it will not always echo the $cid for the 1st connection because $cid will be evaluated before the connection is changed. (However, when running the commands by the command line this doesn't mater unless you use //)

To resolve this issue we can use ! to delay mIRC from evaluating it. For Example:

	scon 1 echo -a * $!cid

We can use the -a or -tM switches to send a command to all connections, or all connections with a specific status. For example, lets say you wanted to msg all channels, on all connections that you were going away, for that you would use this:

	/scid -a ame is away, Reason: food!
Or to be more accurate, as we may have some connections that are not connected, use -tM
	/scid -t1 ame is away, Reason: food!
If you wanted to msg all channels on all connection and say hi to that network. You would use this:
	/scid -t1 amsg hi $network

Above when talking about $scon() and $scid() we gave an example to show information about your connections. Here is a way you might use /scon or /scid to help you with this.

alias connection.info {
   echo -a *** Listing Connection Info ***
   VAR %ctr = 0
   VAR %tot = $scon(0)
   WHILE (%ctr < %tot) {
      INC %ctr
      scon %ctr
      echo -a * Connection %ctr - CID: $cid Status: $status Nick: $me
   }
}

As you can see we eliminated the need to use $scon().id so many times by changing the active connection over for each one.
If you have a lag detector you will most likely need to convert it over to using scon and scid so that it will work with all your connections.

Although this is incomplete code you will hopefully get the idea, your lag detector may look something like this when finished:


alias lag.chk {
   VAR %ctr = 0
   VAR %tot = $scon(0)
   WHILE (%ctr < %tot) {
      INC %ctr
      scon %ctr
      SET $+(%,lagchk.,$cid) $ctime
     .raw lusers
   }
   scon -r
}

We loop though $scon(), and change the "Active Connection" over to each one as we work with it. We could have simply used the scon -tm switch but odds are you will want to do some if-then-else checking for each connection anyway. Notice how I looked though $scon(), then used /scon to tell mIRC to now use that connection as its active connection. This way $cid returns the CID for each scon() we loop though, and the lusers command goes to the server associated to the scon that we are looping though.


4. Conclusion  Back to Top

As you can see scon and scid are going to be very useful with the birth of multi server in mIRC v6.0 and future versions.
I hope that this file has helped you beter understand the CID, what its for, and how you can use it to improve your script.

If you have any questions/comments/corrections regarding this document feel free to contact me online or via email

Thanks, and happy scripting :)

Cobra^ @#mIRC - Webnet