PDA

View Full Version : Web lookups



L1A
09-08-2004, 01:41 PM
Ok I did a proof of concept experiment to lookup mobs on the web by selecting an option off the spawnlist menu. It seems to work well. But I am testing it some more.
I have it hardcoded to spawn a web browser, konqueror, and go to a specific web page and search for the selected mob. It is so neat.

Is this going to be a political issue? I don't want to post a patch if it is going to create problems. I was thinking that I could leave the web page as a #define or something that the user has to select on their own. And maybe the web browser too.

What do you all think?

L1A

purple
09-08-2004, 02:20 PM
Does it use an URL to a public site or a private site?

Ideally, this would all be configurable but that's a decent chunk of work. I'd think in the best case, I could specify a formatted template URL like http://whatever.example.com/mobfinder/mobdetails.asp?mobname=${mobname} and that I could also define an external browser helper so I can pick firefox or lynx or konq or whatever I want.

I guess this could be done using a script too so you don't have to make too many changes in the interface code. Something like showeq-browse-mob.sh and pass in the mob name as argument 1 or something like:



#!/bin/bash
#
# Fetch mob information.
# Parameter 1 - mob name

# Web URL to go to in order to get mob information. Change if you want!
URL="http://whatever.example.com/mobfinder/mobdetails.asp?mobname=$1"

# Web browser to open. This opens in a new tab in a running mozilla if found
# or opens a new moz browser if not. Change this to whatever you want to!
mozilla -remote "ping()" &> /dev/null

if [ $? == "0" ]
then
mozilla -remote "openurl($URL, new-tab)" &
else
mozilla -remote $URL &
fi

Then make sure the script is installed with make install and/or your menu option for launching the script is not shown if the script doesn't exist. If you can write a generic script that uses allakhazam's or some other public site, so that stuff works by default, even better. But putting all the configurable stuff in a script seems good enough to me I guess. If folks want it to use their favorite browser or mob database, they hack the script a bit. If you wanna get fancy and look for different browsers in the default script and whatnot, you can. If you want to pass more info for the search, just pass it in to the script and the script can use whatever. I'm not familiar with what there is for a mob, like is there's an id like items or what, so if there's a mob id or whatnot pass that as parameter 2 of the script and so on.

That's all just my opinion of course.

L1A
09-08-2004, 03:26 PM
Ok let me explain how it works a bit better. Under your NPC list you can highlight a mob and right click to bring up a menu. I have added a selection called Web Search. When you click on it a web browser is spawned and you see that mobs info. It is working right now and I am having a blast playing with it.

I use this public link: http://eqbeastiary.allakhazam.com/search.shtml?name=
And I append the mob name you have selected off the spawnlist at the end.
It pops up Konqueror with the mob stats perfectly.

I think I will just use #defines in the source code and that will be easy enough to update.
#define WEBBROWSER "konqueror"
#define WEBPAGE "http://eqbeastiary.allakhazam.com/search.shtml?name="

Oh I am using beta version 5.0.0.13

I am too paranoid sometimes. Here is my patch.

L1A

Cryonic
09-08-2004, 07:50 PM
I like purple's idea of having the menu option spawn a shell script. Then all you need to do is document what the menu option will pass to the script and let the user have full control of browser, etc... in the script without the need to rebuild SEQ to change browsers or site.

/me ponders what became of his spawnlog script...

I know BA was looking for more spawnlogs... Wonder if Ksmith would be willing to expand his http://eqitems.13th-floor.org site to include mobs

anyways.....

monster69
09-08-2004, 08:46 PM
Patch works great! Great addition L1A.

/cheer

Monster

ksmith
09-08-2004, 08:57 PM
L1A
That certainly looks like a good start. A few concerns/suggestions though.

1. Web browser and url should be run-time options, not compiled in.

2. Some people run showeq suid root, you should use some code like this to make sure the web browser isn't run as root if showeq is setuid root


uid_t savedEUID;
uid_t savedEGID;

savedEGID = getegid();
savedEUID = geteuid();

setegid(getgid());
seteuid(getuid());

// call webbrowser here

setegid(savedEGID);
seteuid(savedEUID);

3. Use QUrl::encode (http://doc.trolltech.com/3.3/qurl.html#encode) instead of QString::replace


Cryonic
Already discussed that here.

L1A
09-08-2004, 10:44 PM
Oh wow thanks for all the great ideas and suggestions. I am liking the shell script idea a lot more now. Yes I was also thinking about the security issues and I am glad you caught that as well. Yes that earlier post got me thinking about all this.

I can come up with a script that works and add it to the project. Then users can write their own if they want or use the default. I can just call the script websearch and users can write it as a #!/usr/bin/perl or #!/bin/bash or any other wierd thing they want. Wow you could fire off all kinds of neat things from one script. Like automagically update your guilds webpage with the mob you hunted today. Uh oh I better focus. My mind is wandering.

I will keep tinkering with it. I love showeq. I have learned all kinds of neat stuff doing this.

L1A

L1A
09-09-2004, 08:05 PM
Arrgh permission problems are killing me. QProcess ignores the seteuid, setguid setup as suggested. So the script is running as root. But I do have everything running via a script now.

Any ideas?

L1A

ksmith
09-09-2004, 08:31 PM
Spawn a process the old fashioned way with fork() and execve().

QProcess should be respecting seteuid() and setegid(), post your code for that section.

L1A
09-10-2004, 10:13 AM
Here is the code using QProcess.
void SpawnListMenu::set_web(int id)
{
if (m_currentItem == NULL)
return;
web_proc = new QProcess( this );
QString webprog = "/usr/local/bin/websearch"; //program or script name to run
QString mobname = m_currentItem->name();
mobname.replace( "#", ""); //Have to remove # from the front of named mobs so script can parse name

web_proc->addArgument( webprog );
web_proc->addArgument( mobname );
uid_t savedEUID;
uid_t savedEGID;
savedEGID = getegid();
savedEUID = geteuid();
setegid(getgid());
seteuid(getuid());
// call external program here

if ( !web_proc->start() ) {
//Error handling
QMessageBox::critical( 0,
tr("Fatal error"),
tr("Could not start external program."));
}

setegid(savedEGID);
seteuid(savedEUID);
}


And here is the ps output showing the web browser process it spawned.
root 20348 1.4 5.3 38568 27788 pts/8 S+ 09:08 0:03 konqueror --profile webbrowsing http://eqbeastiary.allakhazam.com/search.shtml?name=You

L1A

purple
09-10-2004, 10:28 AM
Maybe use fork() instead of QProcess and call setuid(getuid()) to totally drop privs on the forked process back to the original user?

ksmith
09-10-2004, 12:12 PM
This works:


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

extern int errno;

int main (int argc, char **argv)
{
char *prog = "/home/ksmith/tmp/web.sh";
char *mobname = "#My_Mob00";
pid_t pid = 0;
int status;

if ((pid = fork()) == -1)
{
perror("fork() failed!");
}
/* child */
else if (pid == 0)
{
/* fork again to prevent zombies */
if (fork() != 0)
_exit(0);

seteuid(getuid());
setegid(getgid());

printf("execl: %s\n", prog);
execl(prog, prog, mobname, NULL);
perror("execl() failed!");
_exit(0);
}

/* parent continues */
printf("Spawned: %d\n", pid);

if (pid < 0 || waitpid(pid, &status, 0) < 0)
return -1;

return 0;
}


Or you could just try putting
web_proc = new QProcess( this ); after the seteuid/setegid.

L1A
09-10-2004, 04:33 PM
Thank you. That helped me find my mistake. I am glad it was relatively simple goof up.
Part 2 of the problem at my end was that I was not using setuid on showeq. I use sudo so it thinks I am root. This compounded the problem. So I made life simple and switched to setuid. It works fine now.

Here is the patch. The script is called websearch and should be installed in the same directory as showeq. It is a script users can change to their liking. The nice thing is that you don't have to recompile showeq after you make a change.

The patch should create the websearch script in showeq/src.

L1A

purple
09-10-2004, 05:52 PM
I had to add a #include <unistd.h> to spawnlistcommon.cpp to get this to compile.

ksmith
09-10-2004, 06:07 PM
More suggestions:

1. 'websearch' is a very generic name. Either change it to 'showeq-websearch' or something similar, or use the DataLocationMgr class to exec it from the showeq data directory so that it doesn't have to be in the users path. I'd prefer if you used the DataLocationMgr because it would allow individual users to override the global websearch script by putting one in their ~/.showeq directory.

2. Get rid of the 'mobname.replace("#", "")', that can be done by the websearch script.

3. At some point in the future, I'm sure someone will want to add a web search for something else... buffs, items, whatever. Make the first argument of the 'websearch' script be the searchtype --mob or -m. I'd like to get code snippits for as many browsers as possible. I'll take care of making the script fancy.

4. The 'websearch' script needs to be added to the makefiles so that it gets installed when 'make install' is run.

5. You're not passing the zone name to websearch. You should.

purple
09-10-2004, 06:31 PM
Firefox, opens a new tab in a running firefox if open and opens up a new instance if not open:

firefox -remote "ping()" &> /dev/null

if [ $? == "0" ]
then
firefox -remote "openurl($URL, new-tab)" &
else
firefox $URL &
fi

Mozilla is the same as above, just replace firefox with mozilla.

ksmith
09-10-2004, 06:47 PM
Fancy websearch script (http://kyle.13th-floor.org/eq/websearch.txt).

I'll be updating it as I get more browser snippits and potentially more sites to search for mobs?

Once L1A's patch is finished, I'll get someone with CVS access to add it for me.


websearch --mob #King_Tormax00 kael
Searching for #King_Tormax00 in kael
Opening Browser to page http://eqbeastiary.allakhazam.com/search.shtml?name=King+Tormax

lildr00d
09-11-2004, 01:45 PM
This looks like it will be a great feature. Question though. Will the scripts automatically do a search on target or will I have to invoke a shortcut key or menu.

If its automatic it may be bad on people with slower systems who are running thru zones coning mobs to check for see invis

purple
09-11-2004, 03:26 PM
It adds a menu option to the right click menu of the spawn list.

L1A
09-11-2004, 06:35 PM
Hmm lost my reply somewhere.
I am looking into doing everything that KSmith suggests. The only one problem in there is removing the # before passing it to the script. You either have to remove it or escape it so the shell doesn't ignore the whole argument. So I removed it. I am open to any other ideas.

L1A

ksmith
09-11-2004, 08:27 PM
You either have to remove it or escape it so the shell doesn't ignore the whole argument.

So escape it then.


mobname.replace("#", "\#");

If QProcess works like execl(), there should be no reason to escape anything.

L1A
09-12-2004, 10:44 AM
Update.
Ok I have everything working pretty darn well. I am a bit stuck finding the zonename. But I have not given up on it. Oh and I better update Makefiles to add script on install.

In summary:
1. Using showeq_websearch as script name.
2. Using DataLocationMgr class to exec it from the showeq data directory. (I like that class)
3. If script is not found a messagebox pops up saying so. And program continues normally. It just ignores your request to run a script it can't find.
4. Got rid of '#' code mess. It is not needed.

L1A

L1A
09-13-2004, 04:53 PM
Ok here is the patch du jour. It passes zonename to the script now too. The script is called showeq_websearch and should go into ~/.showeq so it can be found.
I am not really good with makefiles so maybe someone else can figure out how to get the script into the right place when showeq is installed.

Figuring out the zone name part was a real pain. This was the best/easiest way I could figure out to do it.

Hope you like it.

L1A

ksmith
09-13-2004, 07:20 PM
I'm not a big fan of using extern. Perhaps you could pass it to the constructor? Or use a signal to update the zone name?

I should be able to take a better look at it over the weekend.

L1A
09-13-2004, 08:58 PM
Ok sounds good. Here are some of my notes. EQInterface class knows the variable shortZoneName. This is the normal zone name. It gets updates off signals in that class. So that is where the data we need is hiding. The SpawnListMenu class doesn't have access to EQInterface though. Which was my main sticking point. I was thinking of passing the zone name as an argument some how but that looked ugly. As I was flipping through my various C++ books I ran across the extern idea. Seems to defeat the purpose of C++ somewhat so I can understand the reluctance to use it. But I was getting frustrated and it worked. hehe.

L1A

Zaphod
10-01-2004, 10:40 AM
Don't do the "Web Lookup" in the menu items slot in the SpawnListMenu class. That's what caused the need for the extern hack you found yourself using. Instead I'd suggest the following possibility:
Add a signal to SpawnListMenu with a prototype that includes a const pointer to Item.
Add a similar signal to both the SpawnList and SpawnList2 classes (since they are both users of SpawnListMenu). In the code within each that instantiates SpawnListMenu (in the menu() method of those classes) have it connect the SpawnListMenu signal to the containing classes signal.
Add a slot matching the prototype to the EQInterface class, and have it handle launching the appropriate browser with the appropriate data.
In the code that instantiates the SpawnList and SpawnList2 classes have it connect the signal you added to them to your slot in EQInterface, and your wiring is all done.


This is one possible suggested implementation, it is not necessarily the only or the best one. It does however improve the maintainability of the code and opens up the possibility for other methods of launching a web browser with a selected Item (like possibly from a map context menu item).

Thanks and Enjoy,
Zaphod (dohpaZ)

L1A
10-03-2004, 10:54 AM
Thank you very much. I will have another go at it.
L1A

cann
10-12-2004, 04:31 AM
heyas, i have applied this patch to showeq-5.0.15 and and iam using ksmith's websearch script.. the script worksfine it spawns the right browser and look up the mob. i successfully patch the source code and got showeq to compile after adding #include <unistd.h> to spawnlistcommon.cpp as purple said. everything looks and works as it should, but some how when i use the "web lookup" tool from the spawn list nothing happens. if i remove the web script from ./showeq dir seq complains it cant find it

my setup :
slackware 8.1
kde 3
qt-3.1.2 - thread
showeq-5.0.15 + L1a's web patch (i start seq from a shortcut as root)

ps. dont know if this should be posted here or in the helpdesk forum

/cann

lildr00d
10-15-2004, 03:16 PM
I've applied the patch and created the showeq_websearch file.

I am getting the error message. Can not start external program error message