em_server.rb 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. #!/usr/bin/env ruby
  2. require 'eventmachine'
  3. require 'digest/md5'
  4. require 'fiber'
  5. require 'geoip'
  6. require 'socket'
  7. require 'timeout'
  8. # .. allowed commands .. ROLES = CAPABILITIES
  9. # normal users have ROLE broadcast. Roles are defined on a per-user basis .. in a config.
  10. $version = "0.6em_specservers_dupe_election"
  11. $debug = 0
  12. $role_commands = Hash[
  13. #noinspection RubyStringKeysInHashInspection
  14. 'everyone' => %w(PING WHO C PART),
  15. 'broadcast_admin' => %w(BC_ID BC BC_ENDCOUNT),
  16. 'broadcast' => %w(REQ_BC BC_RE),
  17. 'specbot_admin' => %w(REQ_ASSIGN REQ_UNASSIGN REQ_PING REQ_ASSIGNMENTS REQ_MAXSERVERS DUPECHECK ELECTION),
  18. 'specbot' => %w(ASSIGN_RE UNASSIGN_RE PING_RE ASSIGNMENTS_RE REQ_DNS MAXSERVERS_RE),
  19. ]
  20. $default_role = "everyone"
  21. # which role is talking to which role?
  22. # effectively it says: this (local) command is sent to that (remote) topic .. that certain topic is read by that user with that role.
  23. $role_dialogs = Hash[
  24. #noinspection RubyStringKeysInHashInspection
  25. 'everyone' => %w(everyone),
  26. 'broadcast_admin' => %w(broadcast),
  27. 'broadcast' => %w(broadcast_admin),
  28. 'specbot_admin' => %w(specbot),
  29. 'specbot' => %w(specbot_admin),
  30. ]
  31. $user_roles = Hash[
  32. #noinspection RubyStringKeysInHashInspection
  33. 'paul_tester' => %w(everyone broadcast specbot_admin),
  34. 'paul_dev_eggdrop' => %w(everyone broadcast),
  35. 'paul_eggdrop' => %w(everyone broadcast),
  36. 'paul_dev_specbot' => %w(everyone broadcast specbot),
  37. 'paul_specbot' => %w(everyone broadcast specbot),
  38. 'qw.nu' => %w(everyone broadcast),
  39. 'qw.nu_poster' => %w(everyone broadcast),
  40. 'mihawk_devA_specbot' => %w(everyone broadcast specbot),
  41. 'mihawk_devB_specbot' => %w(everyone broadcast specbot),
  42. 'mihawk_specbot' => %w(everyone broadcast specbot),
  43. 'armitage_specbot' => %w(everyone broadcast specbot),
  44. 'blaze_specbot' => %w(everyone broadcast specbot),
  45. ]
  46. $qw_list = Array.new
  47. $qw_list = [
  48. "89.149.194.72:27700",
  49. "84.200.47.113:30000",
  50. "89.149.194.72:27500",
  51. "84.200.47.113:28502",
  52. "89.149.194.72:30000",
  53. "89.149.194.72:27600",
  54. "93.186.192.16:28502",
  55. "84.200.47.113:28501",
  56. "93.186.192.16:28501",
  57. "188.40.130.10:27502",
  58. "93.186.192.16:30000",
  59. "188.40.130.10:27599",
  60. "188.40.130.10:27503",
  61. "194.109.69.75:28000",
  62. "194.109.69.75:27500",
  63. "194.109.69.75:27501",
  64. "194.109.69.76:28504",
  65. "188.165.243.56:30000",
  66. "188.40.130.10:27501",
  67. "194.109.69.76:28502",
  68. "194.109.69.76:28501",
  69. "194.109.69.76:28503",
  70. "62.24.64.11:27501",
  71. "62.24.64.11:44444",
  72. "188.40.103.81:27600",
  73. "62.24.64.11:30000",
  74. "194.109.69.75:27502",
  75. "91.121.69.201:30000",
  76. "37.59.63.97:28504",
  77. "188.40.130.10:27500",
  78. "188.165.243.56:28009",
  79. "37.59.63.97:28501",
  80. "62.24.64.11:27500",
  81. "37.59.63.97:28503",
  82. "194.109.69.76:30000",
  83. "188.165.243.56:28008",
  84. "188.165.243.56:28006",
  85. "188.165.243.56:28001",
  86. "188.165.243.56:28002",
  87. "93.186.192.16:27500",
  88. "91.121.69.201:27600",
  89. "37.59.63.97:28502",
  90. "91.121.223.163:28001",
  91. "188.165.243.56:28003",
  92. "109.74.195.224:30000",
  93. "178.79.183.178:27500",
  94. "77.74.194.189:27501",
  95. "91.121.69.201:27502",
  96. "188.165.243.56:28007",
  97. "109.74.195.224:27500",
  98. "213.5.176.135:27502",
  99. "77.74.194.189:27504",
  100. "178.79.172.251:27600",
  101. "77.74.194.189:27502",
  102. "77.74.194.189:27503",
  103. "213.5.176.135:27500",
  104. "109.74.195.224:27501",
  105. "89.104.194.146:27504",
  106. "89.104.194.146:27503",
  107. "89.104.194.146:27508",
  108. "213.5.176.136:27510",
  109. "213.5.176.135:27501",
  110. "213.5.176.135:27503",
  111. "77.74.194.189:30000",
  112. "89.149.194.72:27800",
  113. "89.104.194.146:27502",
  114. "89.104.194.146:27666",
  115. "89.104.194.146:27507",
  116. "89.104.194.146:27509",
  117. "89.104.194.146:30000",
  118. "188.165.243.56:28004",
  119. "89.104.194.146:27500",
  120. "89.104.194.146:27501",
  121. "194.109.69.76:27500",
  122. "89.104.194.146:27506",
  123. "94.236.92.49:27501",
  124. "91.121.69.201:27501",
  125. "213.239.214.134:27500",
  126. "89.104.194.146:27510",
  127. "95.131.48.86:27502",
  128. "94.236.92.49:27500",
  129. "94.236.92.49:30000",
  130. "95.131.48.86:27504",
  131. "78.137.161.109:27501",
  132. "188.165.243.56:28005",
  133. "178.217.185.104:27500",
  134. "178.217.185.104:27600",
  135. "178.217.185.104:30000",
  136. "87.102.202.23:27502",
  137. "78.137.161.109:27500",
  138. "89.104.194.146:27505",
  139. "87.102.202.23:27505",
  140. "178.217.185.104:27501",
  141. "91.121.69.201:27500",
  142. "82.141.152.3:27501",
  143. "82.141.152.3:27500",
  144. "83.179.23.16:28002",
  145. "109.74.7.60:27500",
  146. "212.62.234.153:27502",
  147. "83.179.23.16:28003",
  148. "195.54.182.34:27500",
  149. "83.179.23.16:28005",
  150. "212.62.234.153:27503",
  151. "95.143.243.24:27600",
  152. "83.179.23.16:28001",
  153. "212.62.234.153:27504",
  154. "78.137.161.109:27502",
  155. "83.179.23.16:28004",
  156. "212.62.234.153:27501",
  157. "95.143.243.24:27500",
  158. "109.228.137.161:28501",
  159. "95.143.243.24:27900",
  160. "83.226.149.218:27500",
  161. "83.226.149.218:28001",
  162. "83.226.149.218:28002",
  163. "83.226.149.218:28003",
  164. "193.1.40.166:27975",
  165. "87.237.112.11:30000",
  166. "217.30.184.104:27500",
  167. "78.108.53.19:27500",
  168. "78.108.53.19:27501",
  169. "195.54.142.7:28001",
  170. "193.1.40.166:27500",
  171. "87.237.112.11:27501",
  172. "195.54.142.7:30000",
  173. "195.54.142.7:28007",
  174. "217.119.36.79:30000",
  175. "95.131.48.86:27501",
  176. "195.54.142.7:28002",
  177. "195.54.142.7:28008",
  178. "80.101.105.103:27500",
  179. "87.237.112.11:27502",
  180. "217.119.36.79:27500",
  181. "81.170.128.75:28501",
  182. "217.119.36.79:28001",
  183. "193.1.40.167:27500",
  184. "217.119.36.79:28003",
  185. "217.119.36.79:28002",
  186. "81.170.128.75:30000",
  187. "81.170.128.75:28504",
  188. "195.54.142.7:28005",
  189. "195.54.142.7:28010",
  190. "81.170.128.75:28503",
  191. "93.81.254.63:27502",
  192. "212.42.38.88:27504",
  193. "81.170.128.75:28502",
  194. "93.81.254.63:27500",
  195. "81.170.128.75:28000",
  196. "212.42.38.88:27500",
  197. "82.203.213.117:28002",
  198. "212.42.38.88:30000",
  199. "93.81.254.63:30000",
  200. "82.203.213.117:28001",
  201. "212.42.38.88:27501",
  202. "212.42.38.88:27503",
  203. "93.81.254.63:27503",
  204. "93.81.254.63:27501",
  205. "82.203.213.117:30000",
  206. "95.131.48.86:27503",
  207. "83.252.244.76:27500",
  208. "212.42.38.88:27502",
  209. "195.54.142.7:28003",
  210. "95.31.4.132:30000",
  211. "83.252.244.76:27501",
  212. "93.81.254.63:30001",
  213. "195.54.142.7:28006",
  214. "82.203.213.117:28003",
  215. "95.143.243.24:27700",
  216. "84.234.185.215:27503",
  217. "84.234.185.215:27500",
  218. "84.234.185.215:27519",
  219. "84.234.185.215:27508",
  220. "84.234.185.215:27506",
  221. "84.234.185.215:27505",
  222. "195.54.142.7:28004",
  223. "84.234.185.215:27501",
  224. "195.54.142.7:28009",
  225. "212.109.128.148:27501",
  226. "84.234.185.215:27502",
  227. "31.209.7.104:28501",
  228. "194.79.85.66:27501",
  229. "194.79.85.66:30000",
  230. "95.84.164.245:27501",
  231. "194.79.85.66:27502",
  232. "83.222.112.157:30000",
  233. "212.109.128.148:27500",
  234. "94.100.6.66:27500",
  235. "129.241.205.153:28000",
  236. "84.234.185.215:27507",
  237. "129.241.205.153:27500",
  238. "95.84.164.245:27500",
  239. "69.31.82.226:27501",
  240. "69.31.82.226:30000",
  241. "130.240.207.177:30000",
  242. "69.31.82.226:28100",
  243. "69.31.82.226:27500",
  244. "69.31.82.226:28101",
  245. "69.31.82.226:28010",
  246. "69.31.82.226:30001",
  247. "69.31.82.226:28000",
  248. "69.31.82.226:28002",
  249. "93.186.192.16:28000",
  250. "195.222.130.83:27500",
  251. "84.200.47.113:28000",
  252. "130.85.56.131:27500",
  253. "108.174.51.73:28003",
  254. "96.8.113.36:27501",
  255. "96.8.113.36:27500",
  256. "68.100.130.114:27501",
  257. "65.31.69.75:27500",
  258. "108.174.51.73:28006",
  259. "65.31.69.75:27508",
  260. "108.174.51.73:30000",
  261. "67.81.59.41:27500",
  262. "108.174.51.73:28004",
  263. "217.18.138.23:27505",
  264. "108.174.51.73:28001",
  265. "68.100.130.114:27500",
  266. "174.49.198.60:27502",
  267. "174.49.198.60:27503",
  268. "209.239.113.236:27500",
  269. "174.49.198.60:27515",
  270. "217.119.36.79:28000",
  271. "108.174.51.73:28005",
  272. "74.91.115.244:28001",
  273. "174.49.198.60:27500",
  274. "74.91.115.244:28000",
  275. "199.101.96.48:27501",
  276. "96.8.113.36:30000",
  277. "174.101.185.59:27500",
  278. "67.228.69.114:27502",
  279. "199.101.96.48:27500",
  280. "108.174.51.73:28002",
  281. "199.192.229.74:28001",
  282. "74.86.171.201:27502",
  283. "67.228.69.114:27501",
  284. "74.86.171.201:27500",
  285. "67.228.69.114:26666",
  286. "199.192.228.71:27501",
  287. "199.192.229.74:28003",
  288. "74.86.171.201:27501",
  289. "199.192.229.74:30000",
  290. "199.192.229.74:28002",
  291. "74.91.115.244:28002",
  292. "199.192.228.71:27500",
  293. "199.101.96.48:30000",
  294. "199.101.96.48:28000",
  295. "199.192.229.74:28004",
  296. "199.192.228.71:30000",
  297. "65.31.238.37:27500",
  298. "31.209.7.104:28000",
  299. "208.131.136.169:27500",
  300. "66.212.17.78:27500",
  301. "200.177.229.11:27510",
  302. "200.177.229.11:27522",
  303. "200.177.229.11:27521",
  304. "200.177.229.11:27500",
  305. "190.96.80.67:27500",
  306. "200.177.229.11:27511",
  307. "200.177.229.11:27520",
  308. "190.96.80.67:27000",
  309. "202.37.129.186:27500",
  310. "202.37.129.186:27505",
  311. "202.37.129.186:27501",
  312. "219.88.241.81:27500",
  313. "202.172.99.2:28001",
  314. "202.172.99.2:28002",
  315. "202.172.99.2:28003",
  316. "202.172.99.2:27500",
  317. "210.50.4.11:27501",
  318. "202.172.99.2:27501",
  319. "210.50.4.11:27508",
  320. "210.50.4.11:27511",
  321. "210.50.4.11:27509",
  322. "210.50.4.11:27510",
  323. "122.99.118.2:28001",
  324. "210.50.4.11:27500",
  325. "210.50.4.11:27503",
  326. "210.50.4.11:27506",
  327. "210.50.4.11:27505",
  328. "210.50.4.11:27504",
  329. ]
  330. # for local dev usage ;)
  331. $qw_list = [
  332. "89.104.194.146:27501",
  333. "210.50.4.11:27504",
  334. "122.99.118.2:28001",
  335. ]
  336. class GameServers
  337. attr_accessor :gameservers # now, @gameservers is accessible via GameServers.gameservers
  338. def initialize
  339. @gameservers = Hash.new
  340. @gameservers.default = {
  341. :reverse_dns => "",
  342. :hostname_dns => "",
  343. :cool_dns => "",
  344. :type => "",
  345. :serverinfos => "",
  346. :timestamp => 0,
  347. }
  348. @gameservers["blah:blah"]
  349. wat = Hash.new(@gameservers["blah:blah"])
  350. wat
  351. wat.store(:reverse_dns, "6")
  352. @gameservers.merge(wat)
  353. p @gameservers
  354. p @gameservers.default
  355. wat = Hash.new(@gameservers["blah:blah"])
  356. wat
  357. wat.store(:hostname_dns, "12")
  358. @gameservers.merge(wat)
  359. p @gameservers
  360. p @gameservers.default
  361. end
  362. def scanserver(iphostport, type="qw", force=false)
  363. put_log "scanserver drin"
  364. if iphostport =~ /^(\d+\.\d+\.\d+\.\d+):(\d{1,5})/
  365. iphost = $1
  366. ipport = $2
  367. if type == "qw"
  368. #p current
  369. # check if it already exists
  370. #if @gameservers["#{iphost}:#{ipport}"][:timestamp] > 0
  371. # if old general data, then freshly get general data...
  372. # if @gameservers["#{iphost}:#{ipport}"][:timestamp] + 60 * 60 * 20 < Time.now.utc.to_i || force == true
  373. # end # of old
  374. #else
  375. current = Hash.new
  376. serverinfos = qwstatus(iphost, ipport)
  377. current.store(:serverinfos, serverinfos)
  378. current.store(:reverse_dns, find_reverse_dname(iphost))
  379. current.store(:hostname_dns, qw_find_dname_by_serverinfos(serverinfos))
  380. current.store(:type, "qw")
  381. # set new timestamp
  382. current.store(:timestamp, Time.now.utc.to_i)
  383. @gameservers.store("#{iphost}:#{ipport}", current) # save it all.
  384. current.store(:cool_dns, find_cooldns(iphost, ipport))
  385. @gameservers.store("#{iphost}:#{ipport}", current) # save it all.
  386. #put_log "the saved one: #{@gameservers.fetch("#{iphost}:#{ipport}").fetch(:cool_dns)}"
  387. #puts "ALL"
  388. #p @gameservers
  389. #puts "DEFAULT of the hash"
  390. #p @gameservers.default
  391. #end
  392. end # of type qw
  393. end # of check form of iphostport parameter
  394. end # of scan()
  395. def scanserverlist(gs_array, type="qw")
  396. put_log "scanserverlist drin"
  397. gs_array.each do
  398. |gserver|
  399. scanserver(gserver, type)
  400. end
  401. put_log "End of scanning."
  402. #put_log "foppa: #{@gameservers.fetch("89.104.194.146:27501")} "
  403. end # of scanserverlist()
  404. def get_cool_dns(iphost, ipport)
  405. put_log "get_cool_dns drin"
  406. @gameservers.fetch("#{iphost}:#{ipport}").fetch(:cool_dns)
  407. rescue
  408. scanserver("#{iphost}:#{ipport}", "qw")
  409. @gameservers.fetch("#{iphost}:#{ipport}").fetch(:cool_dns)
  410. end
  411. private # all following methods are private
  412. # returns serverinfo hash
  413. def qwstatus(iphost, ipport)
  414. put_log "qwstatus drin"
  415. udp_payload = [0xFF, 0xFF, 0xFF, 0xFF]
  416. udp_payload.concat(string2bytearray("status 23"))
  417. udp_payload.concat([0x0a]) # linefeed at the end
  418. udp_payload = udp_payload.pack("C*")
  419. #p udp_payload
  420. Timeout::timeout(2) do
  421. begin
  422. u2 = UDPSocket.new
  423. #put_log "#{iphost} #{ipport} #{udp_payload}"
  424. u2.send(udp_payload, 0, iphost, ipport)
  425. #put_log "sent"
  426. the_return = u2.recv(500)
  427. u2.close
  428. #put_log "muh #{the_return}"
  429. if the_return =~ /\W\W\W\Wn\\(.+)$/
  430. line = $1
  431. #put_log "line #{line}"
  432. matches = line.scan(/(.+?)\\(.+?)(\\|$)/)
  433. the_hash = Hash.new
  434. matches.each {
  435. |k, v, _|
  436. the_hash[k] = "#{v}"
  437. }
  438. return the_hash
  439. else
  440. return false
  441. end
  442. rescue Exception => e
  443. puts e.message
  444. end # of begin
  445. end # of Timeout
  446. end
  447. def find_cooldns_full(iphost, ipport)
  448. targetname = iphost
  449. # resolve it to a dns name (reverse lookup)
  450. if targetname == iphost # if it is still default
  451. put_log "Ip not resolved .. we try hostname dns finder"
  452. targetname = qw_find_dname_in_hostnames(iphost, ipport)
  453. end
  454. # resolve it to a dns name (reverse lookup)
  455. if targetname == iphost
  456. put_log "Still no resolve .. we try reverse dns lookup"
  457. targetname = find_reverse_dname(iphost)
  458. end
  459. return targetname
  460. end
  461. def find_cooldns(iphost, ipport)
  462. put_log "find_cooldns drin"
  463. current = @gameservers["#{iphost}:#{ipport}"] # only use this for reading...
  464. my_cooldns = iphost
  465. # we still haven't found a cool dns
  466. if (my_cooldns == iphost) && (not current[:hostname_dns].to_s.empty?)
  467. put_log "Try if #{current[:hostname_dns]} resolves to #{iphost}"
  468. begin
  469. ip_of_hostnamedns = Resolv.getaddress(current[:hostname_dns])
  470. if ip_of_hostnamedns && (ip_of_hostnamedns == iphost)
  471. # ok, we take it.
  472. put_log "Ok, we take #{current[:hostname_dns]} for #{iphost} here.."
  473. my_cooldns = current[:hostname_dns]
  474. else
  475. put_log "Found #{current[:hostname_dns]} but #{ip_of_hostnamedns} is not #{iphost}"
  476. end
  477. rescue Exception => e
  478. my_cooldns = iphost
  479. end
  480. end
  481. # we still haven't found a cool dns
  482. unless current[:reverse_dns].to_s.empty?
  483. if my_cooldns == iphost
  484. rdns = current[:reverse_dns]
  485. # if the resulting dns name...
  486. # .. is too long, use ip-address instead.
  487. # .. has too many dots, use ip-address instead.
  488. # .. has too many numbers, use ip-address instead.
  489. if rdns.length > 21 || rdns.split(".").size > 3 || rdns.scan(/\d/).size > 3
  490. put_log "cutting down host_name: #{rdns}, because:"
  491. put_log "length: #{rdns.length}"
  492. put_log "chunks count: #{rdns.split(".").size}"
  493. put_log "num count: #{rdns.scan(/\d/).size}"
  494. else
  495. my_cooldns = rdns
  496. end # of Resolv.getname
  497. end
  498. end
  499. put_log "COOOOOOOOOOOOLDNS: #{my_cooldns}"
  500. my_cooldns
  501. end
  502. def find_reverse_dname(iphost)
  503. put_log "find_reverse_dname drin"
  504. dname = Resolv.getname(iphost)
  505. ip_of_dname = Resolv.getaddress(dname)
  506. if ip_of_dname == iphost
  507. return dname
  508. end
  509. rescue
  510. iphost
  511. end
  512. def qw_find_dname_by_udp(iphost, ipport)
  513. targetname = iphost # set a default
  514. # get hostname from a qw status packet! perhaps there's a DNS name inside, which we can use!
  515. status = qwstatus(iphost, ipport)
  516. p status
  517. if status["hostname"] =~ /([\w\.-]{3,}\.\w{2,4})/
  518. hostnamedns = $1.downcase
  519. begin
  520. ip_of_hostnamedns = Resolv.getaddress(hostnamedns)
  521. if ip_of_hostnamedns && (ip_of_hostnamedns == iphost)
  522. # ok, we take it.
  523. put_log "Ok, we take #{hostnamedns} for #{iphost} here.."
  524. targetname = hostnamedns
  525. else
  526. put_log "Found #{hostnamedns} but #{ip_of_hostnamedns} is not #{iphost}"
  527. end
  528. rescue Exception => e
  529. targetname = iphost
  530. end
  531. end
  532. targetname
  533. end
  534. def qw_find_dname_by_serverinfos(serverinfos)
  535. hostnamedns = ""
  536. begin
  537. hostname = serverinfos.fetch("hostname")
  538. if hostname =~ /([\w\.-]{3,}\.\w{2,4})/
  539. hostnamedns = $1.downcase
  540. end
  541. return hostnamedns
  542. rescue
  543. return ""
  544. end
  545. end
  546. end # of Class GameServers!
  547. def string2bytearray(text)
  548. return_array = Array.new
  549. text.each_byte{
  550. |b|
  551. return_array.push(b)
  552. }
  553. return_array
  554. end
  555. def put_log(msg)
  556. puts "#{Time.now.utc.strftime("%Y-%m-%d %H:%M:%S")}: #{msg}"
  557. end
  558. module CentralProtocolHandler
  559. @@connected_clients = Array.new
  560. @@broadcasts = Hash.new
  561. put_log "Server started"
  562. # default method that is being run on connection!
  563. def post_init # connection of someone starts here...
  564. @username = nil
  565. @my_servers = Hash.new
  566. @my_servers.default = {
  567. "active" => 0,
  568. "s-p" => "false",
  569. "ping" => 8888,
  570. }
  571. end
  572. ### setters
  573. def set_my_servers(server, key, value)
  574. current = Hash.new
  575. current.store(key, value)
  576. @my_servers[server] = @my_servers[server].merge(current)
  577. end
  578. def specbot_set_sp(username, server, value)
  579. @@connected_clients.each {|c|
  580. if c.username == username
  581. c.set_my_servers(server, "s-p", value)
  582. end
  583. }
  584. end
  585. ### getters
  586. def entered_username?
  587. !@username.nil? && !@username.empty? # then it's true
  588. end
  589. def username
  590. @username
  591. end
  592. def my_active_servers(active = true)
  593. unless @my_servers.nil? || @my_servers.empty?
  594. if active
  595. rethash = Hash.new
  596. @my_servers.each_pair{|k, v|
  597. if v["active"] == 1
  598. rethash[k] = @my_servers[k]
  599. end
  600. }
  601. rethash
  602. else
  603. @my_servers
  604. end
  605. end
  606. end
  607. def specbot_servers(flat = true)
  608. the_servers = Hash.new
  609. @@connected_clients.each {|c|
  610. if flat
  611. unless c.my_active_servers.nil? || c.my_active_servers.empty?
  612. the_servers = the_servers.merge(c.my_active_servers)
  613. end
  614. else
  615. unless c.my_active_servers.nil? || c.my_active_servers.empty?
  616. the_servers[c.username] = c.my_active_servers
  617. end
  618. end
  619. }
  620. the_servers
  621. end
  622. def specbot_ping(server)
  623. ms = my_active_servers(false)
  624. unless ms.nil?
  625. si = ms[server]
  626. p1 = si.fetch("ping").to_s.to_i
  627. return p1
  628. else
  629. return 8888
  630. end
  631. end
  632. def specbot_active?(server)
  633. ms = my_active_servers(false)
  634. unless ms.nil?
  635. si = ms[server]
  636. a1 = si.fetch("active")
  637. else
  638. a1 = 0
  639. end
  640. if a1 == 1
  641. return 1
  642. else
  643. return 0
  644. end
  645. end
  646. def specbot_sp?(server)
  647. ms = my_active_servers(false)
  648. unless ms.nil?
  649. si = ms[server]
  650. a1 = si.fetch("s-p")
  651. else
  652. a1 = "false"
  653. end
  654. if a1 == "true"
  655. return true
  656. else
  657. return false
  658. end
  659. end
  660. def my_maxservers
  661. if @my_maxservers.nil? || @my_maxservers.empty?
  662. nil
  663. else
  664. @my_maxservers
  665. end
  666. end
  667. def my_roles
  668. $user_roles[@username]
  669. end
  670. def my_cmds
  671. my_roles.collect {|v| $role_commands[v]}.flatten
  672. end
  673. # returns online users by default by searching through saved connections
  674. def ousers
  675. users = Array.new
  676. @@connected_clients.each {|c| users.push(c.username)}
  677. users
  678. end # of ousers
  679. # returns online users by searching through saved connections that have the specified role
  680. def ousers_by_role(role)
  681. users = Array.new
  682. @@connected_clients.each {|c|
  683. if c.my_roles.include?(role)
  684. users.push(c.username)
  685. end
  686. }
  687. users
  688. end
  689. # returns connections by searching through saved connections that have the specified role
  690. def connections_by_role(role)
  691. conns = Array.new
  692. @@connected_clients.each {|c|
  693. if c.my_roles.include?(role)
  694. conns.push(c)
  695. end
  696. }
  697. conns
  698. end
  699. ### checkers
  700. def allowed_cmd(inputmessage)
  701. my_cmds.each{|c|
  702. #print "A #{c} B #{inputmessage}\n"
  703. if inputmessage =~ /^#{c}/
  704. return true
  705. end
  706. }
  707. false
  708. end
  709. def online(user)
  710. if ousers.include? user
  711. true
  712. else
  713. false
  714. end
  715. end
  716. ### actions
  717. def specbot_dupecheck()
  718. put_log "specbot_dupecheck drin"
  719. conns = connections_by_role("specbot")
  720. # if this connection has a same key than another connection
  721. # then fight .. which connection should have that server unassigned. REQ_PING
  722. # the one with the better ping will win and the one with the worse ping will get a new free server slot ;)
  723. # we have at least 2 bots... > 1
  724. conns.repeated_combination(2){|c|
  725. c1 = c[0]
  726. c2 = c[1]
  727. unless c1 == c2
  728. unless c1.my_active_servers.nil? || c2.my_active_servers.nil?
  729. dupes = c1.my_active_servers.keys & c2.my_active_servers.keys
  730. if dupes.size > 0
  731. # damn, there's dupes.
  732. put_log "the dupes are: #{dupes.join(", ")} on #{c1.username} and #{c2.username}"
  733. dupes.each{|d|
  734. write_user("REQ_PING #{d}", c1.username)
  735. write_user("REQ_PING #{d}", c2.username)
  736. EventMachine.add_timer 2, proc {
  737. p1 = c1.specbot_ping(d)
  738. p2 = c2.specbot_ping(d)
  739. a1 = c1.specbot_active?(d)
  740. a2 = c2.specbot_active?(d)
  741. if p1 <= p2
  742. unless a2 == 0
  743. write_user("REQ_UNASSIGN #{d}", c2.username)
  744. end
  745. else
  746. unless a1 == 0
  747. write_user("REQ_UNASSIGN #{d}", c1.username)
  748. end
  749. end
  750. }
  751. }
  752. end
  753. end
  754. end
  755. }
  756. end # of specbot_dupecheck
  757. # searches through our hash of saved connections and writes them a messages.
  758. def write_user(input, username = nil)
  759. if username.nil? || username.empty?
  760. username = @username
  761. end
  762. if @@connected_clients.find { |c| c.username == username }
  763. connection = @@connected_clients.find { |c| c.username == username }
  764. put_log("to #{username}: #{input}")
  765. sometime = "#{Time.now.utc.strftime("%Y-%m-%d %H:%M:%S %z")}"
  766. line = "#{sometime}: #{input}\n"
  767. connection.send_data line
  768. end
  769. end # of write_user
  770. # searches through roles and writes those users on their connections
  771. def write_role(role, input, *exempts)
  772. #find users with role
  773. users = Array.new
  774. $user_roles.each {|k, v|
  775. if v.include? role
  776. users.push(k)
  777. end
  778. }
  779. # find users that are online and inside Array "users"
  780. lala = Array.new
  781. users.each {|v|
  782. if ousers.include? v
  783. lala.push(v)
  784. end
  785. }
  786. # now write to them
  787. lala.each {|user|
  788. if not exempts.include? user
  789. write_user(input, user)
  790. end
  791. }
  792. end # of write_role()
  793. # what happens with what input?!
  794. def inputting(input)
  795. put_log("SYS #{@username} typed: #{input}")
  796. #write_user("SYS You typed: #{input}")
  797. if input =~ /^([A-Z_]+)/
  798. cmd = $1
  799. end
  800. # now we have the cmd .. or not ;)
  801. if input =~ /^[A-Z_]+ (.+)/
  802. payload = $1
  803. end
  804. # now we can use payload
  805. if allowed_cmd(cmd)
  806. ### typical system commands follow
  807. if cmd == "PING"
  808. write_user("PONG")
  809. elsif cmd == "C"
  810. if payload =~ /^(.+)$/
  811. write_role($default_role, "C #{@username}: #{$1}")
  812. else
  813. write_user("SYS Format is C <chattext>")
  814. end
  815. elsif cmd == "WHO"
  816. ousers.each {|ouser| write_user("WHO_RE #{ouser} ROLES: #{$user_roles[ouser].join(", ")}")}
  817. elsif cmd == "PART"
  818. write_user("SYS Goodbye '#{@username}'.")
  819. write_role($default_role, "PARTED User '#{@username}' leaves the party.", @username)
  820. return "bye"
  821. ### now for the good stuff, broadcasting role
  822. elsif cmd == "REQ_BC"
  823. # help with format .. but send the raw payload
  824. if payload =~ /^(.+),(.+),(.+),'(.+)','(.+)'$/
  825. # n f s ni txt
  826. error = 0
  827. # $&
  828. # The string matched by the last successful pattern match in this scope, or nil if the last pattern match failed. (Mnemonic: like & in some editors.) This variable is r
  829. network = $1
  830. freqname = $2
  831. source = $3
  832. nickname = $4
  833. text = $5
  834. if not network =~ /^(QWalt)|(QDEV)/
  835. write_user("SYS Network name #{network} is unknown: QWalt or QDEV allowed.")
  836. error = 1
  837. end
  838. if not freqname =~ /^(-qw-)|(-spam-)|(-dev-)/
  839. write_user("SYS Frequency name is unknown #{freqname}")
  840. error = 1
  841. end
  842. if source =~ /^(#.+)|(qw:\/\/)|(http:\/\/)/
  843. else
  844. write_user("SYS Source string is not in the form ^(#.+)|(qw:\/\/)|(http:\/\/) was: #{source}")
  845. error = 1
  846. end
  847. if not nickname =~ /^.+/
  848. write_user("SYS Nickname string is not in the form ^.+ #{nickname}")
  849. error = 1
  850. end
  851. if not text =~ /^.+/
  852. write_user("SYS Message string is not in the form ^.+ #{text}")
  853. error = 1
  854. end
  855. else # of payload has format
  856. write_user("SYS Command format is REQ_BC <network>,<frequency>,<source/channel>,'<nickname>','<message>'")
  857. error = 1
  858. end # of payload has format
  859. if error == 0
  860. # send REQ_BC to the corresponding role.
  861. bcid = Digest::MD5.hexdigest("%d %s %s %s %s %s" % [Time.now.utc.to_i, network, freqname, source, nickname, text])
  862. # so it only reaches the issuer of REQ_BC
  863. write_user("BC_ID #{bcid} for: #{network},#{freqname},#{source}")
  864. @@broadcasts[bcid] = @username
  865. # qw:// ip resolving
  866. if source =~ /^qw:\/\/(.+):(\d+)(.*)$/
  867. # ip address is 15 in length
  868. iphost = $1
  869. targetname = iphost # set a default
  870. ipport = $2
  871. rest = $3
  872. if iphost =~ /^\d+\.\d+\.\d+\.\d+/
  873. targetname = $gs.get_cool_dns(iphost, ipport)
  874. # find country code
  875. cc = GeoIP.new('GeoIP.dat').country(iphost)[3].to_s.downcase
  876. rest.prepend " #{cc}" unless cc.nil? || cc.empty? || targetname =~ /\.#{cc}$/
  877. end # if ip is x.x.x.x
  878. source = "qw://#{targetname}:#{ipport}#{rest}"
  879. end # of if source qw://x.x.x.x:portzzz
  880. # resolve
  881. finalmessage = "BC %s %s,%s,%s,'%s','%s'" % [bcid, network, freqname, source, nickname, text]
  882. write_role("broadcast", finalmessage, @username) # write to the role with the exempt of myself.
  883. end
  884. #end of REQ_BC here..
  885. elsif cmd == "BC_RE"
  886. if payload =~ /^(.+) (.+)=(\d+),(.+)=(\d+)$/
  887. bcid = $1
  888. user_string = $2
  889. user_count = $3
  890. item_string = $4
  891. item_count = $5
  892. payload = "%s %s=%d,%s=%d" % [bcid, user_string, user_count, item_string, item_count]
  893. # according bcid it is possible to get the originating user.. so send only to his topic!
  894. if @@broadcasts[bcid]
  895. to_user = @@broadcasts[bcid]
  896. write_user("SYS Broadcast reply bcid: #{bcid} underway to #{to_user}.")
  897. write_user("#{cmd} #{payload}", to_user)
  898. else
  899. write_user("SYS Broadcast reply bcid: #{bcid} underway.")
  900. write_role("broadcast", "#{cmd} #{payload}", @username)
  901. end
  902. else # of bc_re format check
  903. put_log "SYS Format is BC_RE <bcid> <userstring>=<usercount>,<itemstring>=<itemcount>"
  904. end
  905. ### the specbot-admin ROLE does ...
  906. elsif cmd == "REQ_ASSIGN"
  907. if payload =~ /^([a-zA-Z_\.]+) (\d+\.\d+\.\d+\.\d+:\d{1,5})[,]?(true|false)?/
  908. specbot = $1
  909. hostport = $2
  910. sp = $3
  911. if sp == "true"
  912. specbot_set_sp(specbot, hostport, sp)
  913. write_user("REQ_ASSIGN #{hostport},#{sp}", specbot)
  914. else
  915. write_user("REQ_ASSIGN #{hostport}", specbot)
  916. end
  917. else # of format check
  918. write_user("SYS Format is REQ_ASSIGN <specbot> <ip:port>[,<s-p;true or false>]")
  919. end
  920. elsif cmd == "REQ_UNASSIGN"
  921. if payload =~ /^([a-zA-Z_\.]+) (\d+\.\d+\.\d+\.\d+:\d{1,5})/
  922. specbot = $1
  923. hostport = $2
  924. write_user("REQ_UNASSIGN #{hostport}", specbot)
  925. else # of format check
  926. write_user("SYS Format is REQ_UNASSIGN <specbot> <ip:port>")
  927. end
  928. elsif cmd == "REQ_PING"
  929. if payload =~ /^([a-zA-Z_\.]+) (\d+\.\d+\.\d+\.\d+:\d{1,5})/
  930. specbot = $1
  931. hostport = $2
  932. write_user("REQ_PING #{hostport}", specbot)
  933. else # of format check
  934. write_user("SYS Format is REQ_PING <specbot> <ip:port>")
  935. end
  936. elsif cmd == "REQ_ASSIGNMENTS"
  937. if payload =~ /^([a-zA-Z_\.]+)/
  938. specbot = $1
  939. write_user("REQ_ASSIGNMENTS give me your assignments", specbot)
  940. else # of format check
  941. write_user("SYS Format is REQ_ASSIGNMENTS <specbot>")
  942. end
  943. elsif cmd == "REQ_MAXSERVERS"
  944. if payload =~ /^([a-zA-Z_\.]+)/
  945. specbot = $1
  946. write_user("REQ_MAXSERVERS how many do you do?", specbot)
  947. else # of format check
  948. write_user("SYS Format is REQ_MAXSERVERS <specbot>")
  949. end
  950. elsif cmd == "DUPECHECK"
  951. write_user("SYS specbot server monitoring dupecheck started")
  952. # start the central function for dupechecking:
  953. specbot_dupecheck()
  954. elsif cmd == "ELECTION"
  955. write_user("SYS specbot server ping election on global server list started")
  956. # start the central function for server election:
  957. all_servers = specbot_servers()
  958. put_log("#{all_servers}")
  959. write_user("SYS active unique server count: #{all_servers.size}")
  960. conns = connections_by_role("specbot")
  961. #für jeden server, jeden ping der einzelnen bots vergleichen..
  962. #falls ping info noch nicht vorhanden (>5000), dann einfordern
  963. all_servers.each_key{|k|
  964. put_log("SYS bots race for #{k} now..")
  965. conns.each{|c|
  966. ping = c.specbot_ping(k)
  967. if ping > 5000
  968. write_user("REQ_PING #{k}", c.username)
  969. end
  970. }
  971. EventMachine.add_timer 5, proc {
  972. lastbest = 5000
  973. winner = ""
  974. conns.each{|c|
  975. ping = c.specbot_ping(k)
  976. put_log("SYS #{ping} of #{c.username} to #{k}")
  977. if ping < lastbest
  978. lastbest = ping
  979. winner = c
  980. put_log("SYS - current best bot for #{k} is #{c.username}")
  981. end
  982. }
  983. put_log("SYS --> best bot for #{k} is #{winner.username}")
  984. conns.each{|c|
  985. a = c.specbot_active?(k)
  986. unless c == winner
  987. unless a == 0
  988. write_user("REQ_UNASSIGN #{k}", c.username)
  989. end
  990. end
  991. if c == winner
  992. unless a == 1
  993. as = all_servers[k]
  994. sp = as.fetch("s-p")
  995. if sp == "true" || sp == "1"
  996. write_user("REQ_ASSIGN #{k},true", c.username)
  997. else
  998. write_user("REQ_ASSIGN #{k}", c.username)
  999. end
  1000. end
  1001. end
  1002. }
  1003. } # end of timer
  1004. } # end of all active servers loop
  1005. ### the specbot ROLE does ...
  1006. elsif cmd == "ASSIGNMENTS_RE"
  1007. if payload =~ /^(\d+\.\d+\.\d+\.\d+:\d{1,5}),(.*),(.*)/
  1008. hostport = $1
  1009. sp = $2
  1010. ping = $3.chomp
  1011. p hostport
  1012. p sp
  1013. p ping
  1014. current = Hash.new()
  1015. current.store("active", 1)
  1016. current.store("s-p", sp)
  1017. current.store("ping", ping)
  1018. # save the hash current to the hash @my_servers
  1019. @my_servers[hostport] = @my_servers[hostport].merge(current)
  1020. p @my_servers
  1021. # save new hash to a config file or sth. fixme
  1022. end
  1023. #end of ASSIGNMENTS_RE here..
  1024. elsif cmd == "MAXSERVERS_RE"
  1025. if payload =~ /^(\d+)/
  1026. @my_maxservers = $1
  1027. end
  1028. #end of MAXSERVERS_RE here..
  1029. elsif cmd == "PING_RE"
  1030. if payload =~ /^(\d+\.\d+\.\d+\.\d+:\d{1,5}),(.*)/
  1031. hostport = $1
  1032. ping = $2
  1033. current = Hash.new()
  1034. current.store("ping", ping)
  1035. # save the hash current to the hash @my_servers
  1036. @my_servers[hostport] = @my_servers[hostport].merge(current)
  1037. p @my_servers
  1038. # save new hash to a config file or sth. fixme
  1039. end
  1040. #end of PING_RE here..
  1041. elsif cmd == "ASSIGN_RE"
  1042. #assign_re should only be issued when a req_assign was issued to the specbot before
  1043. if payload =~ /^(\d+\.\d+\.\d+\.\d+:\d{1,5}) ([A-Z]+)[\s]?(.*)/
  1044. hostport = $1
  1045. good = $2
  1046. reason = $3
  1047. if good == "OK"
  1048. current = Hash.new()
  1049. current.store("active", 1)
  1050. @my_servers[hostport] = @my_servers[hostport].merge(current) # this way it preserves previously saved ping values
  1051. write_user("REQ_PING #{hostport}")
  1052. put_log "SYS #{username} assigned to #{hostport} and asked ping for it."
  1053. write_role("specbot_admin", "SYS #{username} assigned to #{hostport} and asked ping for it.")
  1054. else
  1055. put_log "SYS #{username} failed to assign #{hostport}, reason: '#{reason}'"
  1056. write_role("specbot_admin", "SYS #{username} failed to assign #{hostport}, reason: '#{reason}'")
  1057. end
  1058. end
  1059. #end of ASSIGN_RE here..
  1060. elsif cmd == "UNASSIGN_RE"
  1061. if payload =~ /^(\d+\.\d+\.\d+\.\d+:\d{1,5}) ([A-Z]+)[\s]?(.*)/
  1062. hostport = $1
  1063. good = $2
  1064. reason = $3
  1065. if good == "OK"
  1066. current = Hash.new()
  1067. current.store("active", 0)
  1068. @my_servers[hostport] = @my_servers[hostport].merge(current) # this way it preserves previously saved ping values
  1069. put_log "SYS #{username} unassigned #{hostport}"
  1070. write_role("specbot_admin", "SYS #{username} unassigned #{hostport}")
  1071. else
  1072. put_log "SYS #{username} failed to unassign #{hostport}, reason: '#{reason}'"
  1073. write_role("specbot_admin", "SYS #{username} failed to unassign #{hostport}, reason: '#{reason}'")
  1074. end
  1075. end
  1076. #end of UNASSIGN_RE here..
  1077. elsif cmd == "REQ_DNS"
  1078. # help with format .. but send the raw payload
  1079. if payload =~ /^(\d+\.\d+\.\d+\.\d+):(\d{1,5})/
  1080. iphost = $1
  1081. ipport = $2
  1082. targetname = $gs.get_cool_dns(iphost, ipport)
  1083. write_user("DNS_RE #{iphost}:#{ipport} #{targetname}") # write back to asking user.
  1084. else # of payload has format
  1085. write_user("SYS Command format is REQ_DNS <serverip>:<serverport>")
  1086. error = 1
  1087. end # of if payload has format
  1088. #end of REQ_DNS here..
  1089. end
  1090. else # of if allowed command
  1091. if input.length > 15
  1092. input = input.slice(0,14) + ".."
  1093. end
  1094. write_user("SYS Command '#{input}' not allowed, your commands are: #{my_cmds.join(", ")}.")
  1095. end # of if allowed command
  1096. end # of inputting!
  1097. # default method that is being run on receiving data!
  1098. def receive_data(data) # connection receives a line on the server end
  1099. data = data.chomp unless data.chomp.nil?
  1100. data.each_line do |line|
  1101. unless line.empty?
  1102. if entered_username? # oh, it's a known user
  1103. # it's alive! kill ping timer, set a new one:
  1104. if @ping_timer
  1105. EventMachine.cancel_timer(@ping_timer)
  1106. end
  1107. @ping_timer = EventMachine.add_periodic_timer 180, proc { write_user("PING alive?") }
  1108. # work on input
  1109. inputter = inputting(line)
  1110. if inputter == "bye"
  1111. close_connection
  1112. end
  1113. else # of username known, then it seems to be the first connect
  1114. # and this line is the user name coming in!
  1115. username = line
  1116. if online(username)
  1117. put_log "SYS User '#{username}' tried to double connect. Say bye bye."
  1118. send_data "SYS Hey '#{username}', only one connection allowed. Bye.\n"
  1119. EventMachine.add_timer 1, proc {
  1120. write_user("PING check forced", username)
  1121. close_connection
  1122. }
  1123. return false
  1124. end
  1125. if $user_roles.any? {|k| k.include? username}
  1126. @username = username
  1127. @@connected_clients.push(self)
  1128. @ping_timer = EventMachine.add_periodic_timer 90, proc { write_user("PING alive?") }
  1129. # starting a pinger
  1130. put_log("SYS '#{@username}' connected. Online now: #{ousers.join(", ")}")
  1131. write_user("HELLO Hi user '#{@username}'! How are you? I'm '#{$version}'")
  1132. write_user("ROLES #{my_roles.join(", ")}")
  1133. write_user("COMMANDS #{my_cmds.join(", ")}")
  1134. write_role($default_role, "JOINED User '#{@username}' just joined the party.", @username)
  1135. # if that guy is a specbot, ask it for his current list
  1136. if my_roles.include?("specbot")
  1137. EventMachine.add_timer 2, proc {
  1138. write_user("REQ_MAXSERVERS how many can you do?")
  1139. write_user("REQ_ASSIGNMENTS gimme all your servers")
  1140. }
  1141. # if more than 1 specbots are connected, then we want to activate the dupe-check.
  1142. if connections_by_role("specbot").size > 1
  1143. # in 6 seconds we start it. by then, we should have the full serverlists of all bots
  1144. EventMachine.add_timer 6, proc {
  1145. put_log("this guy: #{username} triggers the dupecheck now")
  1146. specbot_dupecheck
  1147. }
  1148. end
  1149. end
  1150. else
  1151. put_log "SYS User '#{username}' unknown to me. Saying bye."
  1152. send_data "SYS User '#{username}' unknown to me. Bye.\n"
  1153. # disconnect him
  1154. close_connection
  1155. end
  1156. end # of username known
  1157. end # of unless line.empty?
  1158. end # of data.each_line
  1159. end # of receive data
  1160. # default method that is being run on disconnection!
  1161. def unbind # a connection goes bye bye
  1162. if @username
  1163. @@connected_clients.delete(self)
  1164. if @ping_timer
  1165. EventMachine.cancel_timer(@ping_timer)
  1166. end
  1167. put_log "SYS '#{@username}' disconnected."
  1168. write_role($default_role, "SYS User '#{@username}' disconnected.", @username)
  1169. end
  1170. put_log "SYS Online users now: #{ousers.join(", ")}"
  1171. end
  1172. end
  1173. def main
  1174. # #run: Note that this will block current thread.
  1175. $gs = GameServers.new
  1176. EventMachine.run do
  1177. # initial scan
  1178. EM.defer do
  1179. $gs.scanserverlist($qw_list, "qw") # initial scan
  1180. end
  1181. # periodic scan, 20 hours
  1182. EventMachine.add_periodic_timer( 60 * 60 * 20 ) {
  1183. EM.defer do
  1184. $gs.scanserverlist($qw_list, "qw") # initial scan
  1185. end
  1186. }
  1187. EventMachine.start_server("0.0.0.0", 7337, CentralProtocolHandler)
  1188. end
  1189. end # of main
  1190. main