Konversation has built-in support for running external scripts which, partnered with D-Bus and Konversation's own D-Bus methods, cover the most common scripting use cases, such as displaying information in the current chat window or controlling another application or even Konversation itself. This guide introduces the basics of writing a Konversation script. It covers only the essential concepts necessary to get started. Language- and system-specific nuances will be left to the user as a learning exercise.
All you need is a text editor and a programming/scripting language. Konversation supports any programming language that:
qdbus"). It is probably better and more secure to use the language's native D-Bus bindings if available, rather than executing a system call to run
At the moment, Python, BASH (or Shell), and Perl are known to work and have examples shipped with Konversation. But any language fulfilling the two requirements above would probably also work. This guide will give examples in Python.
Probably the most common scripting scenario is getting some text to display in the current Konversation tab (channel or private message), with the data usually coming from an external source. This source could be the script itself, another application (such as the
media script included with Konversation), or the Internet (like getting weather information and displaying it in Konversation). Whatever the source, there are 3 steps to perform:
The following is a nonsensical example of a Konversation script that roughly follows that pattern.
#!/usr/bin/env python # mood - a Konversation script to display a witty remark based on the user's mood. # Usage: /exec mood [mood_string] import sys import subprocess command = ['qdbus', 'org.kde.konversation', '/irc'] argc = len(sys.argv) if argc < 2: command.append("error") text = "Server required" elif argc < 3: command.append("error") text = "Target required" elif argc < 4: command.append("error") text = "No mood given" else: server = sys.argv target = sys.argv mood = sys.argv if mood == "hungry": text = "Hungry! Anyone got a horse?" elif mood == "sleepy": text = "I yawn, therefore I am." elif mood == "gloomy": text = "Roses are red. Violets are blue, and so am I ..." elif mood == "happy": text = "Thinking happy thoughts (with a dash of pixie dust)." elif mood == "hyper": text = "Just a spoonful of sugar? I think I took a whole jar! *cartwheels*" elif mood == "excited": text = "Are we there yet? Are we there yet? Are we there yet?" else: text = "What were we talking about again?" command.append("say") command.append(server) command.append(target) command.append(text) subprocess.Popen(command).communicate()
When Konversation calls an external script, it runs it with some predefined arguments, like this:
script_name server target [additional arguments ...]
Arguments are usually stored in a collection (a list or an array, depending on the language) called "argv" indexed from 0 to N, with N being the number of arguments.
|Even if your script doesn't require additional arguments or even if the user didn't supply them, Konversation will always send the system and target arguments. Meaning, argv and argv will always be set. The minimum argument count will always be 3 (remember, indexing starts at 0).|
In the example script, the mood variable, supplied by the user in argv of the script, is compared to predefined values and the appropriate remark is assigned to the text variable. This is a simple example of data processing. You can also fetch data from a predefined data source, like the
fortune script. Or assemble information coming from the operating system, such as done in the
sysinfo script. Whatever information or text you need to be displayed in Konversation, you create that here.
|A word of caution: Be careful when creating multi-line text output, as your server or the channel you're sending to may have anti-flooding policies. When dealing with a potentially large, it might be best to have it displayed in another way (like in a text editor).|
Now that the needed information is processed and assembled, it's time to prepare it for sending back into Konversation, which is discussed in the next section.
Controlling Konversation externally, like through a script or the command-line, involves using its D-Bus methods. Konversation has several of them, including a group whose purpose is to display messages. Sending D-Bus messages can be tedious. Fortunately, Qt provides a much easier way of doing that, using the helper program "
qdbus". Without going into much detail, all the D-Bus commands we will be sending to make Konversation display messages starts with this string:
qdbus org.kde.konversation /irc
Depending on what kind of message the script will be sending, additional options will be added to that command. Here are but a few examples:
qdbus org.kde.konversation /irc say server target message
This is probably the command that will be most commonly used. It sends message to the chat window target connected to server. If you want the message to be sent to the same chat window where the script was called from, use argv and argv for server and target, respectively. Of course you can always change the target to any channel or nick you want in that server.
qdbus org.kde.konversation /irc error message
This displays the message in the same chat window where the script was invoked. The difference is that the message isn't actually sent to the IRC server and is only visible to the user. The message is also formatted as something like "[D-Bus] Error: message". Useful for informing the user about errors in using the script or if something fails in the script. It doesn't need a server or a target.
qdbus org.kde.konversation /irc sayToAll message
Sends the message to all channels and query windows in all servers. Please use sparingly and observe proper netiquette. It doesn't require a server and a target.
qdbus org.kde.konversation /irc actionToAll message
sayToAll's action sibling. Sends the message to all channels and query windows in all servers BUT prepends "/me" to the actual message, resulting in displaying something like "*YourNick message*". Again, netiquette applies.
|To send an action ("/me") message only to the current chat window (where the script was called from), compose the actual message as "/me message" and use the say variant of the command (first one in this list if you got lost ).|
There are other /irc related qdbus commands, some of which change the user's status instead of displaying the command. Feel free to explore other possibilities. Another Qt helper program, "
qdbusviewer", provides a graphical interface for browsing through the available commands for Konversation and other "D-Bus aware" programs. You will probably need it a lot especially for the second scripting scenario discussed later.
Now it's time to actually make the script run in Konversation. Here are the steps:
chmod +x script_namedoes it all. It would be better to not include extensions (.sh, .py, .pl, etc.) in your filename, to make it easier to call the script.
kde4-config --install dataand append
/konversation/scripts/to it. Consult your distribution's support channel if it's still not there.
mediascript as an example, since it comes with any Konversation installation):
/exec --showpath media. Note that if there are two scripts with the same name in the user's home and in the system locations, the one in the user's home will take precedence and be the one shown here.
/exec script_name [additional arguments]
This will call your script and pass the arguments to it. Remember that your script_name is always argv, and that the current server and chat window are passed as argv and argv, even if you didn't include them in your command. Therefore, any additional arguments would be argv and so on.
For the previous example, the script is named "mood" and can be invoked either using
/mood [mood] or
/exec mood [mood], like:
That's basically all you need to know to make a Konversation script. To make it a bit more interesting, let's have an example of another common scripting scenario.
Thanks to D-Bus, and the fact that a lot of KDE applications have D-Bus methods, you can control any KDE application right from within Konversation. Even without D-Bus, you can let your script start, stop, or possibly even control other applications simply with a command in Konversation. This lets you do a lot of things, like sending a command to a terminal emulator, opening a bug report in a browser providing only the report number (like the
bug script), or simply running a system command (and probably displaying the results of that command, as the
cmd script does).
The following script performs the first example. It first makes Yakuake visible and then runs the command supplied by the user. The script is rather simple and doesn't involve displaying anything back to the user, except in the case of an error when calling the script itself.
#!/usr/bin/env python # yakrun - Konversation script that runs the command supplied by the user in Yakuake and toggles Yakuake's state # Usage: /exec yakrun [command] import sys import subprocess errorCommand = ['qdbus', 'org.kde.konversation', '/irc', 'error'] toggleCommand = ['qdbus', 'org.kde.yakuake', '/yakuake/window', 'toggleWindowState'] runCommand = ['qdbus', 'org.kde.yakuake', '/yakuake/sessions', 'runCommand'] argc = len(sys.argv) if argc < 4: text = "No command to run." errorCommand.append(text) subprocess.Popen(errorCommand).communicate() else: command = " ".join(sys.argv[3:]) runCommand.append(command) subprocess.Popen(toggleCommand).communicate() subprocess.Popen(runCommand).communicate()