[svn-commits] marquis: branch marquis/pubsub-distributed-events r215106 - /team/marquis/pub...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Mon Aug 31 17:37:06 CDT 2009


Author: marquis
Date: Mon Aug 31 17:37:02 2009
New Revision: 215106

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=215106
Log:
Mostly this commit adds the publishing of MWI events, though there is some miscellaneous cleanup.  We still don't react to MWI PubSub events yet.

Modified:
    team/marquis/pubsub-distributed-events/res/res_jabber.c

Modified: team/marquis/pubsub-distributed-events/res/res_jabber.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/marquis/pubsub-distributed-events/res/res_jabber.c?view=diff&rev=215106&r1=215105&r2=215106
==============================================================================
--- team/marquis/pubsub-distributed-events/res/res_jabber.c (original)
+++ team/marquis/pubsub-distributed-events/res/res_jabber.c Mon Aug 31 17:37:02 2009
@@ -276,6 +276,12 @@
 static int aji_delete_node_list(void *data, ikspak* pak);
 static void aji_pubsub_purge_nodes(struct aji_client *client,
 	const char* collection_name);
+static void aji_publish_mwi(struct aji_client *client, const char *mailbox,
+	const char *context, const char *oldmsgs, const char *newmsgs);
+static void aji_devstate_cb(const struct ast_event *ast_event, void *data);
+static void aji_mwi_cb(const struct ast_event *ast_event, void *data);
+static iks* aji_build_publish_skeleton(struct aji_client *client, const char *node,
+	const char *event_type);
 /* No transports in this version */
 /*
 static int aji_create_transport(char *label, struct aji_client *client);
@@ -2493,14 +2499,43 @@
 	return 1;
 }
 
-
-/*!
- * \brief Callback function for events
+/*!
+ * \brief Callback function for MWI events
  * \param ast_event
  * \param data void pointer to ast_client structure
  * \return void
  */
-static void aji_event_cb(const struct ast_event *ast_event, void *data)
+static void aji_mwi_cb(const struct ast_event *ast_event, void *data)
+{
+	const char *mailbox;
+	const char *context;
+	char oldmsgs[10];
+	char newmsgs[10];
+	if (ast_eid_cmp(&ast_eid_default, ast_event_get_ie_raw(ast_event, AST_EVENT_IE_EID)))
+	{
+		/* If the event didn't originate from this server, don't send it back out. */
+		ast_log(LOG_DEBUG, "Returning here\n");
+		return;
+	}
+	
+	struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
+	
+	mailbox = ast_event_get_ie_str(ast_event, AST_EVENT_IE_MAILBOX);
+	context = ast_event_get_ie_str(ast_event, AST_EVENT_IE_CONTEXT);
+	snprintf(oldmsgs, sizeof(oldmsgs), "%d",
+		ast_event_get_ie_uint(ast_event, AST_EVENT_IE_OLDMSGS));
+	snprintf(newmsgs, sizeof(newmsgs), "%d",
+		ast_event_get_ie_uint(ast_event, AST_EVENT_IE_NEWMSGS));
+	aji_publish_mwi(client, mailbox, context, oldmsgs, newmsgs);
+
+}
+/*!
+ * \brief Callback function for device state events
+ * \param ast_event
+ * \param data void pointer to ast_client structure
+ * \return void
+ */
+static void aji_devstate_cb(const struct ast_event *ast_event, void *data)
 {
 	const char *device;
 	const char *device_state;
@@ -2512,15 +2547,9 @@
 	}
 	
 	struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
-	
-	if (ast_event_get_type(ast_event) == AST_EVENT_MWI) {
-		ast_log(LOG_ERROR, "Got an MWI event, mailbox: %s\n",
-				ast_event_get_ie_str(ast_event, AST_EVENT_IE_MAILBOX));
-	} else if (ast_event_get_type(ast_event) == AST_EVENT_DEVICE_STATE_CHANGE) {
-		device = ast_event_get_ie_str(ast_event, AST_EVENT_IE_DEVICE);
-		device_state = ast_devstate_str(ast_event_get_ie_uint(ast_event, AST_EVENT_IE_STATE));
-		aji_publish_device_state(client, device, device_state);
-	}
+	device = ast_event_get_ie_str(ast_event, AST_EVENT_IE_DEVICE);
+	device_state = ast_devstate_str(ast_event_get_ie_uint(ast_event, AST_EVENT_IE_STATE));
+	aji_publish_device_state(client, device, device_state);
 }
 
 /*!
@@ -2531,7 +2560,7 @@
 static void aji_init_event_distribution(struct aji_client *client)
 {
 	if (!mwi_sub) {
-		mwi_sub = ast_event_subscribe(AST_EVENT_MWI, aji_event_cb, "aji_mwi_subscription",
+		mwi_sub = ast_event_subscribe(AST_EVENT_MWI, aji_mwi_cb, "aji_mwi_subscription",
 			client, AST_EVENT_IE_END);
 	}
 	if (!device_state_sub) {
@@ -2539,7 +2568,7 @@
 			return;
 		}
 		device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
-			aji_event_cb, "aji_devstate_subscription", client, AST_EVENT_IE_END);
+			aji_devstate_cb, "aji_devstate_subscription", client, AST_EVENT_IE_END);
 		ast_event_dump_cache(device_state_sub);
 	}
 
@@ -2560,16 +2589,15 @@
 static int aji_handle_pubsub_event(void *data, ikspak *pak)
 {
 	char *device, *device_state;
-	iks *item, *entry, *state_node;
+	iks *item, *state_node;
 	struct ast_eid pubsub_eid;
 	struct ast_event *event;
 	item = iks_find(iks_find(iks_find(pak->x, "event"), "items"), "item");
 	if (item) {
 		device = iks_find_attrib(item, "id");
-		entry = iks_find(item, "entry");
-		state_node = iks_find(entry, "state");
+		state_node = iks_find(item, "state");
 		if (state_node) {
-			device_state = iks_find_cdata(entry, "state");
+			device_state = iks_find_cdata(item, "state");
 			ast_str_to_eid(&pubsub_eid, iks_find_attrib(state_node,"eid"));
 			if (!ast_eid_cmp(&ast_eid_default, &pubsub_eid)) {
 				ast_log(LOG_DEBUG, "Returning here, eid of incoming event matches ours!\n");
@@ -2654,7 +2682,33 @@
 }
 
 /*!
- * \brief Publish an item to a PubSub node
+ * \brief Build the skeleton of a publish
+ * \param client the configured XMPP client we use to connect to a XMPP server
+ * \param node Name of the node that will be published to
+ * \param event_type 
+ * \return iks *
+ */
+static iks* aji_build_publish_skeleton(struct aji_client *client, const char *node,
+	const char *event_type)
+{
+	iks *request = aji_pubsub_iq_create(client, "set");
+	iks *pubsub, *publish, *item;
+	pubsub = iks_insert(request, "pubsub");
+	iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
+	publish = iks_insert(pubsub, "publish");
+	if (ast_test_flag(&globalflags, AJI_XEP0248)) {
+		iks_insert_attrib(publish, "node", node);
+	} else {
+		iks_insert_attrib(publish, "node", event_type);
+	}
+	item = iks_insert(publish, "item");
+	iks_insert_attrib(item, "id", node);
+	return item;
+
+}
+
+/*!
+ * \brief Publish device state to a PubSub node
  * \param client the configured XMPP client we use to connect to a XMPP server
  * \param device the name of the device whose state to publish
  * \param device_state the state to publish
@@ -2663,27 +2717,40 @@
 static void aji_publish_device_state(struct aji_client *client, const char *device,
 	const char *device_state)
 {
-	iks *request = aji_pubsub_iq_create(client, "set");
-	iks *pubsub, *publish, *item, *entry, *state;
+	iks *request = aji_build_publish_skeleton(client, device, "device_state");
+	iks *state;
 	char eid_str[20];
 	ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
-
-	pubsub = iks_insert(request, "pubsub");
-	iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
-	publish = iks_insert(pubsub, "publish");
-	if (ast_test_flag(&globalflags, AJI_XEP0248)) {
-		iks_insert_attrib(publish, "node", device);
-	} else {
-		iks_insert_attrib(publish, "node", "device_state");
-	}
-	item = iks_insert(publish, "item");
-	iks_insert_attrib(item, "id", device);
-	entry = iks_insert(item, "entry");
-	iks_insert_attrib(entry, "xmlns", "http://placeholder.org");
-	state = iks_insert(entry, "state");
+	state = iks_insert(request, "state");
+	iks_insert_attrib(state, "xmlns", "http://placeholder.org");
 	iks_insert_attrib(state, "eid", eid_str);
 	iks_insert_cdata(state, device_state, strlen(device_state));
-	ast_aji_send(client, request);
+	ast_aji_send(client, iks_root(request));
+	iks_delete(request);
+}
+
+/*!
+ * \brief Publish MWI to a PubSub node
+ * \param client the configured XMPP client we use to connect to a XMPP server
+ * \param device the name of the device whose state to publish
+ * \param device_state the state to publish
+ * \return void
+ */
+static void aji_publish_mwi(struct aji_client *client, const char *mailbox,
+	const char *context, const char *oldmsgs, const char *newmsgs)
+{
+	char full_mailbox[AST_MAX_EXTENSION+AST_MAX_CONTEXT];
+	char eid_str[20];
+	iks *mailbox_node;
+	snprintf(full_mailbox, sizeof(full_mailbox), "%s@%s", mailbox, context);
+	iks *request = aji_build_publish_skeleton(client, full_mailbox, "message_waiting");
+	ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
+	mailbox_node = iks_insert(request, "mailbox");
+	iks_insert_attrib(mailbox_node, "xmlns", "http://placeholder.org");
+	iks_insert_attrib(mailbox_node, "eid", eid_str);
+	iks_insert_cdata(iks_insert(mailbox_node, "NEWMSGS"), newmsgs, strlen(newmsgs));
+	iks_insert_cdata(iks_insert(mailbox_node, "OLDMSGS"), oldmsgs, strlen(oldmsgs));
+	ast_aji_send(client, iks_root(request));
 	iks_delete(request);
 }
 
@@ -2713,46 +2780,48 @@
 	iks *orig_request;
 	iks *orig_pubsub = iks_find(pak->x, "pubsub");
 	struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
-	if (orig_pubsub) {
-		orig_request = iks_child(orig_pubsub);
-		error = iks_find_attrib(iks_find(pak->x, "error"), "code");
-		node_name = iks_find_attrib(orig_request, "node");
-		if (!sscanf(error, "%30d", &error_num)) {
-			return IKS_FILTER_EAT;
-		}
-		if (error_num > 399 && error_num < 500 && error_num != 404) {
-			ast_log(LOG_ERROR,
-				"Error performing operation on PubSub node %s, %s.\n", node_name, error);
-			return IKS_FILTER_EAT;
-		} else if (error_num > 499 && error_num < 600) {
-			ast_log(LOG_ERROR, "PubSub Server error, %s\n", error);
-			return IKS_FILTER_EAT;
-		}
-		
-		if (!strcasecmp(iks_name(orig_request), "publish")) {
-			if (iks_find(iks_find(iks_find(orig_request, "item"), "entry"), "state")) {
-				if (ast_test_flag(&globalflags, AJI_XEP0248)) {
-					aji_create_pubsub_leaf(client, "device_state", node_name);
-				} else {
-					aji_create_pubsub_node(client, NULL, node_name, NULL);
-				}
-				iks *pubsub;
-				iks *request = aji_pubsub_iq_create(client, "set");
-				pubsub = iks_insert(request, "pubsub");
-				iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
-				iks_insert_node(pubsub, orig_request);
-				ast_aji_send(client, request);
-				iks_delete(request);
-				return IKS_FILTER_EAT;
+	if (!orig_pubsub) {
+		ast_log(LOG_ERROR, "Error isn't a PubSub error, why are we here?\n");
+		return IKS_FILTER_EAT;
+	}
+	orig_request = iks_child(orig_pubsub);
+	error = iks_find_attrib(iks_find(pak->x, "error"), "code");
+	node_name = iks_find_attrib(orig_request, "node");
+	if (!sscanf(error, "%30d", &error_num)) {
+		return IKS_FILTER_EAT;
+	}
+	if (error_num > 399 && error_num < 500 && error_num != 404) {
+		ast_log(LOG_ERROR,
+			"Error performing operation on PubSub node %s, %s.\n", node_name, error);
+		return IKS_FILTER_EAT;
+	} else if (error_num > 499 && error_num < 600) {
+		ast_log(LOG_ERROR, "PubSub Server error, %s\n", error);
+		return IKS_FILTER_EAT;
+	}
+
+	if (!strcasecmp(iks_name(orig_request), "publish")) {
+		if (ast_test_flag(&globalflags, AJI_XEP0248)) {
+			if (iks_find(iks_find(orig_request, "item"), "state")) {
+				aji_create_pubsub_leaf(client, "device_state", node_name);
+			} else if (iks_find(iks_find(orig_request, "item"), "mailbox")) {
+				aji_create_pubsub_leaf(client, "message_waiting", node_name);
 			}
-		} else if (!strcasecmp(iks_name(orig_request), "subscribe")) {
-			if (ast_test_flag(&globalflags, AJI_XEP0248)) {
-				aji_create_pubsub_collection(client, node_name);
-			} else {
-				aji_create_pubsub_node(client, NULL, node_name, NULL);
-			}
-		}
-	}
+		} else {
+			aji_create_pubsub_node(client, NULL, node_name, NULL);
+		}
+		iks *request = aji_pubsub_iq_create(client, "set");
+		iks_insert_node(request, orig_pubsub);
+		ast_aji_send(client, request);
+		iks_delete(request);
+		return IKS_FILTER_EAT;
+	} else if (!strcasecmp(iks_name(orig_request), "subscribe")) {
+		if (ast_test_flag(&globalflags, AJI_XEP0248)) {
+			aji_create_pubsub_collection(client, node_name);
+		} else {
+			aji_create_pubsub_node(client, NULL, node_name, NULL);
+		}
+	}
+
 	return IKS_FILTER_EAT;
 }
 
@@ -2811,12 +2880,10 @@
 	iks *item;
 	if (iks_has_children(pak->query)) {
 		item = iks_first_tag(pak->query);
-		ast_log(LOG_WARNING, "Connection: %s  Node name: %s\n", client->jid->partial,
+		ast_verbose("Connection: %s\nNode name: %s\n", client->jid->partial,
 			iks_find_attrib(item, "node"));
 		while((item = iks_next_tag(item))) {
-			ast_log(LOG_WARNING,
-				"Connection: %s  Node name: %s\n", client->jid->partial,
-				iks_find_attrib(item, "node"));
+			ast_verbose("Node name: %s\n", iks_find_attrib(item, "node"));
 		} 
 	}
 	iks_delete(item);




More information about the svn-commits mailing list