saturnin-iam 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #!/bin/bash
  2. . $(ffoom path)
  3. ffoo import pretty
  4. ffoo import recon
  5. ffoo import saturnin_zleep
  6. #
  7. # self help
  8. #
  9. available_commands() {
  10. echo afk
  11. echo at
  12. echo back
  13. echo bbl
  14. echo bbldone
  15. echo deaf
  16. echo deafnomore
  17. echo gone
  18. echo listening
  19. echo ooo
  20. echo wfh
  21. echo writing
  22. echo undocking
  23. echo zleeping
  24. }
  25. usage() {
  26. cmd_hint=$(
  27. available_commands \
  28. | head -c -1 \
  29. | tr '\n' '|'
  30. )
  31. usage_is "$cmd_hint"
  32. }
  33. #
  34. # querying
  35. #
  36. familiars_at() {
  37. #
  38. # daemons specific to $1 location plus the rest
  39. #
  40. test -n "$1" || usagef "location"
  41. saturnin conf -p iam.running.at.$1
  42. saturnin conf -p iam.running.at.ANYPLACE
  43. }
  44. where_i_am() {
  45. #
  46. # what is my physical location?
  47. #
  48. if=$(saturnin conf -p iam.using.if)
  49. gwmac=$(arp | grep "^gateway\\>.*\\<$if\$" | tr ' ' '\n' | grep :)
  50. saturnin conf -p iam.seeing.gw.$gwmac || echo OUT
  51. }
  52. start_familiar() {
  53. #
  54. # start familiar as indicated by conf file (unless running)
  55. #
  56. local f=$1
  57. pids_matching $f >/dev/null && return 0
  58. local like=$(saturnin conf -p iam.starting.like.this.$f)
  59. debug -v f like
  60. if test -n "$like";
  61. then
  62. $like &
  63. else $f &
  64. fi
  65. }
  66. set_status() {
  67. #
  68. # set public status $1 by command in conf file
  69. #
  70. local what=$1
  71. saturnin conf -p iam.saying.like.this.$what | bash
  72. }
  73. sound_is_muted() {
  74. #
  75. # At least one playback channel is muted
  76. #
  77. local master_status="$(amixer get Master)"
  78. echo "$master_status" \
  79. | grep "Playback channels" \
  80. | sed -e 's/.*: //; s/ - /\n/' \
  81. | while read chname
  82. do
  83. grep -e "^ *$chname" <<<"$master_status"
  84. done \
  85. | grep -qse '\[off\]$'
  86. }
  87. #
  88. # hooks
  89. #
  90. run_hook() {
  91. local name=$1
  92. local cmd=$2
  93. local arg=$3
  94. saturnin conf -p iam.hooking.$name.$cmd \
  95. | SATURNIN_IAM_CMD=$cmd \
  96. SATURNIN_IAM_ARG=$arg \
  97. bash
  98. }
  99. #
  100. # subcommand handlers
  101. #
  102. i_am_afk() {
  103. #
  104. # away from keyboard; blocks until i'm back again
  105. #
  106. mocp --pause
  107. set_status "afk"
  108. i_am_bbldone # make sure to set lang to default before locking
  109. slock
  110. set_status "atk"
  111. }
  112. i_am_at() {
  113. #
  114. # Just say where I am
  115. #
  116. where_i_am
  117. }
  118. i_am_back() {
  119. #
  120. # returning to work (should be called by other subcommands)
  121. #
  122. local f
  123. for f in $(familiars_at $(where_i_am));
  124. do
  125. start_familiar $f
  126. done
  127. set_status "back"
  128. klist -s || urxvt -e kinit
  129. }
  130. i_am_bbl() {
  131. #
  132. # changing language
  133. #
  134. local cur=$(setxkbmap -v | grep '^symbols: ' | cut -d+ -f2)
  135. local all="$(saturnin conf -p iam.typing.lang)"
  136. local def="$(saturnin conf -1 -p iam.typing.lang)"
  137. local nxt=$(
  138. echo -e "$all\n$all" \
  139. | grep -m 1 -A 1 $cur \
  140. | tail -1
  141. )
  142. test -z "$nxt" && nxt=$def
  143. test -z "$nxt" && nxt=us
  144. debug -v all def cur nxt
  145. setxkbmap "$nxt"
  146. }
  147. i_am_bbldone() {
  148. #
  149. # changing language back to normal
  150. #
  151. setxkbmap "$(saturnin conf -1 -p iam.typing.lang)"
  152. }
  153. i_am_deaf() {
  154. #
  155. # so turn volume up
  156. #
  157. amixer -q sset Master 5%+
  158. }
  159. i_am_deafnomore() {
  160. #
  161. # so turn volume back down
  162. #
  163. amixer -q sset Master 5%-
  164. }
  165. i_am_gone() {
  166. #
  167. # gone fishin'
  168. #
  169. kdestroy
  170. ssh-add -D
  171. set_status "gone"
  172. i_am_afk
  173. i_am_back
  174. }
  175. i_am_listening() {
  176. #
  177. # to the real world or to the box
  178. #
  179. if sound_is_muted;
  180. then
  181. amixer -q sset Master unmute;
  182. else
  183. amixer -q sset Master mute;
  184. fi
  185. }
  186. i_am_ooo() {
  187. #
  188. # too dangerous to implement
  189. #
  190. die "not implemented"
  191. }
  192. i_am_wfh() {
  193. #
  194. # too dangerous to implement
  195. #
  196. die "not implemented"
  197. }
  198. i_am_writing() {
  199. #
  200. # writing in that language
  201. #
  202. set -x
  203. DISPLAY=:0 setxkbmap $1
  204. }
  205. i_am_undocking() {
  206. #
  207. # i.e. hibernated
  208. #
  209. saturnin conf -p iam.undocking.like | bash -
  210. }
  211. i_am_zleeping() {
  212. #
  213. # i.e. hibernated
  214. #
  215. set_status "zleeping"
  216. zleep
  217. i_am_back
  218. }
  219. cmd=$1
  220. test -n "$cmd" || usage
  221. available_commands | grep -qse ^$1 || usage
  222. shift
  223. run_hook before $cmd $1
  224. i_am_$cmd $1
  225. run_hook after $cmd $1