##########################################################################################
# Youtube Title 2.0.3
# - fetches and displays video information when a YouTube link is posted in channel.
# - displays title, date and rating of posted video links.
# - supports also HTTPS links.
#
# UPDATES/CHANGES:
# - (2.0.3) removed dislike and updated for Youtube PHP Source 1.3
# - (2.0.2) added new language: Turkish
# - (2.0.1) added UTF-8 support. New german language by doc.
# - (2.0) http package no longer needed. Now You can specify your Youtube php source (get it from tclscripts.net)
# - (1.9.1) changed hosting
# - (1.9) added stars for like display instead of procentages
# - (1.8) added anti-flood support
# - (1.8) added youtube search (it will get info from the first youtube video)
# - (1.8) added support for music.youtube links
# - (1.8) shows total links/searches processed by the website
# - (1.7) shows info about duration/length
# - (1.6) multiple output methods of showing the infos configurable via channel
# - (1.6) multi-language support configurable via channel
# - (1.6) supports eggdrop version less than 1.8.*
# - (1.6) options/settings are now case sensitive
#
# To activate - .chanset #channel +ytitle | BlackTools : .set +ytitle
# To activate Youtube search - .chanset #channel +ytsearch | BlackTools : .set +ytsearch
#
# To chose a different language .set ytlang <RO> / <EN> / <FR> / <ES> / <IT>
#
#                       BLaCkShaDoW ProductionS
#      _   _   _   _   _   _   _   _   _   _   _   _   _   _
#     / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \
#    ( t | c | l | s | c | r | i | p | t | s | . | n | e | t )
#     \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/
#                                    #TCL-HELP @ Undernet.org
#
##########################################################################################

###
# Channel flags
# - to activate the script: .set +ytitle or .chanset #channel +ytitle
#
# - to change script language:
# .set ytlang <ro/en/fr/es/it/tr> or .chanset #channel ytlang <ro/en/fr/es/it/tr>
#
# - to set script color
# .set ytcolor <0/1> ; 1 - colors ; 0 - no colors
#
###

###
# Set the website where from where information will be take from (without http://)
# You can download the "youtube php source" from tclscripts.net if you want different website
###
set ytitle(website_url) "your website here"

###
#  If you put the files in a subdirectory of the  website (rather then directy in public_html or www) please specify here the directory
#  if not, leave here ""
###
set ytitle(website_subdirectory) ""

###
# YOUTUBE SEARCH COMMAND
#
###
set ytitle(youtube_search_cmd) "yt"

###
# WHAT FLAGS CAN SEARCH VIA COMMAND ? (default -|-)
#
###
set ytitle(youtube_search_flags) "-|-"

###
# FLOOD PROTECTION
#Set the number of minute(s) to ignore flooders, 0 to disable flood protection
###
set ytitle(ignore_prot) "1"

###
# FLOOD PROTECTION
#Set the number of requests within specifide number of seconds to trigger flood protection.
# By default, 3:10, which allows for upto 3 queries in 10 seconds. 3 or more quries in 10 seconds would cuase
# the forth and later queries to be ignored for the amount of time specifide above.
###
set ytitle(flood_prot) "3:10"

###
# Language setting
# - what language you want to receive the youTube data
#   ( RO / EN / ES / FR / IT / TR)
#
set ytitle(default_lang) "EN"

###
# Colors setting
# - what format you want to receive the youTube data
#   (1) Enable or (0) disable colors
#
set ytitle(colors) "1"

##########################################################################################

###
# Bindings
# - using commands
###
bind pubm - * check:youtube
bind ctcp - ACTION check:youtube:me
bind pub $ytitle(youtube_search_flags) $ytitle(youtube_search_cmd) search:youtube

setudef flag ytitle
setudef str ytlang
setudef str ytcolor
setudef flag ytsearch

###
# Functions
# Do NOT touch unless you know what you are doing
###

###
proc search:youtube {nick host hand chan arg} {
if {![channel get $chan ytsearch]} {
	return
}
	set flood_protect [youtube:flood:prot $chan $host]
if {$flood_protect == "1"} {
	return
}
	set text [lrange [split $arg] 0 end]
if {$text == ""} {
	return
	}
	set text [join $text "+"]
	youtube:get:title $text $nick $chan 1
}

###
proc check:youtube {nick host hand chan arg} {
	set arg [split $arg]
if {![channel get $chan ytitle]} {
	return
}
	set flood_protect [youtube:flood:prot $chan $host]
if {$flood_protect == "1"} {
	return
}
foreach word $arg {
	set youtube_link "$word"
if {[string match -nocase "*youtube.com/watch*" $youtube_link] || [string match -nocase "*youtu.be*" $youtube_link]} {
	youtube:get:title $youtube_link $nick $chan 0
		}
	}
}

###
proc youtube:get:title {link nick chan type} {
	global ytitle
	set live 0
if {[catch {set sock [socket $ytitle(website_url) 80] } sockerror]} {
	putlog "YotutubeTitle: $sockerror"
} else {
if {$ytitle(website_subdirectory) != ""} {
	set l "/$ytitle(website_subdirectory)/index.php"
} else {
	set l "/index.php"
}
if {$type == "0"} {
	puts $sock "GET $l?link=$link HTTP/1.0"
} else {
	puts $sock "GET $l?search=$link HTTP/1.0"
	}
	puts $sock "User-Agent: lynx"
	puts $sock "Host: $ytitle(website_url)"
	puts $sock "Connection: close"
	puts $sock ""
	flush $sock
	while {[eof $sock] != 1} {
	set bl [gets $sock]
	lappend data [string range $bl 0 end]
}
	close $sock
}
if {![info exists data]} {
	youtube:tell $nick $chan 0 1
	return
}
	set start_write 0
	set output ""
foreach line $data {
	if {[llength [split $line]] == 0} {set start_write 1}
if {$start_write == 1} {
if {$line != ""} {
	lappend output $line
		}
	} else {continue}
}
if {$output == ""} {
	youtube:tell $nick $chan 0 1
	return
}
	set title [string map { "&amp;" "&"
			"&#39;" "'"
			"&quot;" "\""
} [lindex $output 0]]
	set title [encoding convertfrom "utf-8" [concat $title]]
	set views [lindex $output 1]
if {$views == ""} { set views "N/A" }
	set split_views [split $views " "]
	set views [lindex $split_views 0]
	set views [string map {"&nbsp;" "."} $views]
	set likes [youtube:commify [lindex $output 2]]
	set bywho [concat [lindex $output 4]]
	set update [lindex $output 5]
	set duration [lindex $output 6]
	set total_links [lindex $output 8]
	set get_link [lindex $output 7]
	set duration [string map {"PT" ""} $duration]
	set minutes [lindex [split $duration "M"] 0]
	set seconds [concat [string map {"S" ""} [lindex [split $duration "M"] 1]]]
	set scan [clock scan $update -format {%Y-%m-%d}]
	set update [clock format $scan -format {%d/%m/%Y}]
if {$views == ""} { set views "N/A" }
	youtube:tell $nick $chan $type [list $title $bywho $minutes $seconds $update $views $likes $total_links $get_link]
}

###
proc youtube:tell {nick chan search arg} {
	global black ytitle
	set inc 0
foreach s $arg {
	set inc [expr $inc + 1]
	set replace(%msg.$inc%) $s
}
	set getlang [youtube:getlang $chan]
if {$arg == "1"} {
	if {[info exists black(ytitle.$getlang.3)]} {
	set reply [string map [array get replace] $black(ytitle.$getlang.3)]
	putserv "PRIVMSG $chan :$reply"
	}
	return
}
	set setcolor [youtube:getcolor $chan]
if {$search == "1"} {
if {$setcolor == "1"} {
	set type 5
	} else { set type 4 }
if {[info exists black(ytitle.$getlang.$type)]} {
	set reply [string map [array get replace] $black(ytitle.$getlang.$type)]
	putserv "PRIVMSG $chan :$reply"
	}
} else {
if {$setcolor == "1"} {
	set type 2
} else { set type 1 }
if {[info exists black(ytitle.$getlang.$type)]} {
	set reply [string map [array get replace] $black(ytitle.$getlang.$type)]
	putserv "PRIVMSG $chan :$reply"
		}
	}
}

###
proc youtube:getlang {chan} {
	global black ytitle
	set getlang [string tolower [channel get $chan ytlang]]
if {$getlang == ""} {
	set lang "en"
} else {
if {[info exists black(ytitle.$getlang.1)]} {
	set lang $getlang
} else {
	set lang $ytitle(default_lang)
		}
	}
	return [string tolower $lang]
}

###
proc youtube:getcolor {chan} {
	global ytitle
	set getcolor [string tolower [channel get $chan ytcolor]]
if {$getcolor == ""} {
	set type $ytitle(colors)
	} else {
	set type $getcolor
	}
	return $type
}

###
proc check:youtube:me {nick host hand chan keyword arg} {
	check:youtube $nick $host $hand $chan $arg
	return
}

###
proc youtube:flood:prot {chan host} {
	global ytitle
	set number [scan $ytitle(flood_prot) %\[^:\]]
	set timer [scan $ytitle(flood_prot) %*\[^:\]:%s]
if {[info exists ytitle(flood:$host:$chan:act)]} {
	return 1
}
foreach tmr [utimers] {
if {[string match "*youtube:remove:flood $host $chan*" [join [lindex $tmr 1]]]} {
	killutimer [lindex $tmr 2]
	}
}
if {![info exists ytitle(flood:$host:$chan)]} {
	set ytitle(flood:$host:$chan) 0
}
	incr ytitle(flood:$host:$chan)
	utimer $timer [list youtube:remove:flood $host $chan]
if {$ytitle(flood:$host:$chan) > $number} {
	set ytitle(flood:$host:$chan:act) 1
	utimer [expr $ytitle(ignore_prot) * 60] [list youtube:expire:flood $host $chan]
	return 1
	} else {
	return 0
	}
}


###
proc youtube:remove:flood {host chan} {
	global ytitle
if {[info exists ytitle(flood:$host:$chan)]} {
	unset ytitle(flood:$host:$chan)
	}
}

###
proc youtube:expire:flood {host chan} {
	global ytitle
if {[info exists ytitle(flood:$host:$chan:act)]} {
	unset ytitle(flood:$host:$chan:act)
	}
}

set ytitle(projectName) "Youtube Title"
set ytitle(author) "BLaCkShaDoW"
set ytitle(website) "wWw.TCLScriptS.NeT"
set ytitle(version) "v2.0.3"

###
proc youtube:commify {num {sep .}} {
    fixpoint num {
        regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2"
    }
}

###
#http://wiki.tcl.tk/5000
proc fixpoint {varName script} {
    upvar 1 $varName arg
    while {[set res [uplevel 1 $script]] ne $arg} {
        set arg $res
    }
    return $arg
}


#Languages

# Romanian

set black(ytitle.ro.1) "\002\[YouTube\]\002 Titlu:\002 %msg.1%\002 | Publicat de:\002 %msg.2%\002 | Durata:\002 %msg.3%m\002%msg.4%\002s\002 | Data:\002 %msg.5%\002 | Vizionari:\002 %msg.6%\002 | Apreciere: \002\002%msg.7%\002\002 - \[\002%msg.8%\002\] -"
set black(ytitle.ro.2) "\[\002You\0030,4Tube\003\002\003\] \00310Titlu:\0034 %msg.1% \003| \00310Publicat de:\0034 %msg.2% \003| \00310Durata:\0034 %msg.3%m%msg.4%s\003 | \00310Data:\0034 %msg.5% \003| \00310Vizionari:\0034 %msg.6% \003| \00310Apreciere:\003 \0034%msg.7%\003 - \[\00304%msg.8%\003\] -"
set black(ytitle.ro.3) "\002\[YouTube\]\002 Acest videoclip nu exista."
set black(ytitle.ro.4) "\002\[YouTube\]\002 Titlu:\002 %msg.1%\002 | Publicat de:\002 %msg.2%\002 | Durata:\002 %msg.3%m\002%msg.4%\002s\002 | Data:\002 %msg.5%\002 | Vizionari:\002 %msg.6%\002 | Apreciere: \002%msg.7%\002 | Legatura: \002%msg.9%\002 - \[\002%msg.8%\002\] -"
set black(ytitle.ro.5) "\[\002You\0030,4Tube\003\002\003\] \00310Titlu:\0034 %msg.1% \003| \00310Publicat de:\0034 %msg.2% \003| \00310Durata:\0034 %msg.3%m%msg.4%s\003 | \00310Data:\0034 %msg.5% \003| \00310Vizionari:\0034 %msg.6% \003| \00310Apreciere:\003 \00304%msg.7%\003 | \00310Legatura: \0034%msg.9%\003 - \[\00304%msg.8%\003\] -"
# English

set black(ytitle.en.1) "\002\[YouTube\]\002 Title:\002 %msg.1%\002 | Uploaded by:\002 %msg.2%\002 | Length:\002 %msg.3%m\002%msg.4%\002s\002 | Date:\002 %msg.5%\002 | Views:\002 %msg.6%\002 | Like: \002%msg.7%\002 - \[\002%msg.8%\002\] -"
set black(ytitle.en.2) "\[\002You\0030,4Tube\003\002\003\] \00310Title:\0034 %msg.1% \003| \00310Uploaded by:\0034 %msg.2% \003| \00310Length:\0034 %msg.3%m%msg.4%s\003 | \00310Date:\0034 %msg.5% \003| \00310Views:\0034 %msg.6% \003| \00310Like:\003 \00304%msg.7%\003 - \[\00304%msg.8%\003\] -"
set black(ytitle.en.3) "\002\[YouTube\]\002 This video does not exist."
set black(ytitle.en.4) "\002\[YouTube\]\002 Title:\002 %msg.1%\002 | Uploaded by:\002 %msg.2%\002 | Length:\002 %msg.3%m\002%msg.4%\002s\002 | Date:\002 %msg.5%\002 | Views:\002 %msg.6%\002 | Like: \002%msg.7%\002 | Link:\002 %msg.9%\002- \[\002%msg.8%\002\] -"
set black(ytitle.en.5) "\[\002You\0030,4Tube\003\002\003\] \00310Title:\0034 %msg.1% \003| \00310Uploaded by:\0034 %msg.2% \003| \00310Length:\0034 %msg.3%m%msg.4%s\003 | \00310Date:\0034 %msg.5% \003| \00310Views:\0034 %msg.6% \003| \00310Like:\003 \00304%msg.7%\003 | \00310Link: \0034%msg.9%\003 - \[\00304%msg.8%\003\] -"
# French

set black(ytitle.fr.1) "\002\[YouTube\]\002 Titre:\002 %msg.1%\002 | Telecharge par:\002 %msg.2%\002 | Duree:\002 %msg.3%m\002%msg.4%\002s\002 La date:\002 %msg.5%\002 | Vues:\002 %msg.6%\002 | J'aime: \002%msg.7%\002 - \[\002%msg.8%\002\] -"
set black(ytitle.fr.2) "\[\002You\0030,4Tube\003\002\003\] \00310Titre:\0034 %msg.1% \003| \00310Telecharge par:\0034 %msg.2% \003| \00310Duree:\0034 %msg.3%m%msg.4%s\003 | \00310La date:\0034 %msg.5% \003| \00310Vues:\0034 %msg.6% \003| \00310J'aime:\003 \00304%msg.7%\003 - \[\00304%msg.8%\003\] -"
set black(ytitle.fr.3) "\002\[YouTube\]\002 Cette video n'existe pas."
set black(ytitle.fr.4) "\002\[YouTube\]\002 Titre:\002 %msg.1%\002 | Telecharge par:\002 %msg.2%\002 | Duree:\002 %msg.3%m\002%msg.4%\002s\002 La date:\002 %msg.5%\002 | Vues:\002 %msg.6%\002 | J'aime: \002%msg.7%\002 | Lien:\002 %msg.9%\002- \[\002%msg.8%\002\] -"
set black(ytitle.fr.5) "\[\002You\0030,4Tube\003\002\003\] \00310Titre:\0034 %msg.1% \003| \00310Telecharge par:\0034 %msg.2% \003| \00310Duree:\0034 %msg.3%m%msg.4%s\003 | \00310La date:\0034 %msg.5% \003| \00310Vues:\0034 %msg.6% \003| \00310J'aime:\003 \00304%msg.7%\003 | \00310Lien: \0034%msg.9%\003 - \[\00304%msg.8%\003\] -"
# German

set black(ytitle.de.1) "\002\[YouTube\]\002 Titel:\002 %msg.1%\002 | hochgeladen von:\002 %msg.2%\002 | Laenge:\002 %msg.3%m\002%msg.4%\002s\002 | Datum:\002 %msg.5%\002 | Aufrufe:\002 %msg.6%\002 | Like: \002%msg.7%\002 - \[\002%msg.8%\002\] -"
set black(ytitle.de.2) "\[\002You\0030,4Tube\003\002\003\] \00310Title:\0034 %msg.1% \003| \00310Uploaded by:\0034 %msg.2% \003| \00310Length:\0034 %msg.3%m%msg.4%s\003 | \00310Datum:\0034 %msg.5% \003| \00310Aufrufe:\0034 %msg.6% \003| \00310Like:\003 \00304%msg.7%\003 - \[\00304%msg.8%\003\] -"
set black(ytitle.de.3) "\002\[YouTube\]\002 Das Video existiert nicht."
set black(ytitle.de.4) "\002\[YouTube\]\002 Titel:\002 %msg.1%\002 | hochgeladen von:\002 %msg.2%\002 | Laenge:\002 %msg.3%m\002%msg.4%\002s\002 | Datum:\002 %msg.5%\002 | Aufrufe:\002 %msg.6%\002 | Like: \002%msg.7%\002 | Link:\002 %msg.9%\002 - \[\002%msg.8%\002\] -"
set black(ytitle.de.5) "\[\002You\0030,4Tube\003\002\003\] \00310Titel:\0034 %msg.1% \003| \00310hochgeladen von:\0034 %msg.2% \003| \00310Laenge:\0034 %msg.3%m%msg.4%s\003 | \00310Datum:\0034 %msg.5% \003| \00310Aufrufe:\0034 %msg.6% \003| \00310Like:\003 \00304%msg.7%\003 | \00310Link: \0034%msg.9%\003 - \[\00304%msg.8%\003\] -"
# Spanish

set black(ytitle.es.1) "\002\[YouTube\]\002 Titulo:\002 %msg.1%\002 | Cargado por:\002 %msg.2%\002 | Duracion:\002 %msg.3%m\002%msg.4%\002s\002 | Fecha:\002 %msg.5%\002 | Visualizaciones:\002 %msg.6%\002 | Gustos: \002%msg.7%\002 - \[\002%msg.8%\002\] -"
set black(ytitle.es.2) "\[\002You\0030,4Tube\003\002\003\] \00310Titulo:\0034 %msg.1% \003| \00310Cargado por:\0034 %msg.2% \003| \00310Duracion:\0034 %msg.3%m%msg.4%s\003 | \00310Fecha:\0034 %msg.5% \003| \00310Visualizaciones:\0034 %msg.6% \003| \00310Gustos:\003 \00304%msg.7%\003 - \[\00304%msg.8%\003\] -"
set black(ytitle.es.3) "\002\[YouTube\]\002 Este video no existe."
set black(ytitle.es.4) "\002\[YouTube\]\002 Titulo:\002 %msg.1%\002 | Cargado por:\002 %msg.2%\002 | Duracion:\002 %msg.3%m\002%msg.4%\002s\002 | Fecha:\002 %msg.5%\002 | Visualizaciones:\002 %msg.6%\002 | Gustos: \002%msg.7%\002 | Enlazar:\002 %msg.9%\002- \[\002%msg.8%\002\] -"
set black(ytitle.es.5) "\[\002You\0030,4Tube\003\002\003\] \00310Titulo:\0034 %msg.1% \003| \00310Cargado por:\0034 %msg.2% \003| \00310Duracion:\0034 %msg.3%m%msg.4%s\003 | \00310Fecha:\0034 %msg.5% \003| \00310Visualizaciones:\0034 %msg.6% \003| \00310Gustos:\003 \00304%msg.7%\003 | \00310Enlazar: \0034%msg.9%\003 - \[\00304%msg.8%\003\] -"
# Italian

set black(ytitle.it.1) "\002\[YouTube\]\002 Titolo:\002 %msg.1%\002 | Caricato da:\002 %msg.2%\002 | Durata:\002 %msg.3%m\002%msg.4%\002s\002 | Data:\002 %msg.5%\002 | Visualizzazioni:\002 %msg.6%\002 | Piace: \002%msg.7%\002 - \[\002%msg.8%\002\] -"
set black(ytitle.it.2) "\[\002You\0030,4Tube\003\002\003\] \00310Titolo:\0034 %msg.1% \003| \00310Caricato da:\0034 %msg.2% \003| \00310Durata:\0034 %msg.3%m%msg.4%s\003 | \00310Data:\0034 %msg.5% \003| \00310Visualizzazioni:\0034 %msg.6% \003| \00310Piace:\003 \00304%msg.7%\003 - \[\00304%msg.8%\003\] -"
set black(ytitle.it.3) "\002\[YouTube\]\002 Questo video non esiste."
set black(ytitle.it.4) "\002\[YouTube\]\002 Titolo:\002 %msg.1%\002 | Caricato da:\002 %msg.2%\002 | Durata:\002 %msg.3%m\002%msg.4%\002s\002 | Data:\002 %msg.5%\002 | Visualizzazioni:\002 %msg.6%\002 | Piace: \002%msg.7%\002 | Collegamento:\002 %msg.9%\002 - \[\002%msg.8%\002\] -"
set black(ytitle.it.5) "\[\002You\0030,4Tube\003\002\003\] \00310Titolo:\0034 %msg.1% \003| \00310Caricato da:\0034 %msg.2% \003| \00310Durata:\0034 %msg.3%m%msg.4%s\003 | \00310Data:\0034 %msg.5% \003| \00310Visualizzazioni:\0034 %msg.6% \003| \00310Piace:\003 \00304%msg.7%\003 | \00310Collegamento: \0034%msg.9%\003 - \[\00304%msg.8%\003\] -"

set black(ytitle.tr.1) "\002\[YouTube\]\002 Başlık:\002 %msg.1%\002 | Yükleyen:\002 %msg.2%\002 | Uzunluk:\002 %msg.3%m\002%msg.4%\002s\002 | Tarih:\002 %msg.5%\002 | Görüntüleme:\002 %msg.6%\002 | Beğeni: \002%msg.7%\002 - \[\002%msg.8%\002\] -"
set black(ytitle.tr.2) "\[\002You\0030,4Tube\003\002\003\] \00310Başlık:\0034 %msg.1% \003| \00310Yükleyen:\0034 %msg.2% \003| \00310Uzunluk:\0034 %msg.3%m%msg.4%s\003 | \00310Tarih:\0034 %msg.5% \003| \00310Görüntüleme:\0034 %msg.6% \003| \00310Beğeni:\003 \00304%msg.7%\003 - \[\00304%msg.8%\003\] -"
set black(ytitle.tr.3) "\002\[YouTube\]\002 Bu video mevcut değil."
set black(ytitle.tr.4) "\002\[YouTube\]\002 Başlık:\002 %msg.1%\002 | Yükleyen:\002 %msg.2%\002 | Uzunluk:\002 %msg.3%m\002%msg.4%\002s\002 | Tarih:\002 %msg.5%\002 | Görüntüleme:\002 %msg.6%\002 | Beğeni: \002%msg.7%\002 | Link:\002 %msg.9%\002- \[\002%msg.8%\002\] -"
set black(ytitle.tr.5) "\[\002You\0030,4Tube\003\002\003\] \00310Başlık:\0034 %msg.1% \003| \00310Yükleyen:\0034 %msg.2% \003| \00310Uzunluk:\0034 %msg.3%m%msg.4%s\003 | \00310Tarih:\0034 %msg.5% \003| \00310Görüntüleme:\0034 %msg.6% \003| \00310Beğeni:\003 \00304%msg.7%\003 | \00310Link: \0034%msg.9%\003 - \[\00304%msg.8%\003\] -"

putlog "\002$ytitle(projectName) $ytitle(version)\002 coded by $ytitle(author) ($ytitle(website)): Loaded."

##############
##########################################################
##   END                                                 #
##########################################################
