|
| 1 | +# Copyright (c) 2025 by Kamil Wiśniewski <[email protected]> |
| 2 | +# |
| 3 | +# This script sends auto commands on start |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation; either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# |
| 16 | +#[Change Log] |
| 17 | +# |
| 18 | +# 0.3 : Implemented the hook_completion_cb to provide autocompletion for stored commands. |
| 19 | +# : Added Guides. |
| 20 | +# : Added <del> by Index or String values. |
| 21 | +# : Added "time" command to set timer for sending commands after start. |
| 22 | +# |
| 23 | +# 0.2 : added list, add, delete, clear commands |
| 24 | +# : added save and load commands functions |
| 25 | +# |
| 26 | +# 0.1 : Initial release |
| 27 | + |
| 28 | +import weechat |
| 29 | + |
| 30 | + |
| 31 | +SCRIPT_NAME = "auto_commands" |
| 32 | +SCRIPT_AUTHOR = "Tomteipl" |
| 33 | +SCRIPT_VERSION = "0.3" |
| 34 | +SCRIPT_LICENSE = "GPL3" |
| 35 | +SCRIPT_DESC = "Send auto commands on start" |
| 36 | + |
| 37 | +weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", "") |
| 38 | + |
| 39 | + |
| 40 | +# Guides |
| 41 | +help = """ |
| 42 | + Usage:\n |
| 43 | +
|
| 44 | + IMPORTANT: Commands are sent on client start only once ! |
| 45 | + /autocommands add <command> - adds command to the list. You can use spaces and special signs. |
| 46 | + /autocommands del <number/string> - deletes command from the list. You can use /autocommands list to see the numbers or tap TAB for auto completion. |
| 47 | + /autocommands list - shows the list of commands with index. |
| 48 | + /autocommands clear - clears the list of commands. |
| 49 | + /autocommands time <miliseconds> - 1sec = 1000ms, sets the timer for hook in miliseconds. Default value 10000ms = 10 sec. |
| 50 | + """ |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +commands = [] # Commands are stored here |
| 55 | + |
| 56 | +def load_commands(): |
| 57 | + global commands |
| 58 | + saved_commands = weechat.config_get_plugin("commands") |
| 59 | + commands = saved_commands.split(",") if saved_commands else [] |
| 60 | + |
| 61 | +def save_commands(): |
| 62 | + weechat.config_set_plugin("commands", ",".join(commands)) |
| 63 | + |
| 64 | +# adds commands to the list |
| 65 | +def add_command(data, buffer, args): |
| 66 | + commands.append(args) |
| 67 | + save_commands() |
| 68 | + weechat.prnt(buffer, f"Command '{args}' added!") |
| 69 | + return weechat.WEECHAT_RC_OK |
| 70 | + |
| 71 | +def send_auto_commands(data, buffer): |
| 72 | + for command in commands: |
| 73 | + weechat.command("", command) |
| 74 | + weechat.prnt("", f"Command '{command}' sent!") |
| 75 | + return weechat.WEECHAT_RC_OK |
| 76 | + |
| 77 | + |
| 78 | +# [ ---COMMANDS--- ] |
| 79 | +def commands_cb(data, buffer, args): |
| 80 | + if args.startswith("list"): |
| 81 | + weechat.prnt(buffer, "Current commands: \n") |
| 82 | + for i, command in enumerate(commands): |
| 83 | + weechat.prnt(buffer, f"{i + 1}. {command}") |
| 84 | + return weechat.WEECHAT_RC_OK |
| 85 | + |
| 86 | + elif args.startswith("add"): |
| 87 | + add_command(data, buffer, args[len("add "):]) |
| 88 | + return weechat.WEECHAT_RC_OK |
| 89 | + |
| 90 | + elif args.startswith("del"): |
| 91 | + try: |
| 92 | + arg_value = " ".join(args.split()[1:]) |
| 93 | + |
| 94 | + if arg_value.isdigit(): # delete command by index (example: /autocommands del 1) |
| 95 | + index = int(args.split()[1]) -1 |
| 96 | + |
| 97 | + if 0 <= index < len(commands): |
| 98 | + del_command = commands.pop(index) |
| 99 | + save_commands() |
| 100 | + weechat.prnt(buffer, f"Command '{del_command}' deleted!") |
| 101 | + return weechat.WEECHAT_RC_OK |
| 102 | + |
| 103 | + else: |
| 104 | + weechat.prnt(buffer, "Invalid command number!") |
| 105 | + return weechat.WEECHAT_RC_OK |
| 106 | + |
| 107 | + elif arg_value in commands: # delete command by string, you can use autocomplete (example: /autocommands del /join #channel) |
| 108 | + commands.remove(arg_value) |
| 109 | + save_commands() |
| 110 | + weechat.prnt(buffer, f"Command '{arg_value}' deleted!") |
| 111 | + return weechat.WEECHAT_RC_OK |
| 112 | + |
| 113 | + else: |
| 114 | + weechat.prnt(buffer, "Invalid command number or string!") |
| 115 | + return weechat.WEECHAT_RC_OK |
| 116 | + |
| 117 | + except (ValueError, IndexError): |
| 118 | + weechat.prnt(buffer, "Invalid command number!") |
| 119 | + return weechat.WEECHAT_RC_OK |
| 120 | + |
| 121 | + elif args.startswith("time"): # set timer for hook |
| 122 | + try: |
| 123 | + new_time = int(args.split()[1]) |
| 124 | + weechat.config_set_plugin("timer", str(new_time)) |
| 125 | + weechat.prnt(buffer, f"Timer set to {new_time} ms!") |
| 126 | + |
| 127 | + except (ValueError, IndexError): |
| 128 | + weechat.prnt(buffer, "Invalid time value! /autocommands time <miliseconds>") |
| 129 | + |
| 130 | + return weechat.WEECHAT_RC_OK |
| 131 | + |
| 132 | + elif args.startswith("clear"): |
| 133 | + commands.clear() |
| 134 | + weechat.prnt(buffer, "Commands cleared!") |
| 135 | + return weechat.WEECHAT_RC_OK |
| 136 | + |
| 137 | + else: |
| 138 | + weechat.prnt(buffer, f"{help}") |
| 139 | + return weechat.WEECHAT_RC_OK |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | +# Commands completion when using <del> |
| 144 | +def hook_completion_cb(data, completion, buffer, completion_item): |
| 145 | + for command in commands: |
| 146 | + weechat.completion_list_add(completion_item, command, 0, weechat.WEECHAT_LIST_POS_SORT) |
| 147 | + return weechat.WEECHAT_RC_OK |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +load_commands() |
| 152 | + |
| 153 | +weechat.hook_completion("autocommands_cmds", "List auto commands", "hook_completion_cb", "") |
| 154 | +weechat.hook_command( |
| 155 | + "autocommands", |
| 156 | + "List auto commands", |
| 157 | + "", |
| 158 | + "", |
| 159 | + "list || add || del %(autocommands_cmds) || clear || time", |
| 160 | + "commands_cb", |
| 161 | + "", |
| 162 | +) |
| 163 | + |
| 164 | +timer = int(weechat.config_get_plugin("timer") or 10000) |
| 165 | +weechat.hook_timer(timer, 0, 1, "send_auto_commands", "") |
0 commit comments