Tutorials:Modules:Configuration

From AnopeWiki

Jump to: navigation, search

Contents

Configuration

Anope allows you to parse configuration directives in services.conf for use with your module.

struct Directive

Configuration directives are stored in a struct Directive, which looks like this:

typedef struct {
    char *name;
    struct {
        int type;
        int flags;
        void *ptr;
    } params[MAXPARAMS];
} Directive;

In params, type can be:

ValueParam typeType of ptr
PARAM_INTIntegerint
PARAM_POSINTPositive Integerint
PARAM_PORTInteger between 1-65535int
PARAM_STRINGStringchar *
PARAM_TIMETime paramenter (5m, 3d, etc)int
PARAM_SETNo param - set ptr to 1int

The flag field can be used to set various flags for the configuration directive, like PARAM_OPTIONAL to make the param optional, and PARAM_RELOAD to be able to reload the param. You can usually enter PARAM_RELOAD in this field.

The ptr is a pointer to the data type outlined in the table above. The variable stored here will be set to the value found in the directive if it has been parsed.

moduleGetConfigDirective

Parse the given configuration directive.

int moduleGetConfigDirective(Directive *d)

You pass a pointer to your configuration Directive as d. This function will parse the configuration file and fill your directive, if it has been found.

Example

#include <module.h>

int AnopeInit(int argc, char **argv)
{
  char *my_services_root = NULL;
  Directive confvalues[] = {
      { "ServicesRoot", { { PARAM_STRING, PARAM_RELOAD, &my_services_root } } }
  };
  
  moduleGetConfigDirective(confvalues);

  if (my_services_root) {
      alog("ServicesRoot set to '%s'", my_services_root);
  } else {
      alog("ServicesRoot not set");
  }
  
  return MOD_CONT;
}
Personal tools