├── .gitignore ├── CHANGES.md ├── LICENSE.md ├── README.md └── inwee /.gitignore: -------------------------------------------------------------------------------- 1 | *.sw? 2 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | Changelog 2 | ========= 3 | 4 | 0.2.0 (2017-01-21) 5 | ------------------ 6 | - NEW: Support FIFO path for WeeChat 1.7. 7 | - NEW: Specify FIFO path with `-f` or `--fifo` option. 8 | 9 | 0.1.0 (2015-01-20) 10 | ------------------ 11 | - NEW: Send text or commands in file to WeeChat. 12 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | Copyright (c) 2015-2017 Susam Pal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | InWee 2 | ===== 3 | InWee is a convenience wrapper script to send text messages and commands 4 | to WeeChat from shell or from within WeeChat via 5 | [WeeChat's FIFO pipe][1]. 6 | 7 | [![Download][SHIELD]][DOWNLOAD] 8 | 9 | [1]: http://www.weechat.org/files/doc/stable/weechat_user.en.html#fifo_plugin 10 | [SHIELD]: https://img.shields.io/badge/download-inwee-brightgreen.svg 11 | [DOWNLOAD]: https://github.com/susam/inwee/releases/download/0.2.0/inwee 12 | 13 | 14 | Contents 15 | -------- 16 | * [Necessity](#necessity) 17 | * [Installation](#installation) 18 | * [Manual](#manual) 19 | * [With wget](#with-wget) 20 | * [With git](#with-git) 21 | * [Getting Started](#getting-started) 22 | * [Using from Shell](#using-from-shell) 23 | * [Using from WeeChat](#using-from-weechat) 24 | * [Without InWee](#without-inwee) 25 | * [Credits](#credits) 26 | * [License](#license) 27 | * [Contact](#contact) 28 | 29 | 30 | Necessity 31 | --------- 32 | WeeChat keeps configuration in several .conf files in ~/.weechat 33 | directory. The configuration can be edited with WeeChat `/set` commands. 34 | Unfortunately, the same .conf files contain both the default 35 | configuration options that have not been altered by the user as well as 36 | custom configuration options specified by the user. This makes it 37 | clumsy to pick only the user specific custom configuration, save it in a 38 | separate file as backup, so that it can be applied later when needed, 39 | say after reinstallation of operating system or on a new system. 40 | 41 | One solution to this problem is to keep all the user specific 42 | configuration in the form of `/set` commands in a file, say 43 | settings.txt, as shown below. 44 | 45 | /set irc.server.freenode.nicks humpty,humpty_ 46 | /set irc.server.freenode.username humpty 47 | /set irc.server.freenode.realname "Humpty Dumpty" 48 | /set irc.server.freenode.autojoin #weechat,#test,#freenode 49 | /save irc 50 | /print Saved configuration 51 | 52 | This file can be fed into WeeChat's FIFO pipe while WeeChat is running 53 | using the following shell command. 54 | 55 | sed 's/^/*/' settings.txt > ~/.weechat/weechat_fifo_* 56 | 57 | The command first prefixes every line in the file with `*` (asterisk) 58 | because that is how WeeChat's FIFO pipe expects the input to be. Then it 59 | feeds the lines to WeeChat's FIFO pipe. 60 | 61 | If the above solution is good enough for you, then there is no real 62 | reason to use InWee. In that case, you can skip directly to the 63 | [Without InWee](#without-inwee) section and read about how everything 64 | that can be done with InWee can also be done without it. 65 | 66 | InWee only makes the above solution more convenient. For example, the 67 | commands in settings.txt can be fed to WeeChat with InWee by entering 68 | the following command while WeeChat is running. 69 | 70 | inwee settings.txt 71 | 72 | This simple command offers the following advantages over the earlier 73 | command involving sed command's output redirection. 74 | 75 | 1. It is simpler and easier to type. 76 | 2. It automatically prefixes every command in the input file with `*` 77 | (asterisk). 78 | 3. It automatically checks if the FIFO pipe exists at ~/.weechat. If 79 | it does not exist, it quits with an error. 80 | 4. It supports comments in the input file. Any line in the file where 81 | `#` (hash) is the first non-whitespace character is ignored as 82 | comment. 83 | 84 | WeeChat can execute external commands with the `/exec` command and it 85 | accepts the `-r` option to run commands after startup, so when WeeChat 86 | is not running, it can be made to run the commands in settings.txt after 87 | starting up as follows. 88 | 89 | weechat -r '/exec inwee settings.txt' 90 | 91 | 92 | Getting Started 93 | --------------- 94 | InWee is a single-file executable script: [`inwee`][DOWNLOAD]. 95 | 96 | Copy it to a directory specified in the PATH environment variable and 97 | make it executable: `chmod u+x inwee`. In many Linux systems, 98 | `/usr/local/bin` or `~/bin` is a good location to copy this script to. 99 | 100 | InWee may be invoked directly from the shell using the `inwee` command 101 | or from WeeChat using its `/exec` command. Several examples of both 102 | ways of usage are provided below. 103 | 104 | ### Using from Shell ### 105 | The following list shows various ways to use InWee from the shell. 106 | 107 | 1. Here is a typical example of using InWee from the shell. Say 108 | you are in a Linux support channel such as ##linux, #debian, 109 | #centos, etc. and you need to share the network controller details 110 | of your system with the channel while asking for help. To do this 111 | with InWee, you can run the commands you need to get this 112 | output and pipe it into `inwee` as shown below to send the output 113 | to the current buffer in WeeChat. 114 | 115 | lspci | grep -i network | inwee 116 | 117 | 2. Here are some general examples of sending text or commands from 118 | standard input to the current buffer in WeeChat. Any text or 119 | WeeChat command you pipe into the standard input of InWee is sent 120 | to WeeChat. 121 | 122 | echo hello world | inwee 123 | echo '/join #test' | inwee 124 | echo /part | inwee 125 | echo /nick newnick | inwee 126 | 127 | Note that comments in shell begin with `#`, therefore the WeeChat 128 | `join` command in the second shell command above is quoted. Any 129 | text beginning with `/` (slash), followed by a word and whitespace 130 | is considered as a WeeChat command. All other text is considered as 131 | text messages to be sent on chat. 132 | 133 | 3. Multiple texts and/or commands must be separated by a newline. 134 | 135 | printf '/join #test\n/join #flood\n' | inwee 136 | 137 | Every line of text or command must also end with a newline. Since 138 | `printf` does not append a newline in the end like `echo` does, we 139 | end the strings passed to `printf` in each command above with a 140 | newline. 141 | 142 | Blank lines, lines that consist only of whitespace characters and 143 | lines that contain `#`, i.e. hash, as the first non-whitespace 144 | character are ignored. 145 | 146 | 4. The previous example may also be conveniently written in the 147 | following manner. 148 | 149 | echo '/join #flood 150 | /join #test' | inwee 151 | 152 | 5. Text or commands may be sent to a specific WeeChat buffer with the 153 | `-b` or `--buffer` option. 154 | 155 | echo hello world | inwee -b 'irc.freenode.#test' 156 | 157 | With the above command, the text is sent to #test channel even when 158 | this channel is not in the current buffer. 159 | 160 | 6. Send text or commands from a file by specifying the path to the 161 | file as an argument. 162 | 163 | inwee input.txt 164 | 165 | The file may contain one or more lines of text or commands. Blank 166 | lines, lines that consist only of whitespace characters and lines 167 | that contain `#`, i.e. hash, as the first non-whitespace character 168 | are ignored. Therefore, `#` can be used to begin single line 169 | comments. 170 | 171 | 7. To use a custom FIFO path with WeeChat. 172 | 173 | echo "hello world" | inwee --fifo "$HOME/.weechat/weechat_fifo" 174 | 175 | This will make inwee use a custom FIFO path to communicate with 176 | WeeChat. 177 | 178 | ### Using from WeeChat ### 179 | From WeeChat's perspective, InWee is an external command that can be 180 | invoked with the `inwee` command. WeeChat's `/exec` command executes 181 | external commands inside WeeChat and displays the output locally, or 182 | sends it to a buffer. Therefore, `inwee` can be used within WeeChat 183 | using WeeChat's `/exec` command. This section shows a few such examples. 184 | Note that unlike in the previous section where the command examples are 185 | meant to be run in the shell, the commands below are meant to be run 186 | inside WeeChat directly. 187 | 188 | 1. Send text or commands from a file to the current buffer. 189 | 190 | /exec inwee input.txt 191 | 192 | 2. Send text or commands from a file to #test channel. 193 | 194 | /exec inwee -b irc.freenode.#test input.txt 195 | 196 | 197 | Without InWee 198 | ------------- 199 | Everything that can be done with InWee can be done without it, although 200 | it can be a little clumsy for some cases. This section is written for 201 | those who do not want to use InWee but do what can be done with InWee by 202 | working directly with WeeChat's FIFO pipe or WeeChat's `/exec` command. 203 | WeeChat's FIFO pipe is located at ~/.weechat by default and named as 204 | `weechat_fifo_` where `` stands for the PID of the currently 205 | running instance of WeeChat. 206 | 207 | Assuming WeeChat is currently running and there is a FIFO pipe for 208 | WeeChat at ~/.weechat, the list below explains how something that can be 209 | done with InWee can also be done without it. 210 | 211 | 1. Send the output of a shell command from shell to WeeChat. 212 | 213 | With InWee: 214 | 215 | lspci | grep -i network | inwee 216 | 217 | Without InWee: 218 | 219 | lspci | grep -i network | sed 's/^/*/' > ~/.weechat/weechat_fifo_* 220 | 221 | 2. Send a message to a WeeChat channel from shell. 222 | 223 | With InWee: 224 | 225 | echo hello world | inwee 226 | 227 | Without InWee: 228 | 229 | echo '*hello world' > ~/.weechat/weechat_fifo_* 230 | 231 | 3. Send a WeeChat command from shell to WeeChat. 232 | 233 | With InWee: 234 | 235 | echo /nick newnick | inwee 236 | 237 | Without InWee: 238 | 239 | echo */nick newnick > ~/.weechat/weechat_fifo_* 240 | 241 | 4. Send text or command to a specific WeeChat buffer from shell. 242 | 243 | With InWee: 244 | 245 | echo hello world | inwee -b irc.freenode#test 246 | 247 | Without InWee: 248 | 249 | echo 'irc.freenode.#test *hello world' > ~/.weechat/weechat_fifo_* 250 | 251 | 5. Send text or commands in a file to WeeChat from shell. 252 | 253 | With InWee: 254 | 255 | inwee input.txt 256 | 257 | Without InWee: 258 | 259 | sed 's/^/*/' input.txt > ~/.weechat/weechat_fifo_* 260 | 261 | 6. Send the output of a simple shell command in WeeChat to a channel. 262 | 263 | With InWee: 264 | 265 | /exec -sh uname -a | inwee 266 | 267 | Without InWee: 268 | 269 | /exec -o uname -a 270 | 271 | The `-sh` option is necessary in the first command to ensure that 272 | the `/exec` command uses the shell to execute the specified shell 273 | command. This ensures that `|` is interpreted as the pipeline 274 | operator by the shell. Without the `-sh` option, the `/exec` 275 | command executes the specified command itself and it would 276 | interpret `|` as just another argument. 277 | 278 | The `-o` command is necessary in the second command to ensure that 279 | the output of the shell command is sent to the channel as chat 280 | message. Without this option, the output is only printed locally 281 | and not shared with the channel. There is no need of `-o` option in 282 | the first command because InWee takes care of sending the output as 283 | text message to the channel's buffer via WeeChat's FIFO pipe. 284 | 285 | 7. Send output to a specific channel in WeeChat. 286 | 287 | With InWee: 288 | 289 | /exec -sh uname -a | inwee -b irc.freenode.#test 290 | 291 | Without InWee: 292 | 293 | /exec -sh -o -buffer irc.freenode.#test uname -a 294 | 295 | 8. Send the output of a little more complex shell command, say 296 | involving pipeline operators, in WeeChat. 297 | 298 | With InWee: 299 | 300 | /exec -sh lspci | grep -i network | inwee 301 | 302 | Without InWee: 303 | 304 | /exec -sh -o lspci | grep -i network 305 | 306 | 9. Send text or commands in a file to the current buffer in WeeChat. 307 | 308 | With InWee: 309 | 310 | /exec inwee input.txt 311 | 312 | Without InWee: 313 | 314 | /exec -sh sed 's/^/*/' input.txt > ~/.weechat/weechat_fifo_$(pidof weechat) 315 | 316 | 317 | Credits 318 | ------- 319 | Thank you, [Dom Rodriguez][C1], for adding support for WeeChat 1.7. 320 | 321 | [C1]: https://github.com/shymega 322 | 323 | 324 | License 325 | ------- 326 | This is free and open source software. You can use, copy, modify, 327 | merge, publish, distribute, sublicense, and/or sell copies of it, 328 | under the terms of the MIT License. See [LICENSE.md][L] for details. 329 | 330 | This software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, 331 | express or implied. See [LICENSE.md][L] for details. 332 | 333 | [L]: LICENSE.md 334 | 335 | 336 | Contact 337 | ------- 338 | To report bugs, suggest improvements, or ask questions, please create a 339 | new issue at . 340 | -------------------------------------------------------------------------------- /inwee: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # The MIT License (MIT) 4 | # 5 | # Copyright (c) 2015-2017 Susam Pal 6 | # 7 | # Permission is hereby granted, free of charge, to any person obtaining 8 | # a copy of this software and associated documentation files (the 9 | # "Software"), to deal in the Software without restriction, including 10 | # without limitation the rights to use, copy, modify, merge, publish, 11 | # distribute, sublicense, and/or sell copies of the Software, and to 12 | # permit persons to whom the Software is furnished to do so, subject to 13 | # the following conditions: 14 | # 15 | # The above copyright notice and this permission notice shall be 16 | # included in all copies or substantial portions of the Software. 17 | # 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | 26 | 27 | VERSION=0.2.0 28 | 29 | COPYRIGHT="Copyright (c) 2015-2017 Susam Pal" 30 | 31 | LICENSE_URL="http://susam.in/licenses/mit/" 32 | SUPPORT_URL="https://github.com/susam/inwee/issues" 33 | 34 | NOTICE=" 35 | This is free and open source software. You can use, copy, modify, 36 | merge, publish, distribute, sublicense, and/or sell copies of it, 37 | under the terms of the MIT License. You can obtain a copy of the 38 | MIT License at <$LICENSE_URL>. 39 | 40 | This software is provided \"AS IS\", WITHOUT WARRANTY OF ANY KIND, 41 | express or implied. See the MIT License for details." 42 | 43 | NAME=${0##*/} 44 | 45 | 46 | # Starting point of this script. 47 | # 48 | # Arguments: 49 | # arg...: All arguments this script was invoked with 50 | main() 51 | { 52 | parse_arguments "$@" 53 | 54 | if [ -n "$fifo_path" ] 55 | then 56 | # Find FIFO path via the -f|--fifo argument. 57 | if [ -e "$fifo_path" ] 58 | then 59 | debug_output Found FIFO socket on custom argument. 60 | fifo="$fifo_path" 61 | else 62 | quit Cannot find specified FIFO path. 63 | fi 64 | else 65 | # Find FIFO pipe in ~/.weechat. 66 | for f in ~/.weechat/weechat_fifo* 67 | do 68 | if [ "$f" != ~/.weechat/weechat_fifo"*" ] 69 | then 70 | debug_output Found FIFO socket in ~/.weechat. 71 | fifo="$f" 72 | break 73 | fi 74 | done 75 | fi 76 | 77 | if [ -z "$fifo" ] 78 | then 79 | quit Cannot find Weechat FIFO pipe. 80 | fi 81 | 82 | debug_output FIFO: "$fifo" 83 | 84 | # Read from standard input if input file is not specified. 85 | if [ -z "$input_file" ] 86 | then 87 | input_file=- 88 | fi 89 | 90 | # Skip comments and lines consisting only of whitespaces and feed 91 | # the remaining lines to WeeChat's FIFO. 92 | grep -v "^[[:space:]]*#" "$input_file" | grep "[[:graph:]]" | 93 | while read -r line 94 | do 95 | debug_output READ: "$line" 96 | 97 | line="*$line" 98 | if [ -n "$buffer" ] 99 | then 100 | line="$buffer $line" 101 | fi 102 | printf "%s\n" "$line" > "$fifo" 103 | 104 | debug_output SENT: "$line" 105 | done 106 | } 107 | 108 | 109 | # Parse command line arguments passed to this script. 110 | # 111 | # Arguments: 112 | # arg...: All arguments this script was invoked with 113 | # 114 | # Errors: 115 | # If invalid arguments are specified, this function causes the script 116 | # to exit with an error. 117 | parse_arguments() 118 | { 119 | while [ "$#" -gt 0 ] 120 | do 121 | case $1 in 122 | -b | --buffer) 123 | [ -n "$2" ] || quit \""$1"\" must be followed by buffer name. 124 | buffer="$2" 125 | shift 2 126 | ;; 127 | -f | --fifo) 128 | [ -n "$2" ] || quit \""$1"\" must be followed by FIFO path. 129 | fifo_path="$2" 130 | shift 2 131 | ;; 132 | -d | --debug) 133 | debug=yes 134 | shift 135 | ;; 136 | -h | --help) 137 | show_help 138 | exit 139 | ;; 140 | -v | --version) 141 | show_version 142 | exit 143 | ;; 144 | -?*) 145 | quit Unknown option \""$1"\". 146 | ;; 147 | *) 148 | if [ -z "$input_file" ] 149 | then 150 | input_file="$1" 151 | shift 152 | else 153 | quit Surplus argument \""$1"\". 154 | fi 155 | esac 156 | done 157 | } 158 | 159 | 160 | # Output message only in debug mode. 161 | # 162 | # Arguments: 163 | # string...: String to print to standard output stream. 164 | debug_output() 165 | { 166 | if [ "$debug" = "yes" ] 167 | then 168 | printf "%s\n" "$*" 169 | fi 170 | } 171 | 172 | 173 | # Terminate the script with an error message. 174 | # 175 | # Arguments: 176 | # string...: String to print to standard error stream. 177 | # 178 | # Errors: 179 | # Unconditionally cause the script to terminate with an error message 180 | # and exit code 1. 181 | quit() 182 | { 183 | printf "%s: %s\n" "$NAME" "$*" >&2 184 | exit 1 185 | } 186 | 187 | 188 | # Show help. 189 | show_help() 190 | { 191 | printf "%s\n" \ 192 | "Usage: $NAME [-b BUFFER] [-f FIFO] [-d] [-c] [-h] [-v] [FILE] 193 | 194 | Read text or commands from standard input, FILE or from the output of a 195 | shell command, and send it to WeeChat's FIFO pipe. If FILE is not 196 | specified or if it is specified as '-', i.e. a hyphen, then text and 197 | commands are read from standard input. 198 | 199 | Options: 200 | -b, --buffer BUFFER Buffer to send text or command to. 201 | -f, --fifo FIFO FIFO Path to send commands to. 202 | -d, --debug Show diagnostic information. 203 | -h, --help Show this help and exit. 204 | -v, --version Show version and exit. 205 | 206 | Report bugs to <$SUPPORT_URL>." 207 | } 208 | 209 | 210 | # Show version and copyright. 211 | show_version() 212 | { 213 | printf "%s %s\n%s\n%s\n" "$NAME" "$VERSION" "$COPYRIGHT" "$NOTICE" 214 | } 215 | 216 | 217 | # Start. 218 | main "$@" 219 | --------------------------------------------------------------------------------