| | 1592 | |
| | 1593 | /*--------------------------------------------------------------------------- |
| | 1594 | * CRMSClient::Process_HISTORY |
| | 1595 | * |
| | 1596 | * Command: |
| | 1597 | * HISTORY <uin> <lenght> <offset> |
| | 1598 | * |
| | 1599 | * Response: |
| | 1600 | * |
| | 1601 | *-------------------------------------------------------------------------*/ |
| | 1602 | int 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 | } |