irc-macros.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2015 Alexander Barton (alex@barton.de) and Contributors.
  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. #ifndef __irc_macros_h__
  12. #define __irc_macros_h__
  13. /**
  14. * @file
  15. * Macros for functions that handle IRC commands.
  16. */
  17. /**
  18. * Make sure that number of passed parameters is equal to Count.
  19. *
  20. * If there are not exactly Count parameters, send an error to the client and
  21. * return from the function.
  22. */
  23. #define _IRC_ARGC_EQ_OR_RETURN_(Client, Req, Count) \
  24. if (Req->argc != Count) { \
  25. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  26. Client_ID(Client), Req->command); \
  27. }
  28. /**
  29. * Make sure that number of passed parameters is less or equal than Max.
  30. *
  31. * If there are more than Max parameters, send an error to the client and
  32. * return from the function.
  33. */
  34. #define _IRC_ARGC_LE_OR_RETURN_(Client, Req, Max) \
  35. if (Req->argc > Max) { \
  36. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  37. Client_ID(Client), Req->command); \
  38. }
  39. /**
  40. * Make sure that number of passed parameters is greater or equal than Min.
  41. *
  42. * If there aren't at least Min parameters, send an error to the client and
  43. * return from the function.
  44. */
  45. #define _IRC_ARGC_GE_OR_RETURN_(Client, Req, Min) \
  46. if (Req->argc < Min) { \
  47. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  48. Client_ID(Client), Req->command); \
  49. }
  50. /**
  51. * Make sure that number of passed parameters is in between Min and Max.
  52. *
  53. * If there aren't at least Min parameters or if there are more than Max
  54. * parameters, send an error to the client and return from the function.
  55. */
  56. #define _IRC_ARGC_BETWEEN_OR_RETURN_(Client, Req, Min, Max) \
  57. if (Req->argc < Min || Req->argc > Max) { \
  58. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  59. Client_ID(Client), Req->command); \
  60. }
  61. /**
  62. * Make sure that the command has a prefix.
  63. *
  64. * If there is no prefix, send an error to the client and return from
  65. * the function.
  66. */
  67. #define _IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req) \
  68. if (!Req->prefix) { \
  69. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  70. Client_ID(Client), Req->command); \
  71. }
  72. /**
  73. * Get sender of an IRC command.
  74. *
  75. * The sender is either stored in the prefix if the command has been
  76. * received from a server or set to the client. If the sender is invalid,
  77. * send an error to the client and return from the function.
  78. */
  79. #define _IRC_GET_SENDER_OR_RETURN_(Sender, Req, Client) \
  80. if (Client_Type(Client) == CLIENT_SERVER) { \
  81. if (!Req->prefix) \
  82. return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  83. Client_ID(Client), Req->command); \
  84. Sender = Client_Search(Req->prefix); \
  85. } else \
  86. Sender = Client; \
  87. if (!Sender) \
  88. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG, \
  89. Client_ID(Client), \
  90. Req->prefix ? Req->prefix : "(none)");
  91. /**
  92. * Get target of an IRC command and make sure that it is a server.
  93. *
  94. * Set the target to the local server if no target parameter is given in the
  95. * received command, and send an error to the client and return from the
  96. * function if the given target isn't resolvable to a server: the target
  97. * parameter can be a server name, a nick name (then the target is set to
  98. * the server to which this nick is connected), or a mask matching at least
  99. * one server name in the network.
  100. */
  101. #define _IRC_GET_TARGET_SERVER_OR_RETURN_(Target, Req, Argc, From) \
  102. if (Req->argc > Argc) { \
  103. Target = Client_Search(Req->argv[Argc]); \
  104. if (!Target) \
  105. Target = Client_SearchServer(Req->argv[Argc]); \
  106. if (!Target) \
  107. return IRC_WriteErrClient(From, ERR_NOSUCHSERVER_MSG, \
  108. Client_ID(From), Req->argv[Argc]); \
  109. if (Client_Type(Target) != CLIENT_SERVER) \
  110. Target = Client_Introducer(Target); \
  111. } else \
  112. Target = Client_ThisServer();
  113. #endif /* __irc_macros_h__ */
  114. /* -eof- */