Opening a Web page through EQWatcher

EQWatcher Evolution discussion

Moderators: Lavish Software Team, Moderators

Post Reply
spointman
Non-Subscriber
Posts: 8
Joined: Sat Jul 24, 2004 5:40 pm

Opening a Web page through EQWatcher

Post by spointman » Sat Sep 15, 2007 11:03 am

Heya. I'm trying to get a script that will open a Web page whenever it sees certain text in the log file. It doesn't have to actually open it in a browser, but it does have to register a hit on the Web page.

Example:
Soandso tells you, 'GotLink 23456'
This causes a connection to the following page, which would increment an internal counter for item 23456:
http://www.some.site.or.other.com/dir1/ ... item=23456

I would then write another page to show which items have been counted the most. (Yes, I know this is a trivial example; I'm trying to simplify things a bit.)

I saw the sockets documentation in EQWatcher, but I'm hoping someone has a more elegant solution than writing my own mini Web browser into EQW. :) Does anyone know of an existing script that can act as a wrapper to the sockets commands?

Alternately, could I use the Spawn() command to launch a Web browser pointing to that page? Something like:

Spawn("c:\program files\Mozilla Firefox", "firefox.exe http://www.some.site.or.other.com/dir1/ ... item=23456")

Or would the command line parameter cause an error?

spointman
Non-Subscriber
Posts: 8
Joined: Sat Jul 24, 2004 5:40 pm

Post by spointman » Sun Sep 16, 2007 3:16 pm

OK, so it turns out Spawn() doesn't work with command-line parameters, such as the target page to open in the Web browser.

I was able to sort-of get around this limitation by using EQWatcher's SQL support to write directly to the database, but the upshot is that I now have to rewrite all my validation code into EQWatcher scripts. I'm still looking for a way to open or call a Web page with a query string, if anyone knows of one.

Hmm ... this fails too:

Code: Select all

Spawn("", "http://www.yahoo.com?a=b");
I wonder if I could use the file I/O functions to write a batch file, then Spawn() to run the file? That's a bit roundabout, though.

spointman
Non-Subscriber
Posts: 8
Joined: Sat Jul 24, 2004 5:40 pm

Post by spointman » Sun Oct 07, 2007 6:57 pm

For those wondering what happened, I ended up doing it through the sockets functionality in EQWatcher. I basically used sockets to simulate a Web browser request. Since I don't really care about a detailed response, I used the HEAD method instead of GET, and encoded the results into headers that the Web page would generate and include. If you actually need to parse the content of the page, it's not much harder than what's below, although you have to remember that it's parsed line by line.

Here's the gist of what I did. You'll probably have to change most of this to suit your own needs, but the structure should get you started.

Code: Select all

function main () {} // Needed for the script to compile.

// the g prefix indicates global
public string gSender = "";
public string gItem = "";
public unsigned long gSocketID = 0;
public string gSocketName = "";
public string gResponse = "";
public string gCode = "";


// Returns a string containing the hex value of the passed-in char.
function Hex (char Value)
{
	byte HighOrder = Value / 16; // hopefully this truncates
	byte LowOrder = Value % 16;

	string HexResult = "";

	if &#40;HighOrder < 10&#41;
	&#123;
		HexResult = chr&#40;HighOrder + 48&#41;;
	&#125;
	else
	&#123;
		HexResult = chr&#40;HighOrder + 55&#41;;
	&#125;

	if &#40;LowOrder < 10&#41;
	&#123;
		strcat&#40;HexResult, chr&#40;LowOrder + 48&#41;&#41;;
	&#125;
	else
	&#123;
		strcat&#40;HexResult, chr&#40;LowOrder + 55&#41;&#41;;
	&#125;

	return HexResult;
&#125;



// Converts any characters in the incoming string that are outside the normal 
// alphanumeric range into hex equivalents.  The resulting string is URL-safe.
function MakeURLSafe&#40;string incoming&#41;
&#123;
	unsigned long i = 0;
	unsigned long length = strlen&#40;incoming&#41;;
	string ResultString = "";
	char Single = 0;

	// Loop through and replace any character outside our desired ASCII range
	// Basically encode everything except letters and digits.
	// No FOR loop, so we have to improvise with a WHILE loop

	while &#40;i < length&#41;
	&#123;
		Single = strpos&#40;incoming, i&#41;;
		if &#40; Single < 48 ||						// less than number
			 &#40;Single > 57 && Single < 65&#41; ||	// Greater than number, less than Uppercase
			 &#40;Single > 90 && Single < 97&#41; ||	// Greater than Uppercase, less than Lowercase
			 Single > 122						// Greater than Lowercase
			&#41;
		&#123;
			// Replace and append
			strcat&#40;ResultString, "%", Hex&#40;Single&#41;&#41;;
		&#125;
		else
		&#123;
			// Just append
			strcat&#40;ResultString, chr&#40;Single&#41;&#41;;
		&#125;

		i++;
	&#125;
	return ResultString;
&#125;


// Trigger for the incoming tell.  This calls the Web page function
trigger&#40;"[@trash@] @gSender@ tells you, 'GotItem @gItem@'"&#41; 
&#123;
	// If you want validate who's sending you tells, you can do that here.
	// For example, maybe only process tells from your own characters.
	// Or maybe you only want to record certain items.

	// Once you determine whether you want to parse this tell, call the next function.
	ParseTell &#40;gSender, gItem&#41;;
&#125;



function ParseTell &#40;string Sender, string Item&#41;
&#123;
	gSocketID++;
	string SocketName = "Item_" + gSocketID;

	boolean result = false;

	result = SockNew&#40;SocketName, 0, "DoOnReceive"&#41;;
	if &#40;!result&#41;
	&#123;
		// Couldn't initialize the socket. Most likely, the user didn't enable 
		// Internet access from the EQWatcher GUI.
		SpeakSync&#40;"Socket Failure! Is Internet access enabled?"&#41;;
		return;
	&#125;
	
	result = false;
	// Change the Web site in the line below.  
	// The 80 means port 80, which is the standard for Web requests, 
	// so you probably won't have to change that.
	result = SockConnect&#40;SocketName, "www.YourWebSite.com", 80&#41;;

	if &#40;!result&#41;
	&#123;
		// Couldn't find the Web site, or the site didn't respond.
		SpeakSync&#40;"Connection Failure!"&#41;;
		return;
	&#125;

	// We use the HEAD method since we don't need the body of the Web page.
	// If you need the body, use GET instead.
	// Change the path and page to your own page.
	string Request = "HEAD /dir1/dir2/YourPage.php";
	
	// first parameter must be preceded by a question mark
	strcat&#40;Request, "?sender=", MakeURLSafe&#40;Sender&#41;&#41;;
	
	// Subsequent parameters must be separated by ampersands
	strcat&#40;Request, "&item=", MakeURLSafe&#40;Item&#41;&#41;;

	// Add the protocol name and version
	strcat&#40;Request, " HTTP/1.0", chr&#40;13&#41;, chr&#40;10&#41;&#41;;

	// A user-agent string isn't mandatory, but it's polite to include it.
	strcat&#40;Request, "User-Agent&#58; EQWatcher Evolution/2.39"&#41;;
	
	// Two new-lines &#40;CR and LF&#41; indicate the end of your request.
	strcat&#40;Request, chr&#40;13&#41;, chr&#40;10&#41;, chr&#40;13&#41;, chr&#40;10&#41;&#41;;

	SockSend&#40;SocketName, Request&#41;;

	// We don't get a direct response, so we don't know whether the request 
	// made it to the destination server or not.
&#125;

alias &#40;"DoOnReceive \"@gSocketName@\" X-EQWatcher-Result&#58; @gCode@ @gResponse@"&#41;
&#123;
	// Handle the response here.  My setup used a two-part header value, a 
	// number and a response text.  The Code was what I used to decide 
	// whether the request succeeded or not.  The text response was just so
	// I didn't have to keep guessing what happened when I forgot what the 
	// codes meant! &#58;&#41;
	SpeakSync&#40;"Got Response"&#41;;
	SpeakSync&#40;gResponse&#41;;
&#125;

Post Reply