]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-metadata.c
32e04516e07a273740c8115ad6cf3eb5651071a3
[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 /**
33  * Handler for the IRC+ "METADATA" command.
34  *
35  * @param Client The client from which this command has been received.
36  * @param Req Request structure with prefix and all parameters.
37  * @returns CONNECTED or DISCONNECTED.
38  */
39 GLOBAL bool
40 IRC_METADATA(CLIENT *Client, REQUEST *Req)
41 {
42         CLIENT *prefix, *target;
43         char new_flags[COMMAND_LEN];
44
45         assert(Client != NULL);
46         assert(Req != NULL);
47
48         prefix = Client_Search(Req->prefix);
49         if (!prefix)
50                 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
51                                           Client_ID(Client), Req->prefix);
52
53         target = Client_Search(Req->argv[0]);
54         if (!target)
55                 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
56                                           Client_ID(Client), Req->argv[0]);
57
58         LogDebug("Got \"METADATA\" command from \"%s\" for client \"%s\": \"%s=%s\".",
59                  Client_ID(prefix), Client_ID(target),
60                  Req->argv[1], Req->argv[2]);
61
62         /* Mark client: it has received a METADATA command */
63         if (!Client_HasFlag(target, 'M')) {
64                 snprintf(new_flags, sizeof new_flags, "%sM",
65                          Client_Flags(target));
66                 Client_SetFlags(target, new_flags);
67         }
68
69         if (strcasecmp(Req->argv[1], "cloakhost") == 0) {
70                 Client_UpdateCloakedHostname(target, prefix, Req->argv[2]);
71                 if (Client_Conn(target) > NONE && Client_HasMode(target, 'x'))
72                         IRC_WriteStrClientPrefix(target, prefix,
73                                         RPL_HOSTHIDDEN_MSG, Client_ID(target),
74                                         Client_HostnameDisplayed(target));
75                 /* The Client_UpdateCloakedHostname() function already
76                  * forwarded the METADATA command, don't do it twice: */
77                 return CONNECTED;
78         }
79         else if (*Req->argv[2] && strcasecmp(Req->argv[1], "host") == 0) {
80                 Client_SetHostname(target, 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         } else if (strcasecmp(Req->argv[1], "info") == 0)
86                 Client_SetInfo(target, Req->argv[2]);
87         else if (*Req->argv[2] && strcasecmp(Req->argv[1], "user") == 0)
88                 Client_SetUser(target, Req->argv[2], true);
89         else if (strcasecmp(Req->argv[1], "accountname") == 0)
90                 Client_SetAccountName(target, Req->argv[2]);
91         else if (*Req->argv[2] && strcasecmp(Req->argv[1], "certfp") == 0)
92                 Conn_SetCertFp(Client_Conn(target), Req->argv[2]);
93         else
94                 Log(LOG_WARNING,
95                     "Ignored metadata update from \"%s\" for client \"%s\": \"%s=%s\" - unknown key!",
96                     Client_ID(Client), Client_ID(target),
97                     Req->argv[1], Req->argv[2]);
98
99         /* Forward the METADATA command to peers that support it: */
100         IRC_WriteStrServersPrefixFlag(Client, prefix, 'M', "METADATA %s %s :%s",
101                                 Client_ID(target), Req->argv[1], Req->argv[2]);
102         return CONNECTED;
103 }