From: Joshua Colp Date: Mon, 6 Nov 2006 17:05:04 +0000 (+0000) Subject: Add support for manager hooks, so you could fire off manager events over IRC if you... X-Git-Tag: 1.6.0-beta1~3^2~4106 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=980ce017ec0919bbae1373dfe36c0c9799c825be;p=thirdparty%2Fasterisk.git Add support for manager hooks, so you could fire off manager events over IRC if you were crazy enough. (issue #5161 reported by anthm with mods by moi) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47229 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/include/asterisk/manager.h b/include/asterisk/manager.h index 6a44ab9129..c7b4896918 100644 --- a/include/asterisk/manager.h +++ b/include/asterisk/manager.h @@ -60,6 +60,28 @@ #define AST_MAX_MANHEADERS 80 #define AST_MAX_MANHEADER_LEN 256 +/* Manager Helper Function */ +typedef int (*manager_hook_t)(int, const char *, char *); + +struct manager_custom_hook { + /*! Identifier */ + char *file; + /*! helper function */ + manager_hook_t helper; + /*! Linked list information */ + AST_RWLIST_ENTRY(manager_custom_hook) list; +}; + +/*! Add a custom hook to be called when an event is fired */ +/*! \param hook struct manager_custom_hook object to add +*/ +void ast_manager_register_hook(struct manager_custom_hook *hook); + +/*! Delete a custom hook to be called when an event is fired */ +/*! \param hook struct manager_custom_hook object to delete +*/ +void ast_manager_unregister_hook(struct manager_custom_hook *hook); + struct mansession; struct message { diff --git a/main/manager.c b/main/manager.c index 666cf2848e..80cfb07ad4 100644 --- a/main/manager.c +++ b/main/manager.c @@ -183,6 +183,26 @@ static AST_LIST_HEAD_STATIC(users, ast_manager_user); static struct manager_action *first_action = NULL; AST_MUTEX_DEFINE_STATIC(actionlock); +static AST_RWLIST_HEAD_STATIC(manager_hooks, manager_custom_hook); + +/*! \brief Add a custom hook to be called when an event is fired */ +void ast_manager_register_hook(struct manager_custom_hook *hook) +{ + AST_RWLIST_WRLOCK(&manager_hooks); + AST_RWLIST_INSERT_TAIL(&manager_hooks, hook, list); + AST_RWLIST_UNLOCK(&manager_hooks); + return; +} + +/*! \brief Delete a custom hook to be called when an event is fired */ +void ast_manager_unregister_hook(struct manager_custom_hook *hook) +{ + AST_RWLIST_WRLOCK(&manager_hooks); + AST_RWLIST_REMOVE(&manager_hooks, hook, list); + AST_RWLIST_UNLOCK(&manager_hooks); + return; +} + /*! \brief * Event list management functions. * We assume that the event list always has at least one element, @@ -2213,7 +2233,9 @@ int __manager_event(int category, const char *event, const char *file, int line, const char *func, const char *fmt, ...) { struct mansession *s; + struct manager_custom_hook *hook; char auth[80]; + char tmp[4096] = ""; va_list ap; struct timeval now; struct ast_dynamic_str *buf; @@ -2262,6 +2284,22 @@ int __manager_event(int category, const char *event, } AST_LIST_UNLOCK(&sessions); + AST_RWLIST_RDLOCK(&manager_hooks); + if (!AST_RWLIST_EMPTY(&manager_hooks)) { + char *p; + int len; + snprintf(tmp, sizeof(tmp), "event: %s\r\nprivilege: %s\r\n", event, authority_to_str(category, tmp, sizeof(tmp))); + len = strlen(tmp); + p = tmp + len; + va_start(ap, fmt); + vsnprintf(p, sizeof(tmp) - len, fmt, ap); + va_end(ap); + AST_RWLIST_TRAVERSE(&manager_hooks, hook, list) { + hook->helper(category, event, tmp); + } + } + AST_RWLIST_UNLOCK(&manager_hooks); + return 0; } diff --git a/utils/astman.c b/utils/astman.c index d3bbef818b..f58aa6b18d 100644 --- a/utils/astman.c +++ b/utils/astman.c @@ -40,8 +40,8 @@ #include #include "asterisk/md5.h" -#include "asterisk/manager.h" #include "asterisk/linkedlists.h" +#include "asterisk/manager.h" #undef gethostbyname