prog

commands.js

Jan 22nd, 2011
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var MAX_REQUESTS = 2;
  2. var requests = 0;
  3.  
  4. function resetRequests()
  5. {
  6.   if (requests > 0)
  7.     requests--;
  8. }
  9.  
  10. context.CreateTimer("resetRequests", null, 5);
  11.  
  12. var dao = load('scripts/dao.js');
  13.  
  14. function onprivtext()
  15. {
  16.   var args = text.split(' ');
  17.   var t = args.slice(1).join(' ');
  18.   process_command(user, args[0], t);
  19. }
  20.  
  21. function oncommand()
  22. {
  23.   process_command(user, command, text);
  24. }
  25.  
  26.  
  27. function process_command(user, command, text)
  28. {
  29.   if (requests >= MAX_REQUESTS)
  30.     return;
  31.  
  32.   if (!user.hasop('#maroc'))
  33.     return;
  34.  
  35.   var args = text.split(' ');
  36.  
  37.  
  38.   switch (command.toLowerCase())
  39.   {
  40.     case "find":
  41.       find(args);
  42.       break;
  43.  
  44.     case "add":
  45.       add(args);
  46.       break;
  47.  
  48.     case "del":
  49.       del(args);
  50.       break;
  51.   }
  52. }
  53.  
  54. function find(args)
  55. {
  56.   var target = args.join('*');
  57.   var notes = dao.select(target);
  58.   if (notes.length == 0)
  59.   {
  60.     user.notice('No match for ' + target);
  61.   }
  62.   else
  63.   {
  64.     if (notes.length > 1)
  65.     {
  66.       user.notice(notes.length + ' entries found. Use \002/dcc chat prog.ma\002 with password \002test\002 and perform your request there to get the whole information.');
  67.     }
  68.     var i = 0;
  69.     user.notice('Latest note: (' + notes[i].id + ') [' + new Date(notes[i].date).format('yyyy/MM/dd HH:mm:ss') + '] by <' + notes[i].author + '> on <' + notes[i].target + '> ' + notes[i].comment);
  70.   }
  71. }
  72.  
  73. function del(args)
  74. {
  75.   try
  76.   {
  77.     for (var i = 0; i < args.length; i++)
  78.     {
  79.       dao.del(args[i]);
  80.     }
  81.     user.notice('Deletion OK');
  82.   }
  83.   catch (e)
  84.   {
  85.     user.notice(e.description);
  86.   }
  87. }
  88.  
  89. function add(args)
  90. {
  91.   try
  92.   {
  93.     dao.insert(user.nickname, args[0], 'plain', args.slice(1).join(' '));
  94.     user.notice('Note successfully added.');
  95.   }
  96.   catch (e)
  97.   {
  98.     user.notice(e.description);
  99.   }
  100. }
  101.  
  102. String.repeat = function(chr,count)
  103. {
  104.     var str = "";
  105.     for(var x=0;x<count;x++) {str += chr};
  106.     return str;
  107. }
  108.  
  109. String.prototype.padL = function(width,pad)
  110. {
  111.     if (!width ||width<1)
  112.         return this;
  113.  
  114.     if (!pad) pad=" ";
  115.     var length = width - this.length
  116.     if (length < 1) return this.substr(0,width);
  117.  
  118.     return (String.repeat(pad,length) + this).substr(0,width);
  119. }
  120.  
  121. String.prototype.padR = function(width,pad)
  122. {
  123.     if (!width || width<1)
  124.         return this;
  125.  
  126.     if (!pad) pad=" ";
  127.     var length = width - this.length
  128.     if (length < 1) this.substr(0,width);
  129.  
  130.     return (this + String.repeat(pad,length)).substr(0,width);
  131. }
  132.  
  133. Date.prototype.format = function (fmt)
  134. {
  135.     var date = this;
  136.  
  137.     if (!fmt)
  138.       fmt="MM/dd/yyyy";
  139.  
  140.     var month = date.getMonth() + 1;
  141.     var year = date.getFullYear();
  142.  
  143.     fmt = fmt.replace("MM",month.toString().padL(2,"0"));
  144.  
  145.     if (fmt.indexOf("yyyy") > -1)
  146.         fmt = fmt.replace("yyyy",year.toString());
  147.     else if (fmt.indexOf("yy") > -1)
  148.         fmt = fmt.replace("yy",year.toString().substr(2,2));
  149.  
  150.     fmt = fmt.replace("dd",date.getDate().toString().padL(2,"0"));
  151.  
  152.     var hours = date.getHours();
  153.     if (fmt.indexOf("t") > -1)
  154.     {
  155.        if (hours > 11)
  156.         fmt = fmt.replace("t","pm")
  157.        else
  158.         fmt = fmt.replace("t","am")
  159.     }
  160.     if (fmt.indexOf("HH") > -1)
  161.         fmt = fmt.replace("HH",hours.toString().padL(2,"0"));
  162.     if (fmt.indexOf("hh") > -1) {
  163.         if (hours > 12) hours - 12;
  164.         if (hours == 0) hours = 12;
  165.         fmt = fmt.replace("hh",hours.toString().padL(2,"0"));
  166.     }
  167.     if (fmt.indexOf("mm") > -1)
  168.        fmt = fmt.replace("mm",date.getMinutes().toString().padL(2,"0"));
  169.     if (fmt.indexOf("ss") > -1)
  170.        fmt = fmt.replace("ss",date.getSeconds().toString().padL(2,"0"));
  171.     return fmt;
  172. }
Add Comment
Please, Sign In to add comment