
/* OpenWebSpider
 *
 *  Author:     Stefano Alimonti aka Shen139
 *  Mail:       shen139 [at] openwebspider (dot) org
 *
 *
 * This file is part of OpenWebSpider
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


#ifndef __MODULEs
#define __MODULEs

int myLoadModules(char* filename,void* handler)
{
int counter=0;
int modLoaded=0;
char* error;

	printf("\nTrying to open module: %s...",filename);
#ifdef WIN32
/*
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp
*/
	handler = (HANDLE)LoadLibrary(filename);
	if (!handler)
	{
		printf("Error\n");
		exit(0);
	}
#else
/*
dlopen loads a dynamic library from the file named by the null terminated string filename and returns an opaque "handle" for the dynamic library
*/
	handler=dlopen(filename, RTLD_LAZY);
	if (!handler)
	{
		printf("%s\n\n",dlerror());
		exit(0);
	}
#endif

	printf("OK\n");

	printf("+ Trying to import functions:\n");
	while(loadableModules[counter].functName[0])
	{
		printf(" - %s() ...",loadableModules[counter].functName);
	
			loadableModules[counter].handler = myGetProcAddress(handler, loadableModules[counter].functName);
#ifdef WIN32		
			if(loadableModules[counter].handler==NULL)	
#else
			if ((error = dlerror()) != NULL) 
#endif
				printf("not found\n");
			else
			{
				printf("loaded\n");
				printf("   - %s() has init function %s():...",loadableModules[counter].functName,loadableModules[counter].functInit);

				loadableModules[counter].initHandler = myGetProcAddress(handler, loadableModules[counter].functInit);
		
#ifdef WIN32		
				if(loadableModules[counter].handler==NULL)	
#else
				if ((error = dlerror()) != NULL) 
#endif
					printf("no\n");
				else
				{
					printf("yes\n");

					/* here? not here?
					printf("- %s()...",loadableModules[counter].functInit);
					functInit=loadableModules[counter].initHandler;
					if(functInit(sInitError)==0)	//The init function returned 0 -> error
					{
						printf(" %s\n\n",sInitError);
						exit(0);
					}
					else
						printf("OK\n");
					*/
				}

				modLoaded++;
			}
	
		counter++;
	}

	if(modLoaded==0)
	{
		printf("No functions loaded... exiting! (Omit '-f' if you don't want to load functions from a module)\n\n");
		exit(0);
	}
	else
		return 1;
}

int myUnloadModule(void* handler)
{
#ifdef WIN32
	return FreeLibrary((HANDLE)handler);
#else
/*
dlclose decrements the reference count on the dynamic library handle handle. If the reference count drops to zero and no other loaded libraries use symbols in it, then the dynamic library is unloaded. If the dynamic library exports a routine named _fini, then that routine is called just before the library is unloaded
*/
	return dlclose(handler);
#endif
}

void* myGetProcAddress(void* handler,char* funct)
{
#ifdef WIN32
	return GetProcAddress((HANDLE)handler, funct);
#else
	return dlsym(handler, funct);
#endif
}

void* GetModFunctionHandlerByName(char* functName)
{
int counter=0;

	while(loadableModules[counter].functName[0])
	{
		if( strcmp(loadableModules[counter].functName,functName)==0 )
		{
			return loadableModules[counter].handler;
		}
		counter++;
	}
	return NULL;
}

void* GetInitModFunctionHandlerByName(char* functName)
{
int counter=0;

	while(loadableModules[counter].functName[0])
	{
		if( strcmp(loadableModules[counter].functName ,functName)==0 )
		{
			return loadableModules[counter].initHandler;
		}
		counter++;
	}
	return NULL;
}


#endif

