mIRC Scripting
mIRC: Sockets File Download
In this tutorial, we will be downloading this rotated smiley picture.

By right clicking the picture and going to image property (this might be a little different depending on the browser) we can find the full address of the picture. In our case it's “http://www.zigwap.com/mirc/imgs/Smiley_Rotated.png”.
Creating a connection
We will be opening a connection (socket called “GetPic”) to the website, port 80 (http).
SockOpen GetPic www.zigwap.com 80
}
Requesting the file
The second part of the code will be out On SockOpen event. At this point we will be requesting the picture file via the GET header.
sockwrite -nt $sockname GET /mirc/imgs/Smiley_Rotated.png HTTP/1.1
sockwrite $sockname Host: www.zigwap.com $+ $crlf $+ $crlf
}
Receiving the data
This is the main part of our script: the On SockRead event. We are going to receive two things from the server, the header and the file’s content. It is important to separate these two parts as only the file’s content should be saved.
For demonstration purpose we can use the following code to echo everything received to our status window. From the content echoed in the status window you should be able to clearly see where the header ends and the file content starts.
var %a
sockread %a
echo -a %a
}
Results:
Date: Wed, 12 Nov 2008 00:51:25 GMT
Server: Apache
Last-Modified: Fri, 07 Nov 2008 16:57:49 GMT
ETag: "60341a9d-5af7-4914738d"
Accept-Ranges: bytes
Content-Length: 23287
Connection: close
Content-Type: image/png
The end of the header and the begging of the file’s content are determined by a blank line.
Now that we have learned about the header, we can move on to the actual file downloading part.
if (!$sock($sockname).mark) {
var %SockReader
sockread %SockReader
if (%SockReader == $null) { sockmark $sockname 1 }
}
else {
sockread &picture
bwrite $qt($mIRCdir $+ Smiley.png) -1 -1 &picture
}
}
Let’s take a close look at the code above. The event is divided into two sections. Depending on whether or not the socket mark is empty the script will do different things. (Note: although I used the socket mark, a simple variable can do the trick as well) The idea is to separate the header from the actual file.
var %SockReader
sockread %SockReader
if (%SockReader == $null) { sockmark $sockname 1 }
This part will read the received content into a variable, in our case %SockReader. Since the end of the header will follow a blank line, we will keep doing nothing until the variable read is null. At this point we will mark the socket.
sockread &picture
This part of the script will execute once the actual file content starts. This line of the code will read the line from the buffer into a binary variable, named “&picture”.
bwrite $qt($mIRCdir $+ Smiley.png) -1 -1 &picture
This is a very important line. We use the /bwrite command to write the content of the binary variable, &picture, to file. The file we save the data to will be called “Smiley.png”, saved to the $mIRCDir. The first -1 parameter tells mIRC to append the data to the file. The second -1 parameter tells mIRC to write everything to the file.
Remember the header? We can use it to make sure we have saved the whole file.
This line should echo the size of the file, which should also match the size the server told us in the header.
File Size: 23287
Content-Length: 23287
Now that we have downloaded the picture, let’s see it! This alias below will draw the picture to a custom picture window.
window -dp @ZigWap -1 -1 270 270
drawpic @ZigWap -1 -1 $qt($mIRCdir $+ Smiley.png)
}
Here is the complete code:
SockOpen GetPic www.zigwap.com 80
}
On 1:SockOpen:GetPic:{
sockwrite -nt $sockname GET /mirc/imgs/Smiley_Rotated.png HTTP/1.1
sockwrite $sockname Host: www.zigwap.com $+ $crlf $+ $crlf
}
On 1:SockRead:GetPic:{
if (!$sock($sockname).mark) {
var %SockReader
sockread %SockReader
if (%SockReader == $null) { sockmark $sockname 1 }
}
else {
sockread &picture
bwrite $qt($mIRCdir $+ Smiley.png) -1 -1 &picture
}
}
Alias ShowPic {
window -dp @ZigWap -1 -1 270 270
drawpic @ZigWap -1 -1 $qt($mIRCdir $+ Smiley.png)
}
The method used in this tutorial is rather slow. mIRC is required to trigger the event for each line received. An alternative quicker method is to keep looping.
sockread &picture
bwrite $qt($mIRCdir $+ Smiley.png) -1 -1 &picture
}

