Anope IRC Services

Please login or register.

Login with username, password and session length
Advanced search  

News:

Pages: [1]   Go Down

Author Topic: Problem with module [completly solved]  (Read 6573 times)

0 Members and 1 Guest are viewing this topic.

Tzann

  • Anope User
  • Offline Offline
  • Posts: 6
Problem with module [completly solved]
« on: February 12, 2009, 03:31:04 PM »

Hello!

I've written a little Modul to put the userlist of a channel into a sql-table. Compiled after the wiki (on windows, with vs05 pro) and uploaded the module.
But, when i tried do load the module (per /msg operserv modload cs_nicklist) Anope crashed and i got these Error:
Code: [Select]
[Feb 12 14:03:28 2009] Trying to load module [cs_nicklist]
[Feb 12 14:03:28 2009] Runtime module location: .\modules\runtime\cs_nicklist.dll.a29608
[Feb 12 14:03:29 2009] Module cs_nicklist compiled against current or newer anope revision 1899, this is 1489
[Feb 12 14:03:29 2009] Displaying message list for join_channel
[Feb 12 14:03:29 2009] 1: 0x024BF768
[Feb 12 14:03:29 2009] end
[Feb 12 14:03:29 2009] Displaying message list for part_channel
[Feb 12 14:03:29 2009] 1: 0x024BF7B8
[Feb 12 14:03:29 2009] end
[Feb 12 14:03:29 2009] debug: existing msg: (0x024B03A8), new msg (0x024BF838)
[Feb 12 14:03:29 2009] Displaying message list for reload
[Feb 12 14:03:29 2009] 1: 0x024BF838
[Feb 12 14:03:29 2009] 2: 0x024B03A8
[Feb 12 14:03:29 2009] 3: 0x0098C500
[Feb 12 14:03:29 2009] 4: 0x00963B18
[Feb 12 14:03:29 2009] 5: 0x009614E8
[Feb 12 14:03:29 2009] end

So, what does Anope want from me . .  ??? . . or better: how could i fix it?
« Last Edit: April 08, 2009, 08:53:44 AM by Tzann »
Logged

Trystan Scott Lee

  • Contributor
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 343
Re: Problem with module
« Reply #1 on: February 12, 2009, 03:57:33 PM »

From what you have shown it loaded just fine. Putting you code some where so it can be reviewed will help.
Logged
my God my tourniquet, return to me salvation

Tzann

  • Anope User
  • Offline Offline
  • Posts: 6
Re: Problem with module
« Reply #2 on: February 12, 2009, 04:16:25 PM »

cs_nicklist.c:

Code: [Select]
#include "module.h"
#include <mysql.h>

#define AUTHOR "Tzann"
#define VERSION "1.0"

#define GET_MYSQL_ERROR      mysql_error(mysql_obj)

MYSQL_ROW row = NULL;
MYSQL *mysql_obj = NULL;
MYSQL_RES *mysql_result_set = NULL;
my_ulonglong q_mysql_affected_rows;

char *MSQL_HOST = NULL;
unsigned int MSQL_PORT = 0;
char *MSQL_USER = NULL;
char *MSQL_PASS = NULL;
char *MSQL_DB = NULL;

unsigned int i;
struct c_userlist *cu, *next;
char *query;
char *output;

int load_config();
int reload_config(int ac, char **av);
int do_change(int ac, char **av);

int channel_exists(char *chan);
int channel_watch(char *chan);
int channel_add(char *chan);
int channel_update(char *chan);

int do_mysql_connect();
unsigned int do_mysql_query(char *format, ...);
void do_mysql_close();

char *vstr(char *format, va_list args);


int AnopeInit(void)
{
EvtHook *hook;

moduleAddAuthor(AUTHOR);
moduleAddVersion(VERSION);

hook = createEventHook(EVENT_JOIN_CHANNEL, do_change);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

hook = createEventHook(EVENT_PART_CHANNEL, do_change);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

hook = createEventHook(EVENT_RELOAD, reload_config);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

if (load_config() == MOD_STOP) return MOD_STOP;

if (do_mysql_connect())
{
alog("[\002cs_nicklist\002]: Konnte nicht mit MySQL verbinden");
return MOD_STOP;
}

alog("[\002cs_nicklist\002]: geladen . . . [Author: \002%s\002] [Version: \002%s\002]", AUTHOR, VERSION);

return MOD_CONT;
}

void AnopeFini(void)
{
if (MSQL_HOST) { free(MSQL_HOST); MSQL_HOST = NULL; }
if (MSQL_USER) { free(MSQL_USER); MSQL_USER = NULL; }
if (MSQL_PASS) { free(MSQL_PASS); MSQL_PASS = NULL; }
if (MSQL_DB)   { free(MSQL_DB);   MSQL_DB = NULL;   }

do_mysql_close();

alog("[\002cs_nicklist\002]: Modul entladen");
}

int load_config(void)
{
Directive confvalues[][1] = {
{{ "NicklistMysqlHost", { { PARAM_STRING, PARAM_RELOAD, &MSQL_HOST } } }},
{{ "NicklistMysqlPort", { { PARAM_PORT,   PARAM_RELOAD, &MSQL_PORT } } }},
{{ "NicklistMysqlUser", { { PARAM_STRING, PARAM_RELOAD, &MSQL_USER } } }},
{{ "NicklistMysqlPass", { { PARAM_STRING, PARAM_RELOAD, &MSQL_PASS } } }},
{{ "NicklistMysqlName", { { PARAM_STRING, PARAM_RELOAD, &MSQL_DB } } }}
};

MSQL_PORT = 3306;
if(MSQL_HOST) { free(MSQL_HOST); MSQL_HOST = NULL; }
if(MSQL_USER) { free(MSQL_USER); MSQL_USER = NULL; }
if(MSQL_PASS) { free(MSQL_PASS); MSQL_PASS = NULL; }
if(MSQL_DB)   { free(MSQL_DB);   MSQL_DB = NULL;   }

i = 0;
for (i = 0; i < 5; i++)
{
moduleGetConfigDirective(confvalues[i]);
}

if(!MSQL_USER || !MSQL_PASS || !MSQL_DB || !MSQL_HOST)
{
alog("[\002cs_nicklist\002]: Fehlende Konfiguration!");
return MOD_STOP;
}

return MOD_CONT;
}

int reload_config(int ac, char **av)
{
if(ac >= 1)
{
if(!stricmp(av[0], EVENT_START))
{
alog("[\002cs_nicklist\002]: Neuladen der Konfiguration . . .");
return load_config;
}
}
return MOD_CONT;
}

int do_change(int ac, char **av)
{
ChannelInfo *ci;
User *u;
Channel *c;

if (!(ci = cs_findchan(av[2]))) return MOD_CONT;
if (!(c = findchan(ci->name)))  return MOD_CONT;

if(channel_exists(av[2]) == 1)
{
if(channel_watch(av[2]) == 1)
{
channel_update(av[2]);
}
}
else
channel_add(av[2]);

return MOD_CONT;
}

int channel_exists(char *chan)
{
unsigned int ret;

ret = do_mysql_query("SELECT channel FROM `%s`.`cs_nl_channels` WHERE channel = %s;", MSQL_DB, chan);

row = mysql_fetch_row(mysql_result_set);

if(mysql_num_rows(mysql_result_set) == 0)
return 0;
else
return 1;
}

int channel_watch(char *chan)
{
unsigned int ret;

if(channel_exists(chan))
{
ret = do_mysql_query("SELECT watch FROM `%s`.`cs_nl_channels` WHERE channel = %s;", MSQL_DB, chan);

row = mysql_fetch_row(mysql_result_set);

if(row)
{
unsigned long *lengths = mysql_fetch_lengths(mysql_result_set);
int watch = (int)row[1];
if(watch == 1)
return 1;
else
return 0;
}

}
else
return 0;
}

int channel_add(char *chan)
{
do_mysql_query("INSERT INTO `%s`.`cs_nl_channels` (channel, watch) VALUES('%s', '0' )", MSQL_DB, chan);
return 1;
}

int channel_update(char *chan)
{
Channel *c;
ChannelInfo *ci;

if((c = findchan(chan)) && (ci = c->ci))
{
do_mysql_query("DELETE FROM `%s`.`cs_nl_users` WHERE channel='%s'", MSQL_DB, chan);

cu = c->users;
while (cu) {
next = cu->next;

do_mysql_query("INSERT INTO `%s`.`cs_nl_users` (channel, nick) VALUES('%s', '%s' )", MSQL_DB, chan, cu->user);

cu = next;
}
}
}

int do_mysql_connect()
{
if(mysql_obj)
return 0;

mysql_obj = mysql_init(NULL);

if(!mysql_real_connect(mysql_obj, MSQL_HOST, MSQL_USER, MSQL_PASS, MSQL_DB, MSQL_PORT, NULL, CLIENT_IGNORE_SPACE))
{
mysql_close(mysql_obj);
mysql_obj = NULL;
return 1;
}
else
{
#ifdef MYSQL_OPT_RECONNECT
my_bool my_true = true;
mysql_options(mysql_obj, MYSQL_OPT_RECONNECT, &my_true);
#else
mysql_obj->reconnect = 1;
#endif
return 0;
}
}

unsigned int do_mysql_query(char *format, ...)
{
va_list args;
va_start(args,format);
query = vstr(format, args);
va_end(args);

if (mysql_result_set)
{
mysql_free_result(mysql_result_set);
mysql_result_set = NULL;
}

if (mysql_query(mysql_obj, query))
{
free(query);
return 1;
}
 
free(query);

mysql_result_set = mysql_store_result(mysql_obj);
q_mysql_affected_rows = mysql_affected_rows(mysql_obj);

return 0;
}

void do_mysql_close()
{
if (mysql_result_set)
{
mysql_free_result(mysql_result_set);
mysql_result_set = NULL;
}
mysql_close(mysql_obj);
mysql_obj = NULL;
}

char *vstr(char *format, va_list args)
{
  char *buffer = NULL;
  int va_size = 256;

  do
  {
    va_size *= 2;
    buffer = (char*)realloc(buffer, va_size);
    memset(buffer, 0, va_size);
  } while (vsnprintf(buffer, va_size, format, args) < 0);

  *output = (char*)malloc(strlen(buffer) + 1);
  strcpy(output, buffer);
  free(buffer);
  return output;
}

Some thing you must know:
- it is my very first module
- it is my mostly first touch on C
- the module is build upon mikea's bs_quotes-module (-> mostly used newbies best friends: StrgC and StrgV :-/)

aaaand: sorry for missing comments . . . i thought i could "glue something together" and never look at it again . . . well, it didn't work - damn it!


1 Mistake found and corrected
« Last Edit: February 12, 2009, 07:37:05 PM by Tzann »
Logged

katsklaw

  • Guest
Re: Problem with module
« Reply #3 on: February 12, 2009, 06:10:33 PM »

[Feb 12 14:03:29 2009] Module cs_nicklist compiled against current or newer anope revision 1899, this is 1489

completely wrong version for starters.
Logged

Trystan Scott Lee

  • Contributor
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 343
Re: Problem with module
« Reply #4 on: February 13, 2009, 12:43:10 AM »

Well when built under gcc you got the following warnings

cs_nicklist: In function 'reload_config':
cs_nicklist:123: warning: return makes integer from pointer without a cast
cs_nicklist: In function 'do_change':
cs_nicklist:132: warning: unused variable 'u'
cs_nicklist: In function 'channel_watch':
cs_nicklist:177: warning: unused variable 'lengths'
cs_nicklist: In function 'vstr':
cs_nicklist:291: warning: assignment makes integer from pointer without a cast
cs_nicklist: In function 'channel_update':
cs_nicklist:214: warning: control reaches end of non-void function
cs_nicklist: In function 'channel_watch':
cs_nicklist:188: warning: control reaches end of non-void function

and loads fine under linux

[Feb 12 16:29:23.663356 2009] OperServ: Trystan: modload cs_nicklist
[Feb 12 16:29:23.663678 2009] Trying to load module [cs_nicklist]
[Feb 12 16:29:23.664637 2009] Runtime module location: /home/trystan/services/modules/runtime/cs_nicklist.so.jScyfc
[Feb 12 16:29:23.672261 2009] Module cs_nicklist compiled against current or newer anope revision 1915, this is 1915
[Feb 12 16:29:23.672832 2009] Displaying message list for join_channel
[Feb 12 16:29:23.673172 2009] 1: 0x0x85aa748
[Feb 12 16:29:23.673537 2009] end
[Feb 12 16:29:23.673833 2009] Displaying message list for part_channel
[Feb 12 16:29:23.674193 2009] 1: 0x0x85a92c8
[Feb 12 16:29:23.674479 2009] end
[Feb 12 16:29:23.674890 2009] debug: existing msg: (0x0x859d7e8), new msg (0x0x85aa840)
[Feb 12 16:29:23.675211 2009] Displaying message list for reload
[Feb 12 16:29:23.675546 2009] 1: 0x0x85aa840
[Feb 12 16:29:23.675833 2009] 2: 0x0x859d7e8
[Feb 12 16:29:23.676177 2009] 3: 0x0x8598d48
[Feb 12 16:29:23.676460 2009] 4: 0x0x8569660
[Feb 12 16:29:23.676783 2009] 5: 0x0x8566a58
[Feb 12 16:29:23.677091 2009] end
[Feb 12 16:29:23.685536 2009] [cs_nicklist]: geladen . . . [Author: Tzann] [Version: 1.0]


so recommend then that you recompile with current source code and try again
Logged
my God my tourniquet, return to me salvation

Tzann

  • Anope User
  • Offline Offline
  • Posts: 6
Re: Problem with module
« Reply #5 on: February 13, 2009, 12:31:49 PM »

The Problem with loading was a wrong number . . . . now it has an error somewhere with sql (services crash . . ), but i think (/hope) i'll manage it.

When everything is working i'll post the souce again
Logged

Tzann

  • Anope User
  • Offline Offline
  • Posts: 6
Re: Problem with module
« Reply #6 on: February 27, 2009, 09:21:23 PM »

Running Version:

Code: [Select]
#include "module.h"
#include <mysql.h>

#define AUTHOR "Tzann"
#define VERSION "1.2"

// SQL
#define GET_MYSQL_ERROR      mysql_error(mysql_obj)
MYSQL *mysql_obj = NULL;
MYSQL_RES *res = NULL;
my_ulonglong q_mysql_affected_rows;

char *NICK_SQL_HOST = NULL;
unsigned int NICK_SQL_PORT = 0;
char *NICK_SQL_USER = NULL;
char *NICK_SQL_PASS = NULL;
char *NICK_SQL_DB = NULL;

// intern struct
struct c_userlist *cu, *next;
unsigned int i;

// Events
int reload_config(int ac, char **av);
int channels_update(int ac, char **av);
int user_quit(int ac, char **av);

// Methods
int load_config();
int channel_update(char *chan);

// SQL
int do_mysql_connect();
int do_mysql_query(char *q);
void do_mysql_close();

int AnopeInit(void)
{
EvtHook *hook;
unsigned int ret;

moduleAddAuthor(AUTHOR);
moduleAddVersion(VERSION);

hook = createEventHook(EVENT_JOIN_CHANNEL, channels_update);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

hook = createEventHook(EVENT_PART_CHANNEL, channels_update);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

hook = createEventHook(EVENT_CHANGE_NICK, channels_update);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

hook = createEventHook(EVENT_USER_LOGOFF, user_quit);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

hook = createEventHook(EVENT_CHAN_DROP, channels_update);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

hook = createEventHook(EVENT_CHAN_EXPIRE, channels_update);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

hook = createEventHook(EVENT_NICK_LOGOUT, channels_update);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

hook = createEventHook(EVENT_RELOAD, reload_config);
if (moduleAddEventHook(hook) != MOD_ERR_OK)
return MOD_STOP;

if (load_config() == MOD_STOP) return MOD_STOP;

if (do_mysql_connect() == MOD_STOP)
{
alog("[\002cs_nicklist\002] Can't connect to MySQL server");
return MOD_STOP;
}

ret = do_mysql_query("CREATE TABLE IF NOT EXISTS channels"
"(chan char(50),"
"user char(50))");

if(ret == MOD_STOP)
return MOD_STOP;

alog("[\002cs_nicklist\002]: loaded . . . [Author: \002%s\002] [Version: \002%s\002]", AUTHOR, VERSION);

return MOD_CONT;
}

int load_config(void)
{
Directive confvalues[][1] = {
{{ "NickMysqlHost",       { { PARAM_STRING, PARAM_RELOAD, &NICK_SQL_HOST       } } }},
{{ "NickMysqlPort",       { { PARAM_PORT,   PARAM_RELOAD, &NICK_SQL_PORT       } } }},
{{ "NickMysqlUser",       { { PARAM_STRING, PARAM_RELOAD, &NICK_SQL_USER       } } }},
{{ "NickMysqlPass",   { { PARAM_STRING, PARAM_RELOAD, &NICK_SQL_PASS       } } }},
{{ "NickMysqlName",   { { PARAM_STRING, PARAM_RELOAD, &NICK_SQL_DB         } } }}
};

NICK_SQL_PORT = 3306;
if (NICK_SQL_HOST) { free(NICK_SQL_HOST); NICK_SQL_HOST = NULL; }
if (NICK_SQL_USER) { free(NICK_SQL_USER); NICK_SQL_USER = NULL; }
if (NICK_SQL_PASS) { free(NICK_SQL_PASS); NICK_SQL_PASS = NULL; }
if (NICK_SQL_DB)   { free(NICK_SQL_DB);   NICK_SQL_DB = NULL;   }

i = 0;
for (i = 0; i < 5; i++)
{
moduleGetConfigDirective(confvalues[i]);
}

return MOD_CONT;
}

int reload_config(int ac, char **av)
{
  if (ac >= 1)
  {
    if (!stricmp(av[0], EVENT_START))
    {
      alog("[\002cs_nicklist\002]: Reloading configuration directives...");
      return load_config();
    }
  }
  return MOD_CONT;
}

void AnopeFini(void)
{
if (NICK_SQL_HOST) { free(NICK_SQL_HOST); NICK_SQL_HOST = NULL; }
if (NICK_SQL_USER) { free(NICK_SQL_USER); NICK_SQL_USER = NULL; }
if (NICK_SQL_PASS) { free(NICK_SQL_PASS); NICK_SQL_PASS = NULL; }
if (NICK_SQL_DB)   { free(NICK_SQL_DB);   NICK_SQL_DB = NULL;   }

do_mysql_close();

alog("[\002cs_nicklist\002]: Modul unloaded");
}

int channels_update(int ac, char **av)
{
FILE *file;

Channel *c;
ChannelInfo *ci;

    int carryon = 0;
    int i;

carryon = 0;
do_mysql_query("DELETE FROM channels;");
for(i = 0; i < 1024; i++)
{
for(c = chanlist[i]; c; c = c->next)
{
channel_update(c->name);
}
}

return MOD_CONT;
}

int user_quit(int ac, char **av)
{
char query[200];
snprintf(query, 200, "DELETE FROM channels WHERE (user=\"%s\");",av[0]);
do_mysql_query(query);

return 1;
}

int channel_update(char *chan)
{
Channel *c;
char query[200];

c = findchan(chan);
cu = c->users;
while (cu)
{
next = cu->next;

snprintf(query, 200, "INSERT INTO channels(chan,user) VALUES (\"%s\",\"%s\");",c->name,cu->user->nick);
do_mysql_query(query);

cu = next;
}

return 1;
}

int do_mysql_connect()
{
mysql_obj = mysql_init(NULL);

if(mysql_real_connect(mysql_obj, NICK_SQL_HOST, NICK_SQL_USER, NICK_SQL_PASS, NICK_SQL_DB, 3306, NULL, 0) == NULL)
{
alog("[\002cs_nicklist\002]: mysql_real_connect FAILED!");
return MOD_STOP;
}

return MOD_CONT;
}

int do_mysql_query(char *q)
{
char log[300];
unsigned querysize = 0;

querysize = (unsigned) strlen(q);

//snprintf(log, 300, "Query: \"%s\" (%i)",q,querysize);
//alog(log);

if (mysql_real_query(mysql_obj, q, querysize)) {
alog("[\002cs_nicklist\002]: mysql_real_query FAILED!");
return MOD_STOP;
}
if(res != NULL)
{
mysql_free_result(res);
res = NULL;
}

res = mysql_store_result(mysql_obj);

return MOD_CONT;
}

void do_mysql_close()
{
if (res)
{
mysql_free_result(res);
res = NULL;
}
mysql_close(mysql_obj);
}

For now it updates "everything" (nicklists of every chan are put into a sql-table), but at least it's running . . . .
Logged

Jens Voss

  • Team
  • *
  • Offline Offline
  • Posts: 125
Re: Problem with module
« Reply #7 on: March 03, 2009, 08:09:45 AM »

nicknames and channel names can contain some "special" chars like  ", \ and /.
you should use mysql_real_escape_string()

http://dev.mysql.com/doc/refman/5.1/en/mysql-real-escape-string.html
http://dev.mysql.com/doc/refman/5.1/de/mysql-real-escape-string.html (german translation)
Logged

Tzann

  • Anope User
  • Offline Offline
  • Posts: 6
Re: Problem with module
« Reply #8 on: April 08, 2009, 08:53:08 AM »

So, it took a bit, but now I've added mysql_real_escape_string for channel- and nicknames and toLower for channelnames (little bug in previous version . . ).

The source:
Quote
#include "module.h"
#include <mysql.h>

#define AUTHOR "Tzann"
#define VERSION "1.3"

// SQL
#define GET_MYSQL_ERROR      mysql_error(mysql_obj)
MYSQL *mysql_obj = NULL;
MYSQL_RES *res = NULL;
my_ulonglong q_mysql_affected_rows;

char *NICK_SQL_HOST = NULL;
unsigned int NICK_SQL_PORT = 0;
char *NICK_SQL_USER = NULL;
char *NICK_SQL_PASS = NULL;
char *NICK_SQL_DB = NULL;

// intern struct
struct c_userlist *cu, *next;
unsigned int i;

// Events
int reload_config(int ac, char **av);
int channels_update(int ac, char **av);
int user_quit(int ac, char **av);

// Methods
int load_config();
int channel_update(char *chan);

// SQL
int do_mysql_connect();
int do_mysql_query(char *q);
void do_mysql_close();

//Helper
char *lower(char *str);
char *esc_str(char *str);

int AnopeInit(void)
{
   EvtHook *hook;
   unsigned int ret;

   moduleAddAuthor(AUTHOR);
   moduleAddVersion(VERSION);

   hook = createEventHook(EVENT_JOIN_CHANNEL, channels_update);
   if (moduleAddEventHook(hook) != MOD_ERR_OK)
      return MOD_STOP;

   hook = createEventHook(EVENT_PART_CHANNEL, channels_update);
   if (moduleAddEventHook(hook) != MOD_ERR_OK)
      return MOD_STOP;

   hook = createEventHook(EVENT_CHANGE_NICK, channels_update);
   if (moduleAddEventHook(hook) != MOD_ERR_OK)
      return MOD_STOP;

   hook = createEventHook(EVENT_USER_LOGOFF, user_quit);
   if (moduleAddEventHook(hook) != MOD_ERR_OK)
      return MOD_STOP;

   hook = createEventHook(EVENT_CHAN_DROP, channels_update);
   if (moduleAddEventHook(hook) != MOD_ERR_OK)
      return MOD_STOP;

   hook = createEventHook(EVENT_CHAN_EXPIRE, channels_update);
   if (moduleAddEventHook(hook) != MOD_ERR_OK)
      return MOD_STOP;

   hook = createEventHook(EVENT_NICK_LOGOUT, channels_update);
   if (moduleAddEventHook(hook) != MOD_ERR_OK)
      return MOD_STOP;

   hook = createEventHook(EVENT_RELOAD, reload_config);
   if (moduleAddEventHook(hook) != MOD_ERR_OK)
      return MOD_STOP;

   if (load_config() == MOD_STOP) return MOD_STOP;

   if (do_mysql_connect() == MOD_STOP)
   {
      alog("[\002cs_nicklist\002] Can't connect to MySQL server");
      return MOD_STOP;
   }

   ret = do_mysql_query("CREATE TABLE IF NOT EXISTS channels"
      "(chan char(50),"
      "user char(50))");

   if(ret == MOD_STOP)
      return MOD_STOP;

   alog("[\002cs_nicklist\002]: loaded . . . [Author: \002%s\002] [Version: \002%s\002]", AUTHOR, VERSION);

   return MOD_CONT;
}

int load_config(void)
{
   Directive confvalues[][1] = {
      {{ "NickMysqlHost",       { { PARAM_STRING, PARAM_RELOAD, &NICK_SQL_HOST       } } }},
      {{ "NickMysqlPort",       { { PARAM_PORT,   PARAM_RELOAD, &NICK_SQL_PORT       } } }},
      {{ "NickMysqlUser",       { { PARAM_STRING, PARAM_RELOAD, &NICK_SQL_USER       } } }},
      {{ "NickMysqlPass",        { { PARAM_STRING, PARAM_RELOAD, &NICK_SQL_PASS       } } }},
      {{ "NickMysqlName",        { { PARAM_STRING, PARAM_RELOAD, &NICK_SQL_DB         } } }}
   };

   NICK_SQL_PORT = 3306;
   if (NICK_SQL_HOST) { free(NICK_SQL_HOST); NICK_SQL_HOST = NULL; }
   if (NICK_SQL_USER) { free(NICK_SQL_USER); NICK_SQL_USER = NULL; }
   if (NICK_SQL_PASS) { free(NICK_SQL_PASS); NICK_SQL_PASS = NULL; }
   if (NICK_SQL_DB)   { free(NICK_SQL_DB);   NICK_SQL_DB = NULL;   }

   i = 0;
   for (i = 0; i < 5; i++)
   {
      moduleGetConfigDirective(confvalues);
   }

   return MOD_CONT;
}

int reload_config(int ac, char **av)
{
  if (ac >= 1)
  {
    if (!stricmp(av[0], EVENT_START))
    {
      alog("[\002cs_nicklist\002]: Reloading configuration directives...");
      return load_config();
    }
  }
  return MOD_CONT;
}

void AnopeFini(void)
{
   if (NICK_SQL_HOST) { free(NICK_SQL_HOST); NICK_SQL_HOST = NULL; }
   if (NICK_SQL_USER) { free(NICK_SQL_USER); NICK_SQL_USER = NULL; }
   if (NICK_SQL_PASS) { free(NICK_SQL_PASS); NICK_SQL_PASS = NULL; }
   if (NICK_SQL_DB)   { free(NICK_SQL_DB);   NICK_SQL_DB = NULL;   }

   do_mysql_close();

   alog("[\002cs_nicklist\002]: Modul unloaded");
}

int channels_update(int ac, char **av)
{
   FILE *file;

   Channel *c;
   ChannelInfo *ci;

    int carryon = 0;
    int i;

   carryon = 0;
   do_mysql_query("DELETE FROM channels;");
   for(i = 0; i < 1024; i++)
   {
      for(c = chanlist; c; c = c->next)
      {
         channel_update(c->name);
      }
   }

   return MOD_CONT;
}

int user_quit(int ac, char **av)
{
   char query[200];
   snprintf(query, 200, "DELETE FROM channels WHERE (user=\"%s\");",esc_str(av[0]));
   do_mysql_query(query);

   return 1;
}

int channel_update(char *chan)
{
   Channel *c;
   char query[200];

   c = findchan(chan);
   cu = c->users;
   while (cu)
   {
      next = cu->next;

      snprintf(query, 200, "INSERT INTO channels(chan,user) VALUES (\"%s\",\"%s\");",esc_str(lower(c->name)),esc_str(cu->user->nick));
      do_mysql_query(query);

      cu = next;
   }

   return 1;
}

int do_mysql_connect()
{
   mysql_obj = mysql_init(NULL);

   if(mysql_real_connect(mysql_obj, NICK_SQL_HOST, NICK_SQL_USER, NICK_SQL_PASS, NICK_SQL_DB, 3306, NULL, 0) == NULL)
   {
      alog("[\002cs_nicklist\002]: mysql_real_connect FAILED!");
      return MOD_STOP;
   }
   
   return MOD_CONT;
}

int do_mysql_query(char *q)
{
   char log[300];
   unsigned querysize = 0;

   querysize = (unsigned) strlen(q);

   //snprintf(log, 300, "Query: \"%s\" (%i)",q,querysize);
   //alog(log);

   if (mysql_real_query(mysql_obj, q, querysize)) {
      alog("[\002cs_nicklist\002]: mysql_real_query FAILED!");
      return MOD_STOP;
   }
   if(res != NULL)
   {
      mysql_free_result(res);
      res = NULL;
   }
   
   res = mysql_store_result(mysql_obj);

   return MOD_CONT;
}

void do_mysql_close()
{
   if (res)
   {
      mysql_free_result(res);
      res = NULL;
   }
   mysql_close(mysql_obj);
}

char *lower(char *str)
{
   char *toRet = str;
   for(i = 0; i < strlen(toRet); i++)
      toRet = tolower(toRet);
   return toRet;
}

char *esc_str(char *str)
{
   char *toRet = (char *) malloc(strlen(str) * 2 + 1);
   mysql_real_escape_string(mysql_obj, toRet, str, strlen(str));
   return toRet;
}

P.S.: when my Account for the module-section is activated, I'll upload the module there, too.

Logged
Pages: [1]   Go Up