Encapsulate obscure detail that lower numeric priority is higher queue priority
[clinton/bobotpp.git] / source / ServerQueue.C
index 03ae0f3..0f13d36 100644 (file)
@@ -1,6 +1,6 @@
 // ServerQueue.C  -*- C++ -*-
 // Copyright (c) 1997, 1998 Etienne BERNARD
-// Copyright (C) 2002,2005 Clinton Ebadi
+// Copyright (C) 2002,2005,2008 Clinton Ebadi
 
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+// 02110-1301, USA.
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-//#include <limits>
+#include <limits>
 #include "ServerQueue.H"
 #include "Utils.H"
 
@@ -34,7 +35,7 @@ ServerQueue::ServerQueue(Socket * s, bool d)
 
 ServerQueue::~ServerQueue()
 {
-  penalty = INT_MIN;
+  penalty = std::numeric_limits<int>::min ();
   flush();
 }
 
@@ -43,18 +44,26 @@ ServerQueue::addItem(ServerQueueItem *sqi)
 {
   std::list<ServerQueueItem *>::iterator it, it2;
 
-  for (it = serverQueue.begin(); it != serverQueue.end(); ++it) {
-    if ((*it)->priority > sqi->priority)
-      break;
-  }
-  it2 = it; --it2;
-  if (it2 != serverQueue.end() && *it2) {
-    // All right, we try to merge this item to the previous
-    if ((*it2)->merge(sqi)) {
-      delete sqi;
-      return;
+  for (it = serverQueue.begin(); it != serverQueue.end(); ++it)
+    {
+      if (**it < *sqi)
+       {
+         break;
+       }
+    }
+
+  it2 = it; 
+  --it2;
+  if (it2 != serverQueue.end() && *it2) 
+    {
+      // All right, we try to merge this item to the previous
+      if ((*it2)->merge(sqi)) 
+       {
+         delete sqi;
+         return;
+       }
     }
-  }
+
   serverQueue.insert(it, sqi);
 }
 
@@ -71,17 +80,25 @@ ServerQueue::flush()
 {
   // Called every second, we decrement the penalty
   if (penalty > 0)
-    penalty--;
-
-  while (!serverQueue.empty() && (penalty < 5)) {
-    ServerQueueItem * sqi = (*serverQueue.begin());
-    penalty += sqi->penalty + sqi->getLine().length()/100 + 1;
-    bool res = sendLine(sqi->getLine());
-    serverQueue.erase(serverQueue.begin());
-    delete sqi;
-    if (!res)
-      return false;
-  }
+    {
+      penalty--;
+    }
+
+  while (!serverQueue.empty() && (penalty < 5)) 
+    {
+      ServerQueueItem * sqi = (*serverQueue.begin());
+      penalty += sqi->penalty + sqi->getLine().length()/100 + 1;
+
+      bool res = sendLine(sqi->getLine());
+      serverQueue.erase(serverQueue.begin());
+      delete sqi;
+
+      if (!res)
+       {
+         return false;
+       }
+    }
+
   return true;
 }
 
@@ -98,7 +115,7 @@ ServerQueue::sendCTCP(String to, String command,
       Interp::bot->botInterp->RunHooks (Hook::SEND_ACTION,
                                        MNICK + " " + to +
                                        " " + message,
-                                       scm_listify (Utils::
+                                       scm_list_n (Utils::
                                                  str2scm (MNICK),
                                                  Utils::
                                                  str2scm (to),
@@ -108,18 +125,18 @@ ServerQueue::sendCTCP(String to, String command,
     }
   else
     Interp::bot->botInterp->RunHooks (Hook::SEND_CTCP,
-                                  MNICK + " " + to + " " +
-                                  command + " " + message,
-                                  scm_listify (Utils::
-                                             str2scm (MNICK),
-                                             Utils::
-                                             str2scm (to),
-                                             Utils::
-                                             str2scm
-                                             (command),
-                                             Utils::
-                                             str2scm (message),
-                                               SCM_UNDEFINED));
+                                     MNICK + " " + to + " " +
+                                     command + " " + message,
+                                     scm_list_n (Utils::
+                                                  str2scm (MNICK),
+                                                  Utils::
+                                                  str2scm (to),
+                                                  Utils::
+                                                  str2scm
+                                                  (command),
+                                                  Utils::
+                                                  str2scm (message),
+                                                  SCM_UNDEFINED));
 #endif
 
 }
@@ -225,22 +242,21 @@ ServerQueue::sendPrivmsg(String dest, String message)
       Interp::bot->botInterp->RunHooks (Hook::SEND_PUBLIC,
                                        Interp::bot->nickName + " " + dest +
                                        " " + message,
-                                       scm_listify (Utils::
-                                                    str2scm (Interp::bot->nickName),
-                                                    Utils::
-                                                    str2scm (dest),
-                                                    Utils::
-                                                    str2scm
-                                                    (message), SCM_UNDEFINED));
+                                       scm_list_n 
+                                       (Utils::str2scm 
+                                        (Interp::bot->nickName),
+                                        Utils::str2scm (dest),
+                                        Utils::str2scm (message), 
+                                        SCM_UNDEFINED));
     else
-      Interp::bot->botInterp->RunHooks (Hook::SEND_MESSAGE,
-                                       Interp::bot->nickName + " " +
-                                       message,
-                                       scm_listify (Utils::
-                                                    str2scm (Interp::bot->nickName),
-                                                    Utils::
-                                                    str2scm
-                                                    (message), SCM_UNDEFINED));
+      Interp::bot->botInterp->RunHooks 
+       (Hook::SEND_MESSAGE,
+        Interp::bot->nickName + " " + dest +
+        message,
+        scm_list_n (Utils::str2scm (Interp::bot->nickName),
+                     Utils::str2scm (dest),
+                     Utils::str2scm
+                     (message), SCM_UNDEFINED));
 #endif
 }
 
@@ -285,6 +301,14 @@ ServerQueue::sendWho(String who)
 {
   addLine(String("WHO ") + who,
           WHO_PRIORITY, WHO_PENALTY, ServerQueueItem::WHO);
+
+#ifdef USESCRIPTS
+  Interp::bot->botInterp->RunHooks (Hook::SEND_WHO,
+                                   who,
+                                   scm_list_n (Utils::
+                                                str2scm (who),
+                                                SCM_UNDEFINED));
+#endif
 }
 
 void
@@ -292,4 +316,12 @@ ServerQueue::sendWhois(String nick)
 {
   addLine(String("WHOIS ") + nick,
           NICK_PRIORITY, WHOIS_PENALTY, ServerQueueItem::WHOIS);
+
+#ifdef USESCRIPTS
+  Interp::bot->botInterp->RunHooks (Hook::SEND_WHOIS,
+                                   nick,
+                                   scm_list_n (Utils::
+                                                str2scm (nick),
+                                                SCM_UNDEFINED));
+#endif
 }