@@ -282,119 +282,15 @@ return function ( args ) {
282
282
283
283
commands . eval . async = commands . coffee . async = true ;
284
284
285
- var parse = commands . parse = ( function ( ) {
286
- var macros = {
287
- who : function ( msgObj ) {
288
- return msgObj . get ( 'user_name' ) ;
289
- } ,
290
-
291
- someone : function ( ) {
292
- var presentUsers = document . getElementById ( 'sidebar' )
293
- . getElementsByClassName ( 'present-user' ) ;
294
-
295
- //the chat keeps a low opacity for users who remained silent for long,
296
- // and high opacity for those who recently talked
297
- var active = [ ] . filter . call ( presentUsers , function ( user ) {
298
- return Number ( user . style . opacity ) >= 0.5 ;
299
- } ) ,
300
- user = active [ Math . floor ( Math . random ( ) * ( active . length - 1 ) ) ] ;
301
-
302
- if ( ! user ) {
303
- return 'Nobody' ;
304
- }
285
+ commands . parse = function ( args ) {
286
+ var msgObj = args . get ( ) ,
287
+ user = bot . users [ args . get ( 'user_id' ) ] ,
305
288
306
- return user . getElementsByTagName ( 'img' ) [ 0 ] . title ;
307
- } ,
308
-
309
- digit : function ( ) {
310
- return Math . floor ( Math . random ( ) * 10 ) ;
311
- } ,
312
-
313
- encode : function ( msgObj , string ) {
314
- return encodeURIComponent ( string ) ;
315
- } ,
316
-
317
- //random number, min <= n <= max
318
- //treats non-numeric inputs like they don't exist
319
- rand : function ( msgObj , min , max ) {
320
- min = Number ( min ) ;
321
- max = Number ( max ) ;
322
- return Math . rand ( min , max ) ;
323
- }
324
- } ;
325
- var macroRegex = / (?: .| ^ ) \$ ( \w + ) (?: \( ( .* ?) \) ) ? / g;
326
-
327
- //extraVars is for internal usage via other commands
328
- return function parse ( args , extraVars ) {
329
- var isMsg = ! ! args . get ,
330
- //filler objects, solves
331
- // https://github.com/Zirak/SO-ChatBot/issues/66
332
- msgObj = isMsg ? args . get ( ) : { } ,
333
- user = isMsg ? bot . users [ args . get ( 'user_id' ) ] : { } ;
334
-
335
- extraVars = extraVars || { } ;
289
+ extraVars = Object . merge ( msgObj , user ) ;
336
290
bot . log ( args , extraVars , '/parse input' ) ;
337
291
338
- return args . replace ( macroRegex , replaceMacro ) ;
339
-
340
- function replaceMacro ( $0 , filler , fillerArgs ) {
341
- //$$ makes a literal $
342
- if ( $0 . startsWith ( '$$' ) ) {
343
- return $0 . slice ( 1 ) ;
344
- }
345
-
346
- //include the character that was matched in the $$ check, unless
347
- // it's a $
348
- var ret = '' ;
349
- if ( $0 [ 0 ] !== '$' ) {
350
- ret = $0 [ 0 ] ;
351
- }
352
-
353
- var macro = findMacro ( filler ) ;
354
-
355
- //not found? bummer.
356
- if ( ! macro ) {
357
- return filler ;
358
- }
359
-
360
- bot . log ( macro , filler , fillerArgs , '/parse replaceMacro' ) ;
361
- //when the macro is a function
362
- if ( macro . apply ) {
363
- ret += macro . apply ( null , parseMacroArgs ( fillerArgs ) ) ;
364
- }
365
- //when the macro is simply a substitution
366
- else {
367
- ret += macro ;
368
- }
369
- return ret ;
370
- }
371
-
372
- function parseMacroArgs ( macroArgs ) {
373
- bot . log ( macroArgs , '/parse parseMacroArgs' ) ;
374
- if ( ! macroArgs ) {
375
- return [ args ] ;
376
- }
377
-
378
- //parse the arguments, split them into individual arguments,
379
- // and trim'em (to cover the case of "arg,arg" and "arg, arg")
380
- return (
381
- [ args ] . concat (
382
- parse ( macroArgs , extraVars )
383
- . split ( ',' ) . invoke ( 'trim' ) ) ) ;
384
- //this is not good code
385
- }
386
-
387
- function findMacro ( macro ) {
388
- var container = [ macros , msgObj , user , extraVars ] . first ( hasMacro ) ;
389
-
390
- return ( container || { } ) [ macro ] ;
391
-
392
- function hasMacro ( obj ) {
393
- return obj && obj . hasOwnProperty ( macro ) ;
394
- }
395
- }
292
+ return bot . parseMacro ( args . toString ( ) , extraVars ) ;
396
293
} ;
397
- } ( ) ) ;
398
294
399
295
commands . tell = ( function ( ) {
400
296
var invalidCommands = { tell : true , forget : true } ;
0 commit comments