irc-macros.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2013 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. IRC_SetPenalty(Client, 2); \
  26. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  27. Client_ID(Client), Req->command); \
  28. }
  29. /**
  30. * Make sure that number of passed parameters is less or equal than Max.
  31. *
  32. * If there are more than Max parameters, send an error to the client and
  33. * return from the function.
  34. */
  35. #define _IRC_ARGC_LE_OR_RETURN_(Client, Req, Max) \
  36. if (Req->argc > Max) { \
  37. IRC_SetPenalty(Client, 2); \
  38. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  39. Client_ID(Client), Req->command); \
  40. }
  41. /**
  42. * Make sure that number of passed parameters is greater or equal than Min.
  43. *
  44. * If there aren't at least Min parameters, send an error to the client and
  45. * return from the function.
  46. */
  47. #define _IRC_ARGC_GE_OR_RETURN_(Client, Req, Min) \
  48. if (Req->argc < Min) { \
  49. IRC_SetPenalty(Client, 2); \
  50. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  51. Client_ID(Client), Req->command); \
  52. }
  53. /**
  54. * Make sure that number of passed parameters is in between Min and Max.
  55. *
  56. * If there aren't at least Min parameters or if there are more than Max
  57. * parameters, send an error to the client and return from the function.
  58. */
  59. #define _IRC_ARGC_BETWEEN_OR_RETURN_(Client, Req, Min, Max) \
  60. if (Req->argc < Min || Req->argc > Max) { \
  61. IRC_SetPenalty(Client, 2); \
  62. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, \
  63. Client_ID(Client), Req->command); \
  64. }
  65. /**
  66. * Get sender of an IRC command.
  67. *
  68. * The sender is either stored in the prefix if the command has been
  69. * received from a server or set to the client. If the sender is invalid,
  70. * send an error to the client and return from the function.
  71. */
  72. #define _IRC_GET_SENDER_OR_RETURN_(Sender, Req, Client) \
  73. if (Client_Type(Client) == CLIENT_SERVER) \
  74. Sender = Client_Search(Req->prefix); \
  75. else \
  76. Sender = Client; \
  77. if (!Sender) \
  78. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, \
  79. Client_ID(Client), Req->prefix);
  80. /**
  81. * Get target of an IRC command and make sure that it is a server.
  82. *
  83. * Set the target to the local server if no target parameter is given in the
  84. * received command, and send an error to the client and return from the
  85. * function if the given target isn't resolvable to a server: the target
  86. * parameter can be a server name, a nick name (then the target is set to
  87. * the server to which this nick is connected), or a mask matching at least
  88. * one server name in the network.
  89. */
  90. #define _IRC_GET_TARGET_SERVER_OR_RETURN_(Target, Req, Argc, From) \
  91. if (Req->argc > Argc) { \
  92. Target = Client_Search(Req->argv[Argc]); \
  93. if (!Target) \
  94. Target = Client_SearchServer(Req->argv[Argc]); \
  95. if (!Target) \
  96. return IRC_WriteStrClient(From, ERR_NOSUCHSERVER_MSG, \
  97. Client_ID(From), Req->argv[Argc]); \
  98. if (Client_Type(Target) != CLIENT_SERVER) \
  99. Target = Client_Introducer(Target); \
  100. } else \
  101. Target = Client_ThisServer();
  102. #endif /* __irc_macros_h__ */
  103. /* -eof- */