summaryrefslogtreecommitdiff
path: root/contrib/example_module.c
blob: af62ee9c94a1b6b72913acf827d4906847e7bc6a (plain)
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
/************************************************************************
 *   IRC - Internet Relay Chat, doc/example_module.c
 *   Copyright (C) 2001 Hybrid Development Team
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 1, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   $Id$
 */

/* List of ircd includes from ../include/ 
 * These ones are necessary to build THIS module...
 */

#include "stdinc.h" /* includes setup.h */

#include "client.h" /* Required for IsClient, etc. */

#include "send.h" /* sendto_one, most useful function of all time */

#include "parse.h"

#include "modules.h" /* includes msg.h; use for the msgtab */

/* OTHER USEFUL INCLUDES:
 * 
 * #include "handlers.h" <-- include this file to be able to use default
 * functions in place of your own 'Access Denied' kind of function
 * 
 * #include "numeric.h" <-- include this file to be able to use form_str,
 * standard message formats (see messages.tab and *.lang in messages/)
 * Examples are strewn all across the ircd code, so just grep a bit to
 * find one!
 *
 * #include "irc_string.h" <-- best to include this if you use *any*
 * string comparison or parsing functions, although they may be available
 * natively for your OS the prototypes in irc_string.h may be required for
 * others. */

/*
 * Declare the void's initially up here, as modules don't have an
 * include file, we will normally have client_p, source_p, parc
 * and parv[] where:
 *
 * client_p == client issuing command
 * source_p == where the command came from
 * parc     == the number of parameters
 * parv     == an array of the parameters
 */


/*
 * mr_test
 *      parv[0] = sender prefix
 *      parv[1] = parameter
 */

/*
 * Here we have the functions themselves that we declared above,
 * and the fairly normal C coding
 */
static void
mr_test(struct Client *client_p, struct Client *source_p,
        int parc, char *parv[])
{
  if (parc == 1)
    sendto_one(source_p, ":%s NOTICE %s :You are unregistered and sent no parameters",
               me.name, source_p->name);
  else
    sendto_one(source_p, ":%s NOTICE %s :You are unregistered and sent parameter: %s", 
               me.name, source_p->name, parv[1]);
}

/*
 * m_test
 *      parv[0] = sender prefix
 *      parv[1] = parameter
 */
static void
m_test(struct Client *client_p, struct Client *source_p,
       int parc, char *parv[])
{
  if (parc == 1)
    sendto_one(source_p, ":%s NOTICE %s :You are a normal user, and sent no parameters",
               me.name, source_p->name);
  else
    sendto_one(source_p, ":%s NOTICE %s :You are a normal user, and send parameters: %s",
               me.name, source_p->name, parv[1]);
}

/*
 * ms_test
 *      parv[0] = sender prefix
 *      parv[1] = parameter
 */
static void
ms_test(struct Client *client_p, struct Client *source_p,
        int parc, char *parv[])
{
  if (parc == 1)
  {
    if (IsServer(source_p))
      sendto_one(source_p, ":%s NOTICE %s :You are a server, and sent no parameters",
                 me.name, source_p->name);
    else
      sendto_one(source_p, ":%s NOTICE %s :You are a remote client, and sent no parameters",
                 me.name, source_p->name);
  }
  else
  {
    if (IsServer(source_p))
      sendto_one(source_p, ":%s NOTICE %s :You are a server, and sent parameters: %s",
                 me.name, source_p->name, parv[1]);
    else
      sendto_one(source_p, ":%s NOTICE %s :You are a remote client, and sent parameters: %s",
                 me.name, source_p->name, parv[1]);
  }
}

/*
 * mo_test
 *      parv[0] = sender prefix
 *      parv[1] = parameter
 */
static void
mo_test(struct Client *client_p, struct Client *source_p,
        int parc, char *parv[])
{
  if (parc == 1)
    sendto_one(source_p, ":%s NOTICE %s :You are an operator, and sent no parameters",
               me.name, source_p->name);
  else
    sendto_one(source_p, ":%s NOTICE %s :You are an operator, and sent parameters: %s",
               me.name, source_p->name, parv[1]);
}

/*
 * Show the commands this module can handle in a msgtab
 * and give the msgtab a name, here its test_msgtab
 */
static struct Message test_msgtab = {

 /* Fields are in order:
  *-> "COMMAND", 0, 0, parc_count, maxparc, MFLG_SLOW, 0,
  *
  * where:
  * COMMAND == the /command you want
  * parc_count == the number of parameters needed
  *               (the clients name is one param, parv[0])
  * maxparc == the maximum parameters we allow
  * the 0's and MFLG_SLOW should not be changed..
  */

 /*
  * This would add the command "TEST" which requires no additional
  * parameters
  */
  "TEST", 0, 0, 1, MAXPARA, MFLG_SLOW, 0,

 /* Fields are in order:
  *-> {unregged, regged, remote, encap, oper, dummy}
  *
  * where:
  * unregged == function to call for unregistered clients
  * regged == function to call for normal users
  * remote == function to call for servers/remote users
  * encap == function to call for encap'd server/remote commands
  * oper == function to call for operators
  * dummy == function called when client is quarantined
  *
  * There are also some pre-coded functions for use:
  * m_unregistered: prevent the client using this if unregistered
  * m_not_oper:     tell the client it requires being an operator
  * m_ignore:       ignore the command when it comes from certain types
  * rfc1459_command_send_error: give an error when the command comes from certain types
  */
  { mr_test, m_test, ms_test, m_ignore, mo_test, m_ignore }

 /* It is normal for unregistered functions to be prefixed with mr_
  *   "      "       normal users to be prefixed with m_
  *   "      "       remote clients to be prefixed with ms_
  *   "      "       operators to be prefixed with mo_
  */
};
/* That's the msgtab finished */

/* Here we tell it what to do when the module is loaded */
static void
module_init(void)
{
  /* This will add the commands in test_msgtab (which is above) */
  mod_add_cmd(&test_msgtab);
}

/* here we tell it what to do when the module is unloaded */
static void
module_exit(void)
{
  /* This will remove the commands in test_msgtab (which is above) */
  mod_del_cmd(&test_msgtab);
}

struct module module_entry = {
  .node    = { NULL, NULL, NULL },
  .name    = NULL,
  .version = "$Revision$",
  .handle  = NULL,
  .modinit = module_init,
  .modexit = module_exit,
  .flags   = 0
};

/* END OF EXAMPLE MODULE */