mIRC Scripting

mIRC: Initialization Files

Storing Information

An initialization file, or INI file, is a configuration file that contains data for certain things.

a typical INI file will look like this.

[section]
item=value
item=value
item=value

etc....

Basic Writing to an INI

/writeini -n <inifile> <section> <item> <value>

The -n switch is used when the file exceeds 65,536 bytes (64 KB). Its a good idea to place it there if you think the file will get pretty big in the future.

inifile is the actual name. or this tutorial's purposes we will make a simple birthday keeping script. This is how the INI file will look (populated):

[Birthday]
name=birthday
name=birthday
name=birthday
etc....

ON *:TEXT:!add birthday*:#:{
  if ($3) && ($regex($4,/^\d+\/\d+/\d+$/)) {
    writeini birthday.ini Birthday $3 $4
    notice $nick Birthday Saved.
  }
  else {
    notice $nick Syntax Error: !add birthday <NAME> <DD/MM/YYYY>
  }
}

*this regular expression by no means make sure the data entered correctly, but its good enough for the tutorial's purposes.

Now lets say we have 4 people entering their birthdays.

!add birthday Mike 10/10/1990
!add birthday Dave 23/05/1985
!add birthday Lisa 12/13/1981
!add birthday Jenna 28/02/1983

if we open the INI file (double click) or //run $mIRCdir $+ birthday.ini
You will notice that the file will have a structure similar to this:

[Birthday]
Mike=10/10/1990
Dave=23/05/1985
Lisa=12/13/1981
Jenna=28/02/1983

Basic Reading from an INI

var %variable = $readini(filename, [np], section, item)

The n switch is used when you do not want to evaluate the line. (this is especially helpful when you let the users save setting on your bot, you need to always think the worse of the users and how they might exploit your scripts)

The p switch is used to make mIRC evaluate pipes | as is instead of plain text.

Going back to the birthday script, now we want to read from the file.

ON *:TEXT:!birthday*:#:{
  if ($readini(birthday.ini,n,Birthday,$2)) {
    var %nick = $v1
  }
  else { var %nick = $readini(birthday.ini,n,Birthday,$nick) }
  if (!%nick) {
    notice $nick You Didn't Save Your Birthday Yet
  }
  else {
    notice $nick Age: $calc(($ctime - $ctime(%nick))/60/60/24/365) Years Old!
  }
}

The script will first check if the user specified in $2 actually found in the ini file. If not, find the user's birthday. If both are not in the ini file report that, if a birthday in the file, calculate the age.

Deleting Items and Sections

/remini <inifile> <section> [item]

The remini command can be used to remove a single item from a section as well as a whole section.

Example with out birthday script:

/remini birthday.ini Birthday mike

Will remove mike's entry from the ini file.

Other INI file manipulations

var %var = $ini(file,section/N,item/N)

This is a one of the more helpful identifier; it enables you to get the names of the section, the number of sections, the name of the items in a section, and the total items in a section.

Example with our birthday scrtipt:

$ini(birthday.ini,0)
; Will return the totral sections in the ini file, in our case its 1
$ini(birthday.ini,1)
; Will return the name of the first section, "Birthday"
$ini(birthday.ini,$ini(birthday.ini,1),0)
; The $ini(birthday.ini,1) identifier will return the 1st section in the ini file, when we insert that value into the original identifier we can get the total number of items in that section (changing 0 to 1, 2 etc.. will return the item name)