| 1 | #!/bin/sh |
| 2 | |
| 3 | # This file is called for every message received by libpurple clients (pidgin,finch,...) |
| 4 | # - Following env values are passed to this script: |
| 5 | # - ANSW_FROM (who sent you message) |
| 6 | # - ANSW_MSG (text of the message) |
| 7 | # - ANSW_STATUS (unique ID of status. eg.: available, away,...) |
| 8 | # - ANSW_STATUS_MSG (status message set by user) |
| 9 | # - WARNING: You should mind security (don't let attackers to execute their messages/nicks!) |
| 10 | # - Each line of output is sent as reply to that message |
| 11 | # - You can try to rewrite this script in PERL or C for better performance (or different platform) |
| 12 | # - On M$ Windows answerscripts.exe from libpurple directory will be called instead of this script |
| 13 | # |
| 14 | # Basic example can look like this: |
| 15 | # [ "$ANSW_STATUS" != 'available' ] && echo "<$ANSW_FROM> $ANSW_MSG" && echo "My status: $ANSW_STATUS_MSG"; |
| 16 | # |
| 17 | # There are lot of hacks that you can do with this simple framework if you know some scripting. eg.: |
| 18 | # - Forward your instant messages to email, SMS gateway, text-to-speach (eg. espeak) or something... |
| 19 | # - Smart auto-replying messages based on regular expressions |
| 20 | # - Remote control your music player (or anything else on your computer) using instant messages |
| 21 | # - Simple IRC/Jabber/ICQ bot (accepts PM only, you can run finch in screen on server) |
| 22 | # - Providing some service (Searching web, Weather info, System status, RPG game...) |
| 23 | # - BackDoor (even unintentional one - you've been warned) |
| 24 | # - Loging and analyzing messages |
| 25 | # - Connect IM with Arduino |
| 26 | # - Annoy everyone with spam (and probably get banned everywhere) |
| 27 | # - Anything else that you can imagine... |
| 28 | # |
| 29 | # Maybe you will want to add more hooks for receiving messages, so i've made following script |
| 30 | # - It just executes all +x files in answerscripts.d directory so you should do your magic there |
| 31 | # - To disable some of those scripts simply use: chmod -x ./script |
| 32 | # - There is some basic structure, which means that all scripts should start their names with two-digit number |
| 33 | # - Files are executed in order specified by those numbers and some numbers have special meanings: |
| 34 | # - AB?!_ scripts without numbers are NOT executed! |
| 35 | # - 00 executed immediately, zero or single line output (parallel async processing) |
| 36 | # - 01-48 executed immediately, multiline output (serial processing) |
| 37 | # - 49 delay script (adds random delay to emulate human factor) |
| 38 | # - 50 executed after delay, zero or single line output (parallel async processing) |
| 39 | # - 51-79 executed after delay, multiline output (serial processing) |
| 40 | # - 80-99 reserved for future |
| 41 | |
| 42 | #legacy support, please do NOT use PURPLE_* variables in new scripts, |
| 43 | #this will be removed in future releases: |
| 44 | export PURPLE_FROM="$ANSW_FROM" |
| 45 | export PURPLE_MSG="$ANSW_MSG" |
| 46 | |
| 47 | #this may be modified to use run-parts from coreutils in future (can't get it to work): |
| 48 | |
| 49 | dir="$(dirname "$0")"; cd "$dir" #chdir to ~/.purple/ or similar |
| 50 | dir="${dir}/answerscripts.d" |
| 51 | if test -d "$dir"; then |
| 52 | for i in {00..99}; do |
| 53 | |
| 54 | #sleep at 49 (this can be replaced by 49-delay.sh, but this should be faster) |
| 55 | [ $i -eq 49 ] && { |
| 56 | sleep $[ 2 + ($RANDOM % 8) ]; #2-9 seconds of sleep |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | #execute scripts |
| 61 | ls -1 "$dir/$i"* 2>/dev/null | while read script; do |
| 62 | test -x "$script" && { |
| 63 | #determine wheter execute on background or foreground |
| 64 | if [ $i -eq 00 ] || [ $i -eq 50 ]; then |
| 65 | "$script" & |
| 66 | else |
| 67 | "$script" |
| 68 | fi; |
| 69 | } |
| 70 | done; |
| 71 | |
| 72 | wait; #wait for processes on background |
| 73 | |
| 74 | done; |
| 75 | fi |