irc-metadata.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. Client_UpdateCloakedHostname(target, prefix, Req->argv[2]);
  62. if (Client_Conn(target) > NONE && Client_HasMode(target, 'x'))
  63. IRC_WriteStrClientPrefix(target, prefix,
  64. RPL_HOSTHIDDEN_MSG, Client_ID(target),
  65. Client_HostnameDisplayed(target));
  66. /* The Client_UpdateCloakedHostname() function already
  67. * forwarded the METADATA command, don't do it twice: */
  68. return CONNECTED;
  69. }
  70. else if (*Req->argv[2] && strcasecmp(Req->argv[1], "host") == 0) {
  71. Client_SetHostname(target, Req->argv[2]);
  72. if (Client_Conn(target) > NONE && !Client_HasMode(target, 'x'))
  73. IRC_WriteStrClientPrefix(target, prefix,
  74. RPL_HOSTHIDDEN_MSG, Client_ID(target),
  75. Client_HostnameDisplayed(target));
  76. } else if (strcasecmp(Req->argv[1], "info") == 0)
  77. Client_SetInfo(target, Req->argv[2]);
  78. else if (*Req->argv[2] && strcasecmp(Req->argv[1], "user") == 0)
  79. Client_SetUser(target, Req->argv[2], true);
  80. else if (strcasecmp(Req->argv[1], "accountname") == 0)
  81. Client_SetAccountName(target, Req->argv[2]);
  82. else if (*Req->argv[2] && strcasecmp(Req->argv[1], "certfp") == 0)
  83. Conn_SetCertFp(Client_Conn(target), Req->argv[2]);
  84. else
  85. Log(LOG_WARNING,
  86. "Ignored metadata update from \"%s\" for client \"%s\": \"%s=%s\" - unknown key!",
  87. Client_ID(Client), Client_ID(target),
  88. Req->argv[1], Req->argv[2]);
  89. /* Forward the METADATA command to peers that support it: */
  90. IRC_WriteStrServersPrefixFlag(Client, prefix, 'M', "METADATA %s %s :%s",
  91. Client_ID(target), Req->argv[1], Req->argv[2]);
  92. return CONNECTED;
  93. }