/var/lib/sorcery/modules/libtriggers

     1	#!/bin/bash
     2	#---------------------------------------------------------------------
     3	##
     4	## @Synopsis Set of functions used by the internal sorcery scripts
     5	##
     6	## Functions used to manage triggers. Used both by spells
     7	## and by the sorcery scripts.
     8	##
     9	## @Copyright Original version Copyright 2002 by the Source Mage Team
    10	##
    11	#---------------------------------------------------------------------
    12	
    13	
    14	#---------------------------------------------------------------------
    15	## Checks for a TRIGGERS file in SCRIPT_DIRECTORY, and if it is
    16	## executable, runs it.
    17	#---------------------------------------------------------------------
    18	function run_triggers() {
    19	  debug "libtriggers" "Starting run_triggers() on $SPELL"
    20	
    21	  run_spell_file TRIGGERS triggers
    22	
    23	}
    24	
    25	#---------------------------------------------------------------------
    26	## @param spell
    27	##
    28	## Remove's all of a spell's triggers from the list of registered
    29	## triggers.
    30	##
    31	#---------------------------------------------------------------------
    32	function remove_triggers()
    33	{
    34	  debug "libtriggers" "remove_triggers - $*"
    35	
    36	  [ -f $TRIGGER_LIST ] || return 0
    37	  lock_start_transaction "$TRIGGER_LIST" tTRIGGER_LIST
    38	  grep -v "^$SPELL:" $TRIGGER_LIST > $tTRIGGER_LIST
    39	  lock_commit_transaction $TRIGGER_LIST
    40	  return $?
    41	}
    42	
    43	#---------------------------------------------------------------------
    44	## @param event
    45	## @param [spell]
    46	## @Globals SPELL if \$2 is omitted
    47	##
    48	## @Stdout Query and warnings
    49	## @Stdin y/n
    50	## Triggers an event and performs necessary actions. Argument 2 is
    51	## optional. If omitted, the value of SPELL will be used.
    52	##
    53	#---------------------------------------------------------------------
    54	function trigger()
    55	{
    56	  debug "libtriggers" "trigger - $*"
    57	
    58	  local spell TRIGGER action
    59	  action=$1
    60	  esc_str "${SPELL:-$2}" spell
    61	
    62	  [ -f $TRIGGER_LIST ] || return 0
    63	  # no locking of $TRIGGER_LIST since the triggered actions may need it
    64	  iterate do_trigger $'\n' "`grep "^[^:]*:$spell:on_$action" $TRIGGER_LIST`"
    65	}
    66	
    67	#---------------------------------------------------------------------
    68	## Get the triggerees of a given spell event and action
    69	#---------------------------------------------------------------------
    70	function get_triggerees() {
    71	  local spell=$1
    72	  local event=$2
    73	  local each
    74	  # get all the cast/check_self triggers
    75	  [ -f $TRIGGER_LIST ] || return 0
    76	
    77	  lock_file $TRIGGER_LIST
    78	  for each in $3 ; do
    79	    grep "[^:]*:$spell:$event:$each" $TRIGGER_LIST|cut -f1 -d:
    80	  done|sort -u|grep -v '^$'
    81	  unlock_file $TRIGGER_LIST
    82	}
    83	
    84	#---------------------------------------------------------------------
    85	## Get the run_script triggers from $spell on $target when theres a
    86	## $action
    87	#---------------------------------------------------------------------
    88	function get_run_script_triggers() {
    89	  local spell=$1
    90	  local event=$2
    91	  local target=$3
    92	
    93	  lock_file $TRIGGER_LIST
    94	  grep "$target:$spell:$event:run_script" $TRIGGER_LIST|cut -f4 -d:
    95	  unlock_file $TRIGGER_LIST
    96	}
    97	
    98	#---------------------------------------------------------------------
    99	## @param event
   100	## @param causing-spell
   101	## @param action
   102	## @param subject-spell
   103	## Registers a trigger in the list of triggers. Also verifies that
   104	## the trigger and action exist.
   105	##
   106	#---------------------------------------------------------------------
   107	function set_trigger()
   108	{ #1==trigger to set, $2==spell that triggers it, $3=action, $4==(optional)spell this is for
   109	
   110	  debug "libtriggers" "set_trigger - $*"
   111	
   112	  local str spell
   113	  [[ $4 ]] || [[ $SPELL ]] || return 1
   114	  case $3 in
   115	    cast_self|dispel_self|check_self|run_script*)
   116	      ;;
   117	    *)
   118	      message "${PROBLEM_COLOR}$3 is not a valid trigger action.${DEFAULT_COLOR}"
   119	      return 1
   120	      ;;
   121	  esac
   122	  case $1 in
   123	    on_cast|on_pre_cast|on_dispel|on_pre_dispel)
   124	      ;;
   125	    *)
   126	      message "${PROBLEM_COLOR}$1 is not a valid trigger.${DEFAULT_COLOR}"
   127	      return 1
   128	      ;;
   129	  esac
   130	  #perhaps a check to make sure that $2 exists?
   131	
   132	  spell=${SPELL:-$4}
   133	  str="$spell:$2:$1:$3"
   134	
   135	  lock_file $TRIGGER_LIST
   136	  if ! test -f $TRIGGER_LIST || ! grep -q "$str" $TRIGGER_LIST ; then
   137	    echo "$str" >> $TRIGGER_LIST
   138	  fi
   139	  unlock_file $TRIGGER_LIST
   140	}
   141	
   142	#---------------------------------------------------------------------
   143	## @Type API
   144	## @param spell that triggers
   145	## @param action
   146	##
   147	## Used by spells to make adding triggerse nice.
   148	##
   149	#---------------------------------------------------------------------
   150	function real_on_cast() {
   151	  set_trigger "on_cast" "$1" "$2"
   152	}
   153	
   154	#---------------------------------------------------------------------
   155	## @Type API
   156	## @param spell that triggers
   157	## @param action
   158	##
   159	## Used by spells to make adding triggerse nice.
   160	##
   161	#---------------------------------------------------------------------
   162	function real_on_pre_cast() {
   163	  set_trigger "on_pre_cast" "$1" "$2"
   164	}
   165	
   166	#---------------------------------------------------------------------
   167	## @Type API
   168	## @param spell that triggers
   169	## @param action
   170	##
   171	## Used by spells to make adding triggers nice.
   172	##
   173	#---------------------------------------------------------------------
   174	function real_on_dispel() {
   175	  set_trigger "on_dispel" "$1" "$2"
   176	}
   177	
   178	#---------------------------------------------------------------------
   179	## @Type API
   180	## @param spell that triggers
   181	## @param action
   182	##
   183	## Used by spells to make adding triggerse nice.
   184	##
   185	#---------------------------------------------------------------------
   186	function real_on_pre_dispel() {
   187	  set_trigger "on_pre_dispel" "$1" "$2"
   188	}
   189	
   190	
   191	function do_trigger () {
   192	  TRIGGER=()
   193	  explode "$1" ":" TRIGGER
   194	  local DC="$DEFAULT_COLOR"
   195	  message "${SPELL_COLOR}${TRIGGER[1]}${DC}${QUERY_COLOR}" \
   196	          "being $action has triggered a" \
   197	          "\"${DC}${FILE_COLOR}${TRIGGER[3]}${DC}${QUERY_COLOR}\"" \
   198	          "on spell ${DC}${SPELL_COLOR}${TRIGGER[0]}${DC}."
   199	  query "Proceed? " "y"
   200	  [[ $? != 0 ]] && return
   201	
   202	  (
   203	    unset CAST_PASS D_LOG
   204	    function run_script() { eval $@ ; }
   205	    #Ok, now do the required action:
   206	    case ${TRIGGER[3]} in
   207	      cast_self)
   208	        cast -c "${TRIGGER[0]}"
   209	        ;;
   210	      dispel_self)
   211	        dispel --noqueue --nosustain "${TRIGGER[0]}"
   212	        ;;
   213	      check_self)
   214	        cleanse --nofix_quick "${TRIGGER[0]}" ||
   215	        cast -c "${TRIGGER[0]}"
   216	        ;;
   217	      run_script*)
   218	        eval "${TRIGGER[3]}"
   219	        ;;
   220	      *) message "${PROBLEM_COLOR}${TRIGGER[3]} is not" \
   221	                 "a known kind of trigger.${DEFAULT_COLOR}"
   222	    esac
   223	  )
   224	}