summaryrefslogtreecommitdiff
path: root/audio/xmms2-scrobbler/files/extra-patch-xmms2-scrobbler.c
blob: 84380c0cf4d3267795ef3eaee6d0c9b6d99c32a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
--- src/xmms2-scrobbler.c.orig	2009-12-30 17:04:01 UTC
+++ src/xmms2-scrobbler.c
@@ -140,93 +140,126 @@ signal_handler (int sig)
 		keep_running = false;
 }
 
-static size_t
-handle_handshake_reponse (void *rawptr, size_t size, size_t nmemb,
-                          void *data)
-{
-	Server *server = data;
-	size_t total = size * nmemb, left = total;
-	char *ptr = rawptr, *newline;
-	int len;
+char * get_line_from_response (char *answer, int lfpos) {
+    int lfpositions[10];
+    int i, k, lfcounter, start, end, resultlen;
 
-	newline = memchr (ptr, '\n', left);
-	if (!newline) {
-		fprintf (stderr, "no newline (1)\n");
-		return total;
-	}
+    char *result;
 
-	*newline = 0;
+#ifdef DEBUG
+    fprintf (stderr, "DEBUG: got string: %s\n", answer);
+#endif
 
-	if (strcmp (ptr, "OK")) {
-		fprintf (stderr, "handshake failed\n");
-		return total;
+    /* marking the string */
+    i = 0;
+    lfcounter = 0;
+    while (answer[i] != '\0') {
+	if (answer[i] == '\n' && lfcounter < 10) {
+	    lfpositions[lfcounter] = i;
+	    lfcounter++;
+#ifdef DEBUG
+	    fprintf (stderr, "DEBUG: got LF location: %d\n", i);
+#endif
 	}
+	i++;
+    }
+    /* getting the actual line */
+    /* getting the size of it */
 
-	len = newline - ptr + 1;
+    start = lfpos - 1;
+    end = lfpos;
+#ifdef DEBUG
+    if (start >= 0) {
+	fprintf (stderr, "DEBUG: requested line starts at char no. %d and ends at %d.\n", lfpositions[start], lfpositions[end]);
+    } else {
+	fprintf (stderr, "DEBUG: requested line starts at the start of string and ends at char no. %d.\n", lfpositions[end]);
+    }
+#endif
 
-	left -= len;
-	ptr += len;
+    if (start >= 0) {
+	resultlen = lfpositions[end] - lfpositions[start] + 1;
+    } else {
+	resultlen = lfpositions[end] + 1;
+    }
+#ifdef DEBUG
+    fprintf (stderr, "DEBUG: requested line will need %d bytes.\n", resultlen);
+#endif
+    result = malloc(resultlen);
+    bzero(result, resultlen);
 
-	newline = memchr (ptr, '\n', left);
-	if (!newline) {
-		fprintf (stderr, "no newline (1)\n");
-		return total;
+    /* bytecopying the line */
+#ifdef DEBUG
+    if (start >= 0) {
+	fprintf (stderr, "DEBUG: copying the string from char no. %d to %d.\n", lfpositions[start], lfpositions[end]);
+    } else {
+	fprintf (stderr, "DEBUG: copying the string from the beginning to char no. %d.\n", lfpositions[end]);
+    }
+#endif
+    k = 0;
+    if (start >= 0) {
+	i = lfpositions[start];
+    } else {
+	i = 0;
+    }
+    while (i < lfpositions[end]) {
+	/* don't copy LF themselves */
+	if (answer[i] != '\n') {
+	    result[k] = answer[i];
+	    k++;
 	}
+	i++;
+    }
 
-	*newline = 0;
-
-	len = newline - ptr + 1;
+#ifdef DEBUG
+    fprintf (stderr, "DEBUG: returning string: %s\n", result);
+#endif
+    return(result);
+}
 
-	if (len > 255) {
-		fprintf (stderr, "session ID is too long (%i characters)\n", len);
-		return total;
-	}
+static size_t
+handle_handshake_reponse (void *rawptr, size_t size, size_t nmemb,
+                          void *data)
+{
+	Server *server;
+	size_t total, left;
+	char *ptr, *status, *sessionid, *np_url, *subm_url;
 
-	strcpy (server->session_id, ptr);
+	server = data;
+	ptr = rawptr;
+	total = size * nmemb;
+	left = total;
 
-	left -= len;
-	ptr += len;
+#ifdef DEBUG
+	fprintf (stderr, "DEBUG: got string \"%s\".\n", rawptr);
+#endif
 
-	/* now playing URL */
-	newline = memchr (ptr, '\n', left);
-	if (!newline) {
-		fprintf (stderr, "no newline (2)\n");
-		return total;
+	status = get_line_from_response(rawptr, 0);
+	
+	if (strcmp(status, "OK") != 0) {
+	    fprintf (stderr, "WARNING: handshake failed: %s.\n", status);
+	    return(total);
 	}
 
-	*newline = 0;
-
-	len = newline - ptr + 1;
-
-	if (len > 255) {
-		fprintf (stderr, "now_playing URL is too long "
-		         "(%i characters)\n", len);
-		return total;
+	sessionid = get_line_from_response(rawptr, 3);
+	if (strlen(sessionid) > 255) {
+	    fprintf (stderr, "ERROR: session ID is too long (%ld characters).\n", strlen(sessionid));
+	    return(total);
 	}
+	strcpy (server->session_id, sessionid);
 
-	strcpy (server->np_url, ptr);
-
-	left -= len;
-	ptr += len;
-
-	/* submission URL */
-	newline = memchr (ptr, '\n', left);
-	if (!newline) {
-		printf("no newline (3)\n");
-		return total;
+	np_url = get_line_from_response(rawptr, 6);
+	if (strlen(np_url) > 255) {
+	    fprintf (stderr, "ERROR: nowplaying URL is too long (%ld characters).\n", strlen(np_url));
+	    return(total);
 	}
+	strcpy (server->np_url, np_url);
 
-	*newline = 0;
-
-	len = newline - ptr + 1;
-
-	if (len > 255) {
-		fprintf (stderr, "submission URL is too long "
-		         "(%i characters)\n", len);
-		return total;
+	subm_url = get_line_from_response(rawptr, 9);
+	if (strlen(sessionid) > 255) {
+	    fprintf (stderr, "ERROR: submission URL is too long (%ld characters).\n", strlen(subm_url));
+	    return(total);
 	}
-
-	strcpy (server->subm_url, ptr);
+	strcpy (server->subm_url, subm_url);
 
 	fprintf (stderr, "got:\n'%s' '%s' '%s'\n",
 	         server->session_id, server->np_url, server->subm_url);