~ubuntu-branches/ubuntu/oneiric/znc/oneiric-backports

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2004-2011  See the AUTHORS file for details.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation.
 */

#ifndef _TEMPLATE_H
#define _TEMPLATE_H

#include "zncconfig.h"
#include "Utils.h"
#include <iostream>

using std::ostream;
using std::endl;

class CTemplate;

class CTemplateTagHandler {
public:
	CTemplateTagHandler() {}
	virtual ~CTemplateTagHandler() {}

	virtual bool HandleVar(CTemplate& Tmpl, const CString& sName, const CString& sArgs, CString& sOutput) {
		return false;
	}

	virtual bool HandleTag(CTemplate& Tmpl, const CString& sName, const CString& sArgs, CString& sOutput) {
		return false;
	}

	virtual bool HandleIf(CTemplate& Tmpl, const CString& sName, const CString& sArgs, CString& sOutput) {
		return HandleVar(Tmpl, sName, sArgs, sOutput);
	}

	virtual bool HandleValue(CTemplate& Tmpl, CString& sValue, const MCString& msOptions) {
		return false;
	}
private:
};
class CTemplate;

class CTemplateOptions {
public:
	CTemplateOptions() {
		m_eEscapeFrom = CString::EASCII;
		m_eEscapeTo = CString::EASCII;
	}

	virtual ~CTemplateOptions() {}

	void Parse(const CString& sLine);

	// Getters
	CString::EEscape GetEscapeFrom() const { return m_eEscapeFrom; }
	CString::EEscape GetEscapeTo() const { return m_eEscapeTo; }
	// !Getters
private:
	CString::EEscape   m_eEscapeFrom;
	CString::EEscape   m_eEscapeTo;
};


class CTemplateLoopContext {
public:
	CTemplateLoopContext(unsigned long uFilePos, const CString& sLoopName, bool bReverse, vector<CTemplate*>* pRows) {
		m_uFilePosition = uFilePos;
		m_sName = sLoopName;
		m_uRowIndex = 0;
		m_bReverse = bReverse;
		m_pvRows = pRows;
		m_bHasData = false;
	}

	virtual ~CTemplateLoopContext() {}

	// Setters
	void SetHasData(bool b = true) { m_bHasData = b; }
	void SetName(const CString& s) { m_sName = s; }
	void SetRowIndex(unsigned int u) { m_uRowIndex = u; }
	unsigned int IncRowIndex() { return ++m_uRowIndex; }
	unsigned int DecRowIndex() { if (m_uRowIndex == 0) { return 0; } return --m_uRowIndex; }
	void SetFilePosition(unsigned int u) { m_uFilePosition = u; }
	// !Setters

	// Getters
	bool HasData() const { return m_bHasData; }
	const CString& GetName() const { return m_sName; }
	unsigned long GetFilePosition() const { return m_uFilePosition; }
	unsigned int GetRowIndex() const { return m_uRowIndex; }
	unsigned int GetRowCount() { return m_pvRows->size(); }
	vector<CTemplate*>* GetRows() { return m_pvRows; }
	CTemplate* GetNextRow() { return GetRow(IncRowIndex()); }
	CTemplate* GetCurRow() { return GetRow(m_uRowIndex); }

	CTemplate* GetRow(unsigned int uIndex);
	CString GetValue(const CString& sName, bool bFromIf = false);
	// !Getters
private:
	bool                  m_bReverse;       //!< Iterate through this loop in reverse order
	bool                  m_bHasData;       //!< Tells whether this loop has real data or not
	CString               m_sName;          //!< The name portion of the <?LOOP name?> tag
	unsigned int          m_uRowIndex;      //!< The index of the current row we're on
	unsigned long         m_uFilePosition;  //!< The file position of the opening <?LOOP?> tag
	vector<CTemplate*>*   m_pvRows;         //!< This holds pointers to the templates associated with this loop
};


class CTemplate : public MCString {
public:
	CTemplate() : MCString(), m_spOptions(new CTemplateOptions) {
		Init();
	}

	CTemplate(const CString& sFileName) : MCString(), m_sFileName(sFileName), m_spOptions(new CTemplateOptions) {
		Init();
	}

	CTemplate(const CSmartPtr<CTemplateOptions>& Options, CTemplate* pParent = NULL) : MCString(), m_spOptions(Options) {
		Init();
		m_pParent = pParent;
	}

	virtual ~CTemplate();

	//! Class for implementing custom tags in subclasses
	void AddTagHandler(CSmartPtr<CTemplateTagHandler> spTagHandler) {
		m_vspTagHandlers.push_back(spTagHandler);
	}

	vector<CSmartPtr<CTemplateTagHandler> >& GetTagHandlers() {
		if (m_pParent) {
			return m_pParent->GetTagHandlers();
		}

		return m_vspTagHandlers;
	}

	CString ResolveLiteral(const CString& sString);

	void Init();

	CTemplate* GetParent(bool bRoot);
	CString ExpandFile(const CString& sFilename, bool bFromInc = false);
	bool SetFile(const CString& sFileName);

	void SetPath(const CString& sPath);  // Sets the dir:dir:dir type path to look at for templates, as of right now no ../../.. protection
	CString MakePath(const CString& sPath) const;
	void PrependPath(const CString& sPath, bool bIncludesOnly = false);
	void AppendPath(const CString& sPath, bool bIncludesOnly = false);
	void RemovePath(const CString& sPath);
	void ClearPaths();
	bool PrintString(CString& sRet);
	bool Print(ostream& oOut);
	bool Print(const CString& sFileName, ostream& oOut);
	bool ValidIf(const CString& sArgs);
	bool ValidExpr(const CString& sExpr);
	bool IsTrue(const CString& sName);
	bool HasLoop(const CString& sName);
	CString GetValue(const CString& sName, bool bFromIf = false);
	CTemplate& AddRow(const CString& sName);
	CTemplate* GetRow(const CString& sName, unsigned int uIndex);
	vector<CTemplate*>* GetLoop(const CString& sName);
	void DelCurLoopContext();
	CTemplateLoopContext* GetCurLoopContext();
	CTemplate* GetCurTemplate();

	// Getters
	const CString& GetFileName() const { return m_sFileName; }
	// !Getters
private:
	CTemplate*                               m_pParent;
	CString                                  m_sFileName;
	list<pair<CString, bool> >               m_lsbPaths;
	map<CString, vector<CTemplate*> >        m_mvLoops;
	vector<CTemplateLoopContext*>            m_vLoopContexts;
	CSmartPtr<CTemplateOptions>              m_spOptions;
	vector<CSmartPtr<CTemplateTagHandler> >  m_vspTagHandlers;
};

#endif // !_TEMPLATE_H