weechat-open-url 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.7"
  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. ssh ${host_use} env DISPLAY=:0 pqiv "'$1'"
  49. #ssh ${host_use} env DISPLAY=:0 curl -s "'$1'" \| env DISPLAY=:0 swayimg -
  50. #ssh ${host_use} env DISPLAY=:0 curl -s "'$1'" \| env DISPLAY=:0 swayimg -
  51. else
  52. tmux new-window -c ~ bash -c "timg $1 && read -s -n 1 -p 'Press Any Key to Exit'"
  53. fi
  54. }
  55. function open-url {
  56. if [[ ${host_use} != "none" ]]; then
  57. #ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' /usr/share/qutebrowser/scripts/open_url_in_instance.sh "'$1'"
  58. source-ssh
  59. ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' xdg-open "'$1'"
  60. else
  61. tmux new-window -c ~ bash -c "w3m '$1'"
  62. fi
  63. }
  64. function open-video {
  65. ## Only exec if laptop connected
  66. if [[ ${host_use} != "none" ]]; then
  67. source-ssh
  68. ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' mpv --profile=rem "'$1'"
  69. fi
  70. }
  71. function open-sound {
  72. ## Only exec if laptop connected
  73. if [[ ${host_use} != "none" ]]; then
  74. source-ssh
  75. ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' mpv --profile=pod "'$1'"
  76. fi
  77. }
  78. status_check
  79. case $(echo "$1" | tr A-Z a-z) in
  80. *.jpg|*.jpeg|*.png|*.gif|*.svg|*:large|*.webp|*.png/raw)
  81. open-image "$1"
  82. ;;
  83. *youtube.com/*|*youtu.be/*|*.gifv|*.mp4|*.webm)
  84. open-video "$1"
  85. ;;
  86. *.mp3)
  87. open-sound "$1"
  88. ;;
  89. *)
  90. open-url "$1"
  91. ;;
  92. esac