saturnin-iam 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/bin/bash
  2. . $(ffoom path)
  3. ffoo import pretty
  4. ffoo import sync
  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 gone
  16. echo ooo
  17. echo wfh
  18. echo writing
  19. echo undocking
  20. echo zleeping
  21. }
  22. usage() {
  23. cmd_hint=$(
  24. available_commands \
  25. | head -c -1 \
  26. | tr '\n' '|'
  27. )
  28. usage_is "$cmd_hint"
  29. }
  30. #
  31. # querying
  32. #
  33. where_i_am() {
  34. #
  35. # what is my physical location?
  36. #
  37. if=$(saturnin conf -p iam.using.if)
  38. gwmac=$(arp | grep "^gateway\\>.*\\<$if\$" | tr ' ' '\n' | grep :)
  39. saturnin conf -p iam.seeing.gw.$gwmac || echo OUT
  40. }
  41. set_status() {
  42. #
  43. # set public status $1 by command in conf file
  44. #
  45. local what=$1
  46. saturnin conf -p iam.saying.like.this.$what | bash
  47. }
  48. #
  49. # hooks
  50. #
  51. run_hook() {
  52. local name=$1
  53. local cmd=$2
  54. local arg=$3
  55. saturnin conf -p iam.hooking.$name.$cmd \
  56. | SATURNIN_IAM_CMD=$cmd \
  57. SATURNIN_IAM_ARG=$arg \
  58. bash
  59. }
  60. #
  61. # subcommand handlers
  62. #
  63. i_am_afk() {
  64. #
  65. # away from keyboard; blocks until i'm back again
  66. #
  67. mocp --pause
  68. set_status "afk"
  69. i_am_bbldone # make sure to set lang to default before locking
  70. slock
  71. set_status "atk"
  72. }
  73. i_am_at() {
  74. #
  75. # Just say where I am
  76. #
  77. where_i_am
  78. }
  79. i_am_back() {
  80. #
  81. # returning to work (should be called by other subcommands)
  82. #
  83. set_status "back"
  84. klist -s || urxvt -e kinit
  85. }
  86. i_am_bbl() {
  87. #
  88. # changing language
  89. #
  90. local cur=$(setxkbmap -v | grep '^symbols: ' | cut -d+ -f2)
  91. local all="$(saturnin conf -p iam.typing.lang)"
  92. local def="$(saturnin conf -1 -p iam.typing.lang)"
  93. local nxt=$(
  94. echo -e "$all\n$all" \
  95. | grep -m 1 -A 1 $cur \
  96. | tail -1
  97. )
  98. test -z "$nxt" && nxt=$def
  99. test -z "$nxt" && nxt=us
  100. debug -v all def cur nxt
  101. setxkbmap "$nxt"
  102. }
  103. i_am_bbldone() {
  104. #
  105. # changing language back to normal
  106. #
  107. setxkbmap "$(saturnin conf -1 -p iam.typing.lang)"
  108. }
  109. i_am_gone() {
  110. #
  111. # gone fishin'
  112. #
  113. kdestroy
  114. ssh-add -D
  115. set_status "gone"
  116. i_am_afk
  117. i_am_back
  118. }
  119. i_am_ooo() {
  120. #
  121. # too dangerous to implement
  122. #
  123. die "not implemented"
  124. }
  125. i_am_wfh() {
  126. #
  127. # too dangerous to implement
  128. #
  129. die "not implemented"
  130. }
  131. i_am_writing() {
  132. #
  133. # writing in that language
  134. #
  135. set -x
  136. DISPLAY=:0 setxkbmap $1
  137. }
  138. i_am_undocking() {
  139. #
  140. # i.e. hibernated
  141. #
  142. saturnin conf -p iam.undocking.like | bash -
  143. }
  144. i_am_zleeping() {
  145. #
  146. # i.e. hibernated
  147. #
  148. set_status "zleeping"
  149. zleep
  150. i_am_back
  151. }
  152. cmd=$1
  153. test -n "$cmd" || usage
  154. available_commands | grep -qse ^$1 || usage
  155. shift
  156. run_hook before $cmd $1
  157. i_am_$cmd $1
  158. run_hook after $cmd $1