Anope IRC Services

Please login or register.

Login with username, password and session length
Advanced search  

News:

Pages: [1]   Go Down

Author Topic: Need help with PrivMsg function and char pointers  (Read 6954 times)

0 Members and 1 Guest are viewing this topic.

Anthony

  • Anope User
  • Offline Offline
  • Posts: 5
Need help with PrivMsg function and char pointers
« 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  :)
Logged

Jan Milants

  • Team
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 1372
Re: Need help with PrivMsg function and char pointers
« Reply #1 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..
Logged
If you like me donate coins to 1FBmZVT4J8WAUMHKqpWhgNVj3XXnRN1cCk :)

Anthony

  • Anope User
  • Offline Offline
  • Posts: 5
Re: Need help with PrivMsg function and char pointers
« Reply #2 on: February 06, 2011, 02:28:18 PM »

Hey Jan,

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


Thank you
Logged

Jan Milants

  • Team
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 1372
Re: Need help with PrivMsg function and char pointers
« Reply #3 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.
Logged
If you like me donate coins to 1FBmZVT4J8WAUMHKqpWhgNVj3XXnRN1cCk :)

Anthony

  • Anope User
  • Offline Offline
  • Posts: 5
Re: Need help with PrivMsg function and char pointers
« Reply #4 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 :)
Logged

Jan Milants

  • Team
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 1372
Re: Need help with PrivMsg function and char pointers
« Reply #5 on: February 07, 2011, 12:08:22 AM »

that code looks fine to me, and when loading it, it also works fine...  :)
Logged
If you like me donate coins to 1FBmZVT4J8WAUMHKqpWhgNVj3XXnRN1cCk :)

Anthony

  • Anope User
  • Offline Offline
  • Posts: 5
Re: Need help with PrivMsg function and char pointers
« Reply #6 on: February 07, 2011, 01:36:18 AM »

Maybe a memory leak? :s
Logged

Anthony

  • Anope User
  • Offline Offline
  • Posts: 5
Re: Need help with PrivMsg function and char pointers
« Reply #7 on: February 07, 2011, 01:41:40 AM »

* Anthony 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
Logged

Jan Milants

  • Team
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 1372
Re: Need help with PrivMsg function and char pointers
« Reply #8 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...
Logged
If you like me donate coins to 1FBmZVT4J8WAUMHKqpWhgNVj3XXnRN1cCk :)
Pages: [1]   Go Up