(Obama / McCain) - Socket + Dialog Tutorial

By Kirby on Mar 08, 2009

I really like these "_____isyournewbicycle.com" sites. :<
It was originally a small alias that messaged the active window a random way from a site, but I decided to change it to a small dialog for solely one reason:

[size=25]I was bored.[/size]

"Why is the title of the snippet called 'tutorial'?", you say.
Well, since it was such a small snippet to work with, I decided to change it into a small guide on how to use sockets and dialogs effectively.
Although this is probably not the best way of teaching them, learning through snippets can help someone out too.

To use: Just open up the dialog and click the underlined links. It's THAT simple guys.

If you are new to either sockets or dialogs, I suggest you read the code if you are interested.
If not, or you just want the good stuff, you can use the smaller version.

All in all, this script, really, is under 15 lines. The explanations just make this look larger. xD

Oh, and here's a picture:
Image

Enjoy! :D

/*
This is a small, basic example of how to use sockets and dialogs.
I've decompressed the script to make it easier on your eyes and provided some hints and explanations along with almost every line that is part of this script.
Using both sockets and dialogs at the same time in a script can turn out powerful.
Have fun! :-)
*/

;Obama / McCain sockets

alias -l obama {
  ;This is one of the aliases in the script, used for opening a socket towards "www.barackobamaisyournewbicycle.com". The '-l' switch signifies that this is a local alias.
  if ($sock(obama)) { sockclose obama }
  ;This means that if there's a socket called 'obama' open, it closes it. It's not necessary for all scripts, but because I'm being extra careful to avoid errors, I did so.
  sockopen obama www.barackobamaisyournewbicycle.com 80
  ;As previously mentioned, this opens the socket called 'obama' towards "www.barackobamaisyournewbicycle.com" at port 80.
}

on *:sockopen:obama: { sockwrite -nt $sockname GET / HTTP/1.0 | sockwrite -nt $sockname Host: www.barackobamaisyournewbicycle.com $+ $crlf $+ $crlf }
;This is the event when the socket 'obama' has made a successful connection; it writes through a 'GET' method in the '/' directory (happens to be the homepage), on HTTP 1.0.
;Host must be supplied and make sure you have $crlf, or the carriagereturn/linefeed combination.

on *:sockread:obama: {
  ;This is when info is ready to be read through the socket connection made.
  sockread %obama
  ;This command reads bytes (page source) to receive buffer from the given variable (%obama in this case).
  if (<A href="http://barackobamaisyournewbicycle.com/"> isin %obama) { did -o tutorial 3 1 $htmlfree(%obama) }
  ;This part really depends on what you're looking for.
  ;If you find what you want in a specific line, you take the html tags in that line and insert it into the 'if' inside the given variable.
  ;Then, you can supply a command (in this case, overwriting the blank 'link' portion of the dialog.
}

;The same thing applies for McCain as it did for Obama. Literally, only the socket name, address, port, and the page source that you want, changes.

alias -l mccain {
  if ($sock(mccain)) { sockclose mccain }
  sockopen mccain www.johnmccainisyourjalopy.com 80
}
on *:sockopen:mccain: { sockwrite -nt $sockname GET / HTTP/1.0 | sockwrite -nt $sockname Host: www.johnmccainisyourjalopy.com $+ $crlf $+ $crlf }
on *:sockread:mccain: {
  sockread %mccain
  if (<title> isin %mccain) { did -o tutorial 4 1 $remove($htmlfree(%mccain),$chr(9)) }
}

;Htmlfree

alias -l htmlfree { var %x, %i = $regsub($1-,/(^[^<]*>|<[^>]*>|<[^>]*$)/g,$null,%x), %x = $remove(%x, ) | return %x }
;As the title of the alias explains, using regex, it "frees" the html tags in the buffer.
;Here's an example: $htmlfree(<title>Hawkee</title>) would return 'Hawkee'.

;Dialog/Menu portion

dialog tutorial {
  ;'tutorial' is the name of the dialog that we will be working with (name is supplied after /dialog).
  title "Barack Obama / John McCain (Sockets + Dialog Tutorial)"
  ;The "title" is the stuff you see on the bar when you open up the dialog.
  size -1 -1 315 36
  option dbu
  box "Barack Obama", 1, 5 3 153 28
  box "John McCain", 2, 157 3 153 28
  link "", 3, 18 11 127 14
  link "", 4, 169 11 127 14
}
;After the '/dialog' command, the rest is basically the format of the dialog.
;There are many prefixes that you can use in dialogs, but we will only be working with links today.

on *:dialog:tutorial:*:*: {
  ;The general event for when the dialog is opened.
  ;'tutorial' is the name of the dialog, and the rest of the asterisks supplied are for the dialog events and id's.
  if ($devent == init) && ($did == 0) { obama | mccain }
  ;'init' simply is a shortened version of 'initialization'.
  ;Because there cannot be a dialog id '0', you can add 'if ($did == 0)' to the initialization event if you want, but it is not necessary.
  ;In this case, it performs the two aliases, 'obama' and 'mccain', which are used for opening up the two sockets.
  if ($devent == sclick) {
    ;If the dialog event is a single-click, do this:
    if ($did == 3) { obama }
    ;If the dialog id is 3, then perform the command 'obama'.
    if ($did == 4) { mccain }
    ;Same thing. If the dialog id is 4, then perform the command 'mccain'.
    ;Dialog id's 3 and 4 are the two links, 'link "", 3, 18 11 127 14' and 'link "", 4, 169 11 127 14'
  }
  if ($devent == close) { unset %obama %mccain }
  ;If the dialog event is to close, it unsets the two variables used in this whole script, %obama and %mccain.
  ;This event isn't necessary, but sometimes comes in handy when you are using variables that don't unset unless you manually do so.
  ;Really, this is a 'clean-up' event.
}

alias tutorial { dialog $iif($dialog(tutorial),-v,-m) tutorial tutorial }
;Opens up the dialog 'tutorial', but with a few modifications.
;What that means is, if the dialog 'tutorial' is open, make the dialog the active window. If not, create a 'modeless' dialog using 'table'.

menu * {
  Barack Obama / John McCain : tutorial
}
;Simple menu prefix. The name is 'Barack Obama / John McCain', and succeeding the colon is the alias 'tutorial', for opening up the dialog.

/*
If you have actually spent the time reading all of this, then I give you my regards.
To 'play' with this, just open up the dialog, and click each of the links to get a new quote.
*/

Comments

Sign in to comment.
troll   -  Aug 30, 2011

ill check it out tomorrow looks good

 Respond  
Kirby   -  Apr 22, 2009

Oh I see.

 Respond  
Jonesy44   -  Apr 22, 2009

the commemts in the script

 Respond  
Kirby   -  Apr 22, 2009

I don't understand what you're saying jonesy44. D;

 Respond  
Jonesy44   -  Apr 22, 2009

Lol. nice one with the tut idea. Script is a bit.. urr pointless, but i'd have killed to have these comments when i was learning sockets (;

 Respond  
Kirby   -  Apr 21, 2009

I've just realized that the http://johnmccainisyourjalopy.com no longer is available. However, the Obama site still works.
(Don't blame me for that).

 Respond  
PATX   -  Apr 21, 2009

rating: 5 (average)
reason: good kinda tut thing lol.

 Respond  
Kirby   -  Mar 23, 2009

Actually, this is is more of an "explanation", rather than a "tutorial" (because tutorials have to explain all the basic princicples!).
Woops.

 Respond  
ReFuSeR   -  Mar 09, 2009

LOL Kirby... This snippet was random but very impressive. I have not mastered those darned sockets yet.

 Respond  
Kirby   -  Mar 08, 2009

I see...

 Respond  
tv3636   -  Mar 08, 2009

I tried having the /did way too early and making a loop that said "Loading" until the variable I was grabbing changed from its original value. It didn't work, lol.

 Respond  
Aucun50   -  Mar 08, 2009

Nice kirby this could be handy Ty.

 Respond  
Kirby   -  Mar 08, 2009

Hey there tv3636.
Lol, crashing? What did you do?!?

 Respond  
tv3636   -  Mar 08, 2009

Haha, I was just working with a dialog that used sockets today for the first time. Not as hard as I thought it would be, actually. Although I did endlessly loop mIRC into crashing :P

Nice guide/script, looks helpful.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.