Simple bridge server for various bots of mine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

604 lines
15 KiB

1 year ago
12 months ago
1 year ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
12 months ago
12 months ago
1 year ago
1 year ago
12 months ago
12 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
12 months ago
12 months ago
12 months ago
12 months ago
1 year ago
1 year ago
12 months ago
1 year ago
  1. require_relative "proto"
  2. require_relative "hyde/hyde"
  3. require 'webrick/websocket'
  4. require "json"
  5. Users = Heimdall::UserCache.new
  6. Rooms = Heimdall::RoomCache.new
  7. SocketsMap = {}
  8. NotifyList = {}
  9. def _require_keys(dict,key_dict)
  10. raise KeyError, "not a dict" unless dict.kind_of? Hash
  11. key_dict.each_pair { |k,v|
  12. unless (dict.has_key? k.to_s) and (dict[k.to_s].kind_of? v) then
  13. raise KeyError, "key #{k} of type #{v} required"
  14. end
  15. }
  16. end
  17. def _send_json(res,data,code: 200)
  18. res.body = JSON::fast_generate(data)
  19. res['Content-Type'] = "application/json"
  20. res.status = code
  21. end
  22. def _throw_error(res,error)
  23. _send_json(res,{
  24. error: "#{error}"
  25. },code: 400)
  26. end
  27. def _parse_json(body,key_dict)
  28. data = JSON::Parser.new(body).parse
  29. _require_keys(data,key_dict)
  30. return data
  31. end
  32. server = Hyde::Server.new Port: 8000 do
  33. path "user" do
  34. path "find" do
  35. index ["by-name"]
  36. get "by-name" do |ctx|
  37. req,res = ctx.request,ctx.response
  38. begin
  39. _require_keys(req.query, {
  40. username: String
  41. })
  42. _send_json(res, {
  43. "results": Users.search_by_name(req.query['username']).map { |x| x[1].to_card }
  44. })
  45. rescue KeyError => keyerror
  46. _throw_error(res,keyerror)
  47. end
  48. end
  49. get "by-protoid" do |ctx|
  50. req,res = ctx.request,ctx.response
  51. begin
  52. _require_keys(req.query, {
  53. protocol_id: String
  54. })
  55. _send_json(res, {
  56. "results": Users.search_by_protoid(req.query['protocol_id']).map { |x| x[1].to_card; x }
  57. })
  58. rescue KeyError => keyerror
  59. _throw_error(res,keyerror)
  60. end
  61. end
  62. end
  63. post "sync" do |ctx|
  64. req,res = ctx.request,ctx.response
  65. begin
  66. data = _parse_json(req.body, {
  67. data: Array
  68. })
  69. Users.sync(data["data"])
  70. _send_json(res, {
  71. status: true
  72. })
  73. rescue KeyError => keyerror
  74. _throw_error(res,keyerror)
  75. rescue JSON::ParserError => jsonerror
  76. _throw_error(res,jsonerror)
  77. end
  78. end
  79. post "bulk-delete" do |ctx|
  80. req,res = ctx.request,ctx.response
  81. begin
  82. data = _parse_json(req.body, {
  83. users: Array
  84. })
  85. Users.bulk_delete(data["users"])
  86. _send_json(res, {
  87. status: true
  88. })
  89. rescue KeyError => keyerror
  90. _throw_error(res,keyerror)
  91. rescue JSON::ParserError => jsonerror
  92. _throw_error(res,jsonerror)
  93. end
  94. end
  95. get "exists" do |ctx|
  96. req,res = ctx.request,ctx.response
  97. begin
  98. _require_keys(req.query, {
  99. protocol_id: String
  100. })
  101. _send_json(res,{
  102. exists: (Users.get(req.query["protocol_id"]) != nil)
  103. })
  104. rescue KeyError => keyerror
  105. _throw_error(res,keyerror)
  106. rescue Heimdall::ProtocolError => protoerr
  107. _send_json(res, {
  108. exists: false
  109. })
  110. end
  111. end
  112. post "new" do |ctx|
  113. req,res = ctx.request,ctx.response
  114. begin
  115. data = _parse_json(req.body,{
  116. username: String,
  117. protocol_id: String
  118. })
  119. new_user = Heimdall::User.new(data)
  120. Users.add(new_user)
  121. _send_json(res,{
  122. status: true
  123. })
  124. NotifyList[new_user.UID] = []
  125. rescue JSON::ParserError => jsonerror
  126. _throw_error(res,jsonerror)
  127. rescue KeyError => keyerror
  128. _throw_error(res,keyerror)
  129. rescue ProtocolError => protoerr
  130. _throw_error(res,protoerr)
  131. end
  132. end
  133. post "modify" do |ctx|
  134. req,res = ctx.request,ctx.response
  135. begin
  136. data = _parse_json(req.body, {
  137. data: Hash,
  138. protocol_id: String
  139. })
  140. user = Users.get(data["protocol_id"])
  141. user.modify(data["data"])
  142. _send_json(res, {
  143. status: true
  144. })
  145. rescue KeyError => keyerror
  146. _throw_error(res,keyerror)
  147. rescue ProtocolError => protoerr
  148. _throw_error(res,protoerr)
  149. rescue JSON::ParserError => jsonerror
  150. _throw_error(res,jsonerror)
  151. end
  152. end
  153. post "send" do |ctx|
  154. req,res = ctx.request,ctx.response
  155. begin
  156. data = _parse_json(req.body, {
  157. content: String,
  158. from: String,
  159. to: String
  160. })
  161. new_message = Heimdall::Message.new(data)
  162. user = Users.get(new_message.to)
  163. if NotifyList[user.UID].length != 0 then
  164. NotifyList[user.UID].each { |sockid|
  165. sock = SocketsMap[sockid]
  166. msg = new_message.to_struct
  167. msg["user"] = Users.get(msg["from"]).to_card
  168. sock.puts(JSON::fast_generate(msg))
  169. }
  170. user.channel.send_silent(new_message)
  171. else
  172. user.channel.send(new_message)
  173. end
  174. _send_json(res,{
  175. status: true
  176. })
  177. rescue JSON::ParserError => jsonerror
  178. _throw_error(res,jsonerror)
  179. rescue KeyError => keyerror
  180. _throw_error(res,keyerror)
  181. rescue Heimdall::ProtocolError => protoerr
  182. _throw_error(res,protoerr)
  183. end
  184. end
  185. get "get" do |ctx|
  186. req,res = ctx.request,ctx.response
  187. begin
  188. _require_keys(req.query,{
  189. n: Integer,
  190. protocol_id: String
  191. })
  192. number = req.query[:n]
  193. id = req.query["protocol_id"]
  194. user = Users.get(id)
  195. messages = user.channel.get(number)
  196. _send_json(res, {
  197. messages: messages.map { |x|
  198. x = x.to_struct
  199. x["user"] = Users.get(x["from"]).to_card
  200. x
  201. }
  202. })
  203. rescue Heimdall::ProtocolError => protoerr
  204. _throw_error(res,protoerr)
  205. end
  206. end
  207. get "read" do |ctx|
  208. req,res = ctx.request,ctx.response
  209. begin
  210. _require_keys(req.query,{
  211. protocol_id: String
  212. })
  213. id = req.query["protocol_id"]
  214. user = Users.get(id)
  215. messages = user.channel.read
  216. _send_json(res, {
  217. messages: messages.map { |x|
  218. x = x.to_struct
  219. x["user"] = Users.get(x["from"]).to_card
  220. x
  221. }
  222. })
  223. rescue Heimdall::ProtocolError => protoerr
  224. _throw_error(res,protoerr)
  225. end
  226. end
  227. post "listen" do |ctx|
  228. req,res = ctx.request, ctx.response
  229. begin
  230. data = _parse_json(req.body, {
  231. websocket: String,
  232. protocol_id: String
  233. })
  234. uid = Users.get(data["protocol_id"]).UID
  235. raise KeyError, "websocket does not exist" unless SocketsMap.has_key? data["websocket"]
  236. NotifyList[uid].append data["websocket"]
  237. _send_json(res, {
  238. status: true
  239. })
  240. rescue KeyError => keyerror
  241. _throw_error(res,keyerror)
  242. rescue JSON::ParserError => jsonerror
  243. _throw_error(res,jsonerror)
  244. end
  245. end
  246. post "unlisten" do |ctx|
  247. req,res = ctx.request, ctx.response
  248. begin
  249. data = _parse_json(req.body, {
  250. websocket: String,
  251. protocol_id: String
  252. })
  253. uid = Users.get(data["protocol_id"]).UID
  254. raise KeyError, "websocket does not exist" unless SocketsMap.has_key? data["websocket"]
  255. NotfiyList[uid].delete data["websocket"]
  256. _send_json(res, {
  257. status: true
  258. })
  259. rescue KeyError => keyerror
  260. _throw_error(res,keyerror)
  261. rescue JSON::ParserError => jsonerror
  262. _throw_error(res,jsonerror)
  263. end
  264. end
  265. post "delete" do |ctx|
  266. req,res = ctx.request, ctx.response
  267. begin
  268. data = _parse_json(req.body,{
  269. protocol_id: String
  270. })
  271. id = data["protocol_id"]
  272. _send_json(res, {
  273. status: true
  274. })
  275. user = Users.get(id)
  276. NotifyList.delete(user.UID)
  277. Users.delete(id)
  278. rescue Heimdall::ProtocolError => protoerr
  279. _throw_error(res,protoerr)
  280. rescue JSON::ParserError => jsonerror
  281. _throw_error(res,jsonerror)
  282. end
  283. end
  284. end
  285. path "room" do
  286. path "find" do
  287. index ["by-name"]
  288. get "by-name" do |ctx|
  289. req,res = ctx.request,ctx.response
  290. begin
  291. _require_keys(req.query, {
  292. username: String
  293. })
  294. _send_json(res, {
  295. "results": Rooms.search_by_name(req.query['username']).map { |x| x[1].to_card }
  296. })
  297. rescue KeyError => keyerror
  298. _throw_error(res,keyerror)
  299. end
  300. end
  301. get "by-protoid" do |ctx|
  302. req,res = ctx.request,ctx.response
  303. begin
  304. _require_keys(req.query, {
  305. protocol_id: String
  306. })
  307. _send_json(res, {
  308. "results": Rooms.search_by_protoid(req.query['protocol_id']).map { |x| x[1].to_card; x }
  309. })
  310. rescue KeyError => keyerror
  311. _throw_error(res,keyerror)
  312. end
  313. end
  314. end
  315. post "new" do |ctx|
  316. req,res = ctx.request,ctx.response
  317. begin
  318. data = _parse_json(req.body, {
  319. name: String,
  320. protocol_id: String
  321. })
  322. new_room = Heimdall::Room.new(data)
  323. Rooms.add(new_room)
  324. _send_json(res, {
  325. status: true
  326. })
  327. NotifyList[new_room.UID] = []
  328. rescue KeyError => keyerror
  329. _throw_error(res,keyerror)
  330. rescue JSON::ParserError => jsonerror
  331. _throw_error(res,jsonerror)
  332. end
  333. end
  334. post "send" do |ctx|
  335. req,res = ctx.request,ctx.response
  336. begin
  337. data = _parse_json(req.body, {
  338. content: String,
  339. from: String,
  340. to: String
  341. })
  342. new_message = Heimdall::Message.new(data)
  343. room = Rooms.get(new_message.to)
  344. if NotifyList[room.UID].length != 0 then
  345. NotifyList[room.UID].each { |sockid|
  346. sock = SocketsMap[sockid]
  347. msg = new_message.to_struct
  348. msg["user"] = Users.get(msg["from"]).to_card
  349. msg["room"] = room.to_card
  350. sock.puts(JSON::fast_generate(msg))
  351. }
  352. room.channel.send_silent(new_message)
  353. else
  354. room.channel.send(new_message)
  355. end
  356. _send_json(res,{
  357. status: true
  358. })
  359. rescue JSON::ParserError => jsonerror
  360. _throw_error(res,jsonerror)
  361. rescue KeyError => keyerror
  362. _throw_error(res,keyerror)
  363. rescue Heimdall::ProtocolError => protoerr
  364. _throw_error(res,protoerr)
  365. end
  366. end
  367. get "get" do |ctx|
  368. req,res = ctx.request,ctx.response
  369. begin
  370. _require_keys(req.query,{
  371. n: Integer,
  372. protocol_id: String
  373. })
  374. number = req.query[:n]
  375. id = req.query["protocol_id"]
  376. room = Rooms.get(id)
  377. messages = room.channel.get(number)
  378. _send_json(res, {
  379. messages: messages.map { |x|
  380. x = x.to_struct
  381. x["user"] = Users.get(x["from"]).to_card
  382. x["room"] = room.to_card
  383. x
  384. }
  385. })
  386. rescue Heimdall::ProtocolError => protoerr
  387. _throw_error(res,protoerr)
  388. end
  389. end
  390. post "listen" do |ctx|
  391. req,res = ctx.request, ctx.response
  392. begin
  393. data = _parse_json(req.body, {
  394. websocket: String,
  395. protocol_id: String
  396. })
  397. uid = Rooms.get(data["protocol_id"]).UID
  398. raise KeyError, "websocket does not exist" unless SocketsMap.has_key? data["websocket"]
  399. NotifyList[uid].append data["websocket"]
  400. _send_json(res, {
  401. status: true
  402. })
  403. rescue KeyError => keyerror
  404. _throw_error(res,keyerror)
  405. rescue JSON::ParserError => jsonerror
  406. _throw_error(res,jsonerror)
  407. end
  408. end
  409. post "unlisten" do |ctx|
  410. req,res = ctx.request, ctx.response
  411. begin
  412. data = _parse_json(req.body, {
  413. websocket: String,
  414. protocol_id: String
  415. })
  416. uid = Rooms.get(data["protocol_id"]).UID
  417. raise KeyError, "websocket does not exist" unless SocketsMap.has_key? data["websocket"]
  418. NotfiyList[uid].delete data["websocket"]
  419. _send_json(res, {
  420. status: true
  421. })
  422. rescue KeyError => keyerror
  423. _throw_error(res,keyerror)
  424. rescue JSON::ParserError => jsonerror
  425. _throw_error(res,jsonerror)
  426. end
  427. end
  428. post "modify" do |ctx|
  429. req,res = ctx.request,ctx.response
  430. begin
  431. data = _parse_json(req.body, {
  432. data: Hash,
  433. protocol_id: String
  434. })
  435. room = Rooms.get(data["protocol_id"])
  436. room.modify(data["data"])
  437. _send_json(res, {
  438. status: true
  439. })
  440. rescue KeyError => keyerror
  441. _throw_error(res,keyerror)
  442. rescue ProtocolError => protoerr
  443. _throw_error(res,protoerr)
  444. rescue JSON::ParserError => jsonerror
  445. _throw_error(res,jsonerror)
  446. end
  447. end
  448. post "delete" do |ctx|
  449. req,res = ctx.request, ctx.response
  450. begin
  451. data = _parse_json(req.body,{
  452. protocol_id: String
  453. })
  454. id = data["protocol_id"]
  455. _send_json(res, {
  456. status: true
  457. })
  458. room = Rooms.get(id)
  459. NotifyList.delete(room.UID)
  460. Rooms.delete(id)
  461. rescue Heimdall::ProtocolError => protoerr
  462. _throw_error(res,protoerr)
  463. rescue JSON::ParserError => jsonerror
  464. _throw_error(res,jsonerror)
  465. end
  466. end
  467. post "sync" do |ctx|
  468. req,res = ctx.request,ctx.response
  469. begin
  470. data = _parse_json(req.body, {
  471. data: Array
  472. })
  473. Rooms.sync(data["data"])
  474. _send_json(res, {
  475. status: true
  476. })
  477. rescue KeyError => keyerror
  478. _throw_error(res,keyerror)
  479. rescue JSON::ParserError => jsonerror
  480. _throw_error(res,jsonerror)
  481. end
  482. end
  483. post "bulk-delete" do |ctx|
  484. req,res = ctx.request,ctx.response
  485. begin
  486. data = _parse_json(req.body, {
  487. users: Array
  488. })
  489. Rooms.bulk_delete(data["users"])
  490. _send_json(res, {
  491. status: true
  492. })
  493. rescue KeyError => keyerror
  494. _throw_error(res,keyerror)
  495. rescue JSON::ParserError => jsonerror
  496. _throw_error(res,jsonerror)
  497. end
  498. end
  499. get "exists" do |ctx|
  500. req,res = ctx.request,ctx.response
  501. begin
  502. _require_keys(req.query, {
  503. protocol_id: String
  504. })
  505. _send_json(res,{
  506. exists: (Rooms.get(req.query["protocol_id"]) != nil)
  507. })
  508. rescue KeyError => keyerror
  509. _throw_error(res,keyerror)
  510. rescue Heimdall::ProtocolError => protoerr
  511. _send_json(res, {
  512. exists: false
  513. })
  514. end
  515. end
  516. end
  517. get "version" do |ctx|
  518. ctx.response.body = "{\"version\":\"#{Heimdall::VERSION}\"}"
  519. ctx.response['Content-Type'] = "application/json"
  520. end
  521. end
  522. at_exit do
  523. server.shutdown
  524. end
  525. class WebsocketUID < Heimdall::UID
  526. def initialize
  527. @UID_prefix = "websocket"
  528. super
  529. end
  530. end
  531. class EventServlet < WEBrick::Websocket::Servlet
  532. def socket_open(sock)
  533. @UID = WebsocketUID.new
  534. @connected_listeners = []
  535. SocketsMap[@UID.UID] = sock
  536. sock.puts(JSON::fast_generate({
  537. websocket: @UID.UID.to_s
  538. }))
  539. end
  540. def socket_close(sock)
  541. SocketsMap.delete @UID.UID
  542. NotifyList.each do |k,v|
  543. if v.include? @UID.UID
  544. v.delete @UID.UID
  545. end
  546. end
  547. end
  548. def socket_text(sock,text)
  549. # do nothing
  550. end
  551. end
  552. Thread.new do
  553. websocket_server = WEBrick::Websocket::HTTPServer.new Port:8001
  554. websocket_server.mount "/", EventServlet
  555. websocket_server.start
  556. at_exit do
  557. websocket_server.shutdown
  558. end
  559. end
  560. server.start