irc-macros.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. 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. * Get sender of an IRC command.
  63. *
  64. * The sender is either stored in the prefix if the command has been
  65. * received from a server or set to the client. If the sender is invalid,
  66. * send an error to the client and return from the function.
  67. */
  68. #define _IRC_GET_SENDER_OR_RETURN_(Sender, Req, Client) \
  69. if (Client_Type(Client) == CLIENT_SERVER) \
  70. Sender = Client_Search(Req->prefix); \
  71. else \
  72. Sender = Client; \
  73. if (!Sender) \
  74. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG, \
  75. Client_ID(Client), Req->prefix);
  76. /**
  77. * Get target of an IRC command and make sure that it is a server.
  78. *
  79. * Set the target to the local server if no target parameter is given in the
  80. * received command, and send an error to the client and return from the
  81. * function if the given target isn't resolvable to a server: the target
  82. * parameter can be a server name, a nick name (then the target is set to
  83. * the server to which this nick is connected), or a mask matching at least
  84. * one server name in the network.
  85. */
  86. #define _IRC_GET_TARGET_SERVER_OR_RETURN_(Target, Req, Argc, From) \
  87. if (Req->argc > Argc) { \
  88. Target = Client_Search(Req->argv[Argc]); \
  89. if (!Target) \
  90. Target = Client_SearchServer(Req->argv[Argc]); \
  91. if (!Target) \
  92. return IRC_WriteErrClient(From, ERR_NOSUCHSERVER_MSG, \
  93. Client_ID(From), Req->argv[Argc]); \
  94. if (Client_Type(Target) != CLIENT_SERVER) \
  95. Target = Client_Introducer(Target); \
  96. } else \
  97. Target = Client_ThisServer();
  98. #endif /* __irc_macros_h__ */
  99. /* -eof- */