weechat-open-url 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. ## List of [wireguard] hosts to try for GUI viewing
  13. ## laptop, tablet
  14. host_list="10.0.10.3 10.0.10.6"
  15. function status_check {
  16. host_return="none"
  17. for h in ${host_list}; do
  18. ## Reverse SSH Tunnel Check
  19. ## VPN Check, nc faster than a ping
  20. if [ $(nc -z -w 1 ${h} 22 && echo 1 || echo 0) == 1 ]; then
  21. ## If host is reachable, set var and end testing
  22. host_return=${h}
  23. break
  24. fi
  25. done
  26. if [ ${host_return} != "none" ]; then
  27. host_use=${host_return}
  28. else
  29. host_use="none"
  30. fi
  31. }
  32. function source-ssh {
  33. ## Source SSH settings, if applicable
  34. local SSH_ENV="${HOME}/.ssh/environment"
  35. if [ -f "${SSH_ENV}" ]; then
  36. . "${SSH_ENV}" > /dev/null
  37. ps -ef 2>/dev/null | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
  38. pstree -up ${USER} 2>/dev/null | grep ${SSH_AGENT_PID} | grep ^ssh-agent > /dev/null || {
  39. __start_agent;
  40. }
  41. }
  42. fi
  43. }
  44. function open-image {
  45. if [[ ${host_use} != "none" ]]; then
  46. source-ssh
  47. ssh ${host_use} env DISPLAY=:0 feh -x -F --auto-zoom "'$1'"
  48. else
  49. tmux new-window -c ~ bash -c "timg $1 && read -s -n 1 -p 'Press Any Key to Exit'"
  50. fi
  51. }
  52. function open-url {
  53. if [[ ${host_use} != "none" ]]; then
  54. #ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' /usr/share/qutebrowser/scripts/open_url_in_instance.sh "'$1'"
  55. source-ssh
  56. ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' xdg-open "'$1'"
  57. else
  58. tmux new-window -c ~ bash -c "w3m '$1'"
  59. fi
  60. }
  61. function open-video {
  62. ## Only exec if laptop connected
  63. if [[ ${host_use} != "none" ]]; then
  64. source-ssh
  65. ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' mpv --profile=rem "'$1'"
  66. fi
  67. }
  68. function open-sound {
  69. ## Only exec if laptop connected
  70. if [[ ${host_use} != "none" ]]; then
  71. source-ssh
  72. ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' mpv --profile=pod "'$1'"
  73. fi
  74. }
  75. status_check
  76. case $(echo "$1" | tr A-Z a-z) in
  77. *.jpg|*.jpeg|*.png|*.gif|*.svg|*:large)
  78. open-image "$1"
  79. ;;
  80. *youtube.com/*|*youtu.be/*|*.gifv|*.mp4|*.webm)
  81. open-video "$1"
  82. ;;
  83. *.mp3)
  84. open-sound "$1"
  85. ;;
  86. *)
  87. open-url "$1"
  88. ;;
  89. esac