/shoutcast.tcl

https://bitbucket.org/thesky/shoutcastbot-eggdrop · TCL · 945 lines · 748 code · 53 blank · 144 comment · 188 complexity · fda1dfc12ed5c95a88e1b07fe4e7de4b MD5 · raw file

  1. ###############################
  2. ###Shoutcast Radio Bot by Sky##
  3. ###############################
  4. # We need to grab stuff from the internet
  5. package require http
  6. # Set Variables to Shoutcast Webpages
  7. set siteurl "http://216.104.37.26:9005"
  8. set listenurl "http://216.104.37.26:9005/listen.pls"
  9. set lastplayed "http://216.104.37.26:9005/played.html"
  10. # Channels Names (Log Channel, DJ Channel, Radio Channel, Main Channel)
  11. set djchan "#dj"
  12. set mainchan "#main"
  13. set logchan "#log"
  14. set radiochan "#radio"
  15. # Set Variables to Database Info
  16. set dbhost "localhost"
  17. set dbuser "user"
  18. set dbpass "password"
  19. set dbname "db"
  20. # AutoDJ's Name
  21. set autodj "AutoDj"
  22. sset song 0
  23. # Binding ! commands to fucntions
  24. bind pub -|- !lst lastsessions
  25. bind pub -|- !start starttimers
  26. bind pub -|- !lp songlist
  27. bind pub -|- !server serverinfo
  28. bind pub -|- !clearlist clearlist
  29. bind pub -|- !reqlist requestlist
  30. bind pub -|- !np nowplaying
  31. bind pub -|- !request request
  32. bind pub -|- !peak peak
  33. bind pub -|- !dj deejay
  34. bind pub -|- !genre genre
  35. bind pub -|- !listen pls
  36. bind pub -|- !log getlog
  37. bind pub -|- !topdj toplog
  38. bind pub -|- !url site
  39. bind pub -|- !commands commands
  40. # Functions: Start and Stop Timer
  41. proc hasTimers {} {
  42. set timerList [timers];
  43. return [llength $timerList];
  44. }
  45. proc stop {} {
  46. set timerList [timers];
  47. foreach timer $timerList {
  48. killtimer [lindex $timer 2];
  49. }
  50. }
  51. ## Functions for if DJ is Online
  52. proc djonline {} {
  53. global dj
  54. return [info exists dj]
  55. }
  56. proc djauto {} {
  57. global dj
  58. if {![djonline]} { return 0 }
  59. return [isautodj $dj]
  60. }
  61. proc isautodj {string} {
  62. global autodj
  63. expr {$string == $autodj}
  64. }
  65. ## Refreshing bot resets timers
  66. proc start {} {
  67. global logchan
  68. if {[hasTimers] == 0} {
  69. putnow "PRIVMSG $logchan :No Timers Active. Activating Timer Now."
  70. #Starting Check DJ Timer
  71. checkdj
  72. #Starting Check Peak Timer
  73. maxlisteners
  74. #Starting Advertisment Timer
  75. #run_periodically #TI-Radio
  76. #Starting Advertisement Timer
  77. #run_periodically #Torrent-Invites
  78. #Starting Delete Request Timer
  79. deletereq
  80. set firstad 0
  81. } else {
  82. putnow "PRIVMSG $logchan :Timer Active Already. Killing Active Timer Now."
  83. # Stop All Timers 2
  84. stop
  85. putnow "PRIVMSG $logchan :Activating Timer Now"
  86. #Starting Check DJ Timer
  87. checkdj
  88. #Starting Check Peak Timer
  89. maxlisteners
  90. #Starting Advertisment Timer
  91. #run_periodically #Torrent-Invites
  92. #Starting Advertisement Timer
  93. #run_periodically #TI-Radio
  94. #Starting Delete Request Timer
  95. deletereq
  96. set firstad 0
  97. }
  98. }
  99. ## Advertisments for who is !NP
  100. # proc run_periodically {chan} {
  101. # if {[validchan #BotDev] && [botonchan #BotDev]} {
  102. # global siteurl djchan title dj logchan listenurl radiochan dj song genre firstad autodj
  103. # ::http::config -useragent "Mozilla/5.0; Shoutinfo"
  104. # timer 60 [list run_periodically $chan]
  105. # set http_req [::http::geturl $siteurl -timeout 2000]
  106. # if {[::http::status $http_req] != "ok"} {
  107. # putnow "PRIVMSG $logchan :ABORT ABORT"
  108. # } else {
  109. # set data [::http::data $http_req]
  110. # ::http::cleanup $http_req
  111. # if {[regexp {<font class=default>Stream Title: </font></td><td><font class=default><b>([^<]+)</b>} $data x title]} {
  112. # set dj $title
  113. # } else {
  114. # catch {unset dj}
  115. # }
  116. # if {[regexp {<font class=default>Stream Genre: </font></td><td><font class=default><b>([^<]+)</b>} $data x title]} {
  117. # set genre $title
  118. # } else {
  119. # catch {unset genre}
  120. # }
  121. # if {[regexp {<font class=default>Current Song: </font></td><td><font class=default><b>([^<]+)</b>} $data x title]} {
  122. # set song $title
  123. # } else {
  124. # catch {unset song}
  125. # }
  126. # if {$dj == $autodj || $song == 0 || $dj == 0 || $dj == 0} {
  127. # putnow "PRIVMSG $logchan :AutoDJ is On or Server is Offline."
  128. # } else {
  129. # if {[string match *c* [lindex [split [getchanmode $chan]] 0]]} {
  130. # set a "\002$dj is live on the TI-Radio\002 || \002Genre\002: $genre || "
  131. # set b "\002Now Playing\002: $song || \002Listen @ $listenurl \002"
  132. # putnow "PRIVMSG $chan : $a$b"
  133. # } else {
  134. # set c "\002\00303$dj is live on the TI-Radio\002\00303\00307 || "
  135. # set d "\00307\003\002Genre\002: $genre \003\00307|| \00307\003\002Now Playing"
  136. # set e "\002: $song \003\00307||\00307 \003\00304\002 Listen @ $listenurl \002\00304"
  137. # putnow "PRIVMSG $chan : $c$d$e"
  138. # }
  139. # }
  140. # }
  141. # }
  142. # return 1;
  143. # }
  144. ## Checks if DJ has changed - 10s
  145. proc checkdj {} {
  146. global dj logchan siteurl newdj djchan
  147. if {![validchan #BotDev] || ![botonchan #BotDev]} { return }
  148. ::http::config -useragent "Mozilla/5.0; Shoutinfo"
  149. set http_req [::http::geturl $siteurl -timeout 2000]
  150. after 10000 [list checkdj]
  151. if {[::http::status $http_req] != "ok"} {
  152. # we assume the server is offline
  153. putnow "PRIVMSG $logchan :Assumption: Server is Offline or Lagging"
  154. return
  155. }
  156. set data [::http::data $http_req]
  157. ::http::cleanup $http_req
  158. if {![regexp {<font class=default>Stream Title: </font></td><td><font class=default><b>([^<]+)</b>} $data x newdj]} {
  159. if {[djonline]} {
  160. #Online -> Offline
  161. if {[djauto]} {
  162. #Auto DJ -> Offline
  163. serveroffline
  164. catch {unset dj}
  165. } elseif {![isautodj $newdj]} {
  166. #Real DJ -> Offline
  167. offair
  168. finishsave $dj
  169. catch {unset dj}
  170. }
  171. }
  172. return
  173. }
  174. if {![djonline]} {
  175. # Offline -> Online
  176. if {![isautodj $newdj]} {
  177. #Offline -> Real DJ
  178. onair
  179. set dj $newdj
  180. startsave $dj
  181. } else {
  182. #Offline -> Auto DJ
  183. set dj $newdj
  184. autodjon
  185. }
  186. } elseif {$dj != $newdj} {
  187. # Generic state change
  188. if {[djauto]} {
  189. #AutoDJ -> Real DJ
  190. set dj $newdj
  191. startsave $dj
  192. onair
  193. } elseif {[isautodj $newdj]} {
  194. #Real DJ -> AutoDJ
  195. finishsave $dj
  196. autooffair
  197. set dj $newdj
  198. } else {
  199. #Real DJ -> Real DJ
  200. tempoffair
  201. finishsave $dj
  202. set dj $newdj
  203. startsave $dj
  204. onair
  205. }
  206. }
  207. return
  208. }
  209. ## Checks if peaked in listeners
  210. proc maxlisteners {} {
  211. global logchan siteurl peakfile djchan
  212. ::http::config -useragent "Mozilla/5.0; Shoutinfo"
  213. set http_req [::http::geturl $siteurl -timeout 2000]
  214. after 100000 [list maxlisteners]
  215. if {[::http::status $http_req] != "ok"} {
  216. putnow "PRIVMSG $logchan :Assumption: Server is Offline or Lagging"
  217. } else {
  218. set data [::http::data $http_req]
  219. ::http::cleanup $http_req
  220. if {[regexp {<font class=default>Listener Peak: </font></td><td><font class=default><b>([^<]+)</b>} $data x title]} {
  221. set testdata $title
  222. set peakfile [open "peak.txt" r]
  223. gets $peakfile line
  224. set peakhigh $line
  225. close $peakfile
  226. if {$testdata > $peakhigh} {
  227. # Create a Timestamp for the file
  228. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  229. set filename "peak.txt"
  230. # Create a Temp and Backup File
  231. set temp $filename.new.$timestamp
  232. # Set In/Out to Open Peak.txt in Read-Only/Write-Only
  233. set in [open $filename r]
  234. set out [open $temp w]
  235. while {[gets $in line] != -1} {
  236. set line $testdata
  237. puts $out $line
  238. }
  239. putnow "PRIVMSG $djchan :Max Viewers has increased :D! Good job."
  240. # Close Both Files
  241. close $in
  242. close $out
  243. # Rename Temp to Original File Name
  244. file rename -force $temp $filename
  245. }
  246. }
  247. }
  248. }
  249. ## Command to Start Timers
  250. proc starttimers {nick uhost hand chan arg} {
  251. global djchan
  252. if {$chan == $djchan} {
  253. if {[isop $nick $djchan] == 1 || [ishalfop $nick $djchan] == 1} {
  254. putserv "PRIVMSG $chan :$nick started the timers."
  255. putlog "$nick started the timers."
  256. start
  257. }
  258. }
  259. }
  260. ## Command to get Last Played List
  261. proc songlist {nick uhost hand chan arg} {
  262. global dj logchan siteurl newdj lastplayed
  263. if {![validchan #BotDev] || ![botonchan #BotDev]} { return }
  264. ::http::config -useragent "Mozilla/5.0; Shoutinfo"
  265. set http_req [::http::geturl $lastplayed -timeout 2000]
  266. if {[::http::status $http_req] != "ok"} {
  267. # we assume the server is offline
  268. putnow "PRIVMSG $logchan : Unable to connect"
  269. return
  270. }
  271. set data [::http::data $http_req]
  272. ::http::cleanup $http_req
  273. set testvalue 0
  274. foreach {x length title} [regexp -all -inline {<td>(\d\d:\d\d:\d\d)</td><td>([^<]+-[^<]+)<} $data] {
  275. if {$testvalue != 0} {
  276. putnow "notice $nick :#$testvalue: $length | $title"
  277. incr testvalue
  278. } else {
  279. putnow "notice $nick :\002Current Song\002: $length | $title "
  280. incr testvalue
  281. }
  282. }
  283. }
  284. ## Advert the Request List
  285. proc requestlist {nick uhost hand chan arg} {
  286. global djchan
  287. putnow "NOTICE $nick :\002\The OLDEST request will be deleted every 10 minutes.\002"
  288. if {$chan == $djchan} {
  289. set reqnumber 0
  290. set filename "requestlist.txt"
  291. set in [open $filename r]
  292. while {1} {
  293. set line [gets $in]
  294. if {$line == ""} {
  295. putnow "PRIVMSG $djchan :There are no requests at the moment."
  296. break
  297. } else {
  298. if {[eof $in]} {
  299. putnow "PRIVMSG $djchan :Oldest: $line"
  300. close $in
  301. break
  302. }
  303. if {$reqnumber == 0} {
  304. putnow "PRIVMSG $djchan :Newest: $line"
  305. incr reqnumber
  306. } else {
  307. putnow "PRIVMSG $djchan :$reqnumber: $line"
  308. incr reqnumber
  309. }
  310. }
  311. }
  312. }
  313. }
  314. ## Clear the Request List
  315. proc clearlist {nick uhost hand chan arg} {
  316. global djchan
  317. if {$chan == $djchan} {
  318. if {[isop $nick $chan] == 1 || [ishalfop $nick $chan] == 1} {
  319. set filename "requestlist.txt"
  320. set out [open $filename w]
  321. set line ""
  322. puts $out $line
  323. putnow "PRIVMSG $djchan :$nick has cleared the request list."
  324. close $out
  325. }
  326. }
  327. }
  328. ## Delete Request Func + Timer
  329. proc requestproc {reqitem} {
  330. set filename "requestlist.txt"
  331. set testin [open $filename r]
  332. set line [gets $testin]
  333. close $testin
  334. if {$line == ""} {
  335. set out [open $filename [list RDWR APPEND CREAT]]
  336. set newline $reqitem
  337. puts -nonewline $out $newline
  338. close $out
  339. } else {
  340. # Create a Timestamp for the file
  341. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  342. # Create a Temp and Backup File
  343. set temp $filename.new.$timestamp
  344. # Set In/Out to Open Peak.txt in Read-Only/Write-Only
  345. set in [open $filename r]
  346. set out [open $temp [list RDWR APPEND CREAT]]
  347. set line $reqitem
  348. puts $out $line
  349. set file_data [read $in]
  350. puts -nonewline $out $file_data
  351. close $in
  352. close $out
  353. file rename -force $temp $filename
  354. }
  355. }
  356. ## Command to Show Now Playing
  357. proc nowplaying {nick uhost hand chan arg} {
  358. global siteurl radiochan mainchan
  359. set radiochan [string tolower $radiochan]
  360. ::http::config -useragent "Mozilla/5.0; Shoutinfo"
  361. set http_req [::http::geturl $siteurl -timeout 2000]
  362. if {[::http::status $http_req] != "ok"} {
  363. putnow "PRIVMSG $chan :Stream is unavailable";
  364. }
  365. set data [::http::data $http_req]
  366. ::http::cleanup $http_req
  367. if {[regexp {<font class=default>Current Song: </font></td><td><font class=default><b>([^<]+)</b>} $data x title]} {
  368. putnow "PRIVMSG $chan :\002Current Song\002: $title"
  369. } else {
  370. if {$chan != $mainchan} {
  371. putnow "PRIVMSG $chan :Couldn't receive any information, checking server status..."
  372. } else {
  373. putnow "NOTICE $nick :Couldn't receive any information, checking server status..."
  374. }
  375. }
  376. }
  377. ## Command to show who is DJ
  378. proc deejay {nick uhost hand chan arg} {
  379. global siteurl radiochan dj mainchan
  380. set radiochan [string tolower $radiochan]
  381. ::http::config -useragent "Mozilla/5.0; Shoutinfo"
  382. set http_req [::http::geturl $siteurl -timeout 2000]
  383. if {[::http::status $http_req] != "ok"} {
  384. putnow "PRIVMSG $chan :Stream is unavailable";
  385. }
  386. set data [::http::data $http_req]
  387. ::http::cleanup $http_req
  388. if {[regexp {<font class=default>Stream Title: </font></td><td><font class=default><b>([^<]+)</b>} $data x title]} {
  389. if {$chan != $mainchan} {
  390. putnow "PRIVMSG $chan :\002DJ\002: $title"
  391. set dj $title
  392. } else {
  393. putnow "NOTICE $nick :\002DJ\002: $title"
  394. set dj $title
  395. }
  396. } else {
  397. if {$chan != $mainchan} {
  398. putnow "PRIVMSG $chan :Couldn't receive any information, checking server status..."
  399. } else {
  400. putnow "NOTICE $nick :Couldn't receive any information, checking server status..."
  401. }
  402. }
  403. }
  404. ## Command to get URL Info
  405. proc site {nick uhost hand chan arg} {
  406. global siteurl mainchan
  407. if {$chan != $mainchan} {
  408. putnow "PRIVMSG $chan :\002Website\002: $siteurl"
  409. } else {
  410. putnow "NOTICE $nick :\002Website\002: $siteurl"
  411. }
  412. }
  413. ## Command to get Server Info
  414. proc serverinfo {nick uhost hand chan arg} {
  415. global siteurl radiochan mainchan
  416. set radiochan [string tolower $radiochan]
  417. ::http::config -useragent "Mozilla/5.0; Shoutinfo"
  418. set http_req [::http::geturl $siteurl -timeout 2000]
  419. if {[::http::status $http_req] != "ok"} {
  420. putnow "PRIVMSG $chan :Stream is unavailable";
  421. }
  422. set data [::http::data $http_req]
  423. ::http::cleanup $http_req
  424. if {[regexp {<font class=default>Server Status: </font></td><td><font class=default><b>([^<]+)</b>} $data x title]} {
  425. if {$chan != $mainchan} {
  426. putnow "PRIVMSG $chan :\ 002Server \002: $title"
  427. } else {
  428. putnow "PRIVMSG $nick :\ 002Server \002: $title"
  429. }
  430. } else {
  431. if {$chan != $mainchan} {
  432. putnow "PRIVMSG $chan :Couldn't contact the server, please check the configuration and/or streaming server"
  433. } else {
  434. putnow "NOTICE $nick :Couldn't contact the server, please check the configuration and/or streaming server"
  435. }
  436. }
  437. }
  438. ## Command to get current Genre
  439. proc genre {nick uhost hand chan arg} {
  440. global siteurl radiochan mainchan
  441. set radiochan [string tolower $radiochan]
  442. ::http::config -useragent "Mozilla/5.0; Shoutinfo"
  443. set http_req [::http::geturl $siteurl -timeout 2000]
  444. if {[::http::status $http_req] != "ok"} {
  445. putnow "PRIVMSG $chan :Stream is unavailable";
  446. }
  447. set data [::http::data $http_req]
  448. ::http::cleanup $http_req
  449. if {[regexp {<font class=default>Stream Genre: </font></td><td><font class=default><b>([^<]+)</b>} $data x title]} {
  450. if {$chan != $mainchan} {
  451. putnow "PRIVMSG $chan :\002Genre\002: $title"
  452. } else {
  453. putnow "NOTICE $nick :\002Genre\002: $title"
  454. }
  455. } else {
  456. if {$chan != $mainchan} {
  457. putnow "PRIVMSG $chan :Couldn't receive any information, checking server status..."
  458. } else {
  459. putnow "NOTICE $nick :Couldn't receive any information, checking server status..."
  460. }
  461. }
  462. }
  463. ## Command to get Peak Info
  464. proc peak {nick uhost hand chan arg} {
  465. global siteurl radiochan mainchan
  466. set radiochan [string tolower $radiochan]
  467. ::http::config -useragent "Mozilla/5.0; Shoutinfo"
  468. set http_req [::http::geturl $siteurl -timeout 20000]
  469. if {[::http::status $http_req] != "ok"} {
  470. putnow "PRIVMSG $chan :Stream is unavailable";
  471. }
  472. set data [::http::data $http_req]
  473. ::http::cleanup $http_req
  474. if {[regexp {<font class=default>Listener Peak: </font></td><td><font class=default><b>([^<]+)</b>} $data x title]} {
  475. if {$chan != $mainchan} {
  476. set peakfile [open "peak.txt" r]
  477. gets $peakfile line
  478. set peakhigh $line
  479. close $peakfile
  480. putnow "PRIVMSG $chan :\002Session Peak\002: $title \002Overall Peak\002: $peakhigh "
  481. }
  482. } else {
  483. if {$chan != $mainchan} {
  484. putnow "PRIVMSG $chan :Couldn't receive any information, checking server status..."
  485. } else {
  486. putnow "NOTICE $nick :Couldn't receive any information, checking server status..."
  487. }
  488. }
  489. }
  490. ## Command to get Listen
  491. proc pls {nick uhost hand chan arg} {
  492. global listenurl
  493. putnow "PRIVMSG $chan :\002Stream/Listen\002: $listenurl"
  494. }
  495. ## Command to get Command FAQ
  496. proc commands {nick uhost hand chan arg} {
  497. global radiochan djchan mainchan
  498. set radiochan [string tolower $radiochan]
  499. if {$chan != $mainchan} {
  500. if {$chan == $djchan} {
  501. set a "\002DJ/Admin Commands\002: !rehash !start !login "
  502. set b "+cookie -cookie !reqlist !clearlist"
  503. set c "\002Radio Commands\002: !listen !np !lp !dj !peak"
  504. set d "!request !genre !server !topdj"
  505. putnow "PRIVMSG $chan : $a$b"
  506. putnow "PRIVMSG $chan : $c$d"
  507. } else {
  508. set e "\002Radio Commands\002: !listen !np !lp !dj !peak "
  509. set f "!request !genre !server !topdj"
  510. putnow "PRIVMSG $chan : $e$f"
  511. }
  512. } else {
  513. set g "\002Radio Commands\002: !listen !np !lp !dj !peak "
  514. set h "!request !genre !server !topdj"
  515. putnow "NOTICE $nick : $g$h"
  516. }
  517. }
  518. ## Command to submit a request
  519. proc request {nick uhost hand chan arg} {
  520. global radiochan djchan reqitem
  521. set radiochan [string tolower $radiochan]
  522. if {[string tolower $chan] == "$radiochan"} {
  523. if {[llength $arg]==0} {
  524. putnow "PRIVMSG $chan :\002Syntax\002: !request <Artist - Title>"
  525. } else {
  526. if {[string match *Bieber* $arg] || [string match *Beiber* $arg]} {
  527. if {[isop $nick $chan] || [ishalfop $nick $chan]} {
  528. putnow "PRIVMSG $chan :Troll Alert!"
  529. } else {
  530. putnow "PRIVMSG $chan :Troll Alert!"
  531. putkick $chan $nick "Stop trolling.."
  532. }
  533. } else {
  534. set reqitem $arg
  535. requestproc $reqitem
  536. set query1 "http://www.waffles.fm/browse.php?q="
  537. set query2 "http://what.cd/torrents.php?searchstr="
  538. for { set index 0 } { $index<[llength $arg] } { incr index } {
  539. set query1 "$query1[lindex $arg $index]"
  540. if {$index<[llength $arg]-1} then {
  541. set query1 "$query1+"
  542. }
  543. }
  544. for { set index 0 } { $index<[llength $arg] } { incr index } {
  545. set query2 "$query2[lindex $arg $index]"
  546. if {$index<[llength $arg]-1} then {
  547. set query2 "$query2+"
  548. }
  549. }
  550. set a "PRIVMSG $djchan :\002\Request\002: $arg | Requested by:\002 "
  551. set b "$nick \002 | \002Waffles\002: $query1 | \002What.CD\002: $query2"
  552. putnow "$a$b"
  553. putnow "NOTICE $nick :\002$arg\002 is succesfully requested"
  554. set token [http::config -useragent "Mozilla/5.0; Shoutinfo"]
  555. set token [http::geturl $query1]
  556. puts stderr ""
  557. upvar #0 $token state
  558. set max 0
  559. }
  560. }
  561. }
  562. }
  563. ## !log DJ Log Information
  564. proc getlog {nick uhost hand chan arg} {
  565. global dbhost dbuser dbname dbpass
  566. set m [mysqlconnect -host $dbhost -user $dbuser -db $dbname -password $dbpass]
  567. if {[llength $arg]==0} {
  568. set tt_query "SELECT total_time from djlog where dj_name='$nick'"
  569. set ts_query "SELECT total_sessions from djlog where dj_name='$nick'"
  570. set log_name $nick
  571. } else {
  572. set tt_query "SELECT total_time from djlog where dj_name='$arg'"
  573. set ts_query "SELECT total_sessions from djlog where dj_name='$arg'"
  574. set log_name $arg
  575. }
  576. set tt_result [mysqlsel $m $tt_query -list]
  577. set ts_result [mysqlsel $m $ts_query -list]
  578. if {$ts_result != "" && $tt_result != "" } {
  579. set total_time [lindex $tt_result 0]
  580. set total_sessions [lindex $ts_result 0]
  581. set total_time [expr {$total_time*1}]
  582. set totalt [duration $total_time]
  583. putnow "PRIVMSG $chan : $log_name has been on air a total of $total_sessions times for $totalt."
  584. } else {
  585. putnow "PRIVMSG $chan : Sorry but $arg was not found in the log."
  586. }
  587. }
  588. ## First Save: Name + Onair Time
  589. proc startsave {dj} {
  590. global dbhost dbuser dbname dbpass
  591. set start_time [clock seconds]
  592. set m [mysqlconnect -host $dbhost -user $dbuser -db $dbname -password $dbpass]
  593. set query "SELECT dj_name from session_log order by ID desc limit 1"
  594. set result [mysqlsel $m $query -list]
  595. set testname [lindex $result 0]
  596. putnow "PRIVMSG #BotDev : $testname"
  597. set testquery "SELECT offair_time from session_log order by ID desc limit 1"
  598. set testresult [mysqlsel $m $testquery -list]
  599. set testtime [lindex $testresult 0]
  600. putnow "PRIVMSG #BotDev : $testtime"
  601. if {$testtime != "{}" && $testname != $dj} {
  602. #####
  603. ### session_log
  604. ######
  605. set query "INSERT INTO session_log(dj_name, onair_time) VALUES('$dj', '$start_time')"
  606. set result [mysqlexec $m $query]
  607. ######
  608. ### djlog
  609. ######
  610. set query "SELECT dj_name from djlog where dj_name='$dj'"
  611. set result [mysqlsel $m $query -list]
  612. set dj_name [lindex $result 0]
  613. if {$dj_name != ""} {
  614. set query "SELECT total_sessions from djlog where dj_name='$dj'"
  615. set result [mysqlsel $m $query -list]
  616. set total_sessions [lindex $result 0]
  617. incr total_sessions
  618. set query "UPDATE djlog set total_sessions=$total_sessions where dj_name='$dj'"
  619. set result [mysqlexec $m $query]
  620. } else {
  621. set query "INSERT INTO djlog(dj_name, total_sessions) VALUES('$dj', '1')"
  622. set result [mysqlexec $m $query]
  623. }
  624. if {$result} {
  625. putlog "DJ Database Entry Sent. $dj with $start_time"
  626. } else {
  627. putlog "Database error."
  628. }
  629. mysqlclose $m
  630. } elseif {$testname == $dj && $testtime != "{}"} {
  631. #####
  632. ### session_log
  633. ######
  634. set query "INSERT INTO session_log(dj_name, onair_time) VALUES('$dj', '$start_time')"
  635. set result [mysqlexec $m $query]
  636. ######
  637. ### djlog
  638. ######
  639. set query "SELECT dj_name from djlog where dj_name='$dj'"
  640. set result [mysqlsel $m $query -list]
  641. set dj_name [lindex $result 0]
  642. if {$dj_name != ""} {
  643. set query "SELECT total_sessions from djlog where dj_name='$dj'"
  644. set result [mysqlsel $m $query -list]
  645. set total_sessions [lindex $result 0]
  646. incr total_sessions
  647. set query "UPDATE djlog set total_sessions=$total_sessions where dj_name='$dj'"
  648. set result [mysqlexec $m $query]
  649. } else {
  650. set query "INSERT INTO djlog(dj_name, total_sessions) VALUES('$dj', '1')"
  651. set result [mysqlexec $m $query]
  652. }
  653. if {$result} {
  654. putlog "DJ Database Entry Sent. $dj with $start_time"
  655. } else {
  656. putlog "Database error."
  657. }
  658. mysqlclose $m
  659. } elseif {$testname == $dj && $testtime == "{}"} {
  660. putnow "PRIVMSG #DJ :$dj is already in a session."
  661. }
  662. }
  663. ## Second: Offair, Session, Total
  664. proc finishsave {dj} {
  665. global djchan dbhost dbuser dbname dbpass
  666. set end_time [clock seconds]
  667. set m [mysqlconnect -host $dbhost -user $dbuser -db $dbname -password $dbpass]
  668. # Checking out the Top-Row's DJ Name
  669. set query "SELECT dj_name from session_log order by ID desc limit 1"
  670. set result [mysqlsel $m $query -list]
  671. set dj_name [lindex $result 0]
  672. set testquery "SELECT offair_time from session_log order by ID desc limit 1"
  673. set testresult [mysqlsel $m $testquery -list]
  674. set testtime [lindex $testresult 0]
  675. # If Our Arg is equal to DJ Name in Top Row
  676. if {$dj == $dj_name && $testtime == "{}"} {
  677. # Read when this person logged on-air
  678. set query "SELECT onair_time from session_log order by ID desc limit 1"
  679. set result [mysqlsel $m $query -list]
  680. set onair_time [lindex $result 0]
  681. # Calculate the session-time
  682. set session_time [expr {$end_time - $onair_time}]
  683. # Set session-time
  684. set query "UPDATE session_log set session_time=$session_time order by ID desc limit 1"
  685. set result [mysqlexec $m $query]
  686. # Set Offair-time
  687. set query "UPDATE session_log set offair_time=$end_time order by ID desc limit 1"
  688. set result [mysqlexec $m $query]
  689. ######
  690. ### djlog
  691. ######
  692. # Read Total Time
  693. set query "SELECT total_time from djlog where dj_name='$dj'"
  694. set result [mysqlsel $m $query -list]
  695. set total_time [lindex $result 0]
  696. # This needs to be fixed or two tables created
  697. if {$total_time != "{}"} {
  698. set newtotal_time [expr {$total_time + $session_time}]
  699. } else {
  700. set newtotal_time $session_time
  701. }
  702. # Set New Total Time
  703. if {$session_time >= 600} {
  704. set query "UPDATE djlog set total_time=$newtotal_time where dj_name='$dj'"
  705. set result [mysqlexec $m $query]
  706. putnow "PRIVMSG $djchan : Session Time: $session_time Total Time: $newtotal_time"
  707. } else {
  708. putnow "PRIVMSG $djchan : $dj, your session was less than 10 minutes, thus your time was not recorded."
  709. }
  710. if {$result} {
  711. putlog "DJ Database Entry Sent. $dj with $session_time and $newtotal_time"
  712. } else {
  713. putlog "Database error."
  714. }
  715. } else {
  716. putnow "PRIVMSG $djchan :$dj, you never went on-air, thus your time was not recorded."
  717. }
  718. }
  719. ## Top 5 or $arg of DJ Log
  720. proc toplog {nick uhost hand chan arg} {
  721. global dbhost dbuser dbname dbpass
  722. set m [mysqlconnect -host $dbhost -user $dbuser -db $dbname -password $dbpass]
  723. set name_query "SELECT dj_name from djlog order by total_time desc"
  724. set name_result [mysqlsel $m $name_query -list]
  725. set tt_query "SELECT total_time from djlog order by total_time desc"
  726. set ts_query "SELECT total_sessions from djlog order by total_time desc"
  727. set ts_result [mysqlsel $m $ts_query -list]
  728. set tt_result [mysqlsel $m $tt_query -list]
  729. set i 0
  730. set n 1
  731. if {[llength $arg]==0} {
  732. putnow "PRIVMSG $chan : Top 5 DJ Log"
  733. while {$i < 5} {
  734. set name [lindex $name_result $i]
  735. set total_time [lindex $tt_result $i]
  736. set total_sessions [lindex $ts_result $i]
  737. set total_time [expr {$total_time*1}]
  738. set total_time [duration $total_time]
  739. putnow "PRIVMSG $chan : #$n: $name with $total_time on-air and $total_sessions sessions."
  740. incr i
  741. incr n
  742. }
  743. } else {
  744. putnow "PRIVMSG $chan : Top $arg DJ Log"
  745. set arg [expr {$arg*1}]
  746. while {$i < $arg} {
  747. set name [lindex $name_result $i]
  748. set total_time [lindex $tt_result $i]
  749. set total_sessions [lindex $ts_result $i]
  750. set total_time [expr {$total_time*1}]
  751. set total_time [duration $total_time]
  752. putnow "PRIVMSG $chan : #$n: $name with $total_time on-air and $total_sessions sessions."
  753. incr i
  754. incr n
  755. }
  756. }
  757. }
  758. proc lastsessions {nick uhost hand chan arg} {
  759. global dbhost dbuser dbname dbpass
  760. set m [mysqlconnect -host $dbhost -user $dbuser -db $dbname -password $dbpass]
  761. set name_query "SELECT dj_name from session_log order by id desc"
  762. set name_result [mysqlsel $m $name_query -list]
  763. set on_query "SELECT onair_time from session_log order by id desc"
  764. set off_query "SELECT offair_time from session_log order by id desc"
  765. set on_result [mysqlsel $m $on_query -list]
  766. set off_result [mysqlsel $m $off_query -list]
  767. set session_query "SELECT session_time from session_log order by id desc"
  768. set session_result [mysqlsel $m $session_query -list]
  769. set i 0
  770. set n 1
  771. if {[llength $arg]==0} {
  772. putnow "PRIVMSG $chan : Last 5 DJ Sessions"
  773. while {$i < 5} {
  774. set name [lindex $name_result $i]
  775. set onairs [lindex $on_result $i]
  776. set offairs [lindex $off_result $i]
  777. set session_time [lindex $session_result $i]
  778. putnow "PRIVMSG $chan : #$n: $name with $onairs on-air and $offairs offair and $session_time session time."
  779. incr i
  780. incr n
  781. }
  782. } else {
  783. putnow "PRIVMSG $chan : Last $arg DJ Sessions"
  784. set arg [expr {$arg*1}]
  785. while {$i < $arg} {
  786. set name [lindex $name_result $i]
  787. set onairs [lindex $on_result $i]
  788. set offairs [lindex $off_result $i]
  789. set session_time [lindex $session_result $i]
  790. putnow "PRIVMSG $chan : #$n: $name with $onairs on-air and $offairs offair and $session_time session time."
  791. incr i
  792. incr n
  793. }
  794. }
  795. }
  796. ## Delete Request Func + Timer
  797. proc deletereq {} {
  798. global djchan logchan
  799. timer 10 [list deletereq]
  800. set filename "requestlist.txt"
  801. set fp [open $filename r+]
  802. set data [read -nonewline $fp]
  803. set lines [split $data "\n"]
  804. close $fp
  805. set line_to_delete [expr {[llength $lines] - 1}]
  806. set lines [lreplace $lines $line_to_delete $line_to_delete]
  807. set fp [open $filename w+]
  808. if {[gets $fp data] >= 1} {
  809. puts -nonewline $fp $lines
  810. close $fp
  811. } else {
  812. puts -nonewline $fp [join $lines "\n"]
  813. close $fp
  814. }
  815. putnow "PRIVMSG $logchan :Deleting a Request"
  816. }
  817. ## Calculate Seconds to D:H:M:S
  818. proc duration { int_time } {
  819. set timeList [list]
  820. foreach div {604800 86400 3600 60 1} mod {0 7 24 60 60} name {wk day hr min sec} {
  821. set n [expr {$int_time / $div}]
  822. if {$mod > 0} {set n [expr {$n % $mod}]}
  823. if {$n > 1} {
  824. lappend timeList "$n ${name}s"
  825. } elseif {$n == 1} {
  826. lappend timeList "$n $name"
  827. }
  828. }
  829. return [join $timeList]
  830. }
  831. ## Advert if Offline->RealDJ
  832. proc onair {} {
  833. global radiochan djchan dj newdj mainchan
  834. set a "\002$newdj\002 - Is Now Broadcasting Live! Tune "
  835. set b "in @ http://216.104.37.26:9005/listen.pls"
  836. set c "$newdj is now ON AIR @ "
  837. set d "#TI-Radio (http://216.104.37.26:9005/listen.pls)"
  838. set e "Torrent-Invites Radio || Status: DJ On Air || $newdj Is Now Broadcasting || "
  839. set f "URL: http://216.104.37.26:9005/listen.pls || Want to be a DJ?: http://bit.ly/J6cWtN"
  840. putnow "PRIVMSG $radiochan : $a$b"
  841. putnow "PRIVMSG $djchan :\002ON AIR\002: $newdj is now ON AIR."
  842. putnow "PRIVMSG $mainchan : $c$d"
  843. putnow "TOPIC $radiochan : $e$f"
  844. }
  845. ## Advert if RealDJ->RealDJ
  846. proc tempoffair {} {
  847. global radiochan djchan dj
  848. set radiochan [string tolower $radiochan]
  849. if {$dj != 0} {
  850. putnow "PRIVMSG $radiochan :\002OFF AIR\002: $dj is now OFF AIR."
  851. putnow "PRIVMSG $djchan :\002OFF AIR\002: $dj is now OFF AIR."
  852. }
  853. }
  854. ## Advert if Offline->AutoDJ
  855. proc autodjon {} {
  856. global radiochan
  857. set radiochan [string tolower $radiochan]
  858. set a "Torrent-Invites Radio || Status: AutoDJ ||"
  859. set b "URL: http://216.104.37.26:9005/listen.pls || Want to be a DJ?: http://bit.ly/J6cWtN"
  860. putnow "TOPIC $radiochan : $a$b"
  861. }
  862. ## Advert if Someone->Offline
  863. proc serveroffline {} {
  864. global radiochan
  865. set radiochan [string tolower $radiochan]
  866. set a "Torrent-Invites Radio || Status: Stream Offline ||"
  867. set b " URL: http://216.104.37.26:9005/listen.pls || Want to be a DJ?: http://bit.ly/J6cWtN"
  868. putnow "TOPIC $radiochan : $a$b"
  869. }
  870. ## Advert if RealDJ->AutoDJ
  871. proc autooffair {} {
  872. global radiochan djchan dj
  873. set radiochan [string tolower $radiochan]
  874. set a "Torrent-Invites Radio || Status: AutoDJ || "
  875. set b "URL: http://216.104.37.26:9005/listen.pls || Want to be a DJ?: http://bit.ly/J6cWtN"
  876. putnow "PRIVMSG $radiochan :\002OFF AIR\002: $dj is now OFF AIR."
  877. putnow "PRIVMSG $djchan :\002OFF AIR\002: $dj is now OFF AIR."
  878. putnow "TOPIC $radiochan : $a$b"
  879. }
  880. ## Advert if Server if Offline
  881. proc offair {} {
  882. global radiochan djchan dj
  883. set radiochan [string tolower $radiochan]
  884. set a "Torrent-Invites Radio || Status: Stream Offline || "
  885. set b "URL: http://216.104.37.26:9005/listen.pls || Want to be a DJ?: http://bit.ly/J6cWtN"
  886. putnow "PRIVMSG $radiochan :\002OFF AIR\002: $dj is now OFF AIR."
  887. putnow "PRIVMSG $djchan :\002OFF AIR\002: $dj is now OFF AIR."
  888. putnow "PRIVMSG $djchan :\00304$dj, please remember to \002TURN ON AUTODJ\002 \00304"
  889. putnow "TOPIC $radiochan : $a$b"
  890. }
  891. putlog "Radio Script loaded.. | 1"
  892. start