Coverage Report

Created: 2025-12-31 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pidgin/libpurple/protocols/jabber/usertune.c
Line
Count
Source
1
/*
2
 * purple - Jabber Protocol Plugin
3
 *
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.
7
 *
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.
12
 *
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.
17
 *
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
21
 *
22
 */
23
24
#include "internal.h"
25
26
#include "usertune.h"
27
#include "pep.h"
28
#include <string.h>
29
#include "internal.h"
30
#include "request.h"
31
#include "status.h"
32
33
0
static void jabber_tune_cb(JabberStream *js, const char *from, xmlnode *items) {
34
  /* it doesn't make sense to have more than one item here, so let's just pick the first one */
35
0
  xmlnode *item = xmlnode_get_child(items, "item");
36
0
  JabberBuddy *buddy = jabber_buddy_find(js, from, FALSE);
37
0
  xmlnode *tuneinfo, *tune;
38
0
  PurpleJabberTuneInfo tuneinfodata;
39
0
  JabberBuddyResource *resource;
40
0
  gboolean valid = FALSE;
41
42
  /* ignore the tune of people not on our buddy list */
43
0
  if (!buddy || !item)
44
0
    return;
45
46
0
  tuneinfodata.artist = NULL;
47
0
  tuneinfodata.title = NULL;
48
0
  tuneinfodata.album = NULL;
49
0
  tuneinfodata.track = NULL;
50
0
  tuneinfodata.time = -1;
51
0
  tuneinfodata.url = NULL;
52
53
0
  tune = xmlnode_get_child_with_namespace(item, "tune", "http://jabber.org/protocol/tune");
54
0
  if (!tune)
55
0
    return;
56
0
  resource = jabber_buddy_find_resource(buddy, NULL);
57
0
  if(!resource)
58
0
    return; /* huh? */
59
0
  for (tuneinfo = tune->child; tuneinfo; tuneinfo = tuneinfo->next) {
60
0
    if (tuneinfo->type == XMLNODE_TYPE_TAG) {
61
0
      if (purple_strequal(tuneinfo->name, "artist")) {
62
0
        if (tuneinfodata.artist == NULL) /* only pick the first one */
63
0
          tuneinfodata.artist = xmlnode_get_data(tuneinfo);
64
0
        valid = TRUE;
65
0
      } else if (purple_strequal(tuneinfo->name, "length")) {
66
0
        if (tuneinfodata.time == -1) {
67
0
          char *length = xmlnode_get_data(tuneinfo);
68
0
          if (length)
69
0
            tuneinfodata.time = strtol(length, NULL, 10);
70
0
          g_free(length);
71
0
          if (tuneinfodata.time > 0)
72
0
            valid = TRUE;
73
0
        }
74
0
      } else if (purple_strequal(tuneinfo->name, "source")) {
75
0
        if (tuneinfodata.album == NULL) /* only pick the first one */
76
0
          tuneinfodata.album = xmlnode_get_data(tuneinfo);
77
0
        valid = TRUE;
78
0
      } else if (purple_strequal(tuneinfo->name, "title")) {
79
0
        if (tuneinfodata.title == NULL) /* only pick the first one */
80
0
          tuneinfodata.title = xmlnode_get_data(tuneinfo);
81
0
        valid = TRUE;
82
0
      } else if (purple_strequal(tuneinfo->name, "track")) {
83
0
        if (tuneinfodata.track == NULL) /* only pick the first one */
84
0
          tuneinfodata.track = xmlnode_get_data(tuneinfo);
85
0
        valid = TRUE;
86
0
      } else if (purple_strequal(tuneinfo->name, "uri")) {
87
0
        if (tuneinfodata.url == NULL) /* only pick the first one */
88
0
          tuneinfodata.url = xmlnode_get_data(tuneinfo);
89
0
        valid = TRUE;
90
0
      }
91
0
    }
92
0
  }
93
94
0
  if (valid) {
95
0
    purple_prpl_got_user_status(js->gc->account, from, "tune",
96
0
        PURPLE_TUNE_ARTIST, tuneinfodata.artist,
97
0
        PURPLE_TUNE_TITLE, tuneinfodata.title,
98
0
        PURPLE_TUNE_ALBUM, tuneinfodata.album,
99
0
        PURPLE_TUNE_TRACK, tuneinfodata.track,
100
0
        PURPLE_TUNE_TIME, tuneinfodata.time,
101
0
        PURPLE_TUNE_URL, tuneinfodata.url, NULL);
102
0
  } else {
103
0
    purple_prpl_got_user_status_deactive(js->gc->account, from, "tune");
104
0
  }
105
106
0
  g_free(tuneinfodata.artist);
107
0
  g_free(tuneinfodata.title);
108
0
  g_free(tuneinfodata.album);
109
0
  g_free(tuneinfodata.track);
110
0
  g_free(tuneinfodata.url);
111
0
}
112
113
0
void jabber_tune_init(void) {
114
0
  jabber_add_feature("http://jabber.org/protocol/tune", jabber_pep_namespace_only_when_pep_enabled_cb);
115
0
  jabber_pep_register_handler("http://jabber.org/protocol/tune", jabber_tune_cb);
116
0
}
117
118
0
void jabber_tune_set(PurpleConnection *gc, const PurpleJabberTuneInfo *tuneinfo) {
119
0
  xmlnode *publish, *tunenode;
120
0
  JabberStream *js = gc->proto_data;
121
122
0
  publish = xmlnode_new("publish");
123
0
  xmlnode_set_attrib(publish,"node","http://jabber.org/protocol/tune");
124
0
  tunenode = xmlnode_new_child(xmlnode_new_child(publish, "item"), "tune");
125
0
  xmlnode_set_namespace(tunenode, "http://jabber.org/protocol/tune");
126
127
0
  if(tuneinfo) {
128
0
    if(tuneinfo->artist && tuneinfo->artist[0] != '\0')
129
0
      xmlnode_insert_data(xmlnode_new_child(tunenode, "artist"),tuneinfo->artist,-1);
130
0
    if(tuneinfo->title && tuneinfo->title[0] != '\0')
131
0
      xmlnode_insert_data(xmlnode_new_child(tunenode, "title"),tuneinfo->title,-1);
132
0
    if(tuneinfo->album && tuneinfo->album[0] != '\0')
133
0
      xmlnode_insert_data(xmlnode_new_child(tunenode, "source"),tuneinfo->album,-1);
134
0
    if(tuneinfo->url && tuneinfo->url[0] != '\0')
135
0
      xmlnode_insert_data(xmlnode_new_child(tunenode, "uri"),tuneinfo->url,-1);
136
0
    if(tuneinfo->time > 0) {
137
0
      char *length = g_strdup_printf("%d", tuneinfo->time);
138
0
      xmlnode_insert_data(xmlnode_new_child(tunenode, "length"),length,-1);
139
0
      g_free(length);
140
0
    }
141
0
    if(tuneinfo->track && tuneinfo->track[0] != '\0')
142
0
      xmlnode_insert_data(xmlnode_new_child(tunenode, "track"),tuneinfo->track,-1);
143
0
  }
144
145
0
  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) */
148
0
}