]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/commandbuilder.h
Merge insp20
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / commandbuilder.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #pragma once
21
22 #include "utils.h"
23
24 class TreeServer;
25
26 class CmdBuilder
27 {
28  protected:
29         std::string content;
30
31  public:
32         CmdBuilder(const char* cmd)
33                 : content(1, ':')
34         {
35                 content.append(ServerInstance->Config->GetSID());
36                 push(cmd);
37         }
38
39         CmdBuilder(const std::string& src, const char* cmd)
40                 : content(1, ':')
41         {
42                 content.append(src);
43                 push(cmd);
44         }
45
46         CmdBuilder(User* src, const char* cmd)
47                 : content(1, ':')
48         {
49                 content.append(src->uuid);
50                 push(cmd);
51         }
52
53         CmdBuilder& push_raw(const std::string& s)
54         {
55                 content.append(s);
56                 return *this;
57         }
58
59         CmdBuilder& push_raw(const char* s)
60         {
61                 content.append(s);
62                 return *this;
63         }
64
65         CmdBuilder& push_raw(char c)
66         {
67                 content.push_back(c);
68                 return *this;
69         }
70
71         template <typename T>
72         CmdBuilder& push_raw_int(T i)
73         {
74                 content.append(ConvToStr(i));
75                 return *this;
76         }
77
78         CmdBuilder& push(const std::string& s)
79         {
80                 content.push_back(' ');
81                 content.append(s);
82                 return *this;
83         }
84
85         CmdBuilder& push(const char* s)
86         {
87                 content.push_back(' ');
88                 content.append(s);
89                 return *this;
90         }
91
92         CmdBuilder& push(char c)
93         {
94                 content.push_back(' ');
95                 content.push_back(c);
96                 return *this;
97         }
98
99         template <typename T>
100         CmdBuilder& push_int(T i)
101         {
102                 content.push_back(' ');
103                 content.append(ConvToStr(i));
104                 return *this;
105         }
106
107         CmdBuilder& push_last(const std::string& s)
108         {
109                 content.push_back(' ');
110                 content.push_back(':');
111                 content.append(s);
112                 return *this;
113         }
114
115         template<typename T>
116         CmdBuilder& insert(const T& cont)
117         {
118                 for (typename T::const_iterator i = cont.begin(); i != cont.end(); ++i)
119                         push(*i);
120                 return *this;
121         }
122
123         void push_back(const std::string& s) { push(s); }
124
125         const std::string& str() const { return content; }
126         operator const std::string&() const { return str(); }
127
128         void Broadcast() const
129         {
130                 Utils->DoOneToMany(*this);
131         }
132
133         void Forward(TreeServer* omit) const
134         {
135                 Utils->DoOneToAllButSender(*this, omit);
136         }
137
138         bool Unicast(const std::string& target) const
139         {
140                 return Utils->DoOneToOne(*this, target);
141         }
142
143         void Unicast(User* target) const
144         {
145                 Utils->DoOneToOne(*this, target->server);
146         }
147 };