All pastes #272877 Raw Edit

Mine

public text v1 · immutable
·published 2006-12-09 11:20 UTC
rendered paste body
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
#include "xchat-plugin.h"

#define PNAME "example"
#define PDESC "example plugin...buggy?"
#define PVERSION "0.3"
#define FLOOD_PROTECTION_TIME_THRESHOLD 3
#define FLOOD_PROTECTION_MSG_THRESHOLD 3
#define TRIGGER_CHAR "!"

static unsigned int uid = 0;

static xchat_plugin *ph;
static int floodProtectActivated = 0;
static int floodProtDisabled = 0;
static time_t *floodProt[FLOOD_PROTECTION_MSG_THRESHOLD];
char *debugTarget = "slash-kun";

static char* strCopy(char *strFrom, int totalCharsToCopy, int startIndex){
	int length = (int)strlen(strFrom);
	if(totalCharsToCopy == 0)
		totalCharsToCopy = length - startIndex;
	if(startIndex > length)	return NULL;
	if((length - startIndex) < totalCharsToCopy) return NULL;
	char *newString = (char *)calloc(totalCharsToCopy+1,sizeof(char));
	if(!newString) return NULL;
	char *temp = newString;
	int i = 0;
	while(i<startIndex){
		strFrom++;
		i++;
	}
	i = 0;
	while(i<totalCharsToCopy && *strFrom != '\0'){
		*temp++ = *strFrom++;
		i++;
	}
	*temp = '\0';
	return newString;	
}

static int rsetFloodProtection(void *userdata){
	floodProtectActivated = 0;
	return 0;
}

static void triggerProtection(char *nick,int unique){
	xchat_commandf(ph,"msg %s uid = %d triggering flood protection before the floodprotactivated \"if\"",debugTarget,unique);
	if(floodProtectActivated) return;
	xchat_commandf(ph,"msg %s uid = %d triggering flood protection after the floodprotactivated \"if\"",debugTarget,unique);
	floodProtectActivated = 1;
	xchat_commandf(ph,"msg %s uid = %d your trigger has activated revy-chan's flood protection for a duration of 5 seconds.",nick,unique);
	xchat_hook_timer(ph,5000,rsetFloodProtection,NULL);
}

static void FloodProtectionEval(char *triggeringNick,int unique){
	xchat_commandf(ph,"msg %s uid = %d in floodprotectioneval()",debugTarget,unique);
	if(floodProtectActivated) return;
	int i = 0;
	int lastIndex = FLOOD_PROTECTION_MSG_THRESHOLD - 1;
	while(i<FLOOD_PROTECTION_MSG_THRESHOLD){
		xchat_commandf(ph,"msg %s uid = %d in the while statement and i = %d",debugTarget,unique,i);
		if(floodProt[i] == NULL){
			floodProt[i] = (time_t*)malloc(sizeof(time_t));
			if(floodProt[i])
				*floodProt[i] = time(NULL);
			else
				return;

			if(i == lastIndex)
				if(difftime(*floodProt[lastIndex],*floodProt[0]) <= FLOOD_PROTECTION_TIME_THRESHOLD )
					triggerProtection(triggeringNick,unique);
			return;
		}
		i++;
	}
	xchat_commandf(ph,"msg %s uid = %d through the while statement about to hit where i do stuff with floodprot[]",debugTarget,unique);
	for(i=0;i<lastIndex;i++){
		*floodProt[i] = *floodProt[i+1];
	}
	*floodProt[lastIndex] = time(NULL);
	xchat_commandf(ph,"msg %s uid = %d made it through the floodprot[] stuff doing",debugTarget,unique);
	if(difftime(*floodProt[lastIndex],*floodProt[0]) <= FLOOD_PROTECTION_TIME_THRESHOLD ){
		xchat_commandf(ph,"msg %s uid = %d about to definitely run triggerProtection()",debugTarget,unique);
		triggerProtection(triggeringNick,unique);
		xchat_commandf(ph,"msg %s uid = %d successfully ran triggerProtection()",debugTarget,unique);
	}
	return;
}

static void DoSomething(char *from,char *targetNick,int unique){
	
	int var1 = random();
	int var2 = random();
	var1 = (var1 % 60);
	var2 = (var2 % 101);
	
	if(var1 == 0)
		var1 = 20;
	if(var2 == 0)
		var2 = 20;

	xchat_commandf(ph,"say uid = %d %s %s %d %d",unique,from,targetNick,var1,var2);
	return;
}

static int ParseMsg(char *word[], char *word_eol[], void *userdata){
	int i_uid = uid++;
	xchat_commandf(ph,"msg %s uid = %d start of ParseMsg()",debugTarget,i_uid);
	if(floodProtectActivated){
		xchat_commandf(ph,"msg %s uid = %d floodprot activated, returning",debugTarget,i_uid);
		return XCHAT_EAT_NONE;
	}
	//Variable decs
	char *fromNick = NULL; 
	char *fromFullNick = NULL;	
	char *trigger = NULL;	
	char *args = NULL;		
									
	trigger = strCopy(word[4],0,1);
	if(trigger == NULL) return XCHAT_EAT_NONE;
	xchat_commandf(ph,"msg %s uid = %d after strCopy for trigger",debugTarget,i_uid);
	if(strncmp(trigger,TRIGGER_CHAR,1)){
		if(trigger) free(trigger);
		xchat_commandf(ph,"msg %s uid = %d not a trigger, freed trigger, returning",debugTarget,i_uid);
		return XCHAT_EAT_NONE;
	}
	
	fromFullNick = strCopy(word[1],0,1);
	fromNick = strsep(&fromFullNick,"!");
	
	args = strCopy(word_eol[5],0,0);

	if (!strcasecmp(trigger,TRIGGER_CHAR "something")) DoSomething(fromNick,args, i_uid);
	
	if(!floodProtDisabled)
		FloodProtectionEval(fromNick,i_uid);
	
	xchat_commandf(ph,"msg %s uid = %d about to free mem and complete parseMsg()",debugTarget,i_uid);
	if(fromNick) free(fromNick);
	if(trigger) free(trigger);
	if(args) free(args);

	xchat_commandf(ph,"msg %s uid = %d freed mem, returning",debugTarget,i_uid);	
	return XCHAT_EAT_NONE;
}

void xchat_plugin_get_info(char **name, char **desc, char **version, void **reserved)
{
	*name = PNAME;
	*desc = PDESC;
	*version = PVERSION;
}

int xchat_plugin_init(xchat_plugin *plugin_handle,
							 char **plugin_name,
							 char **plugin_desc,
							 char **plugin_version,
							 char *arg)
{
	ph = plugin_handle;


	*plugin_name = PNAME;
	*plugin_desc = PDESC;
	*plugin_version = PVERSION;
	
	xchat_hook_server(ph,"PRIVMSG", XCHAT_PRI_NORM, ParseMsg, NULL);
	xchat_print(ph,"plugin loaded successfully!\n");
	xchat_printf(ph,"Description: %s",*plugin_desc);
	xchat_printf(ph,"Version: %s",*plugin_version);
	return 1;       /* return 1 for success */
}


#######################################

GDB CRASH OUTPUT:

plugin_hook_find (list=0xa0a6688, type=1, name=0xbfb6780f "PRIVMSG")
    at plugin.c:454
454     in plugin.c


######################################

GDB BACKTRACE:


#0  plugin_hook_find (list=0xa0a6688, type=1, name=0xbfb6780f "PRIVMSG")
    at plugin.c:454
#1  0x08088bfa in plugin_hook_run (sess=0x9fbcbe8, name=0xbfb6780f "PRIVMSG",
    word=0xbfb67a64, word_eol=0xbfb679e4, type=1) at plugin.c:483
#2  0x08094ed8 in irc_inline (serv=0x9fbbf38,
    buf=0x9fbc1aa ":slash-kun!~samurai@adamantium.coated.diamond.dipped PRIVMSG #testroom :!something someone", len=90) at proto-irc.c:1031
#3  0x0808aeed in server_read (source=0xa0c34b0, condition=G_IO_IN,
    serv=0x9fbbf38) at server.c:389
#4  0x008a04fc in g_vasprintf () from /usr/lib/libglib-2.0.so.0
#5  0x0087a4ce in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0
#6  0x0087d4d6 in g_main_context_check () from /usr/lib/libglib-2.0.so.0
#7  0x0087d7c3 in g_main_loop_run () from /usr/lib/libglib-2.0.so.0
#8  0x02857a46 in gtk_main () from /usr/lib/libgtk-x11-2.0.so.0
#9  0x0805ad7f in fe_main () at fe-gtk.c:284
#10 0x08093a66 in main (argc=3, argv=0xbfb692c4) at xchat.c:1050