irc-metadata.c 3.4 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. #define __irc_metadata_c__
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * IRC metadata commands
  16. */
  17. #include "imp.h"
  18. #include <assert.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include "conn-func.h"
  22. #include "channel.h"
  23. #include "conn-encoding.h"
  24. #include "irc-write.h"
  25. #include "log.h"
  26. #include "messages.h"
  27. #include "parse.h"
  28. #include "tool.h"
  29. #include "exp.h"
  30. #include "irc-metadata.h"
  31. /**
  32. * Handler for the IRC+ "METADATA" command.
  33. *
  34. * @param Client The client from which this command has been received.
  35. * @param Req Request structure with prefix and all parameters.
  36. * @returns CONNECTED or DISCONNECTED.
  37. */
  38. GLOBAL bool
  39. IRC_METADATA(CLIENT *Client, REQUEST *Req)
  40. {
  41. CLIENT *prefix, *target;
  42. char new_flags[COMMAND_LEN];
  43. assert(Client != NULL);
  44. assert(Req != NULL);
  45. prefix = Client_Search(Req->prefix);
  46. if (!prefix)
  47. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  48. Client_ID(Client), Req->prefix);
  49. target = Client_Search(Req->argv[0]);
  50. if (!target)
  51. return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
  52. Client_ID(Client), Req->argv[0]);
  53. LogDebug("Got \"METADATA\" command from \"%s\" for client \"%s\": \"%s=%s\".",
  54. Client_ID(prefix), Client_ID(target),
  55. Req->argv[1], Req->argv[2]);
  56. /* Mark client: it has received a METADATA command */
  57. if (!Client_HasFlag(target, 'M')) {
  58. snprintf(new_flags, sizeof new_flags, "%sM",
  59. Client_Flags(target));
  60. Client_SetFlags(target, new_flags);
  61. }
  62. if (strcasecmp(Req->argv[1], "cloakhost") == 0) {
  63. Client_UpdateCloakedHostname(target, prefix, Req->argv[2]);
  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. }