mIRC Home    About    Download    Register    News    Help

Print Thread
Simple Socket Script #240015 15/12/12 09:54 PM
Joined: Dec 2012
Posts: 10
M
ManaPot Offline OP
Pikka bird
OP Offline
Pikka bird
M
Joined: Dec 2012
Posts: 10
Hi everyone. I've been banging my head on the wall for the past 4 or so hours trying to get a socket script to work. I'm really new to scripting, and working on my first chat bot. I would really appreciate it if somebody could help me out so I can continue to work on this project.

I understand how sockets work, kind of. Like I said, I'm really new. I do a lot of website coding though, so it's not too different from PHP. I've spent quite a bit of time playing around with socket scripts (from tutorials I've read), but I can't get anything to work for me. Asking for help is usually my last resort, so I'm definitely "in a pickle" here.

All I really need is a socket script (as simple as possible please, so I can see what's going on) that will grab the text displayed on a web page of mine. The script should then output the text to the chat channel the bot is currently in ($chan, right?). That's it, nothing fancy.

The page I need to get text from is: http://www.d3sanc.com/db/manabot/index.php

User types !example -> script connects to web page -> gets the text that is displayed on the page -> outputs it to the chat channel for everybody to see.

So anytime somebody types !example the bot says "test" in chat. That's it.

Thank you in advance!

Last edited by ManaPot; 15/12/12 09:57 PM.
Re: Simple Socket Script [Re: ManaPot] #240018 16/12/12 12:32 AM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
How about showing what you currently have and them someone can show you what you missed? You'll learn how to do it much better that way.


Invision Support
#Invision on irc.irchighway.net
Re: Simple Socket Script [Re: Riamus2] #240020 16/12/12 03:06 AM
Joined: Dec 2012
Posts: 10
M
ManaPot Offline OP
Pikka bird
OP Offline
Pikka bird
M
Joined: Dec 2012
Posts: 10
I really don't have anything to be honest. As I said, I just read quite a few tutorials and used some of the example codes they provided. I've been coding PHP since I was about 13, self-taught. I'm good with picking apart code and learning from it usually. I just can't figure out why I can't get it to properly get "test" from the page and output it. The connection and all of that is fine, I understand that.

http://forum.swiftirc.net/viewtopic.php?f=36&t=60
http://forum.swiftirc.net/viewtopic.php?f=36&t=4378
http://forum.swiftirc.net/viewtopic.php?f=36&t=17810

Are some of the tutorials / guides I used. The most I was ever able to accomplish is having the bot output "HTTP/1.1" or something like that to the status(?) area, but not actually into the chat itself. Whenever I would try to "msg $chan %thetext" it would give me an error saying I don't have permission or parameters (something like that).

Sorry, I've been up for about 30 hours now. Going to go to bed, I'll check this thread in the morning. Thanks.

Re: Simple Socket Script [Re: ManaPot] #240022 16/12/12 03:25 PM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
Basic socket script:

Code:
alias test {
  sockopen test somesite.com 80
}

on *:sockopen:test: {
  sockwrite -n $sockname get /remainder_of_url.html HTTP/1.0
  sockwrite -n $sockname User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
  sockwrite -n $sockname Host: your.host.com
  sockwrite -n $sockname Accept-Language: en-us
  sockwrite -n $sockname Accept: */*
  sockwrite -n $sockname
}

on *:sockread:test: {
  if ($sockerr) {
    echo -a Error.
    halt
  }
  else {
    var %temp
    sockread %temp
    if (something isin %temp) {
      do something
    }
    elseif (somethingelse isin %temp) {
      do something else
    }
  }
}


Take that and change the domain and URL and change the parsing (the IF/ELSEIF part) to what you need.

So...

Code:
alias test {
  sockopen test d3sanc.com 80
}

on *:sockopen:test: {
  sockwrite -n $sockname GET /db/manabot/index.php HTTP/1.0
  sockwrite -n $sockname User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
  sockwrite -n $sockname Host: d3sanc.com
  sockwrite -n $sockname Accept-Language: en-us
  sockwrite -n $sockname Accept: */*
  sockwrite -n $sockname
}

on *:sockread:test: {
  if ($sockerr) {
    echo -a Error.
    halt
  }
  else {
    var %temp
    sockread -f %temp
    if (test isin %temp) {
      echo -a %temp
    }
  }
}


As you can see, I just change the host name in 2 spots, put in the correct path/page from the URL, and changed the parsing.

The -f in /sockread is necessary because you don't have a $crlf at the end of the line on your page. Also, the parsing is there because without it, you'll get all of the HTTP feedback, which you don't really want. Try it without the parsing (just echo %temp) and you'll see what I mean. Obviously you don't want to hard code the text into the IF, so you'll need something to tell it what to echo/use. If you're including scripting (HTML/PHP), you can use that to determine what line to display -- if (<something>* iswm %temp) { do this } -- for example. You could also just use the sockclose event if you always want the very last line read into the %temp variable.

Note that this is just basic. You'd normally have some more in the script, but this should get you started.

Last edited by Riamus2; 16/12/12 03:47 PM.

Invision Support
#Invision on irc.irchighway.net
Re: Simple Socket Script [Re: Riamus2] #240023 16/12/12 03:40 PM
Joined: Dec 2012
Posts: 10
M
ManaPot Offline OP
Pikka bird
OP Offline
Pikka bird
M
Joined: Dec 2012
Posts: 10
Yeah, I've gotten all of that before. But whenever I try to get it to output the text that's on the page (currently "test"), it only puts it to the status window. It won't actually put the text into the chat channel for others to see. I've tried "msg $chan %temp" but I always get an error.

Re: Simple Socket Script [Re: ManaPot] #240024 16/12/12 03:45 PM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
See my edit with an example script. As far as posting it to a channel, you need to provide the channel name. $chan is not valid in a socket. You would have to save it in a variable or hard code it. msg #mychan %somevariable


Invision Support
#Invision on irc.irchighway.net
Re: Simple Socket Script [Re: Riamus2] #240028 16/12/12 06:20 PM
Joined: Dec 2012
Posts: 10
M
ManaPot Offline OP
Pikka bird
OP Offline
Pikka bird
M
Joined: Dec 2012
Posts: 10
Alright, going to test it out in a little bit. Thank you so much for your help!

Re: Simple Socket Script [Re: ManaPot] #240033 16/12/12 10:04 PM
Joined: Nov 2006
Posts: 1,559
H
Horstl Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
Originally Posted By: Riamus2
You would have to save it in a variable or hard code it. msg #mychan %somevariable
Adding another /sock* command to the outline, if you're going to place your /sockopen command inside an event that has a $chan value (or inside an alias called from such an event), you could also /sockmark your socket with that $chan value right after opening it. Then you can call $sock($sockname).mark in subsequent socket events as required.

Re: Simple Socket Script [Re: Horstl] #240035 16/12/12 11:51 PM
Joined: Dec 2012
Posts: 10
M
ManaPot Offline OP
Pikka bird
OP Offline
Pikka bird
M
Joined: Dec 2012
Posts: 10
It all works perfectly, thank you sooo much!

Re: Simple Socket Script [Re: Horstl] #240037 17/12/12 12:03 AM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
True. I guess I should have just said store it rather than save it in a variable. smile


Invision Support
#Invision on irc.irchighway.net
Re: Simple Socket Script [Re: Riamus2] #246285 01/06/14 05:08 PM
Joined: Dec 2013
Posts: 9
R
r4z0r Offline
Nutrimatic drinks dispenser
Offline
Nutrimatic drinks dispenser
R
Joined: Dec 2013
Posts: 9
Thank you very much for the script.
I just tried it in my page.

It worked ok, but in the page I have some formating such as <center>,<div> etc.. tags, and all of the text + the formatting is echoed in the output.

How can I remove the formating and just keep the plain text?

Re: Simple Socket Script [Re: r4z0r] #246291 02/06/14 04:59 AM
Joined: Apr 2014
Posts: 191
B
blessing Offline
Vogon poet
Offline
Vogon poet
B
Joined: Apr 2014
Posts: 191
Use this alias to remove those.
Code:
alias noHTML return $regsubex($1, /<[^>]+(?:>|$)|^[^<>]+>/g, $null)

echo -a $nohtml(%temp)


Re: Simple Socket Script [Re: blessing] #246317 03/06/14 05:53 PM
Joined: Dec 2013
Posts: 9
R
r4z0r Offline
Nutrimatic drinks dispenser
Offline
Nutrimatic drinks dispenser
R
Joined: Dec 2013
Posts: 9
Thanks!