Ticket #1629: rms.patch

File rms.patch, 6.1 kB (added by cuchac, 5 months ago)
  • src/rms.cpp

     
    2222#include "licq_file.h" 
    2323#include "licq_user.h" 
    2424#include "licq_constants.h" 
     25#include "licq_history.h" 
    2526 
    2627extern "C" { const char *LP_Version(); } 
    2728 
     
    6061const unsigned short CODE_SECURExSTAT = 228; 
    6162const unsigned short CODE_NOTIFYxON = 229; 
    6263const unsigned short CODE_NOTIFYxOFF = 230; 
     64const unsigned short CODE_HISTORYxEVENT = 231; 
     65const unsigned short CODE_HISTORYxEND = 232; 
    6366const unsigned short CODE_VIEWxUNKNOWN = 299; 
    6467// 300 - further action required 
    6568const unsigned short CODE_ENTERxUIN = 300; 
     
    9598 
    9699#define NEXT_WORD(s) while (*s != '\0' && *s == ' ') s++; 
    97100 
     101#define FIRST_PARAM(s) s = strtok(s, " "); 
     102#define NEXT_PARAM(s) s = strtok(NULL, " "); 
     103 
    98104struct Command 
    99105{ 
    100106  const char *name; 
     
    138144    "Send an sms { <uin> }." }, 
    139145  { "NOTIFY", &CRMSClient::Process_NOTIFY, 
    140146    "Notify events" }, 
     147  { "HISTORY", &CRMSClient::Process_HISTORY, 
     148    "View history of specific user { <id>[.<protocol>] [<lenght>] [<offset>]}." }, 
    141149}; 
    142150 
    143151static const unsigned short NUM_COMMANDS = sizeof(commands)/sizeof(*commands); 
     
    480488  m_bNotify = false; 
    481489  m_szEventId = 0; 
    482490  m_nEventPPID = 0; 
     491  m_history = 0; 
     492  m_szHistoryLastId = 0; 
     493   
    483494} 
    484495 
    485496 
     
    15781589   
    15791590  return fflush(fs); 
    15801591} 
     1592 
     1593/*--------------------------------------------------------------------------- 
     1594 * CRMSClient::Process_HISTORY 
     1595 * 
     1596 * Command: 
     1597 *   HISTORY <uin> <lenght> <offset> 
     1598 * 
     1599 * Response: 
     1600 * 
     1601 *-------------------------------------------------------------------------*/ 
     1602int CRMSClient::Process_HISTORY() 
     1603{ 
     1604 
     1605  FIRST_PARAM(data_arg); 
     1606 
     1607  int lenght = 10; 
     1608  int offset = 0; 
     1609  ICQUser *u = NULL; 
     1610  ICQOwner *o = gUserManager.FetchOwner(m_nPPID, LOCK_R); 
     1611 
     1612 
     1613  if (data_arg == NULL) 
     1614  { 
     1615    fprintf(fs, "%d Invalid User.\n", CODE_INVALIDxUSER); 
     1616    return fflush(fs); 
     1617  } 
     1618 
     1619  ParseUser(data_arg); 
     1620 
     1621  if ((m_szId == NULL) || (m_szId && (u = gUserManager.FetchUser(m_szId, m_nPPID, LOCK_R)) == NULL)) 
     1622  { 
     1623    fprintf(fs, "%d Invalid User (%s,%lu).\n", CODE_INVALIDxUSER, m_szId, m_nPPID); 
     1624    return fflush(fs); 
     1625  } 
     1626 
     1627  if( (o = gUserManager.FetchOwner(m_nPPID, LOCK_R)) == NULL) 
     1628  { 
     1629    gUserManager.DropUser(u); 
     1630    fprintf(fs, "%d Cannot fetch owner (%lu).\n", CODE_EVENTxERROR, m_nPPID); 
     1631    return fflush(fs); 
     1632  } 
     1633 
     1634  NEXT_PARAM(data_arg); 
     1635 
     1636  if (data_arg != NULL) 
     1637  { 
     1638    lenght = atoi(data_arg); 
     1639  } 
     1640 
     1641  NEXT_PARAM(data_arg); 
     1642 
     1643  if (data_arg != NULL) 
     1644  { 
     1645    offset = atoi(data_arg); 
     1646  } 
     1647 
     1648  if(m_history == NULL) 
     1649    m_history = new CUserHistory; 
     1650 
     1651  if( m_szHistoryLastId == NULL || (m_szHistoryLastId != NULL && strcmp(m_szHistoryLastId, m_szId) != 0) || (m_nHistoryLastPPID != m_nPPID) ) 
     1652  { 
     1653    if(m_szHistoryLastId) 
     1654      free(m_szHistoryLastId); 
     1655     
     1656    m_szHistoryLastId = strdup(m_szId); 
     1657    m_nHistoryLastPPID = m_nPPID; 
     1658    m_history->Clear(m_historyList); 
     1659 
     1660    m_history->SetFile("default", m_szId, m_nPPID); 
     1661    if(!m_history->Load(m_historyList)){ 
     1662      gUserManager.DropOwner(m_nPPID); 
     1663      gUserManager.DropUser(u);       
     1664      fprintf(fs, "%d Cannot load history file.\n", CODE_EVENTxERROR); 
     1665      return fflush(fs); 
     1666    } 
     1667 
     1668  } 
     1669 
     1670       
     1671  int counter = -1; 
     1672  HistoryListRIter it = m_historyList.rbegin(); 
     1673  while (it != m_historyList.rend()) 
     1674  { 
     1675    counter++; 
     1676    if(counter < offset || counter - offset >= lenght) 
     1677    { 
     1678      it++; 
     1679      continue; 
     1680    } 
     1681 
     1682    char szEventHeader[75]; // Allows 50 chars for a nick 
     1683    CUserEvent *e = *it; 
     1684 
     1685    switch (e->SubCommand()) 
     1686    { 
     1687      case ICQ_CMDxSUB_MSG: 
     1688        sprintf(szEventHeader, "%d Message ", CODE_VIEWxMSG); 
     1689        break; 
     1690 
     1691      case ICQ_CMDxSUB_URL: 
     1692        sprintf(szEventHeader, "%d URL ", CODE_VIEWxURL); 
     1693        break; 
     1694 
     1695      case ICQ_CMDxSUB_CHAT: 
     1696        sprintf(szEventHeader, "%d Chat Request ", CODE_VIEWxCHAT); 
     1697        break; 
     1698 
     1699      case ICQ_CMDxSUB_FILE: 
     1700        sprintf(szEventHeader, "%d File Request ", CODE_VIEWxFILE); 
     1701        break; 
     1702 
     1703      default: 
     1704        sprintf(szEventHeader, "%d Unknown Event ", CODE_VIEWxUNKNOWN); 
     1705    } 
     1706 
     1707    strcat(szEventHeader, "from "); 
     1708    if(e->Direction() == D_RECEIVER) 
     1709      strncat(szEventHeader, u->GetAlias(), 50); 
     1710    else 
     1711      strncat(szEventHeader, o->GetAlias(), 50); 
     1712    strcat(szEventHeader, "\n\0"); 
     1713 
     1714    // Write out the event header 
     1715    fprintf(fs, szEventHeader); 
     1716 
     1717    // Timestamp 
     1718    char szTimestamp[39]; 
     1719    char szTime[25]; 
     1720    time_t nMessageTime = e->Time(); 
     1721    struct tm *pTM = localtime(&nMessageTime); 
     1722    strftime(szTime, 25, "%H:%M:%S", pTM); 
     1723    sprintf(szTimestamp, "%d Sent At ", CODE_VIEWxTIME); 
     1724    strncat(szTimestamp, szTime, 25); 
     1725    strcat(szTimestamp, "\n\0"); 
     1726    fprintf(fs, szTimestamp); 
     1727 
     1728    // Message 
     1729    fprintf(fs, "%d Message Start\n", CODE_VIEWxTEXTxSTART); 
     1730    fprintf(fs, "%s", e->Text()); 
     1731    fprintf(fs, "\n"); 
     1732    fprintf(fs, "%d Message Complete\n", CODE_VIEWxTEXTxEND); 
     1733 
     1734    it++; 
     1735  } 
     1736 
     1737  gUserManager.DropOwner(m_nPPID); 
     1738  gUserManager.DropUser(u); 
     1739 
     1740  fprintf(fs, "%d End.\n", CODE_HISTORYxEND); 
     1741  return fflush(fs); 
     1742} 
  • src/rms.h

     
    1616class CICQSignal; 
    1717class ICQEvent; 
    1818class CLogService_Plugin; 
     19class CUserHistory; 
    1920 
    2021using namespace std; 
    2122 
     
    2425 
    2526typedef list<class CRMSClient *> ClientList; 
    2627typedef list<unsigned long> TagList; 
     28typedef list<class CUserEvent *> HistoryList; 
     29typedef HistoryList::reverse_iterator HistoryListRIter; 
    2730 
    2831class CLicqRMS 
    2932{ 
     
    8487  int Process_REMUSER(); 
    8588  int Process_SECURE(); 
    8689  int Process_NOTIFY(); 
     90  int Process_HISTORY(); 
    8791 
    8892protected: 
    8993  TCPSocket sock; 
     
    107111  char *m_szEventId; 
    108112  unsigned long m_nEventPPID; 
    109113 
     114  CUserHistory *m_history; 
     115  HistoryList m_historyList;  
     116  char *m_szHistoryLastId; 
     117  unsigned long m_nHistoryLastPPID; 
     118 
    110119  int StateMachine(); 
    111120  int ProcessCommand(); 
    112121  bool ProcessEvent(ICQEvent *);