some-usable-scripts/c/weechat_plugins/currentsong.c

138 lines
3.4 KiB
C
Raw Normal View History

2011-10-27 22:26:47 +02:00
/* Karchnu : <kane.root@gmail.com>
*
* currentsong : affiche le morceau que vous écoutez dans le buffer courant
*
2013-05-01 23:11:35 +02:00
* Pour l'ajouter : make ; cp song.so ~/.weechat/plugins/
* Puis dans weechat : /plugin load song.so
2011-10-27 22:26:47 +02:00
*
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <weechat/weechat-plugin.h>
#define TAILLE_BUFFER_CURRENT_SONG 100
2011-10-27 22:26:47 +02:00
#define TAILLE_OPTIONS 15
WEECHAT_PLUGIN_NAME("currentsong");
WEECHAT_PLUGIN_DESCRIPTION("Affiche la musique en cours en utilisant mpc");
WEECHAT_PLUGIN_AUTHOR("Karchnu <kane.root@gmail.com>");
WEECHAT_PLUGIN_VERSION("0.1");
WEECHAT_PLUGIN_LICENSE("GPL3");
struct t_weechat_plugin *weechat_plugin = NULL;
2013-05-01 23:11:35 +02:00
int cb_currentsong_plugin(void *data, struct t_gui_buffer *buffer,
int argc, char **argv, char **argv_eol)
{
/* pour que le compilateur C soit content */
(void) data;
(void) buffer;
(void) argv;
2011-10-27 22:26:47 +02:00
int i,status;
int p[2];
2011-10-27 22:26:47 +02:00
char **cmd;
char *song;
char *affichage;
2013-05-01 23:11:35 +02:00
song = malloc(TAILLE_BUFFER_CURRENT_SONG * sizeof(char));
affichage = malloc((TAILLE_BUFFER_CURRENT_SONG + 10) * sizeof(char));
cmd = (char **) malloc(sizeof(char*) * argc+2);
for(i = 0 ; i < argc +1 ; i++)
2011-10-27 22:26:47 +02:00
cmd[i] = malloc(sizeof(char) * TAILLE_OPTIONS);
cmd[i] = NULL;
2013-05-01 23:11:35 +02:00
strcpy(cmd[0],"mpc"); // la commande
strcpy(cmd[1],"current"); // la chanson courante
if(argc >= 3) {
if(strcmp("host", argv[1]) == 0) {
strcpy(cmd[2], "-h");
strcpy(cmd[3], argv[2]);
}
2011-10-27 22:26:47 +02:00
}
2013-05-01 23:11:35 +02:00
if(argc >= 4) {
if(strcmp("port", argv[3]) == 0) {
strcpy(cmd[4], "-p");
strcpy(cmd[5], argv[4]);
cmd[6] = NULL;
}
}
for(i = 0 ; i < TAILLE_BUFFER_CURRENT_SONG ; i++)
song[i] = '\0';
for(i = 0 ; i < TAILLE_BUFFER_CURRENT_SONG + 10 ; i++)
affichage[i] = '\0';
pipe(p);
if(fork() == 0)
{
close(p[0]);
close(0); close(2);
dup2(p[1],1);
execvp("mpc", cmd);
}
close(p[1]);
read(p[0], song, TAILLE_BUFFER_CURRENT_SONG);
close(p[0]);
2011-10-27 22:26:47 +02:00
wait(&status);
if(WEXITSTATUS(status) != 0)
2011-10-28 11:13:09 +02:00
{
for(i = 0 ; i < TAILLE_BUFFER_CURRENT_SONG ; i++)
song[i] = '\0';
2013-05-01 23:11:35 +02:00
snprintf(song, TAILLE_BUFFER_CURRENT_SONG,
"%s didn't work !!! Look at the options !",argv[0]);
snprintf(affichage, TAILLE_BUFFER_CURRENT_SONG, "%s%s",
weechat_prefix("error"),song);
2011-10-28 11:13:09 +02:00
weechat_printf(buffer,affichage);
return WEECHAT_RC_ERROR;
}
if(strcmp("",song) == 0)
snprintf(affichage,TAILLE_BUFFER_CURRENT_SONG, "/me ! ♪");
else
snprintf(affichage,TAILLE_BUFFER_CURRENT_SONG, "/me ♪ %s", song);
2011-10-27 22:26:47 +02:00
2013-05-01 23:11:35 +02:00
//snprintf(affichage, TAILLE_BUFFER_CURRENT_SONG,
//"/me ♪ %s%s", weechat_color ("_red"), song);
//weechat_utf8_normalize(affichage, '?');
2011-10-27 22:26:47 +02:00
weechat_command(buffer,affichage);
2011-10-27 22:26:47 +02:00
free(song); // :')
2013-05-01 23:11:35 +02:00
for(i = 0 ; i < argc + 1 ; i++)
2011-10-27 22:26:47 +02:00
free(cmd[i]);
free(cmd);
free(affichage);
return WEECHAT_RC_OK;
}
2013-05-01 23:11:35 +02:00
int weechat_plugin_init (struct t_weechat_plugin *plugin,
int argc, char *argv[])
{
weechat_plugin = plugin;
weechat_hook_command ("currentsong",
"Affiche le morceau que vous écoutez",
2013-05-01 23:11:35 +02:00
"[host <ip|url> [port <port>]]",
"si mpd n'est pas en local : [host <ip|url> [port <port>]]",
"host %(the_host) port %(the_port)",
&cb_currentsong_plugin, NULL);
return WEECHAT_RC_OK;
}
int weechat_plugin_end (struct t_weechat_plugin *plugin)
{
/* pour que le compilateur C soit content */
(void) plugin;
return WEECHAT_RC_OK;
}