summaryrefslogtreecommitdiff
path: root/plugins/HistoryStats/src/message.h
blob: 39e8af549e75e0819e46d07c76b05c0924fdc041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#if !defined(HISTORYSTATS_GUARD_MESSAGE_H)
#define HISTORYSTATS_GUARD_MESSAGE_H

#include "stdafx.h"
#include "_consts.h"

#include "utils.h"

/*
 * Message
 */

class Message
	: private pattern::NotCopyable<Message>
{
private:
	enum Flags {
		// internal
		Raw          = 0x01,
		WithoutLinks = 0x02,

		// very internal
		PtrIsNonT    = 0x10,
		PtrIsUTF8    = 0x20,
	};

private:
	bool m_bOutgoing;
	DWORD m_Timestamp;
	size_t m_nLength;
	const void* m_RawSource;
	int m_Available;
	ext::string m_Raw;
	ext::string m_WithoutLinks;
	bool m_bStripRawRTF;
	bool m_bStripBBCodes;

private:
	void makeRawAvailable();
	void stripRawRTF();
	void stripBBCodes();
	void filterLinks();

public:
	explicit Message(bool bStripRawRTF, bool bStripBBCodes)
		: m_RawSource(nullptr), m_Available(0),
		m_bStripRawRTF(bStripRawRTF), m_bStripBBCodes(bStripBBCodes),
		m_bOutgoing(false), m_Timestamp(0), m_nLength(0)
	{
	}

	// assigning data
	void assignInfo(bool bOutgoing, DWORD localTimestamp)
	{
		m_bOutgoing = bOutgoing;
		m_Timestamp = localTimestamp;
	}

	void assignText(const wchar_t* msg, size_t len)
	{
		m_RawSource = msg;
		m_nLength = len;
		m_Available = 0;
	}

#if defined(_UNICODE)
	void assignText(const char* msg, size_t len)
	{
		m_RawSource = msg;
		m_nLength = len;
		m_Available = PtrIsNonT;
	}
#endif // _UNICODE

	void assignTextFromUTF8(const char* msg, size_t len)
	{
		m_RawSource = msg;
		m_nLength = len;
		m_Available = PtrIsUTF8;
	}

	// retrieving always available data
	bool isOutgoing()
	{
		return m_bOutgoing;
	}
	
	DWORD getTimestamp()
	{
		return m_Timestamp;
	}
	
	// retrieving on-demand data
	size_t getLength()
	{
		return (!m_bStripBBCodes && !m_bStripRawRTF) ? m_nLength : getRaw().length();
	}

	const ext::string& getRaw()
	{
		if (!(m_Available & Raw))
		{
			makeRawAvailable();
		}

		return m_Raw;
	}

	const ext::string& getWithoutLinks()
	{
		if (!(m_Available & WithoutLinks))
		{
			filterLinks();
		}
		
		return m_WithoutLinks;
	}
};

#endif // HISTORYSTATS_GUARD_MESSAGE_H