summaryrefslogtreecommitdiff
path: root/plugins/shellexec/shellexec.c
blob: f576ca9b617f3cd5b8acda445b9cba3a7ca77378 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
    Shellexec plugin for DeaDBeeF
    Copyright (C) 2009 Viktor Semykin <thesame.ml@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "../../deadbeef.h"

#define trace(...) { fprintf(stderr, __VA_ARGS__); }
//#define trace(fmt,...)

static DB_misc_t plugin;
static DB_functions_t *deadbeef;

static DB_plugin_action_t *actions;

DB_plugin_t *
shellexec_load (DB_functions_t *api) {
    deadbeef = api;
    return DB_PLUGIN (&plugin);
}

static char*
trim (char* s)
{
    char *h, *t;
    
    for (h = s; *h == ' ' || *h == '\t'; h++);
    for (t = s + strlen (s); *t == ' ' || *t == '\t'; t--);
    * (t+1) = 0;
    return h;
}

static int
shell_quote (const char *source, char *dest)
{
    char *sp, *dp;

    for (sp = source, dp = dest; *sp; sp++, dp++)
    {
        if (*sp == '\'')
        {
            strcpy (dp, "'\\''");
            dp += 3;
        }
        else
            *dp = *sp;
    }
    *dp = 0;
}


/*
    format_shell_command function
    Similarly to db_format_title formats track's string
    and quotes all fields to make them safe for shell
    
    %a, %t, %b, %B, %C, %n, %N, %y, %g, %c, %r are handled. Use double-% to
    skip field.


    Example:

    Format string:
    echo %a - %t %%a - %%t

    Output:
    echo 'Blind Faith' - 'Can'\''t Find My Way Home' %a - %t
*/
static const char*
format_shell_command (DB_playItem_t *it, const char *format)
{
    char *p;
    const char *trailing = format;
    const char *field;
    char *res, *res_p;

    res = res_p = malloc (65536); //FIXME: possible heap corruption

    for (;;)
    {
        p = strchr (trailing, '%');
        if (!p) break;

        switch (*(p+1))
        {
            case 'a': field = "artist"; break;
            case 't': field = "title"; break;
            case 'b': field = "album"; break;
            case 'B': field = "band"; break;
            case 'C': field = "composer"; break;
            case 'n': field = "track"; break;
            case 'N': field = "numtracks"; break;
            case 'y': field = "year"; break;
            case 'g': field = "genre"; break;
            case 'c': field = "comment"; break;
            case 'r': field = "copyright"; break;
            case 'f': break;
            default: field = NULL;
        }

        if (field == NULL)
        {
            int l = ((*(p+1) == '%') ? 1 : 0);
            trace ("field is null; p: %s; p+1: %s; l: %d; trailing: %s; res_p: %s\n", p, p+1, l, trailing, res_p);
            strncpy (res_p, trailing, p-trailing+l);
            res_p += (p-trailing+l);
            trailing = p+l+1;
            trace ("res: %s; trailing: %s\n", res, res_p, trailing);
            continue;
        }
        else
        {
            const char *meta;
            if (*(p+1) == 'f')
                meta = it->fname;
            else
                meta = deadbeef->pl_find_meta (it, field);

            char quoted [strlen (meta) * 4 + 1]; //Worst case is when all chars are single quote
            shell_quote (meta, quoted);
            
            strncpy (res_p, trailing, p-trailing);
            res_p += (p-trailing);
            *res_p++ = '\'';
            strcpy (res_p, quoted);
            res_p += strlen (quoted);
            *res_p++ = '\'';
            trailing = p+2;
        }
    }
    strcpy (res_p, trailing);
    return res;
}

static int
shx_callback (DB_playItem_t *it, void *data)
{
    const char *cmd = format_shell_command (it, data);
    printf ("%s\n", cmd);
    system (cmd);
    free (cmd);
    return 0;
}

static DB_plugin_action_t *
shx_get_actions (DB_playItem_t *unused)
{
    return actions;
}

static int
shx_start ()
{
    actions = NULL;
    DB_plugin_action_t *prev = NULL;

    DB_conf_item_t *item = deadbeef->conf_find ("shellexec.", NULL);
    while (item)
    {
        size_t l = strlen (item->value) + 1;
        char tmp[l];
        strcpy (tmp, item->value);
        trace ("Shellexec: %s\n", tmp);

        char *semicolon = strchr (tmp, ':');
        if (!semicolon)
        {
            fprintf (stdout, "Shellexec: wrong option <%s>\n", tmp);
            continue;
        }

        *semicolon = 0;

        DB_plugin_action_t *action = calloc (sizeof (DB_plugin_action_t), 1);

        action->title = strdup (trim (semicolon + 1));
        action->callback = shx_callback;
        action->data = strdup (trim (tmp));
        action->flags = DB_ACTION_SINGLE_TRACK | DB_ACTION_ALLOW_MULTIPLE_TRACKS;
        action->next = NULL;

        if (prev)
            prev->next = action;
        prev = action;

        if (!actions)
            actions = action;

        item = deadbeef->conf_find ("shellexec.", item);
    }
    return 0;
}

// define plugin interface
static DB_misc_t plugin = {
    .plugin.api_vmajor = DB_API_VERSION_MAJOR,
    .plugin.api_vminor = DB_API_VERSION_MINOR,
    .plugin.type = DB_PLUGIN_MISC,
    .plugin.id = "shellexec",
    .plugin.name = "Shell commands",
    .plugin.descr = "Executes configurable shell commands for tracks",
    .plugin.author = "Viktor Semykin",
    .plugin.email = "thesame.ml@gmail.com",
    .plugin.website = "http://deadbeef.sf.net",
    .plugin.start = shx_start,
    .plugin.get_actions = shx_get_actions
};