Skip to content

Commit

Permalink
Frontend-GNOME: implement --execute-command argument
Browse files Browse the repository at this point in the history
This new argument can be used to execute commands of either a new Smuxi
instance or an already running instance. The execute command behaves
exactly the same way as the input entry.

This is useful for remote automation, e.g.:

	ssh my-box-with-running-smuxi "smuxi-frontend-gnome '/say hello from the command line'"
  • Loading branch information
meebey committed May 18, 2023
1 parent 9fd1467 commit 0fcced4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/Frontend-GNOME/Main.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Smuxi - Smart MUltipleXed Irc
*
* Copyright (c) 2005-2008, 2012-2013, 2015 Mirco Bauer <[email protected]>
* Copyright (c) 2005-2008, 2012-2013, 2015, 2023 Mirco Bauer <[email protected]>
*
* Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
*
Expand Down Expand Up @@ -41,6 +41,7 @@ public static void Main(string[] args)
{
var debug = false;
var link = String.Empty;
var command = String.Empty;
var engine = String.Empty;
var newInstance = false;
var options = new OptionSet();
Expand Down Expand Up @@ -76,6 +77,13 @@ public static void Main(string[] args)
link = v;
}
);
options.Add(
"execute|execute-command=",
_("Executes the specified command in Smuxi"),
v => {
command = v;
}
);
options.Add(
"new-instance",
_("Starts a new Smuxi instance and ignores an existing one"),
Expand Down Expand Up @@ -103,6 +111,8 @@ public static void Main(string[] args)
Instance.FirstInstance = new CommandLineInterface();
if (!String.IsNullOrEmpty(link)) {
Instance.FirstInstance.OpenLink(link);
} else if (!String.IsNullOrEmpty(command)) {
Instance.FirstInstance.ExecuteCommand(command);
}
} else {
if (!String.IsNullOrEmpty(link)) {
Expand All @@ -113,6 +123,14 @@ public static void Main(string[] args)
Console.WriteLine(msg);
#endif
Instance.FirstInstance.OpenLink(link);
} else if (!String.IsNullOrEmpty(command)) {
var msg = _("Passing command to already running Smuxi instance...");
#if LOG4NET
_Logger.Info(msg);
#else
Console.WriteLine(msg);
#endif
Instance.FirstInstance.ExecuteCommand(command);
} else if (!newInstance) {
var msg = _("Bringing already running Smuxi instance to foreground...");
#if LOG4NET
Expand Down Expand Up @@ -209,6 +227,22 @@ public void OpenLink(string link)
}
}

public void ExecuteCommand(string cmd)
{
if (Frontend.Session == null) {
#if LOG4NET
Logger.Warn($"Can't execute '{cmd}' as session isn't ready, ignoring...");
#endif
return;
}
Gtk.Application.Invoke((o, e) => {
#if LOG4NET
Logger.Info($"Executing '{cmd}'...");
#endif
Frontend.MainWindow.Entry.ExecuteCommand(cmd);
});
}

public override object InitializeLifetimeService()
{
// live forever
Expand Down
4 changes: 4 additions & 0 deletions src/Frontend-GNOME/smuxi-frontend-gnome.1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
.Op Fl e Ar engine
.Op Fl \-new-instance
.Op Fl \-open Ar uri
.Op Fl \-execute-command Ar command
.Sh DESCRIPTION
.Nm
is a GNOME frontend for the Smuxi chat client, powered by the GTK# library. It can operate standalone or attached to a remote Smuxi engine.
Expand All @@ -29,6 +30,9 @@ Starts a new Smuxi instance and ignores any existing sessions.a
.It Fl \-open Ar uri , Fl \-open-link Ar uri
Opens a URL to an IRC channel, in the following form:
.Ql irc://irc.example.com/examplechannel
.It Fl \-execute Ar command , Fl \-execute-command Ar command
Executes the specified command in Smuxi. For example:
--execute-command '/say hi all from the command line'
.El
.Sh FILES
.Bl -tag -width -compact
Expand Down

0 comments on commit 0fcced4

Please sign in to comment.