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