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