ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/contrib/os_testcmd.C
Revision: 1.3
Committed: Tue Aug 28 17:08:09 2007 UTC (19 years, 2 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.2: +19 -19 lines
Log Message:
- changed name
- updated the example config to the new system
- added more documentation
- enhanced documentation generators
- added a link to the pdf to the website
- added an RSS feed generator
- transitioned hooks to c++ callbacks
- did various merges with upstream along the way
- added const where appropriate
- removed the old block allocator
- fixed most memory leaks
- transitioned some dictionaries to std::map
- transitioned some lists to std::vector
- made some free functions members where appropriate
- renamed string to dynstr and added a static string ststr
- use NOW instead of time (NULL) if possible
- completely reworked database backends, crypto handlers and protocol handlers
  to use an object factory
- removed the old module system. ermyth does not do any dynamic loading anymore
- fixed most of the build system
- reworked how protocol commands work

File Contents

# User Rev Content
1 pippijn 1.1 /*
2 pippijn 1.3 * Copyright © 2006 Jilles Tjoelker, et al
3 pippijn 1.2 * Rights to this code are as documented in doc/pod/license.pod.
4 pippijn 1.1 *
5     * Calls a command without a user_t.
6     *
7 pippijn 1.3 * $Id: os_testcmd.C,v 1.2 2007-07-21 01:29:08 pippijn Exp $
8 pippijn 1.1 */
9    
10     #include "atheme.h"
11 pippijn 1.3 #include <ermyth/module.h>
12 pippijn 1.1
13 pippijn 1.3 static char const rcsid[] = "$Id$";
14    
15     REGISTER_MODULE ("operserv/testcmd", false, "The Ermyth Team <http://ermyth.schmorp.de>");
16 pippijn 1.1
17     struct testcmddata
18     {
19     sourceinfo_t *prevsi;
20     bool got_result;
21     };
22    
23     static void os_cmd_testcmd (sourceinfo_t *si, int parc, char *parv[]);
24    
25 pippijn 1.3 static void testcmd_command_fail (sourceinfo_t *si, faultcode_t code, char const * const message);
26     static void testcmd_command_success_nodata (sourceinfo_t *si, char const * const message);
27     static void testcmd_command_success_string (sourceinfo_t *si, char const * const result, char const * const message);
28 pippijn 1.1
29 pippijn 1.3 command_t const os_testcmd = { "TESTCMD", "Executes a command without a user_t.", AC_NONE, 3, os_cmd_testcmd };
30 pippijn 1.1
31 pippijn 1.3 E cmdvec os_cmdtree;
32     E helpvec os_helptree;
33 pippijn 1.1
34     sourceinfo_vtable testcmd_vtable = { "testcmd", testcmd_command_fail, testcmd_command_success_nodata, testcmd_command_success_string };
35    
36 pippijn 1.3 bool
37     _modinit (module *m)
38 pippijn 1.1 {
39     os_cmdtree << os_testcmd;
40     help_addentry (os_helptree, "TESTCMD", "help/operserv/testcmd", NULL);
41 pippijn 1.3
42     return true;
43 pippijn 1.1 }
44    
45     void
46     _moddeinit ()
47     {
48     os_cmdtree >> os_testcmd;
49     help_delentry (os_helptree, "TESTCMD");
50     }
51    
52     static void
53 pippijn 1.3 testcmd_command_fail (sourceinfo_t *si, faultcode_t code, char const * const message)
54 pippijn 1.1 {
55     testcmddata *udata = static_cast<testcmddata *> (si->callerdata);
56    
57     command_success_nodata (udata->prevsi, "Command failed with fault %d, \"%s\"", code, message);
58     udata->got_result = true;
59     }
60    
61     static void
62 pippijn 1.3 testcmd_command_success_nodata (sourceinfo_t *si, char const * const message)
63 pippijn 1.1 {
64     testcmddata *udata = static_cast<testcmddata *> (si->callerdata);
65    
66     if (udata->got_result)
67     command_success_nodata (udata->prevsi, "More comment \"%s\"", message);
68     else
69     command_success_nodata (udata->prevsi, "Command succeeded with no data, \"%s\"", message);
70     udata->got_result = true;
71     }
72    
73     static void
74 pippijn 1.3 testcmd_command_success_string (sourceinfo_t *si, char const * const result, char const * const message)
75 pippijn 1.1 {
76     testcmddata *udata = static_cast<testcmddata *> (si->callerdata);
77    
78     command_success_nodata (udata->prevsi, "Command succeeded with string \"%s\", \"%s\"", result, message);
79     udata->got_result = true;
80     }
81    
82     static void
83     os_cmd_testcmd (sourceinfo_t *si, int parc, char *parv[])
84     {
85     service_t *svs;
86     command_t const *cmd;
87     sourceinfo_t newsi;
88     testcmddata udata;
89     int newparc;
90     char *newparv[256];
91    
92     if (parc < 2)
93     {
94     command_fail (si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "TESTCMD");
95     command_fail (si, fault_needmoreparams, "Syntax: TESTCMD <service> <command> [arguments]");
96     return;
97     }
98    
99     svs = find_service (parv[0]);
100     if (svs == NULL)
101     {
102     command_fail (si, fault_nosuch_target, "No such service \2%s\2", parv[0]);
103     return;
104     }
105     if (svs->cmdtree == NULL)
106     {
107     command_fail (si, fault_noprivs, "Service \2%s\2 has no commands", svs->name);
108     return;
109     }
110     cmd = svs->cmdtree->find (parv[1]);
111     if (cmd == NULL)
112     {
113     command_fail (si, fault_nosuch_key, "No such command \2%s\2 in service \2%s\2", parv[1], svs->name);
114     return;
115     }
116     udata.prevsi = si;
117     udata.got_result = false;
118     memset (newparv, '\0', sizeof newparv);
119     if (parc >= 3)
120     newparc = sjtoken (parv[2], ';', newparv);
121     else
122     newparc = 0;
123     memset (&newsi, '\0', sizeof newsi);
124     newsi.smu = si->smu;
125     if (si->su != NULL)
126     newsi.sourcedesc = si->su->ip[0] != '\0' ? si->su->ip : si->su->host;
127     else
128     newsi.sourcedesc = si->sourcedesc;
129     newsi.service = svs;
130     newsi.v = &testcmd_vtable;
131     newsi.callerdata = &udata;
132     cmd->exec (svs, &newsi, newparc, newparv);
133     if (!udata.got_result)
134     command_success_nodata (si, "Command returned without giving a result");
135     }