weechat-open-url 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. ## A script to handle opening urls from weechat in tmux, possibly on host maching
  3. ## Inspired by https://toxicfrog.github.io/remote-url-handling-in-weechat-and-tmux/
  4. ##
  5. ## This script is relying on the laptop opening a reverse ssh tunnel to the server
  6. ##
  7. ## Add binding for M-o #, for 1 through 9 (10 can be 0)
  8. ## /key bind meta-o1 /url_hint_replace /exec -bg weechat-open-url {url1}
  9. ## /key bind meta-o2 /url_hint_replace /exec -bg weechat-open-url {url2}
  10. ##
  11. ## You can chose between a split (split-window -h) or new (new-window) in tmux
  12. ## Reverse SSH Tunnel Check
  13. #laptop_status="$(nc -z localhost 2001 && echo 1 || echo 0)"
  14. ## VPN Check, nc faster than a ping
  15. laptop_status="$(nc -z -w 1 10.0.10.3 22 && echo 1 || echo 0)"
  16. laptop_host="wgdoom"
  17. function source-ssh {
  18. ## Source SSH settings, if applicable
  19. local SSH_ENV="${HOME}/.ssh/environment"
  20. if [ -f "${SSH_ENV}" ]; then
  21. . "${SSH_ENV}" > /dev/null
  22. ps -ef 2>/dev/null | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
  23. pstree -up ${USER} 2>/dev/null | grep ${SSH_AGENT_PID} | grep ^ssh-agent > /dev/null || {
  24. __start_agent;
  25. }
  26. }
  27. fi
  28. }
  29. function open-image {
  30. if [ ${laptop_status} == 1 ]; then
  31. source-ssh
  32. ssh ${laptop_host} env DISPLAY=:0 feh -x -F --auto-zoom "'$1'"
  33. else
  34. tmux new-window -c ~ bash -c "timg $1 && read -s -n 1 -p 'Press Any Key to Exit'"
  35. fi
  36. }
  37. function open-url {
  38. if [ ${laptop_status} == 1 ]; then
  39. #ssh ${laptop_host} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' /usr/share/qutebrowser/scripts/open_url_in_instance.sh "'$1'"
  40. source-ssh
  41. ssh ${laptop_host} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' xdg-open "'$1'"
  42. else
  43. tmux new-window -c ~ bash -c "w3m '$1'"
  44. fi
  45. }
  46. function open-video {
  47. ## Only exec if laptop connected
  48. if [ ${laptop_status} == 1 ]; then
  49. source-ssh
  50. ssh ${laptop_host} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' mpv --profile=rem "'$1'"
  51. fi
  52. }
  53. function open-sound {
  54. ## Only exec if laptop connected
  55. if [ ${laptop_status} == 1 ]; then
  56. source-ssh
  57. ssh ${laptop_host} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' mpv --profile=pod "'$1'"
  58. fi
  59. }
  60. case $(echo "$1" | tr A-Z a-z) in
  61. *.jpg|*.jpeg|*.png|*.gif|*.svg|*:large)
  62. open-image "$1"
  63. ;;
  64. *youtube.com/*|*youtu.be/*|*.gifv|*.mp4|*.webm)
  65. open-video "$1"
  66. ;;
  67. *.mp3)
  68. open-sound "$1"
  69. ;;
  70. *)
  71. open-url "$1"
  72. ;;
  73. esac