|
1 |
|
2 /* Copyright 2009-2012 Myhailo Danylenko |
|
3 * |
|
4 * This file is part of mcabber-pep |
|
5 * |
|
6 * mcabber-pep is free software: you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License as published by |
|
8 * the Free Software Foundation, either version 2 of the License, or |
|
9 * (at your option) any later version. |
|
10 * |
|
11 * This program is distributed in the hope that it will be useful, |
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 * GNU General Public License for more details. |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License |
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
18 |
|
19 #include <glib.h> |
|
20 #include <string.h> |
|
21 |
|
22 #include <mcabber/commands.h> |
|
23 #include <mcabber/compl.h> |
|
24 #include <mcabber/utils.h> |
|
25 #include <mcabber/screen.h> |
|
26 #include <mcabber/logprint.h> |
|
27 #include <mcabber/roster.h> |
|
28 #include <mcabber/hbuf.h> // HBUF_PREFIX_* |
|
29 #include <mcabber/hooks.h> |
|
30 #include <mcabber/modules.h> |
|
31 |
|
32 #include "tune.h" |
|
33 |
|
34 #include "config.h" |
|
35 |
|
36 // |
|
37 // module description |
|
38 // |
|
39 |
|
40 void tune_init (void); |
|
41 void tune_uninit (void); |
|
42 |
|
43 #define DESCRIPTION ( \ |
|
44 "User interface for PEP tune events\n" \ |
|
45 "Prints incoming notifications into buddies buffers\n" \ |
|
46 "Provides command /tune" ) |
|
47 |
|
48 static const gchar *deps[] = { "pep_tune", NULL }; |
|
49 |
|
50 static module_info_t info_tune_dev = { |
|
51 .branch = "dev", |
|
52 .api = 20, |
|
53 .version = PROJECT_VERSION, |
|
54 .description = DESCRIPTION, |
|
55 .requires = deps, |
|
56 .init = tune_init, |
|
57 .uninit = tune_uninit, |
|
58 .next = NULL, |
|
59 }; |
|
60 |
|
61 static module_info_t info_tune_0_10_0 = { |
|
62 .branch = "0.10.0", |
|
63 .api = 1, |
|
64 .version = PROJECT_VERSION, |
|
65 .description = DESCRIPTION, |
|
66 .requires = deps, |
|
67 .init = tune_init, |
|
68 .uninit = tune_uninit, |
|
69 .next = &info_tune_dev, |
|
70 }; |
|
71 |
|
72 module_info_t info_tune = { |
|
73 .branch = "0.10.1", |
|
74 .api = 1, |
|
75 .version = PROJECT_VERSION, |
|
76 .description = DESCRIPTION, |
|
77 .requires = deps, |
|
78 .init = tune_init, |
|
79 .uninit = tune_uninit, |
|
80 .next = &info_tune_0_10_0, |
|
81 }; |
|
82 |
|
83 // |
|
84 // globals |
|
85 // |
|
86 |
|
87 #ifdef MCABBER_API_HAVE_CMD_ID |
|
88 static gpointer tune_cmid = NULL; |
|
89 static gboolean tune_set_safe = FALSE; |
|
90 #endif |
|
91 |
|
92 static guint tune_cid = 0; |
|
93 static guint tune_hid_tunein = 0; |
|
94 |
|
95 // |
|
96 // code |
|
97 // |
|
98 |
|
99 static void do_tune (char *arg) |
|
100 { |
|
101 hk_arg_t tags[] = { |
|
102 { "artist", NULL }, |
|
103 { "length", NULL }, |
|
104 { "rating", NULL }, |
|
105 { "source", NULL }, |
|
106 { "title", NULL }, |
|
107 { "track", NULL }, |
|
108 { "uri", NULL }, |
|
109 { NULL, NULL }, |
|
110 }; |
|
111 |
|
112 if (!*arg) { // request |
|
113 |
|
114 GError *error = NULL; |
|
115 |
|
116 tune_request ( CURRENT_JID, &error ); |
|
117 if ( error ) { |
|
118 scr_log_print ( LPRINT_NORMAL, "Error sending request: %s.", error -> message ); |
|
119 g_error_free ( error ); |
|
120 } else |
|
121 scr_log_print ( LPRINT_NORMAL, "Request sent." ); // XXX |
|
122 |
|
123 return; |
|
124 |
|
125 } else if (arg[0] != '-' || arg[1] != '\0') { |
|
126 |
|
127 char *p; |
|
128 char *argstart = NULL; |
|
129 char *argstop = NULL; |
|
130 char *tagstart = NULL; |
|
131 char *tagstop = NULL; |
|
132 char *wordstart = arg; |
|
133 gboolean proceed = TRUE; |
|
134 |
|
135 // pt = p, ws = wordstart, ts = tagstart, tt = tagstop, as = argstart, at = argstop |
|
136 // tag=value value tag=value |
|
137 // w p |
|
138 // s t |
|
139 // t ta |
|
140 // s t* |
|
141 // tag=value value tag=value |
|
142 // t ta p |
|
143 // s t* t |
|
144 // t ta aw |
|
145 // s ts ts |
|
146 // tag=value value tag=value |
|
147 // t ta aw p |
|
148 // s ts ts t |
|
149 // t ta aw |
|
150 // s ts ts |
|
151 // tag=value value tag=value |
|
152 // t ta aw p |
|
153 // s ts ts t |
|
154 // t ta |
|
155 // s t* |
|
156 // tag=value value tag=value |
|
157 // t ta p |
|
158 // s t* t |
|
159 // t ta a |
|
160 // s ts t |
|
161 |
|
162 for (p = arg; proceed; ++p) { |
|
163 switch (*p) { |
|
164 case '=': |
|
165 if (tagstart && tagstop - tagstart) { |
|
166 // process previous args |
|
167 hk_arg_t *tag; |
|
168 |
|
169 for (tag = tags; tag->name; ++tag) { |
|
170 if (!strncmp (tag->name, tagstart, tagstop - tagstart)) { |
|
171 g_free ( (gchar *) tag->value ); |
|
172 if (argstop - argstart) { |
|
173 *argstop = '\0'; |
|
174 tag->value = to_utf8 (argstart); |
|
175 } |
|
176 break; |
|
177 } |
|
178 } |
|
179 } |
|
180 |
|
181 tagstart = wordstart; |
|
182 tagstop = p; |
|
183 argstop = p+1; |
|
184 argstart = p+1; |
|
185 break; |
|
186 |
|
187 case '\0': |
|
188 argstop = p; |
|
189 |
|
190 if (tagstop - tagstart) { |
|
191 // process previous args |
|
192 hk_arg_t *tag; |
|
193 |
|
194 for (tag = tags; tag->name; ++tag) { |
|
195 if (!strncmp (tag->name, tagstart, tagstop - tagstart)) { |
|
196 g_free ( (gchar *) tag->value ); |
|
197 if (argstop - argstart) |
|
198 tag->value = to_utf8 (argstart); |
|
199 break; |
|
200 } |
|
201 } |
|
202 } |
|
203 |
|
204 proceed = FALSE; |
|
205 break; |
|
206 |
|
207 case ' ': |
|
208 argstop = p; |
|
209 wordstart = p+1; |
|
210 break; |
|
211 |
|
212 default: |
|
213 break; |
|
214 } |
|
215 } |
|
216 } |
|
217 |
|
218 hk_run_handlers ( HOOK_TUNE_OUT, tags ); |
|
219 |
|
220 { |
|
221 hk_arg_t *tag; |
|
222 |
|
223 for (tag = tags; tag->name; ++tag) |
|
224 g_free ( (gchar *) tag->value ); |
|
225 } |
|
226 } |
|
227 |
|
228 static guint tune_htih ( const gchar *htype, hk_arg_t *args, gpointer udata ) |
|
229 { |
|
230 GString *mesg = g_string_new ( NULL ); |
|
231 const gchar *from = NULL; |
|
232 |
|
233 { |
|
234 hk_arg_t *arg; |
|
235 for ( arg = args; arg -> name; arg ++ ) |
|
236 if ( arg -> value ) { |
|
237 if ( ! strcmp ( arg -> name, "from" ) ) |
|
238 from = arg -> value; |
|
239 else |
|
240 g_string_append_printf ( mesg, "\n - %s: %s", arg -> name, arg -> value ); |
|
241 } |
|
242 } |
|
243 |
|
244 if (mesg->len) |
|
245 g_string_prepend (mesg, "Now listens to:"); |
|
246 else |
|
247 g_string_overwrite (mesg, 0, "Now listening to nothing"); |
|
248 |
|
249 { // print to buddy's buffer |
|
250 gchar *jid = jidtodisp ( from ); |
|
251 |
|
252 scr_write_incoming_message ( jid, mesg->str, 0, HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0 ); // NO conversion from utf-8 |
|
253 |
|
254 g_free ( jid ); |
|
255 } |
|
256 |
|
257 g_string_free ( mesg, TRUE ); |
|
258 |
|
259 return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
260 } |
|
261 |
|
262 void tune_init(void) |
|
263 { |
|
264 tune_cid = compl_new_category (); |
|
265 if (tune_cid) { |
|
266 compl_add_category_word (tune_cid, "artist="); |
|
267 compl_add_category_word (tune_cid, "length="); |
|
268 compl_add_category_word (tune_cid, "rating="); |
|
269 compl_add_category_word (tune_cid, "source="); |
|
270 compl_add_category_word (tune_cid, "title="); |
|
271 compl_add_category_word (tune_cid, "track="); |
|
272 compl_add_category_word (tune_cid, "uri="); |
|
273 } |
|
274 |
|
275 #ifndef MCABBER_API_HAVE_CMD_ID |
|
276 cmd_add ("tune", "", tune_cid, tune_cid, do_tune, NULL); |
|
277 #else |
|
278 tune_cmid = cmd_add ("tune", "", tune_cid, tune_cid, do_tune, NULL); |
|
279 tune_set_safe = cmd_set_safe ("tune", TRUE); |
|
280 #endif |
|
281 |
|
282 tune_hid_tunein = hk_add_handler (tune_htih, HOOK_TUNE_IN, G_PRIORITY_DEFAULT, NULL); |
|
283 } |
|
284 |
|
285 void tune_uninit(void) |
|
286 { |
|
287 hk_del_handler (HOOK_TUNE_IN, tune_hid_tunein); |
|
288 |
|
289 #ifndef MCABBER_API_HAVE_CMD_ID |
|
290 cmd_del ("tune"); |
|
291 #else |
|
292 if (tune_cmid) |
|
293 cmd_del (tune_cmid); |
|
294 if (tune_set_safe) |
|
295 cmd_set_safe ("tune", FALSE); |
|
296 #endif |
|
297 |
|
298 if (tune_cid) |
|
299 compl_del_category (tune_cid); |
|
300 } |
|
301 |
|
302 /* vim: se ts=4 sw=4: */ |