#Covid19.tcl

set corona(country) "GLOBAL"
set corona(language) "en"
set corona(flags) "-|-"
set corona(ignore_prot) "1"
set corona(flood_prot) "3:10"
set corona(time_check) "60"

#Country list

set corona(country_list) {
"USA"
"China"
"Italy"
"Spain"
"Germany"
"Iran"
"France"
"Switzerland"
"UK"
"S. Korea"
"Netherlands"
"Austria"
"Belgium"
"Canada"
"Turkey"
"Portugal"
"Norway"
"Australia"
"Brazil"
"Sweden"
"Israel"
"Malaysia"
"Czechia"
"Denmark"
"Ireland"
"Luxembourg"
"Japan"
"Ecuador"
"Chile"
"Poland"
"Pakistan"
"Thailand"
"Romania"
"Saudi Arabia"
"Finland"
"South Africa"
"Indonesia"
"Greece"
"Russia"
"Iceland"
"India"
"Diamond Princess"
"Philippines"
"Singapore"
"Peru"
"Slovenia"
"Panama"
"Qatar"
"Estonia"
"Argentina"
"Egypt"
"Croatia"
"Colombia"
"Dominican Republic"
"Mexico"
"Bahrain"
"Serbia"
"Hong Kong"
"Iraq"
"Lebanon"
"Algeria"
"UAE"
"Lithuania"
"Armenia"
"New Zealand"
"Morocco"
"Bulgaria"
"Hungary"
"Taiwan"
"Latvia"
"Costa Rica"
"Slovakia"
"Andorra"
"Uruguay"
"Jordan"
"San Marino"
"Kuwait"
"North Macedonia"
"Tunisia"
"Ukraine"
"Bosnia and Herzegovina"
"Moldova"
"Albania"
"Vietnam"
"Burkina Faso"
"Cyprus"
"Faeroe Islands"
"RÃ©union"
"Malta"
"Ghana"
"Azerbaijan"
"Brunei"
"Kazakhstan"
"Oman"
"Sri Lanka"
"Venezuela"
"Senegal"
"Cambodia"
"Ivory Coast"
"Afghanistan"
"Belarus"
"Palestine"
"Mauritius"
"Georgia"
"Cameroon"
"Uzbekistan"
"Guadeloupe"
"Montenegro"
"Cuba"
"Channel Islands"
"Martinique"
"Nigeria"
"Trinidad and Tobago"
"Liechtenstein"
"Honduras"
"DRC"
"Bangladesh"
"Kyrgyzstan"
"Bolivia"
"Paraguay"
"Rwanda"
"Mayotte"
"Monaco"
"Kenya"
"Macao"
"French Polynesia"
"French Guiana"
"Jamaica"
"Gibraltar"
"Isle of Man"
"Guatemala"
"Madagascar"
"Togo"
"Aruba"
"Barbados"
"Zambia"
"New Caledonia"
"Uganda"
"El Salvador"
"Maldives"
"Tanzania"
"Equatorial Guinea"
"Ethiopia"
"Djibouti"
"Dominica"
"Mongolia"
"Saint Martin"
"Cayman Islands"
"Haiti"
"Namibia"
"Suriname"
"Gabon"
"Niger"
"Bermuda"
"Mozambique"
"Seychelles"
"CuraÃ§ao"
"Benin"
"Greenland"
"Laos"
"Guyana"
"Bahamas"
"Fiji"
"Syria"
"Cabo Verde"
"Angola"
"Congo"
"Eritrea"
"Guinea"
"Vatican City"
"Mali"
"Eswatini"
"Gambia"
"Sudan"
"Zimbabwe"
"Nepal"
"Antigua and Barbuda"
"CAR"
"Chad"
"Liberia"
"Mauritania"
"Myanmar"
"St. Barth"
"Saint Lucia"
"Sint Maarten"
"Belize"
"Bhutan"
"British Virgin Islands"
"Guinea-Bissau"
"Montserrat"
"Nicaragua"
"Saint Kitts and Nevis"
"Somalia"
"Turks and Caicos"
"Grenada"
"Libya"
"Papua New Guinea"
"St. Vincent Grenadines"
"Timor-Leste"
}

#			Try to edit only the language :-)

package require tls
package require http

bind pub $corona(flags) !covid corona:pub

setudef flag covid
setudef flag autocovid

setudef str covid-country
setudef str covid-lang


###
if {![info exists corona(timer_start)]} {
	timer $corona(time_check) corona:auto_timer
	set corona(timer_start) 1
}

###
proc corona:auto_timer {} {
	global corona
	set channels ""
foreach chan [channels] {
	if {[channel get $chan autocovid] && [validchan $chan]} {
	lappend channels $chan
		}
	}
if {$channels != ""} {
	set data [corona:getdata]
	corona:auto_check $data $channels 0
	} else {
	timer $corona(time_check) corona:auto_timer
	}
}

###
proc corona:auto_check {data channels num} {
	global corona
	set chan [lindex $channels $num]
	set country [join [channel get $chan covid-country]]
if {$country == ""} { set country $corona(country) }
if {[string tolower $country] == "global"} {
	set total 1
} else { set total 0 }
	set extract [corona:extract $data $country $total]
	set total_cases [lindex $extract 0]
	set new_cases [lindex $extract 1]
	set total_deaths [lindex $extract 2]
	set new_deaths [lindex $extract 3]
	set total_recovered [lindex $extract 4]
	set active_cases [lindex $extract 5]
	set serious_critical [lindex $extract 6]
	set totalcases_per_milion [lindex $extract 7]
if {$new_cases == ""} { set new_cases - } else { set new_cases "+$new_cases" }
if {$total_deaths == ""} { set total_deaths - }
if {$new_deaths == ""} { set new_deaths - } else { set new_deaths "+$new_deaths" }
if {$total_recovered == ""} { set total_recovered - }
if {$active_cases == ""} { set active_cases - }
if {$totalcases_per_milion == ""} { set totalcases_per_milion - }

if {[info exists corona($chan:autocovid:entry)]} {
if {$corona($chan:autocovid:entry) != $extract} {
	set corona($chan:autocovid:entry) $extract
	corona:say "" $chan [list $country $total_cases $new_cases $total_deaths $new_deaths $total_recovered $active_cases $serious_critical $totalcases_per_milion] 4
	}
} else {
	set corona($chan:autocovid:entry) $extract
	corona:say "" $chan [list $country $total_cases $new_cases $total_deaths $new_deaths $total_recovered $active_cases $serious_critical $totalcases_per_milion] 4
	}
	set next_num [expr $num + 1]
if {[lindex $channels $next_num] != ""} {
	utimer 5 [list corona:auto_check $data $channels $next_num]
	} else {
	timer $corona(time_check) corona:auto_timer
	}
}


###
proc corona:getdata {} {
	set link "https://www.worldometers.info/coronavirus/"
	http::register https 443 [list ::tls::socket -autoservername true]
	set ipq [http::config -useragent "lynx"]
	set ipq [::http::geturl "$link" -timeout 5000] 
	set status [::http::status $ipq]
if {$status != "ok"} { 
	::http::cleanup $ipq
	return 
}
	set data [http::data $ipq]
	::http::cleanup $ipq
	return $data
}

###
proc corona:pub {nick host hand chan arg} {
	global corona
if {![channel get $chan covid]} {
	return
}
	set total 0
	set flood_protect [corona:flood:prot $chan $host]
if {$flood_protect == "1"} {
	set get_seconds [corona:get:flood_time $host $chan]
	corona:say $nick "NOTC" [list $get_seconds] 2
	return
}
	set country [join [lrange [split $arg] 0 end]]
if {$country == ""} {
	set total 1
	set country "GLOBAL"
} else {
	set find_country [lsearch -nocase $corona(country_list) $country]
if {$find_country < 0} {
	corona:say $nick $chan "" 1
	return
} else {
	set country [lindex $corona(country_list) $find_country]
	}
}
	set data [corona:getdata]
	set extract [corona:extract $data $country $total]
	set total_cases [lindex $extract 0]
	set new_cases [lindex $extract 1]
	set total_deaths [lindex $extract 2]
	set new_deaths [lindex $extract 3]
	set total_recovered [lindex $extract 4]
	set active_cases [lindex $extract 5]
	set serious_critical [lindex $extract 6]
	set totalcases_per_milion [lindex $extract 7]
if {$new_cases == ""} { set new_cases - }
if {$total_deaths == ""} { set total_deaths - }
if {$new_deaths == ""} { set new_deaths - }
if {$total_recovered == ""} { set total_recovered - }
if {$active_cases == ""} { set active_cases - }
if {$totalcases_per_milion == ""} { set totalcases_per_milion - }
if {$serious_critical == ""} { set serious_critical "-" }
	corona:say $nick $chan [list $country $total_cases $new_cases $total_deaths $new_deaths $total_recovered $active_cases $serious_critical $totalcases_per_milion] 3
}

###
proc corona:extract {data country total} {
	global corona
if {$total == "1"} {
	set split_data [split $data "\n"]
	set information ""
	set c 0
	set first_line 0
	set last_line 0
	set var "(.*)<td><strong>Total:</strong></td>"
foreach line $split_data {
	set line [concat $line]
if {[regexp -nocase $var $line]} {
	set first_line $c
	continue
	}
if {[string match -nocase "*</tr>" $line] && $first_line > 0} {
	set last_line $c
	break
}
	set c [expr $c + 1]
}
	set new_data [lrange $split_data $first_line $last_line]
foreach line $new_data {
	set test [regexp {<td style="(.*)">\d*(.*)\d*</td>|<td>(.*)</td>} $line match]
if {$test > 0} {
	regsub {</td>(.*)} $match "" m
	lappend information $m
	}
}
	set total_cases [corona:filter [lindex $information 1] 0]
	set new_cases [corona:filter [lindex $information 2] 0]
	set total_deaths [corona:filter [lindex $information 3] 0]
	set new_deaths [corona:filter [lindex $information 4] 0]
	set total_recovered [corona:filter [lindex $information 5] 0]
	set active_cases [corona:filter [lindex $information 6] 0]
	set serious_cases [corona:filter [lindex $information 7] 0]
	set totalcases_per_milion [corona:filter [lindex $information 8] 0]
	return [list $total_cases $new_cases $total_deaths $new_deaths $total_recovered $active_cases $serious_cases $totalcases_per_milion]
} else {
	set split_data [split $data "\n"]
	set information ""
	set c 0
	set first_line 0
	set last_line 0
	set var "(.*)<td style=\"font-weight: bold; font-size:15px; text-align:left;\">$country</td>|<td style=\"font-weight: bold; font-size:15px; text-align:left;\"><a class=\"mt_a\" href=\"(.*)\">$country</a></td>"
foreach line $split_data {
	set line [concat $line]
if {[regexp $var $line]} {
	set first_line $c
	continue
	}
if {[string match -nocase "*</tr>" $line] && $first_line > 0} {
	set last_line $c
	break
}
	set c [expr $c + 1]
}
	set new_data [lrange $split_data $first_line $last_line]
foreach line $new_data {
	set test [regexp {<td style="(.*)">\d*(.*)\d*</td>} $line match]
if {$test > 0} {
	regsub {</td>(.*)} $match "" m
	lappend information $m
	}
}

	set country [corona:filter [lindex $information 0] 0]
	set total_cases [corona:filter [lindex $information 1] 0]
	set new_cases [corona:filter [lindex $information 2] 0]
	set total_deaths [corona:filter [lindex $information 3] 0]
	set new_deaths [corona:filter [lindex $information 4] 0]
	set total_recovered [corona:filter [lindex $information 5] 0]
	set active_cases [corona:filter [lindex $information 6] 0]
	set serious_cases [corona:filter [lindex $information 7] 0]
	set totalcases_per_milion [corona:filter [lindex $information 8] 0]
	return [list $total_cases $new_cases $total_deaths $new_deaths $total_recovered $active_cases $serious_cases $totalcases_per_milion]

	}
}

###
proc corona:filter {text type} {
if {$type == "0"} {
	set split_text [split $text ">"]
	return [concat [lindex $split_text 1]]
} else {
	set text [string map {"<td>" ""} $text]
	return [concat $text]
	}
}

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

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

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

###
proc corona:get:flood_time {host chan} {
	global corona
		foreach tmr [utimers] {
if {[string match "*corona:expire:flood $host $chan*" [join [lindex $tmr 1]]]} {
	return [lindex $tmr 0]
		}
	}
}

###
proc corona:say {nick chan arg num} {
	global corona
	set inc 0
if {$chan == "NOTC"} {
	set lang [string tolower $corona(language)]
} else {
	set lang [string tolower [channel get $chan covid-lang]]
if {$lang == ""} {
	set lang [string tolower $corona(language)]
} else {
if {[info exists corona($lang.lang.1)]} {
	set lang $lang
	} else {
	set lang [string tolower $corona(language)]
		}
	}
}
foreach s $arg {
	set inc [expr $inc + 1]
	set replace(%msg.$inc%) $s
}
	set reply [string map [array get replace] $corona($lang.lang.$num)]
if {$chan == "NOTC"} {
	putserv "NOTICE $nick :$reply"
} else {
	putserv "PRIVMSG $chan :$reply"
	}
}

set corona(name) "Covid-19"
set corona(owner) "dex"
set corona(site) "www.ayochat.or.id"
set corona(version) "1.2.2"

####
#Language
#
###
set corona(en.lang.1) "Invalid country specified or NO CASES"
set corona(en.lang.2) "You exceded the number of commands. Please wait \002%msg.1%\002 seconds."
set corona(en.lang.3) "\002COVID-19\002 Status »» \002%msg.1%\002 »» Positif: \002%msg.2%\002  \037%msg.3%\037 »» Meninggal: \002%msg.4%\002 »» Sembuh: \037%msg.5%\037"
set corona(en.lang.4) "(AUTO) \002COVID-19\002 Status »» \002%msg.1%\002 »» Positif: \002%msg.2%\002 \037%msg.3%\037 »» Meninggal: \002%msg.4%\002 »» Sembuh: \037%msg.5%\037"

set corona(ro.lang.1) "Tara invalida sau nu sunt cazuri."
set corona(ro.lang.2) "Ai depasit number de comenzi. Te rog asteapta \002%msg.1%\002 secunde."
set corona(ro.lang.3) "Statistici \002COVID-19\002 -- \002%msg.1%\002 -- Cazuri totale: \002%msg.2%\002 ; Cazuri noi: \037%msg.3%\037 ; Total decedati: \002%msg.4%\002 ; Noi decedati: \037%msg.5%\037 ; Recuperati: \002%msg.6%\002 ; Cazuri active: \037%msg.7%\037 ; Cazuri critice: \002%msg.8%\002 ; Total cazuri/1M populatie: \002%msg.9%\002"
set corona(ro.lang.4) "(AUTO) Statistici \002COVID-19\002 pentru -- %msg.1% -- Cazuri totale: \002%msg.2%\002 ; Cazuri noi: \037%msg.3%\037 ; Total decedati: \002%msg.4%\002 ; Noi decedati: \037%msg.5%\037 ; Recuperati: \002%msg.6%\002 ; Cazuri active: \037%msg.7%\037 ; Cazuri critice: \002%msg.8%\002 ; Total cazuri/1M populatie: \002%msg.9%\002"

putlog "$corona(name) $corona(version) TCL by $corona(owner) loaded. For more tcls visit -- $corona(site) --"
