1 /*
2  * Copyright (C) 2004-2020 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 %module ZNC %{
18 #ifdef Copy
19 # undef Copy
20 #endif
21 #ifdef Pause
22 # undef Pause
23 #endif
24 #ifdef seed
25 # undef seed
26 #endif
27 #include <utility>
28 #include "znc/Utils.h"
29 #include "znc/Threads.h"
30 #include "znc/Config.h"
31 #include "znc/Socket.h"
32 #include "znc/Modules.h"
33 #include "znc/Nick.h"
34 #include "znc/Chan.h"
35 #include "znc/User.h"
36 #include "znc/IRCNetwork.h"
37 #include "znc/Client.h"
38 #include "znc/IRCSock.h"
39 #include "znc/Listener.h"
40 #include "znc/HTTPSock.h"
41 #include "znc/Template.h"
42 #include "znc/WebModules.h"
43 #include "znc/znc.h"
44 #include "znc/Server.h"
45 #include "znc/ZNCString.h"
46 #include "znc/FileUtils.h"
47 #include "znc/ZNCDebug.h"
48 #include "znc/ExecSock.h"
49 #include "znc/Buffer.h"
50 #include "modperl/module.h"
51 #define stat struct stat
52 %}
53 
54 %apply long { off_t };
55 %apply long { uint16_t };
56 %apply long { uint32_t };
57 %apply long { uint64_t };
58 
59 %begin %{
60 #include "znc/zncconfig.h"
61 %}
62 
63 %include <typemaps.i>
64 %include <stl.i>
65 %include <std_list.i>
66 %include <std_deque.i>
67 
68 namespace std {
69 	template<class K> class set {
70 		public:
71 		set();
72 		set(const set<K>&);
73 	};
74 }
75 %include "modperl/CString.i"
76 
77 %typemap(out) VCString {
78     EXTEND(sp, $1.size());
79     for (int i = 0; i < $1.size(); ++i) {
80         SV* x = newSV(0);
81         SwigSvFromString(x, $1[i]);
82         $result = sv_2mortal(x);
83         argvi++;
84     }
85 }
86 %typemap(out) const VCString& {
87     EXTEND(sp, $1->size());
88     for (int i = 0; i < $1->size(); ++i) {
89         SV* x = newSV(0);
90         SwigSvFromString(x, (*$1)[i]);
91         $result = sv_2mortal(x);
92         argvi++;
93     }
94 }
95 
96 %template(VIRCNetworks) std::vector<CIRCNetwork*>;
97 %template(VChannels) std::vector<CChan*>;
98 %template(VCString) std::vector<CString>;
99 typedef std::vector<CString> VCString;
100 /*%template(MNicks) std::map<CString, CNick>;*/
101 /*%template(SModInfo) std::set<CModInfo>;
102 %template(SCString) std::set<CString>;
103 typedef std::set<CString> SCString;*/
104 %template(PerlMCString) std::map<CString, CString>;
105 class MCString : public std::map<CString, CString> {};
106 /*%template(PerlModulesVector) std::vector<CModule*>;*/
107 %template(VListeners) std::vector<CListener*>;
108 %template(BufLines) std::deque<CBufLine>;
109 %template(VVString) std::vector<VCString>;
110 
111 #define REGISTER_ZNC_MESSAGE(M) \
112     %template(As_ ## M) CMessage::As<M>;
113 
114 %typemap(out) std::map<CString, CNick> {
115 	HV* myhv = newHV();
116 	for (std::map<CString, CNick>::const_iterator i = $1.begin(); i != $1.end(); ++i) {
117 		SV* val = SWIG_NewInstanceObj(const_cast<CNick*>(&i->second), SWIG_TypeQuery("CNick*"), SWIG_SHADOW);
118 		SvREFCNT_inc(val);// it was created mortal
119 		hv_store(myhv, i->first.c_str(), i->first.length(), val, 0);
120 	}
121 	$result = newRV_noinc((SV*)myhv);
122 	sv_2mortal($result);
123 	argvi++;
124 }
125 
126 #define u_short unsigned short
127 #define u_int unsigned int
128 #include "znc/zncconfig.h"
129 #include "znc/ZNCString.h"
130 %include "znc/defines.h"
131 %include "znc/Translation.h"
132 %include "znc/Utils.h"
133 %include "znc/Threads.h"
134 %include "znc/Config.h"
135 %include "znc/Csocket.h"
136 %template(ZNCSocketManager) TSocketManager<CZNCSock>;
137 %include "znc/Socket.h"
138 %include "znc/FileUtils.h"
139 %include "znc/Message.h"
140 %include "znc/Modules.h"
141 %include "znc/Nick.h"
142 %include "znc/Chan.h"
143 %include "znc/User.h"
144 %include "znc/IRCNetwork.h"
145 %include "znc/Client.h"
146 %include "znc/IRCSock.h"
147 %include "znc/Listener.h"
148 %include "znc/HTTPSock.h"
149 %include "znc/Template.h"
150 %include "znc/WebModules.h"
151 %include "znc/znc.h"
152 %include "znc/Server.h"
153 %include "znc/ZNCDebug.h"
154 %include "znc/ExecSock.h"
155 %include "znc/Buffer.h"
156 
157 %include "modperl/module.h"
158 
159 %inline %{
160 	class String : public CString {
161 		public:
String()162 			String() {}
String(const CString & s)163 			String(const CString& s)	: CString(s) {}
CString(d,prec)164 			String(double d, int prec=2): CString(d, prec) {}
CString(f,prec)165 			String(float f, int prec=2) : CString(f, prec) {}
String(int i)166 			String(int i)			   : CString(i) {}
String(unsigned int i)167 			String(unsigned int i)	  : CString(i) {}
String(long int i)168 			String(long int i)		  : CString(i) {}
String(unsigned long int i)169 			String(unsigned long int i) : CString(i) {}
String(char c)170 			String(char c)			  : CString(c) {}
String(unsigned char c)171 			String(unsigned char c)	 : CString(c) {}
String(short int i)172 			String(short int i)		 : CString(i) {}
String(unsigned short int i)173 			String(unsigned short int i): CString(i) {}
String(bool b)174 			String(bool b)			  : CString(b) {}
GetPerlStr()175 			CString GetPerlStr() {
176 				return *this;
177 			}
178 	};
179 %}
180 
181 %extend CModule {
GetNVKeys()182     VCString GetNVKeys() {
183         VCString result;
184         for (auto i = $self->BeginNV(); i != $self->EndNV(); ++i) {
185             result.push_back(i->first);
186         }
187         return result;
188     }
ExistsNV(const CString & sName)189 	bool ExistsNV(const CString& sName) {
190 		return $self->EndNV() != $self->FindNV(sName);
191 	}
192 }
193 
194 %extend CModules {
push_back(CModule * p)195 	void push_back(CModule* p) {
196 		$self->push_back(p);
197 	}
removeModule(CModule * p)198 	bool removeModule(CModule* p) {
199 		for (CModules::iterator i = $self->begin(); $self->end() != i; ++i) {
200 			if (*i == p) {
201 				$self->erase(i);
202 				return true;
203 			}
204 		}
205 		return false;
206 	}
207 }
208 
209 %extend CUser {
GetNetworks_()210 	std::vector<CIRCNetwork*> GetNetworks_() {
211 		return $self->GetNetworks();
212 	}
213 }
214 
215 %extend CIRCNetwork {
GetChans_()216 	std::vector<CChan*> GetChans_() {
217 		return $self->GetChans();
218 	}
219 }
220 
221 %extend CChan {
GetNicks_()222 	std::map<CString, CNick> GetNicks_() {
223 		return $self->GetNicks();
224 	}
225 }
226 
227 /* Web */
228 
229 %template(StrPair) std::pair<CString, CString>;
230 %template(VPair) std::vector<std::pair<CString, CString> >;
231 typedef std::vector<std::pair<CString, CString> > VPair;
232 %template(VWebSubPages) std::vector<TWebSubPage>;
233 
234 %inline %{
_VPair_Add2Str(VPair * self,const CString & a,const CString & b)235 	void _VPair_Add2Str(VPair* self, const CString& a, const CString& b) {
236 		self->push_back(std::make_pair(a, b));
237 	}
238 %}
239 
240 %extend CTemplate {
set(const CString & key,const CString & value)241 	void set(const CString& key, const CString& value) {
242 		(*$self)[key] = value;
243 	}
244 }
245 
246 %inline %{
_CreateWebSubPage(const CString & sName,const CString & sTitle,const VPair & vParams,unsigned int uFlags)247 	TWebSubPage _CreateWebSubPage(const CString& sName, const CString& sTitle, const VPair& vParams, unsigned int uFlags) {
248 		return std::make_shared<CWebSubPage>(sName, sTitle, vParams, uFlags);
249 	}
250 %}
251 
252 %perlcode %{
253 	package ZNC;
254 	sub CreateWebSubPage {
255 		my ($name, %arg) = @_;
256 		my $params = $arg{params}//{};
257 		my $vpair = ZNC::VPair->new;
258 		while (my ($key, $val) = each %$params) {
259 			ZNC::_VPair_Add2Str($vpair, $key, $val);
260 		}
261 		my $flags = 0;
262 		$flags |= $ZNC::CWebSubPage::F_ADMIN if $arg{admin}//0;
263 		return _CreateWebSubPage($name, $arg{title}//'', $vpair, $flags);
264 	}
265 %}
266 
267 %inline %{
268 	void _CleanupStash(const CString& sModname) {
269 		hv_clear(gv_stashpv(sModname.c_str(), 0));
270 	}
271 %}
272 
273 %perlcode %{
274 	package ZNC;
275 	*CONTINUE = *ZNC::CModule::CONTINUE;
276 	*HALT = *ZNC::CModule::HALT;
277 	*HALTMODS = *ZNC::CModule::HALTMODS;
278 	*HALTCORE = *ZNC::CModule::HALTCORE;
279 	*UNLOAD = *ZNC::CModule::UNLOAD;
280 
281 	package ZNC::CIRCNetwork;
282 	*GetChans = *GetChans_;
283 
284 	package ZNC::CUser;
285 	*GetNetworks = *GetNetworks_;
286 
287 	package ZNC::CChan;
288 	sub _GetNicks_ {
289 		my $result = GetNicks_(@_);
290 		return %$result;
291 	}
292 	*GetNicks = *_GetNicks_;
293 %}
294 
295 /* vim: set filetype=cpp: */
296