ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/misc/svsperl.xs
Revision: 1.4
Committed: Thu Aug 30 19:56:22 2007 UTC (19 years, 1 month ago) by pippijn
Branch: MAIN
Changes since 1.3: +9 -7 lines
Log Message:
- put faultcodes into their own namespace
- removed old files
- limited header garbage in atheme.h
- macros to inline bools for connection_t::is_*
- put some connection_t functions into the connection_t class

File Contents

# User Rev Content
1 pippijn 1.3 /*
2     * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team
3     * Rights to this code are as documented in doc/pod/gplicense.pod.
4     *
5     * Perl extension module for Ermyth
6     *
7 pippijn 1.4 * $Id: svsperl.xs,v 1.3 2007-08-28 17:08:09 pippijn Exp $
8 pippijn 1.3 */
9    
10 pippijn 1.1 #include <map>
11    
12     #include <atheme.h>
13     #include <users.h>
14    
15     #include <connection.h>
16    
17 pippijn 1.3 #include <ermyth/module.h>
18    
19 pippijn 1.1 #include <account/chanacs.h>
20     #include <account/kline.h>
21     #include <account/metadata.h>
22     #include <account/mychan.h>
23     #include <account/mymemo.h>
24     #include <account/mynick.h>
25     #include <account/myuser.h>
26     #include <account/svsignore.h>
27     #include <confparse.h>
28     #include <servers.h>
29 pippijn 1.3 #include <sasl.h>
30 pippijn 1.1
31     // macro clashes
32     #undef _
33     #define warn_opers warn
34     #undef warn
35    
36     #include <EXTERN.h>
37     #include <perl.h>
38     #include <XSUB.h>
39    
40     #include "perlxsi.c"
41    
42 pippijn 1.4 static char const rcsid[] = "$Id: svsperl.xs,v 1.3 2007-08-28 17:08:09 pippijn Exp $";
43 pippijn 1.1
44 pippijn 1.3 REGISTER_MODULE ("misc/svsperl", false, "The Ermyth Team <http://services.one09.net>");
45 pippijn 1.1
46     static PerlInterpreter *perl;
47    
48     static HV *stash_svs;
49    
50     // Make an std::map<char *, char *> from a HV
51     typedef std::map<char *, char *, str_lt> charmap;
52    
53     charmap
54     HvMAP (HV *hv)
55     {
56     SV *sv;
57     char *key;
58     STRLEN len;
59     charmap hvmap;
60    
61     I32 i, count = hv_iterinit (hv);
62    
63     for (i = 1; i <= count; i++)
64     {
65     sv = hv_iternextsv(hv, &key, (I32*) &len);
66     hvmap[key] = SvPV(sv, len);
67     }
68    
69     return hvmap;
70     }
71    
72    
73     void *
74     get_mortalspace (int nbytes)
75     {
76     SV *mortal;
77     mortal = sv_2mortal (NEWSV (2040, nbytes));
78     return (void *) SvPVX (mortal);
79     }
80    
81     // convert a perl arrayref to a char* array
82     char **
83     XS_unpack_charPtrPtr (SV *arg)
84     {
85     AV *avref;
86     char **array;
87     int len;
88     SV **elem;
89     int i;
90    
91     avref = (AV *) SvRV (arg);
92     len = av_len (avref) + 1;
93    
94     array = (char **) get_mortalspace ((len + 1) * sizeof (*array));
95    
96     for (i = 0; i < len; i++) {
97     elem = av_fetch (avref, i, 0);
98     array[i] = SvPV_nolen (*elem);
99     }
100    
101     array[len] = NULL;
102    
103     return array;
104     }
105    
106     // convert char* array to perl arrayref
107     void
108     XS_pack_charPtrPtr (SV *arg, char **array, int count)
109     {
110     int i;
111     AV *avref;
112    
113     avref = (AV *) sv_2mortal ((SV *) newAV ());
114    
115     for (i = 0; i < count; i++)
116     av_push (avref, newSVpv (array[i], strlen (array[i])));
117    
118     SvSetSV (arg, newRV ((SV *) avref));
119     }
120    
121    
122     SV *sv_cast (char const * v) { return newSVpv (v, 0); }
123     SV *sv_cast (bool v) { return newSViv (v); }
124     SV *sv_cast ( signed char v) { return newSViv (v); }
125     SV *sv_cast (unsigned char v) { return newSViv (v); }
126     SV *sv_cast ( signed short v) { return newSViv (v); }
127     SV *sv_cast (unsigned short v) { return newSVuv (v); }
128     SV *sv_cast ( signed int v) { return newSViv (v); }
129     SV *sv_cast (unsigned int v) { return newSVuv (v); }
130     SV *sv_cast ( signed long v) { return newSViv (v); }
131     SV *sv_cast (unsigned long v) { return newSVuv (v); }
132     SV *sv_cast (float v) { return newSVnv (v); }
133     SV *sv_cast (double v) { return newSVnv (v); }
134    
135     // base template for casting from SV *
136     template<class outT> outT sv_cast (SV *from);
137    
138     template<> char const * sv_cast<char const * > (SV *from) { return SvOK (from) ? SvPV_nolen (from) : 0; }
139     template<> bool sv_cast<bool > (SV *from) { return SvIV (from); }
140     template<> signed char sv_cast< signed char > (SV *from) { return SvIV (from); }
141     template<> unsigned char sv_cast<unsigned char > (SV *from) { return SvUV (from); }
142     template<> signed short sv_cast< signed short> (SV *from) { return SvIV (from); }
143     template<> unsigned short sv_cast<unsigned short> (SV *from) { return SvUV (from); }
144     template<> signed int sv_cast< signed int > (SV *from) { return SvIV (from); }
145     template<> unsigned int sv_cast<unsigned int > (SV *from) { return SvUV (from); }
146     template<> signed long sv_cast< signed long > (SV *from) { return SvIV (from); }
147     template<> unsigned long sv_cast<unsigned long > (SV *from) { return SvUV (from); }
148     template<> float sv_cast<float > (SV *from) { return SvNV (from); }
149     template<> double sv_cast<double > (SV *from) { return SvNV (from); }
150    
151    
152     void
153     _perlinit ()
154     {
155     stash_svs = gv_stashpvn ("svs", 3, 1);
156     }
157    
158 pippijn 1.3 bool
159     _modinit (module *m)
160 pippijn 1.1 {
161     PERL_SYS_INIT3 (&me.argc, &me.argv, 0);
162     perl = perl_alloc ();
163     perl_construct (perl);
164    
165     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
166    
167     char *argv[] = {
168     me.argv [0],
169     "-e"
170     "use strict; use warnings; use utf8;"
171     "svs->bootstrap;"
172     "unshift @INC, svs::DATADIR ();"
173     "require svs;"
174     };
175    
176     if (perl_parse (perl, xs_init, 2, argv, NULL)
177     || perl_run (perl))
178 pippijn 1.3 return false;
179 pippijn 1.1
180     dSP;
181    
182     PUSHMARK (SP);
183     PUTBACK;
184     call_pv ("svs::init", G_DISCARD | G_VOID);
185 pippijn 1.3
186     return true;
187 pippijn 1.1 }
188    
189     void
190     _moddeinit ()
191     {
192     perl_destruct (perl);
193     perl_free (perl);
194     PERL_SYS_TERM ();
195     }
196    
197     MODULE = svs PACKAGE = svs
198    
199     BOOT:
200     {
201 pippijn 1.3 #define const_iv(name) { const_cast<char *> (#name), name } // XXX: constness hack
202     #define stash_pv(name) newCONSTSUB (stash_svs, const_cast<char *> (#name), newSVpv (name, sizeof (name) - 1))
203 pippijn 1.1
204     _perlinit ();
205    
206     // provided on the commandline
207     stash_pv (PREFIX);
208     stash_pv (LOCALEDIR);
209     stash_pv (SHAREDIR);
210     stash_pv (SYSCONFDIR);
211     stash_pv (LOGDIR);
212     stash_pv (RUNDIR);
213     stash_pv (DATADIR);
214    
215     stash_pv (PRIV_USER_AUSPEX);
216     stash_pv (PRIV_USER_ADMIN);
217     stash_pv (PRIV_USER_SENDPASS);
218     stash_pv (PRIV_USER_VHOST);
219     stash_pv (PRIV_USER_FREGISTER);
220     stash_pv (PRIV_CHAN_AUSPEX);
221     stash_pv (PRIV_CHAN_ADMIN);
222     stash_pv (PRIV_CHAN_CMODES);
223     stash_pv (PRIV_JOIN_STAFFONLY);
224     stash_pv (PRIV_MARK);
225     stash_pv (PRIV_HOLD);
226     stash_pv (PRIV_REG_NOLIMIT);
227     stash_pv (PRIV_SERVER_AUSPEX);
228     stash_pv (PRIV_VIEWPRIVS);
229     stash_pv (PRIV_FLOOD);
230     stash_pv (PRIV_METADATA);
231     stash_pv (PRIV_ADMIN);
232     stash_pv (PRIV_OMODE);
233     stash_pv (PRIV_AKILL);
234     stash_pv (PRIV_MASS_AKILL);
235     stash_pv (PRIV_JUPE);
236     stash_pv (PRIV_NOOP);
237     stash_pv (PRIV_GLOBAL);
238     stash_pv (PRIV_GRANT);
239    
240     stash_pv (AC_IRCOP);
241     stash_pv (AC_SRA);
242    
243     stash_pv (PACKAGE);
244     stash_pv (PACKAGE_BUGREPORT);
245     stash_pv (PACKAGE_NAME);
246     stash_pv (PACKAGE_STRING);
247     stash_pv (PACKAGE_TARNAME);
248     stash_pv (PACKAGE_VERSION);
249     stash_pv (VERSION);
250     #undef stash_pv
251    
252     static const struct {
253     char *name;
254     IV iv;
255     } *civ, const_iv[] = {
256     // channels.h
257     const_iv (MAXEXTMODES),
258    
259     const_iv (CMODE_OP), const_iv (CMODE_VOICE), const_iv (CMODE_INVITE), const_iv (CMODE_KEY), const_iv (CMODE_LIMIT),
260     const_iv (CMODE_MOD), const_iv (CMODE_NOEXT), const_iv (CMODE_PRIV), const_iv (CMODE_SEC), const_iv (CMODE_TOPIC),
261    
262     const_iv (MTYPE_NUL), const_iv (MTYPE_ADD), const_iv (MTYPE_DEL),
263    
264     // common.h
265     const_iv (BUFSIZE), const_iv (MAXMODES), const_iv (MAX_EVENTS), const_iv (HOSTLEN), const_iv (NICKLEN), const_iv (IDLEN),
266     const_iv (CHANNELLEN), const_iv (USERLEN), const_iv (HOSTIPLEN), const_iv (GECOSLEN), const_iv (KEYLEN), const_iv (EMAILLEN),
267     const_iv (MEMOLEN), const_iv (MAXMSIGNORES),
268    
269 pippijn 1.4 // connection.h XXX: class ?
270     const_iv (connection_t::CF_UPLINK), const_iv (connection_t::CF_DCCOUT), const_iv (connection_t::CF_DCCIN),
271     const_iv (connection_t::CF_CONNECTING), const_iv (connection_t::CF_LISTENING), const_iv (connection_t::CF_CONNECTED),
272     const_iv (connection_t::CF_DEAD), const_iv (connection_t::CF_NONEWLINE), const_iv (connection_t::CF_SEND_EOF),
273     const_iv (connection_t::CF_SEND_DEAD),
274 pippijn 1.1
275     // global.h
276     const_iv (AUTH_NONE), const_iv (AUTH_EMAIL),
277    
278     const_iv (RF_LIVE), const_iv (RF_SHUTDOWN), const_iv (RF_STARTING), const_iv (RF_RESTART), const_iv (RF_REHASHING),
279    
280     // match.h
281     const_iv (MATCH_RFC1459), const_iv (MATCH_ASCII), const_iv (C_ALPHA), const_iv (C_DIGIT), const_iv (AREGEX_ICASE),
282    
283 pippijn 1.4 // metadata.h XXX: namespace?
284     const_iv (metadata::MYUSER), const_iv (metadata::MYCHAN), const_iv (metadata::CHANACS),
285 pippijn 1.1
286     // phandler.h
287     const_iv (PROTOCOL_ASUKA), const_iv (PROTOCOL_BAHAMUT), const_iv (PROTOCOL_CHARYBDIS), const_iv (PROTOCOL_DREAMFORGE),
288     const_iv (PROTOCOL_HYPERION), const_iv (PROTOCOL_INSPIRCD), const_iv (PROTOCOL_IRCNET), const_iv (PROTOCOL_MONKEY),
289     const_iv (PROTOCOL_PLEXUS), const_iv (PROTOCOL_PTLINK), const_iv (PROTOCOL_RATBOX), const_iv (PROTOCOL_SCYLLA),
290     const_iv (PROTOCOL_SHADOWIRCD), const_iv (PROTOCOL_SORCERY), const_iv (PROTOCOL_ULTIMATE3), const_iv (PROTOCOL_UNDERNET),
291     const_iv (PROTOCOL_UNREAL), const_iv (PROTOCOL_SOLIDIRCD), const_iv (PROTOCOL_NEFARIOUS), const_iv (PROTOCOL_OFFICEIRC),
292     const_iv (PROTOCOL_OTHER),
293    
294     const_iv (IRCD_CIDR_BANS),
295    
296     const_iv (FNC_REGAIN), const_iv (FNC_FORCE),
297    
298     // privs.h
299     const_iv (OPERCLASS_NEEDOPER), const_iv (SOPER_CONF),
300    
301     // sasl.h
302     const_iv (ASASL_FAIL), const_iv (ASASL_MORE), const_iv (ASASL_DONE), const_iv (ASASL_MARKED_FOR_DELETION),
303     const_iv (ASASL_NEED_LOG),
304    
305     // servers.h
306     const_iv (SF_HIDE), const_iv (SF_EOB), const_iv (SF_EOB2), const_iv (SF_JUPE_PENDING),
307    
308     // tools.h
309     const_iv (EMAIL_REGISTER), const_iv (EMAIL_SENDPASS), const_iv (EMAIL_SETEMAIL),
310     const_iv (EMAIL_MEMO), const_iv (EMAIL_SETPASS),
311    
312     const_iv (LG_NONE), const_iv (LG_INFO), const_iv (LG_ERROR), const_iv (LG_IOERROR), const_iv (LG_DEBUG),
313     const_iv (LG_CMD_ADMIN), const_iv (LG_CMD_REGISTER), const_iv (LG_CMD_SET), const_iv (LG_CMD_DO),
314     const_iv (LG_CMD_LOGIN), const_iv (LG_CMD_GET), const_iv (LG_NETWORK), const_iv (LG_WALLOPS),
315     const_iv (LG_RAWDATA), const_iv (LG_REGISTER), const_iv (LG_WARN1), const_iv (LG_WARN2),
316     const_iv (LG_CMD_ALL), const_iv (LG_ALL),
317    
318     const_iv (CMDLOG_ADMIN), const_iv (CMDLOG_REGISTER), const_iv (CMDLOG_SET), const_iv (CMDLOG_DO),
319     const_iv (CMDLOG_LOGIN), const_iv (CMDLOG_GET),
320    
321     // users.h
322     const_iv (UF_AWAY), const_iv (UF_INVIS), const_iv (UF_IRCOP),
323     const_iv (UF_ADMIN), const_iv (UF_SEENINFO), const_iv (UF_HIDEHOSTREQ),
324    
325     // account/chanacs.h
326     const_iv (CA_VOICE), const_iv (CA_AUTOVOICE), const_iv (CA_OP), const_iv (CA_AUTOOP), const_iv (CA_TOPIC),
327     const_iv (CA_SET), const_iv (CA_REMOVE), const_iv (CA_INVITE), const_iv (CA_RECOVER), const_iv (CA_FLAGS),
328     const_iv (CA_HALFOP), const_iv (CA_AUTOHALFOP), const_iv (CA_ACLVIEW), const_iv (CA_AKICK), const_iv (CA_NONE),
329     const_iv (CA_VOP_DEF), const_iv (CA_HOP_DEF), const_iv (CA_AOP_DEF), const_iv (CA_SOP_DEF), const_iv (CA_SUCCESSOR_0),
330     const_iv (CA_FOUNDER_0), const_iv (CA_INITIAL), const_iv (CA_USEDUPDATE), const_iv (CA_ALLPRIVS),
331     const_iv (CA_ALL_ALL), const_iv (OLD_CA_AOP),
332     const_iv (SHRIKE_CA_VOP), const_iv (SHRIKE_CA_AOP), const_iv (SHRIKE_CA_SOP), const_iv (SHRIKE_CA_FOUNDER),
333     const_iv (SHRIKE_CA_SUCCESSOR),
334    
335     // account/mychan.h
336     const_iv (MC_HOLD), const_iv (MC_NOOP), const_iv (MC_RESTRICTED), const_iv (MC_SECURE), const_iv (MC_VERBOSE),
337     const_iv (MC_STAFFONLY), const_iv (MC_KEEPTOPIC), const_iv (MC_VERBOSE_OPS), const_iv (MC_TOPICLOCK),
338     const_iv (MC_GUARD), const_iv (MC_INHABIT), const_iv (MC_MLOCK_CHECK), const_iv (MC_FORCEVERBOSE),
339     const_iv (MC_VERBOSE_MASK),
340    
341     // account/mymemo.h
342     const_iv (MEMO_READ), const_iv (MEMO_CHANNEL), const_iv (MEMO_MAX_NUM), const_iv (MEMO_MAX_TIME),
343    
344     // account/myuser.h
345     const_iv (MU_HOLD), const_iv (MU_NEVEROP), const_iv (MU_NOOP), const_iv (MU_WAITAUTH), const_iv (MU_HIDEMAIL),
346     const_iv (MU_OLD_ALIAS), const_iv (MU_NOMEMO), const_iv (MU_EMAILMEMOS), const_iv (MU_CRYPTPASS),
347     const_iv (MU_OLD_SASL), const_iv (MU_NOBURSTLOGIN)
348     };
349     #undef const_iv
350    
351     for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
352     newCONSTSUB (stash_svs, civ->name, newSViv (civ->iv));
353     }
354    
355     void slog (unsigned level, char *msg)
356    
357     char *gen_pw (int sz)
358    
359     void tb2sp (char *line)
360    
361     char *replace (char *s, int size, char const *src, char const *dest)
362    
363     #--------------------------------------------------
364     # char *itoa (int num)
365     # CODE:
366     # RETVAL = itoa<char> (num);
367     # OUTPUT:
368     # RETVAL
369     #
370     # int validemail (char *email)
371     #
372     # bool validhostmask (char *host)
373     #
374     # char const *sbytes (float x)
375     #
376     # float bytes (float x)
377     #
378     # unsigned long makekey ()
379     #
380     # unsigned int shash (unsigned char const *text)
381     #
382     # char *time_ago (time_t event)
383     #
384     # char *timediff (time_t seconds)
385     #
386     # int sjtoken (char *message, char delimiter, AV *parv)
387     # CODE:
388     # {
389     # char *cparv[256];
390     # int i;
391     # RETVAL = sjtoken (message, delimiter, cparv);
392     #
393     # /if (!parv)
394     # {
395     # parv = newAV ();
396     # av_extend (parv, RETVAL);
397     # }
398     #
399     # for (i = 0; i < RETVAL; i++)
400     # av_push (parv, newSVpv (cparv[i], 0));
401     # }
402     # OUTPUT:
403     # RETVAL
404     #
405     # char const *uinttobase64 (char *buf, uint64_t v, int64_t count)
406     #
407     # unsigned int base64touint (char const *buf)
408     #
409     #
410     # MODULE = svs PACKAGE = svs::kline_t
411     #
412     # kline_t *
413     # new (char const *klass, char *user, char *host, char *reason, long duration)
414     # CODE:
415     # RETVAL = kline_t::create (user, host, reason, duration);
416     # OUTPUT:
417     # RETVAL
418     #
419     # void
420     # destroy (char const *klass, char const *user, char const *host)
421     # CODE:
422     # kline_t::destroy (user, host);
423     #
424     # kline_t *
425     # find (char const *klass, char const *user, char const *host)
426     # CODE:
427     # RETVAL = kline_t::find (user, host);
428     # OUTPUT:
429     # RETVAL
430     #
431     # kline_t *
432     # find_num (char const *klass, unsigned number)
433     # CODE:
434     # RETVAL = kline_t::find (number);
435     # OUTPUT:
436     # RETVAL
437     #
438     # kline_t *
439     # find_user (char const *klass, user_t *u)
440     # CODE:
441     # RETVAL = kline_t::find (u);
442     # OUTPUT:
443     # RETVAL
444     #
445     # INCLUDE: $PERL $SRCDIR/svsperl/genacc kline_t $SRCDIR/../../include/account/kline.h |
446     #
447     #
448     # MODULE = svs PACKAGE = svs::server_t
449     #
450     # server_t *
451     # new (char const *klass, char const *name, unsigned int hops, char const *uplink, char const *id, char const *desc)
452     # CODE:
453     # RETVAL = server_t::create (name, hops, uplink, id, desc);
454     # OUTPUT:
455     # RETVAL
456     #
457     # server_t *
458     # find (char const *klass, char const *name)
459     # CODE:
460     # RETVAL = server_t::find (name);
461     # OUTPUT:
462     # RETVAL
463     #
464     # void
465     # destroy (char const *klass, char const *name)
466     # CODE:
467     # server_delete (name);
468     #
469     # INCLUDE: $PERL $SRCDIR/svsperl/genacc server_t $SRCDIR/../../include/servers.h |
470     #
471     #
472     # MODULE = svs PACKAGE = svs::svsignore_t
473     #
474     # svsignore_t *
475     # new (char const *klass, char *mask, char *reason)
476     # CODE:
477     # RETVAL = svsignore_add (mask, reason);
478     # OUTPUT:
479     # RETVAL
480     #
481     # void
482     # svsignore_t::DESTROY ()
483     # CODE:
484     # svsignore_delete (THIS);
485     #
486     # svsignore_t *
487     # find (char const *klass, user_t *u)
488     # CODE:
489     # RETVAL = svsignore_find (u);
490     # OUTPUT:
491     # RETVAL
492     #
493     # INCLUDE: $PERL $SRCDIR/svsperl/genacc svsignore_t $SRCDIR/../../include/account/svsignore.h |
494     #
495     #
496     # MODULE = svs PACKAGE = svs::user_t
497     #
498     # user_t *
499     # new (char const *klass, char const *nick, char const *user, char const *host, char const *vhost, char const *ip, char const *uid, char const *gecos, server_t *server, time_t ts)
500     # CODE:
501     # RETVAL = user_t::create (nick, user, host, vhost, ip, uid, gecos, server, ts);
502     # OUTPUT:
503     # RETVAL
504     #
505     # void
506     # user_t::DESTROY ()
507     # CODE:
508     # user_delete (THIS);
509     #
510     # user_t *
511     # find (char const *klass, char const *nick)
512     # CODE:
513     # RETVAL = user_t::find (nick);
514     # OUTPUT:
515     # RETVAL
516     #
517     # user_t *
518     # find_named (char const *klass, char const *nick)
519     # CODE:
520     # RETVAL = user_t::find_named (nick);
521     # OUTPUT:
522     # RETVAL
523     #
524     # INCLUDE: $PERL $SRCDIR/svsperl/genacc user_t $SRCDIR/../../include/users.h |
525     #--------------------------------------------------