about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-10-15 01:19:24 +0100
committerJames Booth <boothj5@gmail.com>2015-10-15 01:19:24 +0100
commit904a5a81cfcc19c7029f0a961cfb97f7d1a2fe9f (patch)
tree92a1a43f43f7a5fbbd022fe9b4ccd2fbb1390cb8 /src
parentfb60a755e50e8edf8c00ebbf2ba1e707da6ec101 (diff)
downloadprofani-tty-904a5a81cfcc19c7029f0a961cfb97f7d1a2fe9f.tar.gz
Add ability to script commands after connect
Diffstat (limited to 'src')
-rw-r--r--src/command/command.c10
-rw-r--r--src/command/commands.c7
-rw-r--r--src/config/account.c9
-rw-r--r--src/config/account.h3
-rw-r--r--src/config/accounts.c28
-rw-r--r--src/config/accounts.h2
-rw-r--r--src/config/scripts.c106
-rw-r--r--src/config/scripts.h38
-rw-r--r--src/event/server_events.c6
-rw-r--r--src/profanity.c2
-rw-r--r--src/ui/console.c4
-rw-r--r--src/xmpp/connection.c4
12 files changed, 210 insertions, 9 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 6309ce93..cd3a8c03 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1488,12 +1488,14 @@ static struct cmd_t command_defs[] =
             "/account set <account> nick <nick>",
             "/account set <account> otr <policy>",
             "/account set <account> pgpkeyid <pgpkeyid>",
+            "/account set <account> startscript <script>",
             "/account clear <account> password",
             "/account clear <account> eval_password",
             "/account clear <account> server",
             "/account clear <account> port",
             "/account clear <account> otr",
-            "/account clear <account> pgpkeyid")
+            "/account clear <account> pgpkeyid",
+            "/account clear <account> startscript")
         CMD_DESC(
             "Commands for creating and managing accounts. "
             "Calling with no arguments will display information for the current account.")
@@ -1520,12 +1522,14 @@ static struct cmd_t command_defs[] =
             { "set <account> nick <nick>",              "The default nickname to use when joining chat rooms." },
             { "set <account> otr <policy>",             "Override global OTR policy for this account, see /otr." },
             { "set <account> pgpkeyid <pgpkeyid>",      "Set the ID of the PGP key for this account, see /pgp." },
+            { "set <account> startscript <script>",     "Set the script to execute after connecting." },
             { "clear <account> server",                 "Remove the server setting for this account." },
             { "clear <account> port",                   "Remove the port setting for this account." },
             { "clear <account> password",               "Remove the password setting for this account." },
             { "clear <account> eval_password",          "Remove the eval_password setting for this account." },
             { "clear <account> otr",                    "Remove the OTR policy setting for this account." },
-            { "clear <account> pgpkeyid",               "Remove pgpkeyid associated with this account." })
+            { "clear <account> pgpkeyid",               "Remove pgpkeyid associated with this account." },
+            { "clear <account> startscript",            "Remove startscript associated with this account." })
         CMD_EXAMPLES(
             "/account add me",
             "/account set me jid me@chatty",
@@ -1915,6 +1919,7 @@ cmd_init(void)
     autocomplete_add(account_set_ac, "nick");
     autocomplete_add(account_set_ac, "otr");
     autocomplete_add(account_set_ac, "pgpkeyid");
+    autocomplete_add(account_set_ac, "startscript");
 
     account_clear_ac = autocomplete_new();
     autocomplete_add(account_clear_ac, "password");
@@ -1923,6 +1928,7 @@ cmd_init(void)
     autocomplete_add(account_clear_ac, "port");
     autocomplete_add(account_clear_ac, "otr");
     autocomplete_add(account_clear_ac, "pgpkeyid");
+    autocomplete_add(account_clear_ac, "startscript");
 
     account_default_ac = autocomplete_new();
     autocomplete_add(account_default_ac, "set");
diff --git a/src/command/commands.c b/src/command/commands.c
index df730c63..6e8ccfd9 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -586,6 +586,9 @@ cmd_account(ProfWin *window, const char * const command, gchar **args)
                     cons_show("PGP support is not included in this build.");
 #endif
                     cons_show("");
+                } else if (strcmp(property, "startscript") == 0) {
+                    accounts_set_script_start(account_name, value);
+                    cons_show("Updated start script for account %s: %s", account_name, value);
                 } else if (valid_resource_presence_string(property)) {
                     int intval;
                     char *err_msg = NULL;
@@ -668,6 +671,10 @@ cmd_account(ProfWin *window, const char * const command, gchar **args)
                     accounts_clear_pgp_keyid(account_name);
                     cons_show("Removed PGP key ID for account %s", account_name);
                     cons_show("");
+                } else if (strcmp(property, "startscript") == 0) {
+                    accounts_clear_script_start(account_name);
+                    cons_show("Removed start script for account %s", account_name);
+                    cons_show("");
                 } else {
                     cons_show("Invalid property: %s", property);
                     cons_show("");
diff --git a/src/config/account.c b/src/config/account.c
index de48ba02..71b05a96 100644
--- a/src/config/account.c
+++ b/src/config/account.c
@@ -51,7 +51,7 @@ account_new(const gchar * const name, const gchar * const jid,
     int priority_away, int priority_xa, int priority_dnd,
     const gchar * const muc_service, const gchar * const muc_nick,
     const gchar * const otr_policy, GList *otr_manual, GList *otr_opportunistic,
-    GList *otr_always, const gchar * const pgp_keyid)
+    GList *otr_always, const gchar * const pgp_keyid, const char *const startscript)
 {
     ProfAccount *new_account = malloc(sizeof(ProfAccount));
 
@@ -150,6 +150,12 @@ account_new(const gchar * const name, const gchar * const jid,
         new_account->pgp_keyid = NULL;
     }
 
+    if (startscript != NULL) {
+        new_account->startscript = strdup(startscript);
+    } else {
+        new_account->startscript = NULL;
+    }
+
     return new_account;
 }
 
@@ -217,6 +223,7 @@ account_free(ProfAccount *account)
         free(account->muc_nick);
         free(account->otr_policy);
         free(account->pgp_keyid);
+        free(account->startscript);
         g_list_free_full(account->otr_manual, g_free);
         g_list_free_full(account->otr_opportunistic, g_free);
         g_list_free_full(account->otr_always, g_free);
diff --git a/src/config/account.h b/src/config/account.h
index 22c29161..d2b55569 100644
--- a/src/config/account.h
+++ b/src/config/account.h
@@ -60,6 +60,7 @@ typedef struct prof_account_t {
     GList *otr_opportunistic;
     GList *otr_always;
     gchar *pgp_keyid;
+    gchar *startscript;
 } ProfAccount;
 
 ProfAccount* account_new(const gchar * const name, const gchar * const jid,
@@ -69,7 +70,7 @@ ProfAccount* account_new(const gchar * const name, const gchar * const jid,
     int priority_away, int priority_xa, int priority_dnd,
     const gchar * const muc_service, const gchar * const muc_nick,
     const gchar * const otr_policy, GList *otr_manual, GList *otr_opportunistic,
-    GList *otr_always, const gchar * const pgp_keyid);
+    GList *otr_always, const gchar * const pgp_keyid, const char *const startscript);
 char* account_create_full_jid(ProfAccount *account);
 gboolean account_eval_password(ProfAccount *account);
 void account_free(ProfAccount *account);
diff --git a/src/config/accounts.c b/src/config/accounts.c
index 3e556a42..7ae34983 100644
--- a/src/config/accounts.c
+++ b/src/config/accounts.c
@@ -264,11 +264,16 @@ accounts_get_account(const char * const name)
             pgp_keyid = g_key_file_get_string(accounts, name, "pgp.keyid", NULL);
         }
 
+        gchar *startscript = NULL;
+        if (g_key_file_has_key(accounts, name, "script.start", NULL)) {
+            startscript = g_key_file_get_string(accounts, name, "script.start", NULL);
+        }
+
         ProfAccount *new_account = account_new(name, jid, password, eval_password, enabled,
             server, port, resource, last_presence, login_presence,
             priority_online, priority_chat, priority_away, priority_xa,
             priority_dnd, muc_service, muc_nick, otr_policy, otr_manual,
-            otr_opportunistic, otr_always, pgp_keyid);
+            otr_opportunistic, otr_always, pgp_keyid, startscript);
 
         g_free(jid);
         g_free(password);
@@ -281,6 +286,7 @@ accounts_get_account(const char * const name)
         g_free(muc_nick);
         g_free(otr_policy);
         g_free(pgp_keyid);
+        g_free(startscript);
 
         return new_account;
     }
@@ -347,7 +353,8 @@ accounts_rename(const char * const account_name, const char * const new_name)
         "otr.opportunistic",
         "otr.always",
         "pgp.keyid",
-        "last.activity"
+        "last.activity",
+        "script.start"
     };
 
     int i;
@@ -462,6 +469,15 @@ accounts_set_pgp_keyid(const char * const account_name, const char * const value
 }
 
 void
+accounts_set_script_start(const char * const account_name, const char * const value)
+{
+    if (accounts_account_exists(account_name)) {
+        g_key_file_set_string(accounts, account_name, "script.start", value);
+        _save_accounts();
+    }
+}
+
+void
 accounts_clear_password(const char * const account_name)
 {
     if (accounts_account_exists(account_name)) {
@@ -507,6 +523,14 @@ accounts_clear_pgp_keyid(const char * const account_name)
 }
 
 void
+accounts_clear_script_start(const char * const account_name)
+{
+    if (accounts_account_exists(account_name)) {
+        g_key_file_remove_key(accounts, account_name, "script.start", NULL);
+        _save_accounts();
+    }
+}
+void
 accounts_clear_otr(const char * const account_name)
 {
     if (accounts_account_exists(account_name)) {
diff --git a/src/config/accounts.h b/src/config/accounts.h
index b64cafa6..610ed6a3 100644
--- a/src/config/accounts.h
+++ b/src/config/accounts.h
@@ -82,12 +82,14 @@ void accounts_set_priority_all(const char * const account_name, const gint value
 gint accounts_get_priority_for_presence_type(const char * const account_name,
     resource_presence_t presence_type);
 void accounts_set_pgp_keyid(const char * const account_name, const char * const value);
+void accounts_set_script_start(const char * const account_name, const char * const value);
 void accounts_clear_password(const char * const account_name);
 void accounts_clear_eval_password(const char * const account_name);
 void accounts_clear_server(const char * const account_name);
 void accounts_clear_port(const char * const account_name);
 void accounts_clear_otr(const char * const account_name);
 void accounts_clear_pgp_keyid(const char * const account_name);
+void accounts_clear_script_start(const char * const account_name);
 void accounts_add_otr_policy(const char * const account_name, const char * const contact_jid, const char * const policy);
 
 #endif
diff --git a/src/config/scripts.c b/src/config/scripts.c
new file mode 100644
index 00000000..17beb697
--- /dev/null
+++ b/src/config/scripts.c
@@ -0,0 +1,106 @@
+/*
+ * scripts.c
+ *
+ * Copyright (C) 2012 - 2015 James Booth <boothj5@gmail.com>
+ *
+ * This file is part of Profanity.
+ *
+ * Profanity 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Profanity 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 Profanity.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * In addition, as a special exception, the copyright holders give permission to
+ * link the code of portions of this program with the OpenSSL library under
+ * certain conditions as described in each individual source file, and
+ * distribute linked combinations including the two.
+ *
+ * You must obey the GNU General Public License in all respects for all of the
+ * code used other than OpenSSL. If you modify file(s) with this exception, you
+ * may extend this exception to your version of the file(s), but you are not
+ * obligated to do so. If you do not wish to do so, delete this exception
+ * statement from your version. If you delete this exception statement from all
+ * source files in the program, then also delete it here.
+ *
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include "common.h"
+#include "log.h"
+#include "window_list.h"
+#include "command/command.h"
+#include "ui/ui.h"
+
+void
+scripts_init(void)
+{
+    gchar *data_home = xdg_get_data_home();
+    GString *scriptsdir = g_string_new(data_home);
+    free(data_home);
+
+    g_string_append(scriptsdir, "/profanity/scripts");
+
+    // mkdir if doesn't exist
+    errno = 0;
+    int res = g_mkdir_with_parents(scriptsdir->str, S_IRWXU);
+    if (res == -1) {
+        char *errmsg = strerror(errno);
+        if (errmsg) {
+            log_error("Error creating directory: %s, %s", scriptsdir->str, errmsg);
+        } else {
+            log_error("Error creating directory: %s", scriptsdir->str);
+        }
+    }
+}
+
+gboolean
+scripts_exec(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 FALSE;
+    }
+
+    g_string_free(scriptpath, TRUE);
+
+    char * line = NULL;
+    size_t len = 0;
+    ssize_t read;
+
+    while ((read = getline(&line, &len, scriptfile)) != -1) {
+        ProfWin *win = wins_get_current();
+        cmd_process_input(win, line);
+        jabber_process_events(10);
+        ui_update();
+    }
+
+    fclose(scriptfile);
+    if (line) free(line);
+
+    return TRUE;
+}
+
diff --git a/src/config/scripts.h b/src/config/scripts.h
new file mode 100644
index 00000000..114452af
--- /dev/null
+++ b/src/config/scripts.h
@@ -0,0 +1,38 @@
+/*
+ * scripts.h
+ *
+ * Copyright (C) 2012 - 2015 James Booth <boothj5@gmail.com>
+ *
+ * This file is part of Profanity.
+ *
+ * Profanity 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Profanity 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 Profanity.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * In addition, as a special exception, the copyright holders give permission to
+ * link the code of portions of this program with the OpenSSL library under
+ * certain conditions as described in each individual source file, and
+ * distribute linked combinations including the two.
+ *
+ * You must obey the GNU General Public License in all respects for all of the
+ * code used other than OpenSSL. If you modify file(s) with this exception, you
+ * may extend this exception to your version of the file(s), but you are not
+ * obligated to do so. If you do not wish to do so, delete this exception
+ * statement from your version. If you delete this exception statement from all
+ * source files in the program, then also delete it here.
+ *
+ */
+
+#include <glib.h>
+
+void scripts_init(void);
+gboolean scripts_exec(const char *const script);
diff --git a/src/event/server_events.c b/src/event/server_events.c
index 697eb6c9..f46f14ad 100644
--- a/src/event/server_events.c
+++ b/src/event/server_events.c
@@ -43,6 +43,7 @@
 #include "muc.h"
 #include "config/preferences.h"
 #include "config/account.h"
+#include "config/scripts.h"
 #include "roster_list.h"
 #include "window_list.h"
 #include "config/tlscerts.h"
@@ -84,6 +85,11 @@ sv_ev_login_account_success(char *account_name, int secured)
     g_list_free(curr);
 
     log_info("%s logged in successfully", account->jid);
+
+    if (account->startscript) {
+        scripts_exec(account->startscript);
+    }
+
     account_free(account);
 }
 
diff --git a/src/profanity.c b/src/profanity.c
index 3bec7a99..4dc2af3d 100644
--- a/src/profanity.c
+++ b/src/profanity.c
@@ -51,6 +51,7 @@
 #include "config/accounts.h"
 #include "config/preferences.h"
 #include "config/theme.h"
+#include "config/scripts.h"
 #include "command/command.h"
 #include "common.h"
 #include "contact.h"
@@ -331,6 +332,7 @@ _init(const int disable_tls, char *log_level)
     roster_init();
     muc_init();
     tlscerts_init();
+    scripts_init();
 #ifdef HAVE_LIBOTR
     otr_init();
 #endif
diff --git a/src/ui/console.c b/src/ui/console.c
index f5014013..6590cb78 100644
--- a/src/ui/console.c
+++ b/src/ui/console.c
@@ -713,7 +713,9 @@ cons_show_account(ProfAccount *account)
     if (account->login_presence) {
         cons_show   ("Login presence    : %s", account->login_presence);
     }
-
+    if (account->startscript) {
+        cons_show   ("Start script      : %s", account->startscript);
+    }
     if (account->otr_policy) {
         cons_show   ("OTR policy        : %s", account->otr_policy);
     }
diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c
index ba694bfd..2b7db8be 100644
--- a/src/xmpp/connection.c
+++ b/src/xmpp/connection.c
@@ -482,6 +482,8 @@ _connection_handler(xmpp_conn_t * const conn,
     // login success
     if (status == XMPP_CONN_CONNECT) {
         log_debug("Connection handler: XMPP_CONN_CONNECT");
+        jabber_conn.conn_status = JABBER_CONNECTED;
+
         int secured = xmpp_conn_is_secured(jabber_conn.conn);
 
         // logged in with account
@@ -520,8 +522,6 @@ _connection_handler(xmpp_conn_t * const conn,
             iq_enable_carbons();
         }
 
-        jabber_conn.conn_status = JABBER_CONNECTED;
-
         if (prefs_get_reconnect() != 0) {
             if (reconnect_timer) {
                 g_timer_destroy(reconnect_timer);