1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Merge branch 'master' into osx-functional

This commit is contained in:
James Booth 2015-10-17 23:15:49 +01:00
commit cb33ae13fc
7 changed files with 176 additions and 8 deletions

View File

@ -51,6 +51,7 @@
#include "config/preferences.h"
#include "config/theme.h"
#include "config/tlscerts.h"
#include "config/scripts.h"
#include "contact.h"
#include "roster_list.h"
#include "jid.h"
@ -108,6 +109,7 @@ static char * _receipts_autocomplete(ProfWin *window, const char * const input);
static char * _help_autocomplete(ProfWin *window, const char * const input);
static char * _wins_autocomplete(ProfWin *window, const char * const input);
static char * _tls_autocomplete(ProfWin *window, const char * const input);
static char * _script_autocomplete(ProfWin *window, const char * const input);
GHashTable *commands = NULL;
@ -1693,19 +1695,17 @@ static struct cmd_t command_defs[] =
},
{ "/script",
cmd_script, parse_args, 2, 2, NULL,
cmd_script, parse_args, 1, 2, NULL,
CMD_NOTAGS
CMD_SYN(
"/script run <script>",
"/script remove <script>",
"/script list",
"/script show <script>")
CMD_DESC(
"Manage and run command scripts. "
"Run command scripts. "
"Scripts are stored in $XDG_DATA_HOME/profanity/scripts/ which is usually $HOME/.local/share/profanity/scripts/.")
CMD_ARGS(
{ "script run <script>", "Execute a script." },
{ "script remove <script>", "Remove a script TODO." },
{ "script list", "List all scripts TODO." },
{ "script show <script>", "Show the commands in script TODO." })
CMD_EXAMPLES(
@ -1778,6 +1778,7 @@ static Autocomplete pgp_log_ac;
static Autocomplete tls_ac;
static Autocomplete tls_certpath_ac;
static Autocomplete script_ac;
static Autocomplete script_show_ac;
/*
* Initialise command autocompleter and history
@ -2202,8 +2203,9 @@ cmd_init(void)
script_ac = autocomplete_new();
autocomplete_add(script_ac, "run");
autocomplete_add(script_ac, "list");
autocomplete_add(script_ac, "remove");
autocomplete_add(script_ac, "show");
script_show_ac = NULL;
}
void
@ -2272,6 +2274,7 @@ cmd_uninit(void)
autocomplete_free(tls_ac);
autocomplete_free(tls_certpath_ac);
autocomplete_free(script_ac);
autocomplete_free(script_show_ac);
}
gboolean
@ -2457,6 +2460,10 @@ cmd_reset_autocomplete(ProfWin *window)
autocomplete_reset(tls_ac);
autocomplete_reset(tls_certpath_ac);
autocomplete_reset(script_ac);
if (script_show_ac) {
autocomplete_free(script_show_ac);
script_show_ac = NULL;
}
if (window->type == WIN_CHAT) {
ProfChatWin *chatwin = (ProfChatWin*)window;
@ -2669,8 +2676,8 @@ _cmd_complete_parameters(ProfWin *window, const char * const input)
}
}
gchar *cmds[] = { "/prefs", "/disco", "/close", "/subject", "/room", "/script" };
Autocomplete completers[] = { prefs_ac, disco_ac, close_ac, subject_ac, room_ac, script_ac };
gchar *cmds[] = { "/prefs", "/disco", "/close", "/subject", "/room" };
Autocomplete completers[] = { prefs_ac, disco_ac, close_ac, subject_ac, room_ac };
for (i = 0; i < ARRAY_SIZE(cmds); i++) {
result = autocomplete_param_with_ac(input, cmds[i], completers[i], TRUE);
@ -2711,6 +2718,7 @@ _cmd_complete_parameters(ProfWin *window, const char * const input)
g_hash_table_insert(ac_funcs, "/receipts", _receipts_autocomplete);
g_hash_table_insert(ac_funcs, "/wins", _wins_autocomplete);
g_hash_table_insert(ac_funcs, "/tls", _tls_autocomplete);
g_hash_table_insert(ac_funcs, "/script", _script_autocomplete);
int len = strlen(input);
char parsed[len+1];
@ -3209,6 +3217,52 @@ _theme_autocomplete(ProfWin *window, const char * const input)
return NULL;
}
static char *
_script_autocomplete(ProfWin *window, const char * const input)
{
char *result = NULL;
if ((strncmp(input, "/script show ", 13) == 0) && (strlen(input) > 13)) {
if (script_show_ac == NULL) {
script_show_ac = autocomplete_new();
GSList *scripts = scripts_list();
GSList *curr = scripts;
while (curr) {
autocomplete_add(script_show_ac, curr->data);
curr = g_slist_next(curr);
}
g_slist_free_full(scripts, g_free);
}
result = autocomplete_param_with_ac(input, "/script show", script_show_ac, TRUE);
if (result) {
return result;
}
}
if ((strncmp(input, "/script run ", 12) == 0) && (strlen(input) > 12)) {
if (script_show_ac == NULL) {
script_show_ac = autocomplete_new();
GSList *scripts = scripts_list();
GSList *curr = scripts;
while (curr) {
autocomplete_add(script_show_ac, curr->data);
curr = g_slist_next(curr);
}
g_slist_free_full(scripts, g_free);
}
result = autocomplete_param_with_ac(input, "/script run", script_show_ac, TRUE);
if (result) {
return result;
}
}
result = autocomplete_param_with_ac(input, "/script", script_ac, TRUE);
if (result) {
return result;
}
return NULL;
}
static char *
_resource_autocomplete(ProfWin *window, const char * const input)
{

View File

@ -698,6 +698,14 @@ cmd_script(ProfWin *window, const char * const command, gchar **args)
if (!res) {
cons_show("Could not find script %s", args[1]);
}
} else if (g_strcmp0(args[0], "list") == 0) {
GSList *scripts = scripts_list();
cons_show_scripts(scripts);
g_slist_free_full(scripts, g_free);
} else if ((g_strcmp0(args[0], "show") == 0) && args[1]) {
GSList *commands = scripts_read(args[1]);
cons_show_script(args[1], commands);
g_slist_free_full(commands, g_free);
} else {
cons_bad_cmd_usage(command);
}

View File

@ -66,6 +66,70 @@ scripts_init(void)
log_error("Error creating directory: %s", scriptsdir->str);
}
}
g_string_free(scriptsdir, TRUE);
}
GSList*
scripts_list(void)
{
gchar *data_home = xdg_get_data_home();
GString *scriptsdir = g_string_new(data_home);
free(data_home);
g_string_append(scriptsdir, "/profanity/scripts");
GSList *result = NULL;
GDir *scripts = g_dir_open(scriptsdir->str, 0, NULL);
g_string_free(scriptsdir, TRUE);
if (scripts) {
const gchar *script = g_dir_read_name(scripts);
while (script) {
result = g_slist_append(result, strdup(script));
script = g_dir_read_name(scripts);
}
g_dir_close(scripts);
}
return result;
}
GSList*
scripts_read(const char *const script)
{
gchar *data_home = xdg_get_data_home();
GString *scriptpath = g_string_new(data_home);
free(data_home);
g_string_append(scriptpath, "/profanity/scripts/");
g_string_append(scriptpath, script);
FILE *scriptfile = g_fopen(scriptpath->str, "r");
if (!scriptfile) {
log_info("Script not found: %s", scriptpath->str);
g_string_free(scriptpath, TRUE);
return NULL;
}
g_string_free(scriptpath, TRUE);
char *line = NULL;
size_t len = 0;
ssize_t read;
GSList *result = NULL;
while ((read = getline(&line, &len, scriptfile)) != -1) {
if (g_str_has_suffix(line, "\n")) {
result = g_slist_append(result, g_strndup(line, strlen(line) -1));
} else {
result = g_slist_append(result, strdup(line));
}
}
fclose(scriptfile);
if (line) free(line);
return result;
}
gboolean
@ -87,7 +151,7 @@ scripts_exec(const char *const script)
g_string_free(scriptpath, TRUE);
char * line = NULL;
char *line = NULL;
size_t len = 0;
ssize_t read;

View File

@ -35,4 +35,6 @@
#include <glib.h>
void scripts_init(void);
GSList* scripts_list(void);
GSList* scripts_read(const char *const script);
gboolean scripts_exec(const char *const script);

View File

@ -1581,6 +1581,42 @@ cons_show_themes(GSList *themes)
cons_alert();
}
void
cons_show_scripts(GSList *scripts)
{
cons_show("");
if (scripts == NULL) {
cons_show("No scripts available.");
} else {
cons_show("Scripts:");
while (scripts) {
cons_show(scripts->data);
scripts = g_slist_next(scripts);
}
}
cons_alert();
}
void
cons_show_script(const char *const script, GSList *commands)
{
cons_show("");
if (commands == NULL) {
cons_show("Script not found: %s", script);
} else {
cons_show("%s:", script);
while (commands) {
cons_show(" %s", commands->data);
commands = g_slist_next(commands);
}
}
cons_alert();
}
void
cons_prefs(void)
{

View File

@ -278,6 +278,8 @@ void cons_show_status(const char * const barejid);
void cons_show_info(PContact pcontact);
void cons_show_caps(const char * const fulljid, resource_presence_t presence);
void cons_show_themes(GSList *themes);
void cons_show_scripts(GSList *scripts);
void cons_show_script(const char *const script, GSList *commands);
void cons_show_aliases(GList *aliases);
void cons_show_login_success(ProfAccount *account, int secured);
void cons_show_software_version(const char * const jid,

View File

@ -415,6 +415,8 @@ void cons_show_status(const char * const barejid) {}
void cons_show_info(PContact pcontact) {}
void cons_show_caps(const char * const fulljid, resource_presence_t presence) {}
void cons_show_themes(GSList *themes) {}
void cons_show_scripts(GSList *scripts) {}
void cons_show_script(const char *const script, GSList *commands) {}
void cons_show_aliases(GList *aliases)
{