irc-metadata.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #define __irc_metadata_c__
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * IRC metadata commands
  16. */
  17. #include <assert.h>
  18. #include <strings.h>
  19. #include <stdio.h>
  20. #include "conn-func.h"
  21. #include "channel.h"
  22. #include "irc-macros.h"
  23. #include "irc-write.h"
  24. #include "log.h"
  25. #include "messages.h"
  26. #include "parse.h"
  27. #include "irc-metadata.h"
  28. /**
  29. * Handler for the IRC+ "METADATA" command.
  30. *
  31. * @param Client The client from which this command has been received.
  32. * @param Req Request structure with prefix and all parameters.
  33. * @returns CONNECTED or DISCONNECTED.
  34. */
  35. GLOBAL bool
  36. IRC_METADATA(CLIENT *Client, REQUEST *Req)
  37. {
  38. CLIENT *prefix, *target;
  39. char new_flags[COMMAND_LEN];
  40. assert(Client != NULL);
  41. assert(Req != NULL);
  42. _IRC_REQUIRE_PREFIX_OR_RETURN_(Client, Req)
  43. prefix = Client_Search(Req->prefix);
  44. if (!prefix)
  45. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  46. Client_ID(Client), Req->prefix);
  47. target = Client_Search(Req->argv[0]);
  48. if (!target)
  49. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  50. Client_ID(Client), Req->argv[0]);
  51. LogDebug("Got \"METADATA\" command from \"%s\" for client \"%s\": \"%s=%s\".",
  52. Client_ID(prefix), Client_ID(target),
  53. Req->argv[1], Req->argv[2]);
  54. /* Mark client: it has received a METADATA command */
  55. if (!Client_HasFlag(target, 'M')) {
  56. snprintf(new_flags, sizeof new_flags, "%sM",
  57. Client_Flags(target));
  58. Client_SetFlags(target, new_flags);
  59. }
  60. if (strcasecmp(Req->argv[1], "cloakhost") == 0) {
  61. /* Set or remove a "cloaked hostname". */
  62. Client_UpdateCloakedHostname(target, prefix,
  63. *Req->argv[2] ? Req->argv[2] : NULL);
  64. if (Client_Conn(target) > NONE && Client_HasMode(target, 'x'))
  65. IRC_WriteStrClientPrefix(target, prefix,
  66. RPL_HOSTHIDDEN_MSG, Client_ID(target),
  67. Client_HostnameDisplayed(target));
  68. /* The Client_UpdateCloakedHostname() function already
  69. * forwarded the METADATA command, don't do it twice: */
  70. return CONNECTED;
  71. }
  72. else if (*Req->argv[2] && strcasecmp(Req->argv[1], "host") == 0) {
  73. Client_SetHostname(target, Req->argv[2]);
  74. if (Client_Conn(target) > NONE && !Client_HasMode(target, 'x'))
  75. IRC_WriteStrClientPrefix(target, prefix,
  76. RPL_HOSTHIDDEN_MSG, Client_ID(target),
  77. Client_HostnameDisplayed(target));
  78. } else if (strcasecmp(Req->argv[1], "info") == 0)
  79. Client_SetInfo(target, Req->argv[2]);
  80. else if (*Req->argv[2] && strcasecmp(Req->argv[1], "user") == 0)
  81. Client_SetUser(target, Req->argv[2], true);
  82. else if (strcasecmp(Req->argv[1], "accountname") == 0)
  83. Client_SetAccountName(target, Req->argv[2]);
  84. else if (*Req->argv[2] && strcasecmp(Req->argv[1], "certfp") == 0)
  85. Conn_SetCertFp(Client_Conn(target), Req->argv[2]);
  86. else
  87. Log(LOG_WARNING,
  88. "Ignored metadata update from \"%s\" for client \"%s\": \"%s=%s\" - unknown key!",
  89. Client_ID(Client), Client_ID(target),
  90. Req->argv[1], Req->argv[2]);
  91. /* Forward the METADATA command to peers that support it: */
  92. IRC_WriteStrServersPrefixFlag(Client, prefix, 'M', "METADATA %s %s :%s",
  93. Client_ID(target), Req->argv[1], Req->argv[2]);
  94. return CONNECTED;
  95. }