Remove redundant NULL checks
[pidgin-git.git] / libpurple / protocols / jabber / pep.c
blob0063f9e611b279961cf8b6847e8a2dc017f68979
1 /*
2 * purple - Jabber Protocol Plugin
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #include "internal.h"
26 #include "pep.h"
27 #include "iq.h"
28 #include <string.h>
29 #include "useravatar.h"
30 #include "usermood.h"
31 #include "usernick.h"
32 #include "usertune.h"
34 static GHashTable *pep_handlers = NULL;
36 void jabber_pep_init(void) {
37 if(!pep_handlers) {
38 pep_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
40 /* register PEP handlers */
41 jabber_avatar_init();
42 jabber_mood_init();
43 jabber_tune_init();
44 jabber_nick_init();
48 void jabber_pep_uninit(void) {
49 /* any PEP handlers that need to clean things up go here. The standard
50 * cleanup of removing the handler and feature are handled here and by
51 * jabber_features_destroy() in jabber.c
53 g_hash_table_destroy(pep_handlers);
54 pep_handlers = NULL;
57 void jabber_pep_init_actions(GList **m) {
58 /* register the PEP-specific actions */
59 jabber_nick_init_action(m);
62 void jabber_pep_register_handler(const char *xmlns, JabberPEPHandler handlerfunc) {
63 gchar *notifyns = g_strdup_printf("%s+notify", xmlns);
64 jabber_add_feature(notifyns, NULL); /* receiving PEPs is always supported */
65 g_free(notifyns);
66 g_hash_table_replace(pep_handlers, g_strdup(xmlns), handlerfunc);
69 static void
70 do_pep_iq_request_item_callback(JabberStream *js, const char *from,
71 JabberIqType type, const char *id,
72 xmlnode *packet, gpointer data)
74 xmlnode *pubsub;
75 xmlnode *items = NULL;
76 JabberPEPHandler *cb = data;
78 if (type == JABBER_IQ_RESULT) {
79 pubsub = xmlnode_get_child_with_namespace(packet, "pubsub", "http://jabber.org/protocol/pubsub");
80 if(pubsub)
81 items = xmlnode_get_child(pubsub, "items");
84 cb(js, from, items);
87 void jabber_pep_request_item(JabberStream *js, const char *to, const char *node, const char *id, JabberPEPHandler cb) {
88 JabberIq *iq = jabber_iq_new(js, JABBER_IQ_GET);
89 xmlnode *pubsub, *items;
91 if (to)
92 xmlnode_set_attrib(iq->node, "to", to);
94 pubsub = xmlnode_new_child(iq->node,"pubsub");
95 xmlnode_set_namespace(pubsub, "http://jabber.org/protocol/pubsub");
97 items = xmlnode_new_child(pubsub, "items");
98 xmlnode_set_attrib(items,"node",node);
100 if (id) {
101 xmlnode *item = xmlnode_new_child(items, "item");
102 xmlnode_set_attrib(item, "id", id);
103 } else
104 /* Most recent item */
105 xmlnode_set_attrib(items, "max_items", "1");
107 jabber_iq_set_callback(iq,do_pep_iq_request_item_callback,(gpointer)cb);
109 jabber_iq_send(iq);
112 gboolean jabber_pep_namespace_only_when_pep_enabled_cb(JabberStream *js, const gchar *namespace) {
113 return js->pep;
116 void jabber_handle_event(JabberMessage *jm) {
117 /* this may be called even when the own server doesn't support pep! */
118 JabberPEPHandler *jph;
119 GList *itemslist;
120 char *jid;
122 if (jm->type != JABBER_MESSAGE_EVENT)
123 return;
125 jid = jabber_get_bare_jid(jm->from);
127 for(itemslist = jm->eventitems; itemslist; itemslist = itemslist->next) {
128 xmlnode *items = (xmlnode*)itemslist->data;
129 const char *nodename = xmlnode_get_attrib(items,"node");
131 if(nodename && (jph = g_hash_table_lookup(pep_handlers, nodename)))
132 jph(jm->js, jid, items);
135 /* discard items we don't have a handler for */
136 g_free(jid);
139 void jabber_pep_delete_node(JabberStream *js, const gchar *node)
141 JabberIq *iq;
142 xmlnode *pubsub, *del;
144 g_return_if_fail(node != NULL);
145 g_return_if_fail(js->pep);
147 iq = jabber_iq_new(js, JABBER_IQ_SET);
149 pubsub = xmlnode_new_child(iq->node, "pubsub");
150 xmlnode_set_namespace(pubsub, "http://jabber.org/protocol/pubsub#owner");
152 del = xmlnode_new_child(pubsub, "delete");
153 xmlnode_set_attrib(del, "node", node);
155 jabber_iq_send(iq);
158 void jabber_pep_publish(JabberStream *js, xmlnode *publish) {
159 JabberIq *iq;
160 xmlnode *pubsub;
162 if(js->pep != TRUE) {
163 /* ignore when there's no PEP support on the server */
164 xmlnode_free(publish);
165 return;
168 iq = jabber_iq_new(js, JABBER_IQ_SET);
170 pubsub = xmlnode_new("pubsub");
171 xmlnode_set_namespace(pubsub, "http://jabber.org/protocol/pubsub");
173 xmlnode_insert_child(pubsub, publish);
175 xmlnode_insert_child(iq->node, pubsub);
177 jabber_iq_send(iq);