44ce2be292ec5952b1e4b072618a1edb98edd6ac
[mirrors/libpurple-core-answerscripts.git] / purple / answerscripts.sh
1 #!/bin/bash
2 #
3 # This file is called for every message received by libpurple clients (pidgin,finch,...)
4 # - You can try to rewrite this script in PERL or C for better performance (or different platform) - let me know
5 # - On M$ Windows answerscripts.exe from libpurple directory will be called instead of this script
6 #
7 # Maybe you will want to add more hooks for receiving messages, so i've made following script
8 # - It just executes all +x files in answerscripts.d directory so you should do your magic there
9 # - To disable some of those scripts simply use: chmod -x ./script
10 # - There is some basic structure, which means that all scripts should start their names with two-digit number
11 # - Files are executed in order specified by those numbers and some numbers have special meanings:
12 # - AB?!_ scripts without numbers are NOT executed!
13 # - 00 executed immediately, zero or single line output (parallel async processing)
14 # - 01-48 executed immediately, multiline output (serial processing)
15 # - 49 delay script (adds random delay to emulate human factor, no user scripts at this level!)
16 # - 50 executed after delay, zero or single line output (parallel async processing)
17 # - 51-79 executed after delay, multiline output (serial processing)
18 # - 80-99 reserved for future
19
20 #legacy support, please do NOT use PURPLE_* variables in new scripts,
21 #this will be removed in future releases:
22 export PURPLE_FROM="$ANSW_FROM"
23 export PURPLE_MSG="$ANSW_MSG"
24
25 #this may be modified to use run-parts from coreutils in future (can't get it to work):
26
27 dir="$(dirname "$0")"; cd "$dir" #chdir to ~/.purple/ or similar
28 dir="${dir}/answerscripts.d"
29 if test -d "$dir"; then
30 for i in {00..99}; do
31
32 #sleep at 49 (this can be replaced by 49-delay.sh, but this should be faster)
33 [ $i -eq 49 ] && {
34 find "$dir"/[5-9][0-9]-* -executable | grep . >/dev/null && #check if it's worth waiting
35 sleep $(( 2 + ($RANDOM % 8) )); #2-9 seconds of sleep
36 continue;
37 }
38
39 #execute scripts
40 ls -1 "$dir/$i"* 2>/dev/null | while read script; do
41 test -x "$script" && {
42 #determine wheter execute on background or foreground
43 if [ $i -eq 00 ] || [ $i -eq 50 ]; then
44 "$script" &
45 else
46 "$script"
47 fi;
48 }
49 done;
50
51 wait; #wait for processes on background
52
53 done;
54 fi
This page took 0.270999 seconds and 3 git commands to generate.