This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

module programming - step 2

Old posts that have not been replied to for several years.
s
serra
Voice
Posts: 17
Joined: Sat Feb 22, 2003 10:24 pm

module programming - step 2

Post by serra »

HI!

Thanks for helping and for your quick answer!

I've tried that but the bot is still crashing when I type !baa in channel.

The code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
  
#define MODULE_NAME "xpto"
#define MAKING_XPTO
#include "src/mod/module.h"
#include "../irc.mod/irc.h"
#include "../server.mod/server.h"
 
#undef global
static Function *global = NULL;
static Function *irc_funcs = NULL;
static Function *server_funcs = NULL;
  
static int xpto_expmem() {
  return(0);
}
  
static int pub_sheep (char* nick, char* host, char* handle, char* chan, char* text) {
  
  dprintf(DP_SERVER, "privmsg %s :ooo yes you sexy sheep, baa for me!\r\n", chan);
  dprintf(DP_SERVER, "privmsg %s :%cACTION sniffs your sweet tail%c\r\n", 1, 1);
  putlog(LOG_CMDS, "*", "#%s@%s# baaaaaa (params: '%s')", nick, chan, text);
  
  return(0);
}
 
static void xpto_report (int idx, int details) {
  dprintf(idx, "YOU ARE COOL FOR USING SHEEP.MOD! Yes you are. Pat yourself on the back!");
}   

static cmd_t xpto_pub[] = {
  {"!baa", "", pub_sheep, NULL},
  {0, 0, 0, 0}
};

static char *xpto_close()
{
  rem_builtins(H_pub, xpto_pub);
  module_undepend("xpto");
  return(NULL);
}

EXPORT_SCOPE char *xpto_start();  

static Function xpto_table[] = {
  (Function) xpto_start,  
  (Function) xpto_close,
  (Function) xpto_expmem,
  (Function) xpto_report,
};
  
char *xpto_start(Function *global_funcs) {
  global = global_funcs;
  module_register(MODULE_NAME, xpto_table, 0, 9);
  
  if(!(irc_funcs = module_depend(MODULE_NAME, "irc", 1, 0)))
    return "You need the irc module to use the xpto module.";
  if (!(server_funcs = module_depend(MODULE_NAME, "server", 1, 0)))
    return "You need the server module to use the xpto module.";
  
  if (!module_depend(MODULE_NAME, "eggdrop", 106, 0)) {
    module_undepend(MODULE_NAME);
    return "This module requires eggdrop1.4.0 or later";
  }
  
  add_builtins(H_pub, xpto_pub);
  putlog(LOG_MISC, "*", "xpto.mod loaded.");

  return NULL;
}


So.. I think the code is like you said :-?
Have you got some sugestions?

thanks :D

pedro
s
serra
Voice
Posts: 17
Joined: Sat Feb 22, 2003 10:24 pm

Post by serra »

this message was suppost to be in "public trigger" topic! sorry :p

pedro
R
RedAlert
Voice
Posts: 17
Joined: Mon Nov 19, 2001 8:00 pm

Re: module programming - step 2

Post by RedAlert »

Code: Select all

  dprintf(DP_SERVER, "privmsg %s :%cACTION sniffs your sweet tail%c\r\n", 1, 1);
There's a "chan" argument missing there; your compiler should warn you about this...
You can also use \001 or \x01 instead of the %c construction by the way.
s
serra
Voice
Posts: 17
Joined: Sat Feb 22, 2003 10:24 pm

Post by serra »

Oh yeah! It worked! Thanks! :D
Pedro
s
serra
Voice
Posts: 17
Joined: Sat Feb 22, 2003 10:24 pm

Post by serra »

Hello!

So.. now that the module is running, i wonder how can i find functions like isop, isvoice, matchattr, userlist etc That are implemented in TCL scripts?
I've searched in a couple of modules but I think in general the modules available dont need to use this to reach the objective.

Thanks for helping
pedro
s
serra
Voice
Posts: 17
Joined: Sat Feb 22, 2003 10:24 pm

Post by serra »

Neverming, i think i got it!

Code: Select all

static int isop(char *nick, struct chanset_t *chan) {
  memberlist *m;
  return ((m = ismember(chan, nick)) && (chan_hasop(m)));
}  
      
static int isvoice(char *nick, struct chanset_t *chan) {
  memberlist *m;
  return ((m = ismember(chan, nick) ) && (chan_hasvoice(m)));
}

Code: Select all

static int matchattr(struct userrec *u, char *flags, char *chan) {
  /*
   * function matchattr: from botnetop.mod
   * by Teemu Hjelt <temex@iki.fi> 
   */

  struct flag_record plus, minus, user;
  int ok = 0, f;
  
  if (u && (!chan || findchan_by_dname(chan))) {
    user.match = FR_GLOBAL | (chan ? FR_CHAN : 0) | FR_BOT;
    get_user_flagrec(u, &user, chan);
    plus.match = user.match;
    break_down_flags(flags, &plus, &minus);
    f = (minus.global || minus.udef_global || minus.chan ||
         minus.udef_chan || minus.bot);
    if (flagrec_eq(&plus, &user)) {
      if (!f)
        ok = 1;
      else {
        minus.match = plus.match ^ (FR_AND | FR_OR);
        if (!flagrec_eq(&minus, &user))
          ok = 1;
      }
    }
  }
  return ok;
}
and to get the userlist:

Code: Select all

  for (aux = (chan -> channel).member ; aux->next != NULL ; aux = aux -> next)

Is there an simpler way to do this?
I think isop(), isvoice(), and matchattr(), at least, should realy come with eggdrop source by default.

bye
Pedro
s
serra
Voice
Posts: 17
Joined: Sat Feb 22, 2003 10:24 pm

Post by serra »

oh!, by the way.

Im using a raw binding, and i had to split args to get access to each word of the string. I searched, in eggdrop sources, for something like split() that seemed to TCL split funcion, but with no results.
So i've made it myself but now i know i should have used strtok() to get it simple:


Code: Select all

static char **split(char s[]) {
  /*
   * Description:
   *   creates a dynamic arraw of strings with NULL in the last position
   *   each position of this arraw has a word from the string in argument
   *   a word is a group of chars limited by 2 wite spaces.
   * Argument:
   *   char *s: string to get divided in words
   * Retorna:
   *   char **v: arraw of strings
   */
  char **v;
  int i,n,j, k,h;

  /*inicio do vector com a primeira posicao a NULL*/
  v = (char**) calloc(1,sizeof(char));
  v[0] = (char*) calloc(1, sizeof(char*));
  v[0] = NULL;
 
  for (i=h=0, n = 2 ; s[i] != '\0' ; i++) { 
    /*condicao para entrar*/
    if ((i==0 && s[i] != ' ') || (s[i] == ' ')) {
     
      /*uma vez verificada a condicao, significa que temos uma palavra nova
        vamos entao reservar espaco para ela no nosso vector*/
      v = (char**) realloc(v ,n*sizeof(char**) );
  
      /* vamos ag comer os espacos que estao a mais! */
      while(s[i+1] == ' ') i++;
      if(s[i+1] == '\0') break;
 
      /*determinar o nr de posicoes da palavra*/
      if (i == 0) j = 0; else j = i+1;
      for (k=1 ; ((s[j] != '\0') && (s[j] != ' ')); j++, k++);
     
      /*reservar espaco de memoria com a dimensao da palavra*/
      v[h] = (char*) calloc(k, sizeof(char));
      
      /*copiar a palavra para a string dinamica*/
      if (i==0) j = 0; else j = i+1;
      for (k=0 ;((s[j] != '\0')  && (s[j] != ' ')); k++, j++) v[h][k] = s[j];
      v[h][k] = 0; /*terminador*/
 
      h++;n++; /*progressao*/

    }
  }
     
  /*terminar o vector para mais facil leitura por meio de cilcos*/
  v[h] = (char*) calloc(1, sizeof(char*));   
  v[h] = NULL;
      
  return v;
}
sorry for the portuguese comments but... wel sorry :p

the question is the same, is there a simpler way of doing this?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Eggdrop provides 3 functions to help you split strings.

newsplit()
splitc()
splitcn()

Check out src/misc.c to see how they work.
P
Photon
Op
Posts: 170
Joined: Wed Aug 28, 2002 8:00 am
Location: Liverpool, England

Post by Photon »

look in modules.h - these are all the functions externed to you, via the setting up of pointers with moduel_depends....

You are reinventing the wheel. (apart from matchattr, annoyingly)
l
lobz

module and c++

Post by lobz »

Is it possible to use some classes in an eggdrop module? I'm trying to compile the module with g++ but I get "too many arguments" error when using functions like putlog as if EGG_VARARGS_DEF in the function's definition would not be expanded or something.
P
Photon
Op
Posts: 170
Joined: Wed Aug 28, 2002 8:00 am
Location: Liverpool, England

Post by Photon »

by that I presume you mean C++...

I've never tried it .. I know gcc does c++ if you just change the extension of the file to .cpp but I also know this does affect a number of things.

My question is this, does it compile as C - if it does, then you have your answer. Dont use C++.

Real programmers use C anyway :D
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

Photon wrote: Real programmers use C anyway :D
hehe... either you use c++ or assembler :P *g*
l
lobz

Post by lobz »

Photon wrote:by that I presume you mean C++...
g++ is the compiler, c++ the language. I tried compiling it with gcc but it does not take the file as a c++ one so I used g++ but I got the error above.
l
lobz

Post by lobz »

It should work fine but I dont understand why the EGG_VARARGS_DEF macro doesnt get expanded or how do you call that :)
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

In module.h, putlog isn't defined with EGG_VARARGS_DEF.

#define putlog (global[197])

Try one of these:

1. Change putlog in your program to (*putlog)

2. Right after you include module.h, try these lines:

#undef putlog
#define putlog ((int(*)(...))global[197])
Locked