From 32273a0c3f66d98dbdef18e72b18107437c3c8fe Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Fri, 23 Dec 2005 02:24:56 +0000 Subject: [PATCH] xmppmas git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@196 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- conf/xmpp_event.conf | 9 +++++ src/include/switch_event.h | 2 + src/include/switch_types.h | 1 + src/mod/mod_xmpp_event/mod_xmpp_event.c | 49 +++++++++++++++++++++++++ src/switch_console.c | 4 +- src/switch_event.c | 45 +++++++++++++++++++++-- 6 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 conf/xmpp_event.conf diff --git a/conf/xmpp_event.conf b/conf/xmpp_event.conf new file mode 100644 index 0000000000..f53002f365 --- /dev/null +++ b/conf/xmpp_event.conf @@ -0,0 +1,9 @@ +[settings] + +debug => 0 + +#jid => somebody@jabber.org +#passwd => pass +#target_jid => somebodyelse@jabber.org + + diff --git a/src/include/switch_event.h b/src/include/switch_event.h index 4f2e28b5a5..21bb02605d 100644 --- a/src/include/switch_event.h +++ b/src/include/switch_event.h @@ -54,6 +54,7 @@ struct switch_event { char *owner; switch_event_subclass *subclass; struct switch_event_header *headers; + char *body; void *bind_user_data; void *event_user_data; struct switch_event *next; @@ -83,6 +84,7 @@ SWITCH_DECLARE(char *) switch_event_name(switch_event_t event); SWITCH_DECLARE(switch_status) switch_event_reserve_subclass_detailed(char *owner, char *subclass_name); SWITCH_DECLARE(switch_status) switch_event_serialize(switch_event *event, char *buf, size_t buflen, char *fmt, ...); SWITCH_DECLARE(switch_status) switch_event_running(void); +SWITCH_DECLARE(switch_status) switch_event_add_body(switch_event *event, char *fmt, ...); #define switch_event_reserve_subclass(subclass_name) switch_event_reserve_subclass_detailed(__FILE__, subclass_name) #define switch_event_create(event, id) switch_event_create_subclass(event, id, SWITCH_EVENT_SUBCLASS_ANY) diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 73dac14287..d155d3925d 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -125,6 +125,7 @@ typedef enum { SWITCH_EVENT_CUSTOM, SWITCH_EVENT_CHANNEL_STATE, SWITCH_EVENT_CHANNEL_ANSWER, + SWITCH_EVENT_API, SWITCH_EVENT_LOG, SWITCH_EVENT_INBOUND_CHAN, SWITCH_EVENT_OUTBOUND_CHAN, diff --git a/src/mod/mod_xmpp_event/mod_xmpp_event.c b/src/mod/mod_xmpp_event/mod_xmpp_event.c index e35c6aa949..9add6719b2 100644 --- a/src/mod/mod_xmpp_event/mod_xmpp_event.c +++ b/src/mod/mod_xmpp_event/mod_xmpp_event.c @@ -204,6 +204,50 @@ int on_stream (struct session *sess, int type, iks *node) return IKS_OK; } +int on_msg (void *user_data, ikspak *pak) +{ + switch_api_interface *api; + char *cmd = iks_find_cdata (pak->x, "body"); + char *arg = NULL; + switch_event *event; + char retbuf[512] = ""; + char *p; + + if ((p = strchr(cmd, '\r'))) { + *p++ = '\0'; + } else if ((p = strchr(cmd, '\n'))) { + *p++ = '\0'; + } + + if ((arg = strchr(cmd, ' '))) { + *arg++ = '\0'; + } + if (arg && (p = strchr(arg, '\r'))) { + *p++ = '\0'; + } else if ((p = strchr(cmd, '\n'))) { + *p++ = '\0'; + } + + if ((api = loadable_module_get_api_interface(cmd))) { + api->function(arg, retbuf, sizeof(retbuf)); + } else { + snprintf(retbuf, sizeof(retbuf), "INVALID COMMAND [%s]", cmd); + } + + if (switch_event_create(&event, SWITCH_EVENT_API) == SWITCH_STATUS_SUCCESS) { + if (cmd) { + switch_event_add_header(event, "re_command", cmd); + } + if (arg) { + switch_event_add_header(event, "re_command_arg", arg); + } + switch_event_add_body(event, retbuf); + switch_event_fire(&event); + } + + return 0; +} + int on_error (void *user_data, ikspak *pak) { switch_console_printf(SWITCH_CHANNEL_CONSOLE, "authorization failed\n"); @@ -221,6 +265,11 @@ void j_setup_filter (struct session *sess) { if (my_filter) iks_filter_delete (my_filter); my_filter = iks_filter_new (); + iks_filter_add_rule (my_filter, on_msg, 0, + IKS_RULE_TYPE, IKS_PAK_MESSAGE, + IKS_RULE_SUBTYPE, IKS_TYPE_CHAT, + IKS_RULE_FROM, globals.target_jid, + IKS_RULE_DONE); iks_filter_add_rule (my_filter, (iksFilterHook *) on_result, sess, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, diff --git a/src/switch_console.c b/src/switch_console.c index 47256e687e..f59c0a70ac 100644 --- a/src/switch_console.c +++ b/src/switch_console.c @@ -57,9 +57,7 @@ char *arg = NULL; return 1; } - if ((arg = strchr(cmd, ' '))) { - *arg++ = '\0'; - } + if ((api = loadable_module_get_api_interface(cmd))) { char retbuf[512] = ""; api->function(arg, retbuf, sizeof(retbuf)); diff --git a/src/switch_event.c b/src/switch_event.c index 36701efcef..076007967f 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -90,6 +90,7 @@ static char *EVENT_NAMES[] = { "CUSTOM", "CHANNEL_STATE", "CHANNEL_ANSWER", + "API", "LOG", "INBOUND_CHAN", "OUTBOUND_CHAN", @@ -367,6 +368,28 @@ SWITCH_DECLARE(switch_status) switch_event_add_header(switch_event *event, char } + +SWITCH_DECLARE(switch_status) switch_event_add_body(switch_event *event, char *fmt, ...) +{ + int ret = 0; + char data[2048]; + + va_list ap; + va_start(ap, fmt); + vsnprintf(data, sizeof(data), fmt, ap); + va_end(ap); + + if (ret == -1) { + return SWITCH_STATUS_MEMERR; + } else { + event->body = DUP(data); + return SWITCH_STATUS_SUCCESS; + } + + return (ret >= 0) ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_GENERR; + +} + SWITCH_DECLARE(void) switch_event_destroy(switch_event **event) { #ifdef MALLOC_EVENTS @@ -423,7 +446,7 @@ SWITCH_DECLARE(switch_status) switch_event_serialize(switch_event *event, char * { size_t len; switch_event_header *hp; - char *data = NULL; + char *data = NULL, *body = NULL; int ret = 0; va_list ap; @@ -450,13 +473,29 @@ SWITCH_DECLARE(switch_status) switch_event_serialize(switch_event *event, char * for (hp = event->headers; hp; hp = hp->next) { snprintf(buf+len, buflen-len, "%s: %s\n", hp->name, hp->value); len = strlen(buf); + } + if (data) { - snprintf(buf+len, buflen-len, "Content-Length: %d\n\n%s", (int)strlen(data), data); - free(data); + body = data; + } else if (event->body) { + body = event->body; + } + + if (body) { + int blen = (int)strlen(body); + if (blen) { + snprintf(buf+len, buflen-len, "Content-Length: %d\n\n%s", blen, body); + } else { + snprintf(buf+len, buflen-len, "\n"); + } } else { snprintf(buf+len, buflen-len, "\n"); } + + if (data) { + free(data); + } return SWITCH_STATUS_SUCCESS; }