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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*
* Copyright (C) 2004-2025 ZNC, see the NOTICE file for details.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
%module znc_core
%{
#include <utility>
#include "znc/Utils.h"
#include "znc/Threads.h"
#include "znc/Config.h"
#include "znc/Socket.h"
#include "znc/Modules.h"
#include "znc/Nick.h"
#include "znc/Chan.h"
#include "znc/User.h"
#include "znc/IRCNetwork.h"
#include "znc/Query.h"
#include "znc/Client.h"
#include "znc/IRCSock.h"
#include "znc/Listener.h"
#include "znc/HTTPSock.h"
#include "znc/Template.h"
#include "znc/WebModules.h"
#include "znc/znc.h"
#include "znc/Server.h"
#include "znc/ZNCString.h"
#include "znc/FileUtils.h"
#include "znc/ZNCDebug.h"
#include "znc/ExecSock.h"
#include "znc/Buffer.h"
#include "modpython/module.h"
#include "modpython/ret.h"
#define stat struct stat
using std::allocator;
%}
%apply long { off_t };
%apply long { uint16_t };
%apply long { uint32_t };
%apply long { uint64_t };
// Just makes generated python code slightly more beautiful.
%feature("python:defaultargs");
// Probably can be removed when swig is fixed to not produce bad code for some cases
%feature("python:defaultargs", "0") CDir::MakeDir; // 0700 doesn't work in python3
%feature("python:defaultargs", "0") CUtils::GetNumInput; // SyntaxError: non-default argument follows default argument
%feature("python:defaultargs", "0") CModules::GetAvailableMods; // NameError: name 'UserModule' is not defined
%feature("python:defaultargs", "0") CModules::GetDefaultMods; // NameError: name 'UserModule' is not defined
%begin %{
#include "znc/zncconfig.h"
%}
%include <pyabc.i>
%include <typemaps.i>
%include <stl.i>
%include <std_list.i>
%include <std_set.i>
%include <std_deque.i>
%include <std_shared_ptr.i>
%shared_ptr(CAuthBase);
%shared_ptr(CWebSession);
%shared_ptr(CClientAuth);
%include "modpython/cstring.i"
%template(_stringlist) std::list<CString>;
%typemap(out) CModules::ModDirList %{
$result = PyList_New($1.size());
if ($result) {
for (size_t i = 0; !$1.empty(); $1.pop(), ++i) {
PyList_SetItem($result, i, Py_BuildValue("ss", $1.front().first.c_str(), $1.front().second.c_str()));
}
}
%}
%template(VIRCNetworks) std::vector<CIRCNetwork*>;
%template(VChannels) std::vector<CChan*>;
%template(MNicks) std::map<CString, CNick>;
%template(SModInfo) std::set<CModInfo>;
%template(SCString) std::set<CString>;
typedef std::set<CString> SCString;
%template(VCString) std::vector<CString>;
typedef std::vector<CString> VCString;
%template(PyMCString) std::map<CString, CString>;
%template(PyMStringVString) std::map<CString, VCString>;
class MCString : public std::map<CString, CString> {};
%template(PyModulesVector) std::vector<CModule*>;
%template(VListeners) std::vector<CListener*>;
%template(BufLines) std::deque<CBufLine>;
%template(VVString) std::vector<VCString>;
%template(VClients) std::vector<CClient*>;
%template(VServers) std::vector<CServer*>;
%template(VQueries) std::vector<CQuery*>;
#define REGISTER_ZNC_MESSAGE(M) \
%template(As_ ## M) CMessage::As<M>;
%typemap(in) CString& {
String* p;
int res = SWIG_IsOK(SWIG_ConvertPtr($input, (void**)&p, SWIG_TypeQuery("String*"), 0));
if (SWIG_IsOK(res)) {
$1 = &p->s;
} else {
SWIG_exception_fail(SWIG_ArgError(res), "need znc.String object as argument $argnum $1_name");
}
}
%typemap(out) CString&, CString* {
if ($1) {
$result = CPyRetString::wrap(*$1);
} else {
$result = Py_None;
Py_INCREF(Py_None);
}
}
%typemap(typecheck) CString&, CString* {
String* p;
$1 = SWIG_IsOK(SWIG_ConvertPtr($input, (void**)&p, SWIG_TypeQuery("String*"), 0));
}
/*TODO %typemap(in) bool& to be able to call from python functions which get bool& */
%typemap(out) bool&, bool* {
if ($1) {
$result = CPyRetBool::wrap(*$1);
} else {
$result = Py_None;
Py_INCREF(Py_None);
}
}
#define u_short unsigned short
#define u_int unsigned int
#include "znc/zncconfig.h"
#include "znc/ZNCString.h"
%include "znc/defines.h"
%include "znc/Translation.h"
%include "znc/Utils.h"
%include "znc/Threads.h"
%include "znc/Config.h"
%include "znc/Csocket.h"
%template(ZNCSocketManager) TSocketManager<CZNCSock>;
%include "znc/Socket.h"
%include "znc/FileUtils.h"
%include "znc/Message.h"
%include "znc/Modules.h"
%include "znc/Nick.h"
%include "znc/Chan.h"
%include "znc/User.h"
%include "znc/IRCNetwork.h"
%include "znc/Query.h"
%include "znc/Client.h"
%include "znc/IRCSock.h"
%include "znc/Listener.h"
%include "znc/HTTPSock.h"
%include "znc/Template.h"
%include "znc/WebModules.h"
%include "znc/znc.h"
%include "znc/Server.h"
%include "znc/ZNCDebug.h"
%include "znc/ExecSock.h"
%include "znc/Buffer.h"
%include "modpython/module.h"
/* Really it's CString& inside, but SWIG shouldn't know that :) */
class CPyRetString {
CPyRetString();
public:
CString s;
};
%extend CPyRetString {
CString __str__() {
return $self->s;
}
};
%extend String {
CString __str__() {
return $self->s;
}
};
class CPyRetBool {
CPyRetBool();
public:
bool b;
};
%extend CPyRetBool {
bool __bool__() {
return $self->b;
}
}
%extend Csock {
PyObject* WriteBytes(PyObject* data) {
if (!PyBytes_Check(data)) {
PyErr_SetString(PyExc_TypeError, "socket.WriteBytes needs bytes as argument");
return nullptr;
}
char* buffer;
Py_ssize_t length;
if (-1 == PyBytes_AsStringAndSize(data, &buffer, &length)) {
return nullptr;
}
if ($self->Write(buffer, length)) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
}
%extend CModule {
CString __str__() {
return $self->GetModName();
}
MCString_iter BeginNV_() {
return MCString_iter($self->BeginNV());
}
bool ExistsNV(const CString& sName) {
return $self->EndNV() != $self->FindNV(sName);
}
void AddServerDependentCapability(const CString& sName, PyObject* serverCb,
PyObject* clientCb) {
$self->AddServerDependentCapability(sName, std::make_unique<CPyCapability>(serverCb, clientCb));
}
}
%extend CModules {
bool removeModule(CModule* p) {
for (CModules::iterator i = $self->begin(); $self->end() != i; ++i) {
if (*i == p) {
$self->erase(i);
return true;
}
}
return false;
}
}
%extend CUser {
CString __str__() {
return $self->GetUsername();
}
CString __repr__() {
return "<CUser " + $self->GetUsername() + ">";
}
std::vector<CIRCNetwork*> GetNetworks_() {
return $self->GetNetworks();
}
};
%extend CIRCNetwork {
CString __str__() {
return $self->GetName();
}
CString __repr__() {
return "<CIRCNetwork " + $self->GetName() + ">";
}
std::vector<CChan*> GetChans_() {
return $self->GetChans();
}
std::vector<CServer*> GetServers_() {
return $self->GetServers();
}
std::vector<CQuery*> GetQueries_() {
return $self->GetQueries();
}
}
%extend CChan {
CString __str__() {
return $self->GetName();
}
CString __repr__() {
return "<CChan " + $self->GetName() + ">";
}
std::map<CString, CNick> GetNicks_() {
return $self->GetNicks();
}
};
%extend CNick {
CString __str__() {
return $self->GetNick();
}
CString __repr__() {
return "<CNick " + $self->GetHostMask() + ">";
}
};
%extend CMessage {
CString __str__() {
return $self->ToString();
}
CString __repr__() {
return $self->ToString();
}
};
%extend CZNC {
PyObject* GetUserMap_() {
PyObject* result = PyDict_New();
auto user_type = SWIG_TypeQuery("CUser*");
for (const auto& p : $self->GetUserMap()) {
PyObject* user = SWIG_NewInstanceObj(p.second, user_type, 0);
PyDict_SetItemString(result, p.first.c_str(), user);
Py_CLEAR(user);
}
return result;
}
};
/* To allow module-loaders to be written on python.
* They can call CreatePyModule() to create CModule* object, but one of arguments to CreatePyModule() is "CModule* pModPython"
* Pointer to modpython is already accessible to python modules as self.GetModPython(), but it's just a pointer to something, not to CModule*.
* So make it known that CModPython is really a CModule.
*/
class CModPython : public CModule {
private:
CModPython();
CModPython(const CModPython&);
~CModPython();
};
/* Web */
%template(StrPair) std::pair<CString, CString>;
%template(VPair) std::vector<std::pair<CString, CString> >;
typedef std::vector<std::pair<CString, CString> > VPair;
%template(VWebSubPages) std::vector<TWebSubPage>;
%inline %{
void VPair_Add2Str_(VPair* self, const CString& a, const CString& b) {
self->push_back(std::make_pair(a, b));
}
%}
%extend CTemplate {
void set(const CString& key, const CString& value) {
DEBUG("WARNING: modpython's CTemplate.set is deprecated and will be removed. Use normal dict's operations like Tmpl['foo'] = 'bar'");
(*$self)[key] = value;
}
}
%inline %{
TWebSubPage CreateWebSubPage_(const CString& sName, const CString& sTitle, const VPair& vParams, unsigned int uFlags) {
return std::make_shared<CWebSubPage>(sName, sTitle, vParams, uFlags);
}
%}
/* vim: set filetype=cpp: */
|