Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / libpurple / protocols / jabber / usertune.c
blob82c8f11ca5126f5e7b0a8791121594d3964a6c8f
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 "usertune.h"
27 #include "pep.h"
28 #include <string.h>
29 #include "internal.h"
30 #include "request.h"
31 #include "status.h"
33 static void jabber_tune_cb(JabberStream *js, const char *from, PurpleXmlNode *items) {
34 /* it doesn't make sense to have more than one item here, so let's just pick the first one */
35 PurpleXmlNode *item = purple_xmlnode_get_child(items, "item");
36 JabberBuddy *buddy = jabber_buddy_find(js, from, FALSE);
37 PurpleXmlNode *tuneinfo, *tune;
38 PurpleJabberTuneInfo tuneinfodata;
39 JabberBuddyResource *resource;
40 gboolean valid = FALSE;
42 /* ignore the tune of people not on our buddy list */
43 if (!buddy || !item)
44 return;
46 tuneinfodata.artist = NULL;
47 tuneinfodata.title = NULL;
48 tuneinfodata.album = NULL;
49 tuneinfodata.track = NULL;
50 tuneinfodata.time = -1;
51 tuneinfodata.url = NULL;
53 tune = purple_xmlnode_get_child_with_namespace(item, "tune", "http://jabber.org/protocol/tune");
54 if (!tune)
55 return;
56 resource = jabber_buddy_find_resource(buddy, NULL);
57 if(!resource)
58 return; /* huh? */
59 for (tuneinfo = tune->child; tuneinfo; tuneinfo = tuneinfo->next) {
60 if (tuneinfo->type == PURPLE_XMLNODE_TYPE_TAG) {
61 if (!strcmp(tuneinfo->name, "artist")) {
62 if (tuneinfodata.artist == NULL) /* only pick the first one */
63 tuneinfodata.artist = purple_xmlnode_get_data(tuneinfo);
64 valid = TRUE;
65 } else if (!strcmp(tuneinfo->name, "length")) {
66 if (tuneinfodata.time == -1) {
67 char *length = purple_xmlnode_get_data(tuneinfo);
68 if (length)
69 tuneinfodata.time = strtol(length, NULL, 10);
70 g_free(length);
71 if (tuneinfodata.time > 0)
72 valid = TRUE;
74 } else if (!strcmp(tuneinfo->name, "source")) {
75 if (tuneinfodata.album == NULL) /* only pick the first one */
76 tuneinfodata.album = purple_xmlnode_get_data(tuneinfo);
77 valid = TRUE;
78 } else if (!strcmp(tuneinfo->name, "title")) {
79 if (tuneinfodata.title == NULL) /* only pick the first one */
80 tuneinfodata.title = purple_xmlnode_get_data(tuneinfo);
81 valid = TRUE;
82 } else if (!strcmp(tuneinfo->name, "track")) {
83 if (tuneinfodata.track == NULL) /* only pick the first one */
84 tuneinfodata.track = purple_xmlnode_get_data(tuneinfo);
85 valid = TRUE;
86 } else if (!strcmp(tuneinfo->name, "uri")) {
87 if (tuneinfodata.url == NULL) /* only pick the first one */
88 tuneinfodata.url = purple_xmlnode_get_data(tuneinfo);
89 valid = TRUE;
94 if (valid) {
95 purple_protocol_got_user_status(purple_connection_get_account(js->gc), from, "tune",
96 PURPLE_TUNE_ARTIST, tuneinfodata.artist,
97 PURPLE_TUNE_TITLE, tuneinfodata.title,
98 PURPLE_TUNE_ALBUM, tuneinfodata.album,
99 PURPLE_TUNE_TRACK, tuneinfodata.track,
100 PURPLE_TUNE_TIME, tuneinfodata.time,
101 PURPLE_TUNE_URL, tuneinfodata.url, NULL);
102 } else {
103 purple_protocol_got_user_status_deactive(purple_connection_get_account(js->gc), from, "tune");
106 g_free(tuneinfodata.artist);
107 g_free(tuneinfodata.title);
108 g_free(tuneinfodata.album);
109 g_free(tuneinfodata.track);
110 g_free(tuneinfodata.url);
113 void jabber_tune_init(void) {
114 jabber_add_feature("http://jabber.org/protocol/tune", jabber_pep_namespace_only_when_pep_enabled_cb);
115 jabber_pep_register_handler("http://jabber.org/protocol/tune", jabber_tune_cb);
118 void jabber_tune_set(PurpleConnection *gc, const PurpleJabberTuneInfo *tuneinfo) {
119 PurpleXmlNode *publish, *tunenode;
120 JabberStream *js = purple_connection_get_protocol_data(gc);
122 publish = purple_xmlnode_new("publish");
123 purple_xmlnode_set_attrib(publish,"node","http://jabber.org/protocol/tune");
124 tunenode = purple_xmlnode_new_child(purple_xmlnode_new_child(publish, "item"), "tune");
125 purple_xmlnode_set_namespace(tunenode, "http://jabber.org/protocol/tune");
127 if(tuneinfo) {
128 if(tuneinfo->artist && tuneinfo->artist[0] != '\0')
129 purple_xmlnode_insert_data(purple_xmlnode_new_child(tunenode, "artist"),tuneinfo->artist,-1);
130 if(tuneinfo->title && tuneinfo->title[0] != '\0')
131 purple_xmlnode_insert_data(purple_xmlnode_new_child(tunenode, "title"),tuneinfo->title,-1);
132 if(tuneinfo->album && tuneinfo->album[0] != '\0')
133 purple_xmlnode_insert_data(purple_xmlnode_new_child(tunenode, "source"),tuneinfo->album,-1);
134 if(tuneinfo->url && tuneinfo->url[0] != '\0')
135 purple_xmlnode_insert_data(purple_xmlnode_new_child(tunenode, "uri"),tuneinfo->url,-1);
136 if(tuneinfo->time > 0) {
137 char *length = g_strdup_printf("%d", tuneinfo->time);
138 purple_xmlnode_insert_data(purple_xmlnode_new_child(tunenode, "length"),length,-1);
139 g_free(length);
141 if(tuneinfo->track && tuneinfo->track[0] != '\0')
142 purple_xmlnode_insert_data(purple_xmlnode_new_child(tunenode, "track"),tuneinfo->track,-1);
145 jabber_pep_publish(js, publish);
146 /* publish is freed by jabber_pep_publish -> jabber_iq_send -> jabber_iq_free
147 (yay for well-defined memory management rules) */