saturnin-iam 3.7KB

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