Advertisement
cirrus

mpdbot

Jan 29th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. #!/bin/zsh
  2.  
  3. autoload -U tcp_open
  4. c='#freenodechannel' #channel for bot to enter
  5. cp='channelpw' #channel pw if prevalant
  6. e='#' # this is your command starter.
  7. n=irc.freenode.net #irc network
  8. l=n
  9. p=6667 #port
  10. u='cirrus_minor' #this is you, or your usernampe ion freenode
  11. w="cirrus_minor!~cirrus_mi@unaffiliated/cirrus-minor"
  12. ip='freenodeircpw' # CIRRUS LIKES IT IN DA JUNGLE! :D
  13.  
  14. function aix { ( x=$(ix $@); notice "$x - autodeletion in 60"; sleep 60; ix -d ${x##*/} ) >&/dev/null & }
  15. function lyrics { aix ".lyrics/`mpc current`.txt" }
  16. function permit { [[ "$r" =~ "^:$w" || "$l" = 'n' ]] || { notice "You do not have permission to issue the following command: $2."; return 1 } }
  17. function notice { [[ $#@ > 0 ]] && >&3 <<< "notice $(<.curchan) :$@" }
  18.  
  19. tcp_open $n $p
  20. tcp_open ${MPD_HOST:-localhost} ${MPD_PORT:-6600}
  21. >&3 <<< "nick $u|$$" <<< "user $u|$$ 8 * : $u the bot"
  22. while :; do >&4 <<< 'ping'; sleep 60; done &
  23.  
  24. while read r; do
  25. [[ "$r" =~ '^ACK' ]] && notice $r
  26. [[ "$r" =~ '^(Title|Artist|Time|time|file|OK)' ]] && t=${r%%\:*} || continue
  27. v=${r#*\: }
  28. case "$t" in
  29. (Artist) artist=$v ;;
  30. (Title) title=$v ;;
  31. (Time) mm=$[$v / 60] ss=$[$v - $[$mm * 60]]
  32. (( $#ss < 2 )) && ss=0$ss; (( $#mm < 2 )) && mm=0$mm ;;
  33. (time) v=${v%\:*}; m=$[$v / 60] s=$[$v - $[$m * 60]]
  34. (( $#s < 2 )) && s=0$s; (( $#m < 2 )) && m=0$m
  35. notice "[$m:$s/$mm:$ss] $title by $artist"
  36. unset artist title mm ss m s ;;
  37. esac
  38. unset t v
  39. done <&4 &
  40.  
  41. while read r; do <<< $r
  42. [[ "$r" =~ '^:[a-z\.]+ 396' ]] && >&3 <<< "nick ${u}MPD" <<< "join $c${cp:+ $cp}"
  43. [[ "$r" =~ '^PING' ]] && >&3 <<< "${r/PING/PONG}" <<< "nick ${u}MPD" <<< "join $c"
  44. [[ "$r" =~ '^:[a-z\.]+ 376' ]] && >&3 <<< "privmsg nickserv :identify $u $ip"
  45. [[ "$r" =~ '^:[a-z\.]+ 391' ]] && notice "${r#:*:}"
  46. [[ "$r" =~ "PRIVMSG .* :$e" ]] || continue
  47. s=${${r#:}%%$e*}; d=${${r##*$e}%\r\n}; c=${${r#*PRIVMSG }% :*}; W=${${d#:}%%\!*}
  48. >.curchan<<<$c
  49. [[ "$d" =~ '[a-z]+' ]] && case "$MATCH" in
  50.  
  51. ## Help Section
  52. (help) [[ "${d#help }" =~ '[a-z]+' ]] && case "$MATCH" in
  53. (currentsong) notice 'Display information about current song' ;;
  54. (find) notice "Given a query and term will search the database. Usage: ${e}find <query> <term> -- reference ${e}help query" ;;
  55. (load) notice 'Loads a playlist' ;;
  56. (next) notice 'Plays next song' ;;
  57. (play) notice 'Plays song specified by number from the playlist, alternatively if no song is specified it shows current song playing or resumes the playlist if stopped' ;;
  58. (save) notice 'Saves a playlist' ;;
  59. (seek) notice "Seeks to the given time in the given song. Usage ${e}seek <songID> <seconds>" ;;
  60. (clear) notice 'Clears the whole playlist' ;;
  61. (query) notice 'Name, Artist, Album, Albumartist, Composer, Title, Track, File' ;;
  62. (delete) notice 'Removes a song from the playlist' ;;
  63. (repeat) notice "Sets the repeat mode to either True or False. Usage: ${e}repeat [01]" ;;
  64. (single) notice "Sets the single mode to either True or False. Usage: ${e}single [01]" ;;
  65. (findadd) notice "Given a query and term will search through the database and add all results. Usage: ${e}findadd <query> <term> -- referene ${e}help query" ;;
  66. (previous) notice 'Plays previous song' ;;
  67. (random) notice "Sets the random mode to either True or False. Usage: ${e}random [01]" ;;
  68. (time) notice "Requests the time of a given user. Usage: ${e}time <user>" ;;
  69. (*) notice "Use ${e}help <command>; see ${e}commands for the list."
  70. esac ;;
  71. (commands) notice 'Playlist Navigation: currentsong, next, play, previous, seek'
  72. notice 'Playlist Management: findadd, add, clear, crossfade, delete, load, move, ping, random, repeat, save, setvol, shuffle, single'
  73. notice ' Extra Commands: playlist, lyrics, help.'
  74. notice ' Admin Commands: lock, unlock, quit' ;;
  75.  
  76. (currentsong) >&4 <<< "$d" <<< 'currentsong' <<< 'status' ;;
  77. (playlist) mpc playlist | cat -n | aix ;;
  78. (lyrics) lyrics & ;;
  79.  
  80. (next|play|previous|seek) permit "$r" "$MATCH" && >&4 <<< "$d" <<< 'currentsong' <<< 'status' ;;
  81. (findadd|add|crossfade|delete|load|move|pause|ping|random|repeat|save|shuffle|single)
  82. permit "$r" "$MATCH" && notice 'Done!' && echo "$r" "$match" "$d"
  83. >&4 <<< "$d" ;; # This could be exploitable to send anything to the seerver... But I would expect ACKs...
  84. (time) >&3 <<< "ctcp ${${d#time }%\r\n} :time" ;;
  85. (lstatus) notice "The bot is currently on $l status" ;;
  86.  
  87. (quit) [[ "$r" =~ "^:$w" ]] || continue
  88. >&3 <<< 'quit'
  89. >&4 <<< 'close' ;;
  90. (clear) [[ "$r" =~ "^:$w" ]] || { notice "You do not have permission to use this command." && continue }
  91. >&4 <<< "$d"
  92. notice 'Playlist Cleared' ;;
  93. (delete) [[ "$r" =~ "^:$w" ]] || { notice "You do not have permission to use this command." && continue }
  94. >&4 <<< "$d"
  95. notice 'Deleted' ;;
  96. (lock) [[ "$r" =~ "^:$w" ]] || { notice "You do not have permission to use this command." && continue }
  97. l='y'
  98. notice 'Locked' ;;
  99. (unlock) [[ "$r" =~ "^:$w" ]] || { notice "You do not have permission to use this command." && continue }
  100. l='n'
  101. notice 'Unlocked' ;;
  102. (setvol) [[ "$r" =~ "^:$w" ]] || { notice "You do not have permission to use this command." && continue }
  103. >&4 <<< "$d"
  104. notice "Volume set to: ${d#setvol }" ;;
  105. (*) notice "No such command: \"$MATCH\""
  106. esac
  107. done <&3 &
  108. while read r; do >&3 <<< "$r"; sleep 0.3; done
  109.  
  110. >&3 <<< 'quit'
  111. >&4 <<< 'close'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement