Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:06 2009

## Asterisk developer's documentation

# app_hasnewvoicemail.c File Reference

HasVoicemail application. More...

`#include <stdlib.h>`
`#include <stdio.h>`
`#include <string.h>`
`#include <unistd.h>`
`#include <dirent.h>`
`#include <sys/types.h>`
`#include "asterisk.h"`
`#include "asterisk/file.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/module.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/utils.h"`
`#include "asterisk/app.h"`
`#include "asterisk/options.h"`

Include dependency graph for app_hasnewvoicemail.c:

Go to the source code of this file.

## Functions

static char * | acf_vmcount_exec (struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
char * | description (void)
| Provides a description of the module.
static int | hasvoicemail_exec (struct ast_channel *chan, void *data)
static int | hasvoicemail_internal (const char *context, const char *mailbox, const char *folder)
char * | key ()
| Returns the ASTERISK_GPL_KEY.
static int | load_config (void)
int | load_module (void)
| Initialize the module.
int | reload (void)
| Reload stuff.
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

struct ast_custom_function | acf_vmcount
static char * | app_hasnewvoicemail = "HasNewVoicemail"
static char * | app_hasvoicemail = "HasVoicemail"
static char * | hasnewvoicemail_descrip
static char * | hasnewvoicemail_synopsis = "Conditionally branches to priority + 101 with the right options set"
static char * | hasvoicemail_descrip
static char * | hasvoicemail_synopsis = "Conditionally branches to priority + 101 with the right options set"
| LOCAL_USER_DECL
| STANDARD_LOCAL_USER
static char * | tdesc = "Indicator for whether a voice mailbox has messages in a given folder."

## Detailed Description

HasVoicemail application.

Definition in file app_hasnewvoicemail.c.

## Function Documentation

static char* acf_vmcount_exec | ( | struct ast_channel * | chan,
|  | char * | cmd,
|  | char * | data,
|  | char * | buf,
|  | size_t | len |
| ) |  |  | `[static]`

Definition at line 252 of file app_hasnewvoicemail.c.

References ast_log(), ast_strdupa, context, hasvoicemail_internal(), LOCAL_USER_ACF_ADD, LOCAL_USER_REMOVE, LOG_ERROR, and strsep().

    00253 {
    00254    struct localuser *u;
    00255    char *args, *context, *box, *folder;
    00256
    00257    LOCAL_USER_ACF_ADD(u);
    00258
    00259    buf[0] = '\0';
    00260
    00261    args = ast_strdupa(data);
    00262    if (!args) {
    00263       ast_log(LOG_ERROR, "Out of memory\n");
    00264       LOCAL_USER_REMOVE(u);
    00265       return buf;
    00266    }
    00267
    00268    box = strsep(&args, "|");
    00269    if (strchr(box, '@')) {
    00270       context = box;
    00271       box = strsep(&context, "@");
    00272    } else {
    00273       context = "default";
    00274    }
    00275
    00276    if (args) {
    00277       folder = args;
    00278    } else {
    00279       folder = "INBOX";
    00280    }
    00281
    00282    snprintf(buf, len, "%d", hasvoicemail_internal(context, box, folder));
    00283
    00284    LOCAL_USER_REMOVE(u);
    00285
    00286    return buf;
    00287 }

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 348 of file app_hasnewvoicemail.c.

    00349 {
    00350    return tdesc;
    00351 }

static int hasvoicemail_exec | ( | struct ast_channel * | chan,
|  | void * | data |
| ) |  |  | `[static]`

Definition at line 174 of file app_hasnewvoicemail.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_goto_if_exists(), ast_log(), AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), ast_channel::context, context, dep_warning, ast_channel::exten, hasvoicemail_internal(), input(), LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_ERROR, LOG_WARNING, option_priority_jumping, pbx_builtin_setvar_helper(), ast_channel::priority, and strsep().

Referenced by load_module().

    00175 {
    00176    struct localuser *u;
    00177    char *input, *varname = NULL, *vmbox, *context = "default";
    00178    char *vmfolder;
    00179    int vmcount = 0;
    00180    static int dep_warning = 0;
    00181    int priority_jump = 0;
    00182    char tmp[12];
    00183    AST_DECLARE_APP_ARGS(args,
    00184       AST_APP_ARG(vmbox);
    00185       AST_APP_ARG(varname);
    00186       AST_APP_ARG(options);
    00187    );
    00188
    00189    if (!dep_warning) {
    00190       ast_log(LOG_WARNING, "The applications HasVoicemail and HasNewVoicemail have been deprecated.  Please use the VMCOUNT() function instead.\n");
    00191       dep_warning = 1;
    00192    }
    00193
    00194    if (!data) {
    00195       ast_log(LOG_WARNING, "HasVoicemail requires an argument (vm-box[/folder][@context][|varname[|options]])\n");
    00196       return -1;
    00197    }
    00198
    00199    LOCAL_USER_ADD(u);
    00200
    00201    input = ast_strdupa((char *)data);
    00202    if (! input) {
    00203       ast_log(LOG_ERROR, "Out of memory error\n");
    00204       LOCAL_USER_REMOVE(u);
    00205       return -1;
    00206    }
    00207
    00208    AST_STANDARD_APP_ARGS(args, input);
    00209
    00210    if ((vmbox = strsep(&args.vmbox, "@")))
    00211       if (!ast_strlen_zero(args.vmbox))
    00212          context = args.vmbox;
    00213    if (!vmbox)
    00214       vmbox = args.vmbox;
    00215
    00216    vmfolder = strchr(vmbox, '/');
    00217    if (vmfolder) {
    00218       *vmfolder = '\0';
    00219       vmfolder++;
    00220    } else {
    00221       vmfolder = "INBOX";
    00222    }
    00223
    00224    if (args.options) {
    00225       if (strchr(args.options, 'j'))
    00226          priority_jump = 1;
    00227    }
    00228
    00229    vmcount = hasvoicemail_internal(context, vmbox, vmfolder);
    00230    /* Set the count in the channel variable */
    00231    if (varname) {
    00232       snprintf(tmp, sizeof(tmp), "%d", vmcount);
    00233       pbx_builtin_setvar_helper(chan, varname, tmp);
    00234    }
    00235
    00236    if (vmcount > 0) {
    00237       /* Branch to the next extension */
    00238       if (priority_jump || option_priority_jumping) {
    00239          if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101))
    00240             ast_log(LOG_WARNING, "VM box %s@%s has new voicemail, but extension %s, priority %d doesn't exist\n", vmbox, context, chan->exten, chan->priority + 101);
    00241       }
    00242    }
    00243
    00244    snprintf(tmp, sizeof(tmp), "%d", vmcount);
    00245    pbx_builtin_setvar_helper(chan, "HASVMSTATUS", tmp);
    00246
    00247    LOCAL_USER_REMOVE(u);
    00248
    00249    return 0;
    00250 }

static int hasvoicemail_internal | ( | const char * | context,
|  | const char * | mailbox,
|  | const char * | folder |
| ) |  |  | `[static]`

Definition at line 147 of file app_hasnewvoicemail.c.

References ast_config_AST_SPOOL_DIR, and ast_strlen_zero().

Referenced by acf_vmcount_exec(), and hasvoicemail_exec().

    00148 {
    00149    DIR *dir;
    00150    struct dirent *de;
    00151    char fn[256];
    00152    int count = 0;
    00153
    00154    if (ast_strlen_zero(folder))
    00155       folder = "INBOX";
    00156    if (ast_strlen_zero(context))
    00157       context = "default";
    00158    /* If no mailbox, return immediately */
    00159    if (ast_strlen_zero(mailbox))
    00160       return 0;
    00161    snprintf(fn, sizeof(fn), "%s/voicemail/%s/%s/%s", ast_config_AST_SPOOL_DIR, context, mailbox, folder);
    00162    dir = opendir(fn);
    00163    if (!dir)
    00164       return 0;
    00165    while ((de = readdir(dir))) {
    00166       if (!strncasecmp(de->d_name, "msg", 3) && !strcasecmp(de->d_name + 8, "txt"))
    00167          count++;
    00168    }
    00169    closedir(dir);
    00170    return count;
    00171 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 360 of file app_hasnewvoicemail.c.

References ASTERISK_GPL_KEY.

    00361 {
    00362    return ASTERISK_GPL_KEY;
    00363 }

static int load_config | ( | void |  | ) | `[static]`

Definition at line 299 of file app_hasnewvoicemail.c.

References ast_config_destroy(), ast_config_load(), ast_variable_retrieve(), and cfg.

    00300 {
    00301 #ifdef USE_ODBC_STORAGE
    00302    struct ast_config *cfg;
    00303    char *tmp;
    00304    cfg = ast_config_load("voicemail.conf");
    00305    if (cfg) {
    00306       if (! (tmp = ast_variable_retrieve(cfg, "general", "odbcstorage")))
    00307          tmp = "asterisk";
    00308       ast_copy_string(odbc_database, tmp, sizeof(odbc_database));
    00309
    00310       if (! (tmp = ast_variable_retrieve(cfg, "general", "odbctable")))
    00311          tmp = "voicemessages";
    00312       ast_copy_string(odbc_table, tmp, sizeof(odbc_table));
    00313       ast_config_destroy(cfg);
    00314    }
    00315 #endif
    00316    return 0;
    00317 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 337 of file app_hasnewvoicemail.c.

References ast_custom_function_register(), ast_register_application(), hasvoicemail_exec(), and load_config().

    00338 {
    00339    int res;
    00340    load_config();
    00341    res = ast_custom_function_register(&acf_vmcount);
    00342    res |= ast_register_application(app_hasvoicemail, hasvoicemail_exec, hasvoicemail_synopsis, hasvoicemail_descrip);
    00343    res |= ast_register_application(app_hasnewvoicemail, hasvoicemail_exec, hasnewvoicemail_synopsis, hasnewvoicemail_descrip);
    00344
    00345    return res;
    00346 }

int reload | ( | void |  | ) |

Reload stuff.

This function is where any reload routines take place. Re-read config files, change signalling, whatever is appropriate on a reload.

Returns:
  The return value is not used.

Definition at line 319 of file app_hasnewvoicemail.c.

References load_config().

    00320 {
    00321    return load_config();
    00322 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 324 of file app_hasnewvoicemail.c.

References ast_custom_function_unregister(), ast_unregister_application(), and STANDARD_HANGUP_LOCALUSERS.

    00325 {
    00326    int res;
    00327
    00328    res = ast_custom_function_unregister(&acf_vmcount);
    00329    res |= ast_unregister_application(app_hasvoicemail);
    00330    res |= ast_unregister_application(app_hasnewvoicemail);
    00331
    00332    STANDARD_HANGUP_LOCALUSERS;
    00333
    00334    return res;
    00335 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 353 of file app_hasnewvoicemail.c.

References STANDARD_USECOUNT.

    00354 {
    00355    int res;
    00356    STANDARD_USECOUNT(res);
    00357    return res;
    00358 }

## Variable Documentation

struct ast_custom_function acf_vmcount

Definition at line 289 of file app_hasnewvoicemail.c.

char* app_hasnewvoicemail = "HasNewVoicemail"`[static]`

Definition at line 70 of file app_hasnewvoicemail.c.

char* app_hasvoicemail = "HasVoicemail"`[static]`

Definition at line 58 of file app_hasnewvoicemail.c.

char* hasnewvoicemail_descrip`[static]`

Definition at line 72 of file app_hasnewvoicemail.c.

char* hasnewvoicemail_synopsis = "Conditionally branches to priority + 101 with the right options set"`[static]`

Definition at line 71 of file app_hasnewvoicemail.c.

char* hasvoicemail_descrip`[static]`

Definition at line 60 of file app_hasnewvoicemail.c.

char* hasvoicemail_synopsis = "Conditionally branches to priority + 101 with the right options set"`[static]`

Definition at line 59 of file app_hasnewvoicemail.c.

LOCAL_USER_DECL

Definition at line 84 of file app_hasnewvoicemail.c.

STANDARD_LOCAL_USER

Definition at line 82 of file app_hasnewvoicemail.c.

char* tdesc = "Indicator for whether a voice mailbox has messages in a given folder."`[static]`

Definition at line 57 of file app_hasnewvoicemail.c.