]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-macros.h
Add new _IRC_ARGC_BETWEEN_OR_RETURN_ macro to irc-macros.h
[ngircd-alex.git] / src / ngircd / irc-macros.h
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 Alexander Barton (alex@barton.de).
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #ifndef __irc_macros_h__
13 #define __irc_macros_h__
14
15 /**
16  * @file
17  * Macros for functions that handle IRC commands.
18  */
19
20 /**
21  * Make sure that number of passed parameters is less or equal than Max.
22  *
23  * If there are more than Max parameters, send an error to the client and
24  * return from the function.
25  */
26 #define _IRC_ARGC_LE_OR_RETURN_(Client, Req, Max) \
27 if (Req->argc > Max) \
28         return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
29                                   Client_ID(Client), Req->command);
30
31 /**
32  * Make sure that number of passed parameters is greater or equal than Min.
33  *
34  * If there aren't at least Min parameters, send an error to the client and
35  * return from the function.
36  */
37 #define _IRC_ARGC_GE_OR_RETURN_(Client, Req, Min) \
38 if (Req->argc < Min) \
39         return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
40                                   Client_ID(Client), Req->command);
41
42 /**
43  * Make sure that number of passed parameters is in between Min and Max.
44  *
45  * If there aren't at least Min parameters or if there are more than Max
46  * parameters, send an error to the client and return from the function.
47  */
48 #define _IRC_ARGC_BETWEEN_OR_RETURN_(Client, Req, Min, Max) \
49 if (Req->argc < Min || Req->argc > Max) \
50         return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
51                                   Client_ID(Client), Req->command);
52
53 /**
54  * Get sender of an IRC command.
55  *
56  * The sender is either stored in the prefix if the command has been
57  * received from a server or set to the client. If the sender is invalid,
58  * send an error to the client and return from the function.
59  */
60 #define _IRC_GET_SENDER_OR_RETURN_(Sender, Req, Client) \
61         if (Client_Type(Client) == CLIENT_SERVER) \
62                 Sender = Client_Search(Req->prefix); \
63         else \
64                 Sender = Client; \
65         if (!Sender) \
66                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, \
67                                           Client_ID(Client), Req->prefix);
68
69 /**
70  * Get target of an IRC command and make sure that it is a server.
71  *
72  * Set the target to the local server if no target parameter is given in the
73  * received command, and send an error to the client and return from the
74  * function if the given target isn't resolvable to a server: the target
75  * parameter can be a server name, a nick name (then the target is set to
76  * the server to which this nick is connected), or a mask matching at least
77  * one server name in the network.
78  */
79 #define _IRC_GET_TARGET_SERVER_OR_RETURN_(Target, Req, Argc, From) \
80         if (Req->argc > Argc) { \
81                 Target = Client_Search(Req->argv[Argc]); \
82                 if (!Target) \
83                         Target = Client_SearchServer(Req->argv[Argc]); \
84                 if (!Target) \
85                         return IRC_WriteStrClient(From, ERR_NOSUCHSERVER_MSG, \
86                                           Client_ID(From), Req->argv[Argc]); \
87                 if (Client_Type(Target) != CLIENT_SERVER) \
88                         Target = Client_Introducer(Target); \
89         } else \
90                 Target = Client_ThisServer();
91
92 #endif  /* __irc_macros_h__ */
93
94 /* -eof- */