saturnin-iam 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. # subcommand handlers
  89. #
  90. i_am_afk() {
  91. #
  92. # away from keyboard; blocks until i'm back again
  93. #
  94. mocp --pause
  95. set_status "afk"
  96. i_am_bbldone # make sure to set lang to default before locking
  97. slock
  98. set_status "atk"
  99. }
  100. i_am_at() {
  101. #
  102. # Just say where I am
  103. #
  104. where_i_am
  105. }
  106. i_am_back() {
  107. #
  108. # returning to work (should be called by other subcommands)
  109. #
  110. local f
  111. for f in $(familiars_at $(where_i_am));
  112. do
  113. start_familiar $f
  114. done
  115. set_status "back"
  116. klist -s || urxvt -e kinit
  117. }
  118. i_am_bbl() {
  119. #
  120. # changing language
  121. #
  122. local cur=$(setxkbmap -v | grep '^symbols: ' | cut -d+ -f2)
  123. local all="$(saturnin conf -p iam.typing.lang)"
  124. local def="$(saturnin conf -1 -p iam.typing.lang)"
  125. local nxt=$(
  126. echo -e "$all\n$all" \
  127. | grep -m 1 -A 1 $cur \
  128. | tail -1
  129. )
  130. test -z "$nxt" && nxt=$def
  131. test -z "$nxt" && nxt=us
  132. debug -v all def cur nxt
  133. setxkbmap "$nxt"
  134. }
  135. i_am_bbldone() {
  136. #
  137. # changing language back to normal
  138. #
  139. setxkbmap "$(saturnin conf -1 -p iam.typing.lang)"
  140. }
  141. i_am_deaf() {
  142. amixer -q sset Master 5%+
  143. }
  144. i_am_deafnomore() {
  145. amixer -q sset Master 5%-
  146. }
  147. i_am_gone() {
  148. #
  149. # gone fishin'
  150. #
  151. kdestroy
  152. ssh-add -D
  153. set_status "gone"
  154. i_am_afk
  155. i_am_back
  156. }
  157. i_am_listening() {
  158. if sound_is_muted;
  159. then
  160. amixer -q sset Master unmute;
  161. else
  162. amixer -q sset Master mute;
  163. fi
  164. }
  165. i_am_ooo() {
  166. #
  167. # too dangerous to implement
  168. #
  169. die "not implemented"
  170. }
  171. i_am_wfh() {
  172. #
  173. # too dangerous to implement
  174. #
  175. die "not implemented"
  176. }
  177. i_am_writing() {
  178. #
  179. # writing in that language
  180. #
  181. set -x
  182. DISPLAY=:0 setxkbmap $1
  183. }
  184. i_am_undocking() {
  185. #
  186. # i.e. hibernated
  187. #
  188. saturnin conf -p iam.undocking.like | bash -
  189. }
  190. i_am_zleeping() {
  191. #
  192. # i.e. hibernated
  193. #
  194. set_status "zleeping"
  195. zleep
  196. i_am_back
  197. }
  198. cmd=$1
  199. test -n "$cmd" || usage
  200. available_commands | grep -qse ^$1 || usage
  201. shift
  202. i_am_$cmd $1