Advertisement
Aluf

Chatango Derplib.py [frame.js]

Feb 1st, 2015
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. //
  2. // All credits to WIC by lumirayz
  3. // deserialization of frames.
  4. //
  5.  
  6. //
  7. // Requires
  8. //
  9. var MM = module.parent,
  10. utils = MM.libary.load('utils'),
  11. _ = require("underscore");
  12.  
  13. //
  14. // Utility
  15. //
  16. function makePremium(num) {
  17. return {
  18. media: (num & 16) == 16,
  19. bg: (num & 8) == 8};
  20. }
  21.  
  22. function makeUser(name, alias, id, key, ip) {
  23. return {
  24. name: name != "" ? name.toLowerCase() : undefined,
  25. alias: alias != "" ? alias : undefined,
  26. id: id != "" ? id : undefined,
  27. key: key != "" ? key : undefined,
  28. ip: ip != "" ? ip : undefined};
  29. }
  30.  
  31.  
  32. //
  33. // Frame parsing
  34. //
  35. var frameTypesRoom = {
  36. ok: function(owner, myuid, mystatus, myname, jointime, myip, mods) {
  37. return {
  38. type: "ok",
  39. owner: owner,
  40. myuid: myuid,
  41. mystatus: mystatus,
  42. myname: myname,
  43. jointime: jointime,
  44. myip: myip,
  45. mods: mods.split(";")};
  46. },
  47.  
  48. inited: function() {
  49. return {type: "inited"};
  50. },
  51.  
  52. premium: function(premium, expire) {
  53. return {
  54. type: "premium",
  55. premium: makePremium(premium),
  56. expire: parseInt(expire)};
  57. },
  58.  
  59. pwdok: function() {
  60. return {type: "pwdok"};
  61. },
  62.  
  63. badlogin: function() {
  64. return {type: "badlogin"};
  65. },
  66.  
  67. logoutok: function(){
  68. return {type: "logoutok"};
  69. },
  70.  
  71. tb: function(seconds) { // Might give time occasionally need to check
  72. return {type: "tb", seconds: seconds};
  73. },
  74.  
  75. show_fw: function() { // same as tb
  76. return {type: "show_fw"};
  77. },
  78.  
  79. fw: function() { // Removed?
  80. return {type: "fw"};
  81. },
  82.  
  83. end_fw: function() {
  84. return {type: "end_fw"};
  85. },
  86.  
  87. show_tb: function(seconds) {
  88. return {type: "show_tb", seconds: seconds};
  89. },
  90.  
  91. "delete": function(msgid) {
  92. return {type: "delete", msgid: msgid};
  93. },
  94.  
  95. deleteall: function() {
  96. return {type: "deleteall", msgids: _.toArray(arguments)};
  97. },
  98.  
  99. clearall: function(answer) {
  100. return {type: "clearall", answer: answer};
  101. },
  102.  
  103. mods: function(mods) {
  104. return {type: "mods", mods: _.toArray(arguments)};
  105. },
  106.  
  107. i: function(time, name, alias, user_id, user_key, msgid, ip, prem, _noidea) {
  108. return {
  109. time: parseFloat(time),
  110. type: "i",
  111. user: makeUser(name, alias, user_id, user_key, ip),
  112. id: msgid,
  113. premium: makePremium(parseInt(prem, 10)),
  114. body: _.toArray(arguments).slice(9).join(":")};
  115. },
  116.  
  117. b: function(time, name, alias, user_id, user_key, number, ip, prem) {
  118. return {
  119. time: parseFloat(time),
  120. type: "b",
  121. user: makeUser(name, alias, user_id, user_key, ip),
  122. number: number,
  123. id: false,
  124. premium: makePremium(parseInt(prem, 10)),
  125. body: _.toArray(arguments).slice(9).join(":")};
  126. },
  127.  
  128. u: function(number, msgid) {
  129. return {type: "u", number: number, msgid: msgid};
  130. },
  131.  
  132. n: function(num) {
  133. return {type: "n", count: parseInt(num, 16)};
  134. },
  135.  
  136. blocked: function(key, ip, name, by, time) {
  137. var ban = makeUser(name, "", "", key, ip);
  138. ban.time = parseFloat(time);
  139. ban.by = by;
  140. return {
  141. type: "blocked",
  142. ban: ban};
  143. },
  144.  
  145. unblocked: function() {
  146. var args = _.toArray(arguments);
  147. var unban = {
  148. key: args.shift(),
  149. ip: args.shift(),
  150. time: parseInt(args.pop()),
  151. by: args.pop()
  152. }
  153. var name = args.join(':').split(';');
  154. unban.name = name.shift();
  155. if(name.length > 0){
  156. unban.extra = _.map(name, function(row){
  157. row = row.split(':');
  158. return {key: row[0], ip: row[1], name: row[2]};
  159. });
  160. }
  161. return {
  162. type: "unblocked",
  163. unban: unban};
  164. },
  165.  
  166. // Blocklist is overwriting multiple bans, can stay that way.
  167. blocklist: function() {
  168. if(_.filter(_.toArray(arguments), function(x){ return x; }).length === 0) return {type: "blocklist",bans:{}};
  169. var bans = _.reduce(_.toArray(arguments).join(':').split(';'), function(bans, args){
  170. args = args.split(':');
  171. var ban = makeUser(args[2], "", "", args[0], args[1]);
  172. ban.time = parseFloat(args[3]);
  173. ban.by = args[4];
  174. bans[ban.name] = ban;
  175. return bans;
  176. },{});
  177. return {
  178. type: "blocklist",
  179. bans: bans};
  180. },
  181.  
  182. unblocklist: function() {
  183. var unbans = _.reduce(_.toArray(arguments).join(':').split(';'), function(unbans, args){
  184. args = args.split(':');
  185. var unban = makeUser(args[2], "", "", args[0], args[1]);
  186. unban.time = parseFloat(args[3]);
  187. unban.by = args[4];
  188. unbans[unban.name] = unban;
  189. return unbans;
  190. },{});
  191. return {
  192. type: "unblocklist",
  193. unbans: unbans};
  194. },
  195.  
  196. bansearchresult: function(_noidea, name, ip, key, by, date){
  197. if(undefined === name){
  198. return {type: "bansearchresult", result: false};
  199. }
  200. var result = makeUser(name, "", "", key, ip);
  201. result.time = Math.round(new Date(_.toArray(arguments).slice(5).join(":"))/1000);
  202. result.by = by;
  203. return {type: "bansearchresult", result: result};
  204. },
  205.  
  206. g_participants: function(){
  207. var users = _.reduce(_.toArray(arguments).join(':').split(';'), function(users, args){
  208. args = args.split(':');
  209. if(args[3] === "None") args[3] = "";
  210. if(args[4] === "None") args[4] = "";
  211. var user = makeUser(args[3], args[4], args[2], "", "");
  212. user.sess = args[0];
  213. user.time = parseFloat(args[1]);
  214. if(!user.name && !user.alias) user.name = utils.getAnonName(user.id, user.time);
  215. if(user.alias) user.alias = '#'+user.alias;
  216. users[user.sess] = user;
  217. return users;
  218. },{});
  219. return {
  220. type: "g_participants",
  221. users: users};
  222. },
  223.  
  224. participant: function(mode, sess, user_id, name, alias, ip, time) {
  225. if(name === "None") name = "";
  226. if(alias === "None") alias = "";
  227. var user = makeUser(name, alias, user_id, "", "");
  228. user.time = parseFloat(time);
  229. user.sess = sess;
  230. if(!user.name && !user.alias) user.name = utils.getAnonName(user_id, time);
  231. if(user.alias) user.alias = '#'+user.alias;
  232. return {
  233. type: "participant",
  234. mode: ( mode == "0" ? "leave" : mode == "1" ? "join" : mode == "2" ? "change" : undefined ),
  235. user: user};
  236. },
  237.  
  238. updateprofile: function(name){
  239. return {
  240. type: "g_participants",
  241. name: name};
  242. },
  243.  
  244. getbannedwords: function(partly, exact){
  245. return {
  246. type: "getbannedwords",
  247. partly: _.filter(decodeURIComponent(partly).split(','), function(x){ return x; }),
  248. exact: _.filter(decodeURIComponent(exact).split(','), function(x){ return x; })};
  249. },
  250.  
  251. bw: function(partly, exact){
  252. return {
  253. type: "getbannedwords",
  254. partly: _.filter(decodeURIComponent(partly).split(','), function(x){ return x; }),
  255. exact: _.filter(decodeURIComponent(exact).split(','), function(x){ return x; })};
  256. },
  257.  
  258. ubw: function(){
  259. return {type: "ubw"};
  260. },
  261. };
  262.  
  263. var frameTypesPM = {
  264. OK: function() {
  265. return {type: "ok"};
  266. },
  267. settings: function(a, disabe_idle_time, b, allow_anon, c, email_offline_msg) {
  268. return {
  269. type: "settings",
  270. disabe_idle_time: disabe_idle_time == "on",
  271. allow_anon: allow_anon == "on",
  272. email_offline_msg: email_offline_msg == "on"};
  273. },
  274. wl: function() {
  275. var args = _.toArray(arguments),
  276. contacts = [];
  277. for(var i=0; i<args.length; i+= 4){
  278. contacts.push({
  279. name: args[i],
  280. time: args[i+1],
  281. state: args[i+2],
  282. idle: args[i+3]
  283. });
  284. }
  285. return {type: "wl", contacts: contacts};
  286. },
  287. premium: function(prem, time) {
  288. return {
  289. type: "premium",
  290. premium: makePremium(parseInt(prem, 10)),
  291. time: parseFloat(time)};
  292. },
  293. idleupdate: function(name, state) {
  294. return {
  295. type: "idleupdate",
  296. name: name,
  297. state: state == "1" ? 'online' : 'idle'};
  298. },
  299. msg: function(name, alias, prem, time) {
  300. return {
  301. type: "msg",
  302. time: parseFloat(time),
  303. user: makeUser(name, alias, '', '', ''),
  304. premium: makePremium(parseInt(prem, 10)),
  305. body: _.toArray(arguments).slice(5).join(":")};
  306. },
  307. msgoff: function (name, alias, prem, time) {
  308. return {
  309. type: "msgoff",
  310. time: parseFloat(time),
  311. user: makeUser(name, alias, '', '', ''),
  312. premium: makePremium(parseInt(prem, 10)),
  313. body: _.toArray(arguments).slice(5).join(":")};
  314. },
  315. status: function(name, time, state) {
  316. return {
  317. type: "status",
  318. name: name,
  319. time: parseFloat(time),
  320. state: state};
  321. },
  322. wloffline: function(name, time) {
  323. return {
  324. type: "wloffline",
  325. name: name,
  326. time: parseFloat(time)};
  327. },
  328. wlonline: function(name, time) {
  329. return {
  330. type: "wlonline",
  331. name: name,
  332. time: parseFloat(time)};
  333. },
  334. reload_profile: function(name) {
  335. return {
  336. type: "reload_profile",
  337. name: name};
  338. },
  339. wladd: function(name, state, time) {
  340. return {
  341. type: "wladd",
  342. name: name,
  343. state: state.replace('off','offline').replace('on','online'),
  344. time: parseFloat(time)};
  345. },
  346. connect: function(name, time, state) {
  347. return {
  348. type: "connect",
  349. name: name,
  350. time: parseInt(time),
  351. state: state};
  352. },
  353. show_fw: function() {
  354. return {type: "show_fw"};
  355. }
  356. };
  357.  
  358. function parseFramePM(data) {
  359. data = data.replace(/[\r\n\0]+$/, "");
  360. var
  361. tmp = data.split(":"),
  362. cmd = tmp[0],
  363. args = tmp.slice(1);
  364. if(frameTypesPM.hasOwnProperty(cmd)) {
  365. return frameTypesPM[cmd].apply(null, args);
  366. }
  367. else {
  368. return null;
  369. }
  370. };
  371.  
  372.  
  373. function parseFrameRoom(data) {
  374. data = data.replace(/[\r\n\0]+$/, "");
  375. var
  376. tmp = data.split(":"),
  377. cmd = tmp[0],
  378. args = tmp.slice(1);
  379. if(frameTypesRoom.hasOwnProperty(cmd)) {
  380. return frameTypesRoom[cmd].apply(null, args);
  381. }
  382. else {
  383. return null;
  384. }
  385. };
  386.  
  387. //
  388. // Exports
  389. //
  390.  
  391. exports.parseFramePM = parseFramePM;
  392. exports.parseFrameRoom = parseFrameRoom;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement