Anope IRC Services

Anope Development => Modules => Topic started by: Anthony on February 06, 2011, 12:47:13 PM

Title: Need help with PrivMsg function and char pointers
Post by: Anthony on February 06, 2011, 12:47:13 PM
Hello,

I am developing a module for my IRC network and I can't get some of the code to work.

I have the following:
Code: [Select]
int os_add_member(User *u)
{
    char *nick;
    buf  = moduleGetLastBuffer();
    nick = myStrGetToken(buf, ' ', 0);
    privmsg(s_NickServ, "#channel", "%s has been added", nick);
}

However when I run the command I like so:
Code: [Select]
ADDMEMBER John
I get the following as a privmsg:
Code: [Select]
<NickOP> pµê has been added
<NickOP> @¹ê has been added
<NickOP>  ¼ê has been added

As you can see, it appears to be printing the pointer, rather than the string.
I assume that's why it varies each time I run it.

How do I get it to print the value of nick?


Thank you  :)
Title: Re: Need help with PrivMsg function and char pointers
Post by: Jan Milants on February 06, 2011, 02:20:46 PM
is this just an excerpt or is this the whole stuff?
Reason I am asking is that there doesn't seem to be a declaration of the "buf" pointer..
Title: Re: Need help with PrivMsg function and char pointers
Post by: Anthony on February 06, 2011, 02:28:18 PM
Hey Jan,

This in an excerpt. buf is defined as char *buf


Thank you
Title: Re: Need help with PrivMsg function and char pointers
Post by: Jan Milants on February 06, 2011, 08:38:29 PM
can you post working code that has the issue.. something I could just compile and see it do what you mentioned.
Title: Re: Need help with PrivMsg function and char pointers
Post by: Anthony on February 06, 2011, 11:11:16 PM
Hello Jan,

The only thing missing is the declarations and init function but I am more than happy to post this.

Code: [Select]
#include "module.h"

#define AUTHOR  "Anthony Myatt"
#define VERSION "1.0.0"
#define MODNAME "os_tribefm"

int os_add_member(User *u);
int AnopeInit(int argc, char **argv) {

    moduleAddAuthor(AUTHOR);
    moduleAddVersion(VERSION);

    Command *c1;
    c1 = createCommand("ADD_MEMBER", os_add_member, is_services_admin, -1, -1, -1, -1, -1);
    moduleAddCommand(OPERSERV, c1, MOD_HEAD);

    return MOD_CONT;
}

int os_add_member(User *u) {
    char *buf, *nick, *pass, *email;

    buf = moduleGetLastBuffer();
    nick = myStrGetToken(buf, ' ', 0);
    pass = myStrGetToken(buf, ' ', 1);
    email = myStrGetToken(buf, ' ', 2);

    privmsg(s_NickServ, "#TribeFM", "%s has been added.", nick);
    return MOD_STOP;
}

Thank you for your help :)
Title: Re: Need help with PrivMsg function and char pointers
Post by: Jan Milants on February 07, 2011, 12:08:22 AM
that code looks fine to me, and when loading it, it also works fine...  :)
Title: Re: Need help with PrivMsg function and char pointers
Post by: Anthony on February 07, 2011, 01:36:18 AM
Maybe a memory leak? :s
Title: Re: Need help with PrivMsg function and char pointers
Post by: Anthony on February 07, 2011, 01:41:40 AM
/me feels stupid now.

Thank you for your help Jan. Maybe I should post the entire module next time :s
I found out that I was doing free(nick) in a seperate function just before calling the privmsg command in the function I thought was failing.

Working like a charm :D
Title: Re: Need help with PrivMsg function and char pointers
Post by: Jan Milants on February 07, 2011, 07:43:17 AM
hence why I asked you to post the code with which to reproduce.. posting excerpts is usually utterly useless...