Hi,

A quick hack for Auto-Away and Auto-NA in console mode. Got tired of
being asked what I was doing online at 5 am. ;)

No guarantee that it's bugfree, but It Works For Me [tm].

Brgds,
//Humming

diff -Naur console/licq_console.conf console.new/licq_console.conf
--- console/licq_console.conf   2000-08-21 22:29:37.000000000 +0200
+++ console.new/licq_console.conf       2003-02-19 13:20:16.000000000 +0100
@@ -17,6 +17,12 @@
 OfflineFormat = %-20a
 CommandCharacter = /
 
+[startup]
+AutoAway = 10
+AutoNA = 30
+AutoAwayMessage = "Currently Away"
+AutoNAMessage = "Currently N/A"
+
 [macros]
 NumMacros = 3
 # Reply macro
diff -Naur console/src/console.cpp console.new/src/console.cpp
--- console/src/console.cpp     2003-02-16 22:40:56.000000000 +0100
+++ console.new/src/console.cpp 2003-02-19 13:23:28.000000000 +0100
@@ -176,6 +176,15 @@
     }
   }
 
+  if (licqConf.SetSection("startup"))
+  {
+    licqConf.ReadNum("AutoAway", m_autoAway, 0);
+    licqConf.ReadNum("AutoNA", m_autoNA, 0);
+//    To be done:
+//    licqConf.ReadBool("AutoResetStatus", m_autoResetStatus, false);
+//    licqConf.ReadBool("AutoResetOnlyAutoStatus", m_autoResetOnlyAutoStatus, true);
+  }
+
   // Set the colors
   m_cColorOnline    = &aColorMaps[m_nColorOnline];
   m_cColorAway      = &aColorMaps[m_nColorAway];
@@ -299,6 +308,7 @@
 
   //fd_set fdSet;
   int nResult;
+  struct timeval tv;
 
   while (!m_bExit)
   {
@@ -308,7 +318,7 @@
     FD_SET(log->Pipe(), &fdSet);
 
     int nNumDesc = log->Pipe() + 1;
-
+    
     // Check to see if we want to add in the file xfer manager..
     list<CFileTransferManager *>::iterator iter;
     for (iter = m_lFileStat.begin(); iter != m_lFileStat.end(); iter++)
@@ -317,43 +327,53 @@
       nNumDesc += (*iter)->Pipe();
     }
 
-    nResult = select(nNumDesc, &fdSet, NULL, NULL, NULL);
-    if (nResult == -1)
-    {
-      if (errno != EINTR)
-      {
-        gLog.Error("Error in select(): %s.\n", strerror(errno));
-        m_bExit = true;
-      }
-    }
-    else
-    {
-      if (FD_ISSET(STDIN_FILENO, &fdSet))
-      {
-        ProcessStdin();
-        continue;
-      }
-      else if (FD_ISSET(m_nPipe, &fdSet))
-      {
-        ProcessPipe();
-        continue;
-      }
-      else if (FD_ISSET(log->Pipe(), &fdSet))
-      {
-        ProcessLog();
-        continue;
-      }
+    // Setup timeout
+    GetTimeouts(&tv);
 
-      list<CFileTransferManager *>::iterator iter;
-      for (iter = m_lFileStat.begin(); iter != m_lFileStat.end(); iter++)
-      {
-        if (FD_ISSET((*iter)->Pipe(), &fdSet))
+    nResult = select(nNumDesc, &fdSet, NULL, NULL, &tv);
+    switch(nResult)
+    {
+      case -1:
+        // Error
+        if (errno != EINTR)
         {
-          ProcessFile(iter);
-          break;
+          gLog.Error("Error in select(): %s.\n", strerror(errno));
+          m_bExit = true;
+        }
+        break;
+      case 0:
+        // Timeout
+        DoTimeouts();
+        break;
+      default:
+        // Some filedesc was set
+        if (FD_ISSET(STDIN_FILENO, &fdSet))
+        {
+          ProcessStdin();
+          ResetTimeouts();
+          continue;
+        }
+        else if (FD_ISSET(m_nPipe, &fdSet))
+        {
+          ProcessPipe();
+          continue;
+        }
+        else if (FD_ISSET(log->Pipe(), &fdSet))
+        {
+          ProcessLog();
+          continue;
+        }
+        list<CFileTransferManager *>::iterator iter;
+        for (iter = m_lFileStat.begin(); iter != m_lFileStat.end(); iter++)
+        {
+          if (FD_ISSET((*iter)->Pipe(), &fdSet))
+          {
+            ProcessFile(iter);
+            break;
+          }
         }
-      }
     }
+
   }
 
   winMain->wprintf("Exiting\n\n");
@@ -1042,6 +1062,108 @@
   }
 }
 
+/*---------------------------------------------------------------------------
+ * CLicqConsole::GetTimeouts
+ *-------------------------------------------------------------------------*/
+void CLicqConsole::GetTimeouts(struct timeval* tv)
+{
+  struct timeval currentTime;
+  ICQOwner *owner = gUserManager.FetchOwner(LOCK_R);
+
+  gettimeofday(&currentTime, NULL);
+
+  tv->tv_usec = 0;
+  tv->tv_sec = 3600; // select will wake up unnessary each hour
+  // We can only be 'away' if we're 'online' or 'freeforchat'
+  // We can only be 'na' if we're 'freeforchat', 'away' or 'online'
+  // Thus, we never override 'dnd', 'occ' and 'offline'
+  switch(owner->Status())
+  {
+    case ICQ_STATUS_OFFLINE:
+    case ICQ_STATUS_DND:
+    case ICQ_STATUS_OCCUPIED:
+    case ICQ_STATUS_NA:
+      // never do anything
+      break;
+    case ICQ_STATUS_FREEFORCHAT:
+    case ICQ_STATUS_ONLINE:
+      // interested in away
+      if(tv->tv_sec > (m_timeToAway - currentTime.tv_sec))
+        tv->tv_sec = (m_timeToAway - currentTime.tv_sec);
+      // Fall through!
+    case ICQ_STATUS_AWAY:
+      // interested in NA
+      if(tv->tv_sec > (m_timeToNA - currentTime.tv_sec))
+        tv->tv_sec = (m_timeToNA - currentTime.tv_sec);
+      break;
+    default:
+      // Huh?
+      winMain->wprintf(">Error in %s:%s; unknown status\n", 
+          __FILE__, __LINE__);
+      break;
+  }
+  if(tv->tv_sec < 0)
+    tv->tv_sec = 0; // Could happen, and it would be bad
+
+  gUserManager.DropOwner();
+}
+
+
+/*---------------------------------------------------------------------------
+ * CLicqConsole::ResetTimeouts
+ *-------------------------------------------------------------------------*/
+void CLicqConsole::ResetTimeouts()
+{
+  struct timeval currentTime;
+  // Reset timer
+  gettimeofday(&currentTime, NULL);
+  if(m_autoAway)
+    m_timeToAway = currentTime.tv_sec + 60 * m_autoAway;
+  if(m_autoNA)
+    m_timeToNA = currentTime.tv_sec + 60 * m_autoNA;
+}
+
+
+/*---------------------------------------------------------------------------
+ * CLicqConsole::DoTimeouts
+ *-------------------------------------------------------------------------*/
+void CLicqConsole::DoTimeouts()
+{
+  struct timeval currentTime;
+  ICQOwner *owner = gUserManager.FetchOwner(LOCK_R);
+
+  gettimeofday(&currentTime, NULL);
+
+  // We can only be 'away' if we're 'online' or 'freeforchat'
+  // We can only be 'na' if we're 'freeforchat', 'away' or 'online'
+  // Thus, we never override 'dnd', 'occ' and 'offline'
+  switch(owner->Status())
+  {
+    case ICQ_STATUS_OFFLINE:
+    case ICQ_STATUS_DND:
+    case ICQ_STATUS_OCCUPIED:
+    case ICQ_STATUS_NA:
+      // never do anything
+      break;
+    case ICQ_STATUS_FREEFORCHAT:
+    case ICQ_STATUS_ONLINE:
+      // interested in away
+      if(m_autoAway && currentTime.tv_sec >= m_timeToAway)
+        licqDaemon->icqSetStatus(ICQ_STATUS_AWAY);
+      // Fall through!
+    case ICQ_STATUS_AWAY:
+      // interested in NA
+      if(m_autoNA && currentTime.tv_sec >= m_timeToNA)
+        licqDaemon->icqSetStatus(ICQ_STATUS_NA);
+      break;
+    default:
+      // Huh?
+      winMain->wprintf(">Error in %s:%s; unknown status\n", 
+          __FILE__, __LINE__);
+      break;
+  }
+  gUserManager.DropOwner();
+}
 
 /*---------------------------------------------------------------------------
  * CLicqConsole::InputCommand
diff -Naur console/src/console.h console.new/src/console.h
--- console/src/console.h       2003-02-11 08:00:13.000000000 +0100
+++ console.new/src/console.h   2003-02-19 13:21:13.000000000 +0100
@@ -70,6 +70,11 @@
   char m_szOfflineFormat[30];
   char m_szCommandChar[30];
 
+  unsigned long m_autoAway;
+  unsigned long m_autoNA;
+  long m_timeToAway;
+  long m_timeToNA;
+
   unsigned short m_nCurrentGroup, m_nCon;
   GroupType m_nGroupType;
   list<char *> m_lCmdHistory;
@@ -96,6 +101,10 @@
   void SwitchToCon(unsigned short nCon);
   void CreateUserList();
 
+  void GetTimeouts(struct timeval* tv);
+  void ResetTimeouts();
+  void DoTimeouts();
+
   void InputCommand(int cIn);
   void InputLogWindow(int cIn);
   void InputMessage(int cIn);

Reply via email to