;;; -*- guile-scheme -*-
;;; this shows off some interesting things...(based on boulet)
;;; This is unknown_lamer's bot EvilBot

;;; yes, this uses a sql db!
(use-modules (database simplesql)
	     (ice-9 debug))
(load "bobot-utils.scm")

;;; Activate or desactivate the bot
(define talk 1)

(define (shutup)
  (set! talk 0))
(bot-addcommand "shutup" shutup #f 0 2)

(define (dotalk)
  (set! talk 1))
(bot-addcommand "talk" dotalk #f 0 2)


;;; Miscellaneous list functions
(define (removeChannel c l)
  (if (null? l)
      (list)
      (let ((deb (car l)))
        (if (string-ci=? c (car deb))
            (cdr l)
            (append (list deb) (removeChannel c (cdr l)))))))

(define (changeChannel c n l)
  (if (null? l)
      (list)
      (let ((deb (car l)))
        (if (string-ci=? c (car deb))
            (append (list (list c n)) (cdr l))
            (append (list deb) (changeChannel c n (cdr l)))))))

(define (addChannel c n l)
  (append l (list (list c n))))

;;; When someone joins a channel
(define dumbTimer (list))
(define (joinLourd n c)
  (if (string-ci=? n (bot-getnickname))
    (begin
      (if (= 1 talk) (bot-say c "I like limos"))
      (set! dumbTimer 
	    (addChannel c (bot-addtimer (+ 600 (bot-random 300)) 
					(dumbLourd c)) dumbTimer)))
    (if (= 1 talk) (bot-say c (string-append "Arf Moo Arf! " n
					     " I like pizza...")))))
(bot-addhook hooks/join ".*" joinLourd)

;;; When someone leaves the channel
(define (partLourd n c)
  (bot-flushport)
  (if (not (string-ci=? n (bot-getnickname)))
      (if (= 1 talk) (bot-msg n "I HATED YOU ANYWAY"))
      (let* ((timer (assoc c dumbTimer)))
        (bot-deltimer (cadr timer))
        (set! dumbTimer (removeChannel c dumbTimer)))))
(bot-addhook hooks/part ".*" partLourd)

;;; Random action to say something dumb
 (define (dumbLourd c)
   (lambda ()
   (if (= 1 talk) 
     (bot-say 
      c 
      (vector-ref (list-ref dumbList (bot-random (length dumbList))) 0)))
   (set! dumbTimer 
 	(changeChannel 
 	 c
 	 (bot-addtimer (+ 600 (bot-random 300)) (dumbLourd c)) dumbTimer))))


(define dumbDB (simplesql-open 'postgresql "bot_sayings"))
;(define dumbList #f)
; (define (reload-sayings)
;   (set! dumbList (cdr (simplesql-query dumbDB "SELECT saying FROM sayings")))
;   (display "Reloaded sayings from db\n" (bot-logport))
;   (bot-flushport))
; (bot-addcommand "reloadsayings" reload-sayings #f 0 4)
; (reload-sayings)

(define (_length db)
  (let ((length (+ 1 
		   (vector-ref 
		    (list-ref (cdr 
			       (simplesql-query 
				db
				"SELECT count(saying) FROM sayings")) 0) 0))))
    length ))

(define (_select db entry)
  (list-ref
   (cdr
    (simplesql-query db 
		     (str-app "SELECT saying FROM sayings "
			      "WHERE id = " 
			      (number->string entry)))) 0) 0)
	       
  


(define (random-saying)
  (vector-ref (_select dumbDB (bot-random (_length dumbDB))) 0))

;;; Misc
;;; note that public hooks match the channel name too!
;;; match-not-channel prevents this

;;; this should be the first so it doesn't clobber the other hooks
(bot-addhook 
 hooks/public (match-to-me "")
 (lambda (f t p)
   (if (= 1 talk)
     (bot-say t
	      (string-append f ": " (random-saying))))))


(bot-addhook hooks/public (match-not-channel "oracle")
	     (lambda (f t p) 
	       (if (= 1 talk) 
		 (bot-say t "Oracle is evil! Use Postgres instead!"))))

(define (add-gnu word)
  (bot-addhook hooks/public (match-not-channel word)
	     (lambda (f t p) 
	       (if (= 1 talk)
		 (bot-say t 
			  (string-append f ": I hope you meant GNU!"))))))
(map add-gnu '("[^/g]linux" "microsoft" "bsd"))

(bot-addhook hooks/public (match-not-channel "boulet")
	     (lambda (f t p) 
	       (if (= 1 talk) (bot-say t "on parle de moi ?"))))

(bot-addhook hooks/public (match-not-channel "arf")
	     (lambda (f t p) 
	       (if (= 1 talk) 
		 (bot-say t (random-saying)))))

(bot-addhook hooks/public (match-not-channel "csn")
	     (lambda (f t p) 
	       (if (= 1 talk) (bot-say t "???"))))

(bot-addhook hooks/public (match-not-channel "pizza|pasta|soda")
	     (lambda (f t p)
	       (if (= 1 talk) 
		 (bot-say t (string-append f ": give me one too!")))))

(define (add-foul word)
  (bot-addhook hooks/public (match-not-channel word)
	     (lambda (f t p) 
	       (if (= 1 talk)
		 (bot-action 
		  t (string-append "hits " f 
				   " for having a potty mouth"))))))
(map add-foul '("bitch" "fuck" "whore" "cunt" "tit" "shit" "dick" "cock"))

(bot-addhook hooks/public (match-not-channel "anal sex")
	     (lambda (f t p) 
	       (if (= 1 talk)
		 (if (not (string-match "unknown_lamer" f))
		   (bot-action 
		    t (string-append "grabs " f 
				     " and buggers " f " up the ass"))))))

(bot-addhook 
 hooks/public (match-not-channel "[[:space:]]*lame[[:space:]]+")
 (lambda (f t p)
   (if (= 1 talk)
     (bot-say t
	      (string-append f ": NO NO YOU ARE THE LAMER")))))

(bot-addhook hooks/public (match-to-me "burn")
	     (lambda (f t p)
	       (if (= 1 talk)
		 (bot-say
		  t
		  (string-append "How about you burn " f "?")))))
	     

;;; 9h00, on arrive au boulot
;(define (newDay l)
;  (if (not (null? l))
;      (begin
;        (bot-say (caar l) "What is up my homey g-d0g?")
;        (newDay (cdr l))
;        )
;      )
;)

;(bot-addhook hooks/timer "09:00" (lambda (h) (newDay dumbTimer) (talk)))

;;; 18h00, il est temps de rentrer
;(define (timeToGo l)
;  (if (not (null? l))
;      (begin
;        (bot-say (caar l) "Give me food")
;        (bot-action (caar l) "I'm bored")
;        (timeToGo (cdr l))
;        )
;      )
;)
;(bot-addhook hooks/timer "18:00" (lambda (h) (timeToGo dumbTimer) (shutup)))