funwar.tcl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # funwar.tcl
  2. #
  3. # This script was created to post fun clan wars/matches for the two multiplayer
  4. # games RTCW and ET in channels periodically and with the commands below. The
  5. # matches are retrieved from tables in postreSQL databases.
  6. #
  7. # Usage:
  8. # !et post ET matches
  9. # !rtcw post RTCW matches
  10. # !funwars post ET and RTCW matches
  11. #
  12. # Enable for a channel with: .chanset #channel +funwar
  13. # Disable for a channel with: .chanset #channel -funwar
  14. # tested versions, might run on earlier versions
  15. package require Tcl 8.6
  16. package require eggdrop 1.8.4
  17. # postgres sql
  18. package require tdbc::postgres
  19. namespace eval ::funwar {
  20. # channel flag for enabling/disabling
  21. setudef flag funwar
  22. # sql server address, username and password
  23. variable sqlServer "eggdroppostgres"
  24. variable sqlUser "eggdrop"
  25. variable sqlPassword "eggdropPassword"
  26. # ET database and table on sql server
  27. variable sqlDbnameEt "postgres"
  28. variable sqlTblnameEt "test.funmatch"
  29. # RTCW database and table on sql server
  30. variable sqlDbnameRtcw "postgres"
  31. variable sqlTblnameRtcw "test.funmatch"
  32. # crontab style definition of how often funwars are posted in channels
  33. # format:
  34. # "MIN HOUR DAY MONTH YEAR"
  35. # examples:
  36. # "20,50 * * * *" post at minute 20 and 50 of every hour
  37. # "*/10 * * * *" post every ten minutes
  38. variable autoCron "20,50 * * * *"
  39. # trigger configuration for ET, RTCW and both
  40. variable triggerEt "!et"
  41. variable triggerRtcw "!rtcw"
  42. variable triggerBoth "!funwars"
  43. # output configuration
  44. variable outputHeader "*** Funwars: ***"
  45. variable outputHeader2 "Game: Date/Time: XonX: Clantag: IRC:"
  46. variable outputFooter "*** end of funwars list ***"
  47. }
  48. # post the funwar header in the channel
  49. proc ::funwar::postHeader {chan} {
  50. variable outputHeader
  51. variable outputHeader2
  52. set fmt "%-7s %-20s %-6s %-12s %-12s"
  53. set lineHeader [format $fmt {*}$outputHeader2]
  54. puthelp "PRIVMSG $chan :$outputHeader"
  55. puthelp "PRIVMSG $chan :$lineHeader"
  56. }
  57. # post a funwar line in the channel
  58. proc ::funwar::post {game id date time xonx clantag irc www server org chan } {
  59. set fmt "%-7s %-20s %-6s %-12s %-12s"
  60. set outputBody [format $fmt $game $date/$time $xonx $clantag $irc]
  61. puthelp "PRIVMSG $chan :$outputBody"
  62. }
  63. # post the funwar footer in the channel
  64. proc ::funwar::postFooter {chan} {
  65. variable outputFooter
  66. puthelp "PRIVMSG $chan :$outputFooter"
  67. }
  68. # get rows from sql table
  69. proc ::funwar::sqlGet {server user password dbname tblname order limit} {
  70. # connect to data base
  71. # add -sslmode require if you want to enforce ssl
  72. tdbc::postgres::connection create db -host $server -database $dbname \
  73. -user $user -password $password
  74. # query table in data base
  75. set columns "id,date,time,xonx,clantag,irc,www,server,org"
  76. set top "FETCH FIRST $limit ROWS ONLY"
  77. set queryStr "SELECT $columns FROM $tblname ORDER BY $order DESC $top"
  78. set query [db prepare $queryStr]
  79. # collect all rows and return them
  80. set rows ""
  81. $query foreach row {
  82. lappend rows $row
  83. }
  84. $query close
  85. db close
  86. return $rows
  87. }
  88. # query db, parse everything and post in channels
  89. proc ::funwar::sqlParsedb {server user password dbname tblname order chan \
  90. game limit command} {
  91. # query rows from table in database
  92. set rows [sqlGet $server $user $password $dbname $tblname $order \
  93. $limit]
  94. if {$rows == ""} {
  95. return
  96. }
  97. if { $command != "noheader" } {
  98. # post header in every given channel
  99. foreach channel $chan {
  100. postHeader $channel
  101. }
  102. }
  103. # parse earch row and post them in channels
  104. foreach row $rows {
  105. set id [dict get $row id]
  106. set date [dict get $row date]
  107. set time [dict get $row time]
  108. set xonx [dict get $row xonx]
  109. set clantag [dict get $row clantag]
  110. set irc [dict get $row irc]
  111. set www [dict get $row www]
  112. set server [dict get $row server]
  113. set org [dict get $row org]
  114. foreach channel $chan {
  115. # post row in every given channel
  116. post $game $id $date $time $xonx $clantag $irc $www \
  117. $server $org $channel
  118. }
  119. }
  120. if { $command != "nofooter" } {
  121. # post footer in every given channel
  122. foreach channel $chan {
  123. postFooter $channel
  124. }
  125. }
  126. }
  127. # helper for posting rtcw entries
  128. proc ::funwar::postRtcw {chan limit command} {
  129. variable sqlServer
  130. variable sqlUser
  131. variable sqlPassword
  132. variable sqlDbnameRtcw
  133. variable sqlTblnameRtcw
  134. set order "id"
  135. set game "RTCW"
  136. sqlParsedb $sqlServer $sqlUser $sqlPassword $sqlDbnameRtcw \
  137. $sqlTblnameRtcw $order $chan $game $limit $command
  138. }
  139. # helper for posting et entries
  140. proc ::funwar::postEt {chan limit command} {
  141. variable sqlServer
  142. variable sqlUser
  143. variable sqlPassword
  144. variable sqlDbnameEt
  145. variable sqlTblnameEt
  146. set order "id"
  147. set game "ET"
  148. sqlParsedb $sqlServer $sqlUser $sqlPassword $sqlDbnameEt \
  149. $sqlTblnameEt $order $chan $game $limit $command
  150. }
  151. # handle the !rtcw trigger
  152. proc ::funwar::rtcw { nick host hand chan arg } {
  153. # check channel flag if enabled in this channel
  154. if {![channel get $chan funwar]} {
  155. return 0
  156. }
  157. set limit "10"
  158. set command ""
  159. postRtcw $chan $limit $command
  160. return 1
  161. }
  162. # handle the !et trigger
  163. proc ::funwar::et { nick host hand chan arg } {
  164. # check channel flag if enabled in this channel
  165. if {![channel get $chan funwar]} {
  166. return 0
  167. }
  168. set limit "10"
  169. set command ""
  170. postEt $chan $limit $command
  171. return 1
  172. }
  173. # handle the !funwars trigger
  174. proc ::funwar::both { nick host hand chan arg } {
  175. # check channel flag if enabled in this channel
  176. if {![channel get $chan funwar]} {
  177. return 0
  178. }
  179. set limit "3"
  180. set command "nofooter"
  181. postRtcw $chan $limit $command
  182. set command "noheader"
  183. postEt $chan $limit $command
  184. return 1
  185. }
  186. # handle cron auto posting
  187. proc ::funwar::auto { min hour day month year } {
  188. # determine list of channels to post in based on channel flag
  189. set autoChannels ""
  190. foreach botChan [channels] {
  191. # only use channels the bot is on and have the flag enabled
  192. if {![botonchan $botChan] || ![channel get $botChan funwar]} {
  193. continue
  194. }
  195. lappend autoChannels $botChan
  196. }
  197. if {$autoChannels == ""} {
  198. # no channels to post in
  199. return
  200. }
  201. set limit "3"
  202. set command "nofooter"
  203. postRtcw $autoChannels $limit $command
  204. set command "noheader"
  205. postEt $autoChannels $limit $command
  206. }
  207. namespace eval ::funwar {
  208. bind pub - $triggerEt ::funwar::et
  209. bind pub - $triggerRtcw ::funwar::rtcw
  210. bind pub - $triggerBoth ::funwar::both
  211. bind cron - $autoCron ::funwar::auto
  212. putlog "Loaded funwars.tcl"
  213. }