Anope IRC Services

Please login or register.

Login with username, password and session length
Advanced search  

News:

Pages: [1]   Go Down

Author Topic: Adding to commands  (Read 7077 times)

0 Members and 1 Guest are viewing this topic.

lavinpj1

  • Guest
Adding to commands
« on: April 25, 2006, 06:45:23 PM »

I'm playing with getting botserv to send a notice when they say command is used. I have added all the correct parts etc. and it works ok using the following code as the function...


Code: [Select]
int bs_sayalert(User *u)
{
    notice(s_BotServ, u->nick, "You have used the Say command!");
    return MOD_STOP;
}


Yet, if I then want to return the inputted params in the notice, using the following code...

Code: [Select]
int bs_sayalert(User *u)
{
    char *curmodchan = strtok(NULL, " ");
    char *saidtext = strtok(NULL, " ");
    notice(s_BotServ, u->nick, "You have used the Say command in %s saying %s!", curmodchan, saidtext);
    return MOD_CONT;
}


It sends the notice fine, yet it seems to strip those 2 params out of the command as it relays to botserv, in such that it gives me the message (18:39) -BotServ- Syntax: SAY channel text. This is confirmed if I enter /bs say #channel test #channel test2. The module tells me I said "test" in the channel, yet the botserv bot says "test2" in the channel.

How can I continue module execution with the original command rather than the somewhat ripped up one?

Cheers

Phil
Logged

Dave Robson

  • Team
  • *
  • Offline Offline
  • Posts: 357
(No subject)
« Reply #1 on: April 25, 2006, 08:11:38 PM »

you cant use strtok() to parse the input.
Logged

lavinpj1

  • Guest
(No subject)
« Reply #2 on: April 25, 2006, 08:13:48 PM »

Would you care to enlighten me on what I can use?

That works fine for all my other modules in which they do not have to continue execution.

Cheers

Phil
Logged

Jan Milants

  • Team
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 1372
(No subject)
« Reply #3 on: April 25, 2006, 09:48:45 PM »

try using myStrGetToken instead, but you will have to give the params to the function as well

quote from anope's wiki
Code: [Select]
Is strtok() used?
    * The use of strtok will often indicate a potential error as strtok is not thread safe, and is used inside of Anope's core.

[Edited on 25-4-2006 by Viper]
Logged
If you like me donate coins to 1FBmZVT4J8WAUMHKqpWhgNVj3XXnRN1cCk :)

Dave Robson

  • Team
  • *
  • Offline Offline
  • Posts: 357
(No subject)
« Reply #4 on: April 25, 2006, 09:52:24 PM »

/**
 * Return a copy of the complete last buffer.
 * This is needed for modules who cant trust the strtok() buffer, as we dont know who will have already messed about with it.
 * @reutrn a pointer to a copy of the last buffer - DONT mess with this, copy if first if you must do things to it.
 **/
char *moduleGetLastBuffer(void);

/**
 * Will return a malloced copy of the token specified in token_number splitting the string based on dilim.  It is the modules responsibility to free() the returned pointer
 * @param str String to search in
 * @param dilim Character to search for
 * @param token_number the token number
 * @return token
 **/
char *myStrGetToken(const char *str, const char dilim, int token_number);

/**
 * Will only return a token if a valid dilimiter is found, i.e. the end of the string dosnt count.
 * @param str String to search in
 * @param dilim Character to search for
 * @param token_number the token number
 * @return token
 **/
char *myStrGetOnlyToken(const char *str, const char dilim, int token_number);

/**
 * Will return from the given token to the end of the string, regardless or other tokens
 * @param str String to search in
 * @param dilim Character to search for
 * @param token_number the token number
 * @return token
 **/
char *myStrGetTokenRemainder(const char *str, const char dilim,int token_number);
Logged

lavinpj1

  • Guest
(No subject)
« Reply #5 on: April 25, 2006, 10:00:30 PM »

Hrmmm, looks like what I need. This is what I came up with, but I feel it is wrong...


Code: [Select]

    char *gettehinfo = moduleGetLastBuffer();
    char *curmodchan = myStrGetToken(gettehinfo, " ", 1);
    char *saidtext = myStrGetToken(gettehinfo, " ", 2);


I want it to get the 1st and 2nd parameter words.

Cheers

Phil
Logged

lavinpj1

  • Guest
(No subject)
« Reply #6 on: April 25, 2006, 10:18:34 PM »

Oh, Ok. Scratch the last point. Checked out cs_disablexop.c and saw the format was slightly wrong. It should have been...

Code: [Select]

    char *curmodchan = myStrGetToken(gettehinfo,' ', 0);
    char *saidtext = myStrGetToken(gettehinfo,' ', 2);


What I would be interested to know though, is how I can get every word after a particular point.

E.g. /bs whatever #channel reason for whatever here.

I can get #channel into a variable, but I would also like another variable with "reason for whatever here." in it.

Cheers

Phil
Logged

Dave Robson

  • Team
  • *
  • Offline Offline
  • Posts: 357
(No subject)
« Reply #7 on: April 25, 2006, 10:31:32 PM »

/**
* Will return from the given token to the end of the string, regardless or other tokens
* @param str String to search in
* @param dilim Character to search for
* @param token_number the token number
* @return token
**/
char *myStrGetTokenRemainder(const char *str, const char dilim,int token_number);
Logged

lavinpj1

  • Guest
(No subject)
« Reply #8 on: April 25, 2006, 10:39:32 PM »

Ah, yes, sorry, I didn't read that fully.

LAST thing (I prommise)

I am trying to get the founder of a channel. From other modules, I have got this...


Code: [Select]

    char *gettehinfo = moduleGetLastBuffer();
    char *chan = myStrGetToken(gettehinfo,' ', 0);
    ChannelInfo *ci;
    char *saidtext = myStrGetTokenRemainder(gettehinfo,' ', 1);
    notice(s_BotServ, u->nick, "You have used the Say command in %s saying %s which has the founder %s", chan, saidtext, ci->founder);


Yet, on compile, I get the message:

bs_sayalert.c:44: warning: `ci' might be used uninitialized in this function

Am I doing something wrong?

Cheers

Phil
Logged

lavinpj1

  • Guest
(No subject)
« Reply #9 on: April 25, 2006, 11:17:12 PM »

Oh, Slight improvement here...


Code: [Select]

    char *chan = myStrGetToken(gettehinfo,' ', 0);
    ChannelInfo *ci;
    char *saidtext = myStrGetTokenRemainder(gettehinfo,' ', 1);
    char *cifoundernick;
    if (ci = cs_findchan(chan)) {
       notice(s_BotServ, u->nick, "You have used the Say command in %s saying %s which has the founder %s", chan, saidtext, ci->founder);  
    }


No errors/crashing services, yet it seems that the ci->founder returns nothing :/

Cheers

Phil
Logged

Jan Milants

  • Team
  • *
  • Offline Offline
  • Gender: Male
  • Posts: 1372
(No subject)
« Reply #10 on: April 25, 2006, 11:39:46 PM »

you can check http://anope.zero.org/doxy ... all the types are listed there

ci->founder can also be a nickcore i think (if there even is something that returns a founder as char in ci, not sure), so it ll return a reference to an object which will prolly be ignored or so

try ci->founder->display

could be wrong here... it s late and i m not really any gd with c++

[Edited on 25-4-2006 by Viper]

[Edited on 25-4-2006 by Viper]
Logged
If you like me donate coins to 1FBmZVT4J8WAUMHKqpWhgNVj3XXnRN1cCk :)

lavinpj1

  • Guest
(No subject)
« Reply #11 on: April 26, 2006, 07:17:59 AM »

You're a genius! Thanks so much :)
Logged
Pages: [1]   Go Up