irc-metadata.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2012 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. if (Req->argc != 3)
  46. return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
  47. Client_ID(Client), Req->command);
  48. prefix = Client_Search(Req->prefix);
  49. if (!prefix)
  50. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  51. Client_ID(Client), Req->prefix);
  52. target = Client_Search(Req->argv[0]);
  53. if (!target)
  54. return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
  55. Client_ID(Client), Req->argv[0]);
  56. LogDebug("Got \"METADATA\" command from \"%s\" for client \"%s\": \"%s=%s\".",
  57. Client_ID(prefix), Client_ID(target),
  58. Req->argv[1], Req->argv[2]);
  59. /* Mark client: it has receiveda a METADATA command */
  60. if (!strchr(Client_Flags(target), 'M')) {
  61. snprintf(new_flags, sizeof new_flags, "%sM",
  62. Client_Flags(target));
  63. Client_SetFlags(target, new_flags);
  64. }
  65. if (strcasecmp(Req->argv[1], "cloakhost") == 0) {
  66. Client_UpdateCloakedHostname(target, prefix, Req->argv[2]);
  67. if (Client_Conn(target) > NONE && Client_HasMode(target, 'x'))
  68. IRC_WriteStrClientPrefix(target, prefix,
  69. RPL_HOSTHIDDEN_MSG, Client_ID(target),
  70. Client_HostnameDisplayed(target));
  71. /* The Client_UpdateCloakedHostname() function already
  72. * forwarded the METADATA command, don't do it twice: */
  73. return CONNECTED;
  74. }
  75. else if (*Req->argv[2] && strcasecmp(Req->argv[1], "host") == 0) {
  76. Client_SetHostname(target, Req->argv[2]);
  77. if (Client_Conn(target) > NONE && !Client_HasMode(target, 'x'))
  78. IRC_WriteStrClientPrefix(target, prefix,
  79. RPL_HOSTHIDDEN_MSG, Client_ID(target),
  80. Client_HostnameDisplayed(target));
  81. } else if (strcasecmp(Req->argv[1], "info") == 0)
  82. Client_SetInfo(target, Req->argv[2]);
  83. else if (*Req->argv[2] && strcasecmp(Req->argv[1], "user") == 0)
  84. Client_SetUser(target, Req->argv[2], true);
  85. else
  86. Log(LOG_WARNING,
  87. "Ignored metadata update from \"%s\" for client \"%s\": \"%s=%s\" - unknown key!",
  88. Client_ID(Client), Client_ID(target),
  89. Req->argv[1], Req->argv[2]);
  90. /* Forward the METADATA command to peers that support it: */
  91. IRC_WriteStrServersPrefixFlag(Client, prefix, 'M', "METADATA %s %s :%s",
  92. Client_ID(target), Req->argv[1], Req->argv[2]);
  93. return CONNECTED;
  94. }