7d8a6e1abd2730bf0f69b9ba5c7ae64a5558fdce
[ircu2.10.12-pk.git] / ircd / m_relay.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_relay.c
3  * Written by David Herrmann.
4  */
5
6 #include "config.h"
7
8 #include "channel.h"
9 #include "client.h"
10 #include "handlers.h"
11 #include "hash.h"
12 #include "parse.h"
13 #include "ircd.h"
14 #include "ircd_chattr.h"
15 #include "ircd_features.h"
16 #include "ircd_reply.h"
17 #include "ircd_string.h"
18 #include "msg.h"
19 #include "numeric.h"
20 #include "numnicks.h"
21 #include "s_auth.h"
22 #include "s_debug.h"
23 #include "s_user.h"
24 #include "send.h"
25 #include "sys.h"
26
27 #include <string.h>
28 #include <ctype.h>
29 #include <stdlib.h>
30
31 static void loc_handler_LR(const char *num, char *parv[], signed int parc) {
32     if(num[0] != '!') return;
33         if(parc > 0)
34                 auth_loc_reply(&num[3], NULL, NULL, NULL, 0);
35         else
36                 auth_loc_reply(&num[3], NULL, NULL, &parv[0] , parc);
37 }
38
39 static void loc_handler_LA(const char *num, char *parv[], signed int parc) {
40     if(num[0] != '!' || parc < 1) return;
41         char *fakehost = NULL;
42         if (parc > 1 && parv[1] != "0")
43                 fakehost=parv[1];
44                 
45     if(parc > 2)
46         auth_loc_reply(&num[3], parv[0], fakehost, &parv[2] , parc - 2);
47     else if(parc > 1)
48         auth_loc_reply(&num[3], parv[0], fakehost, NULL, 0);
49     else
50         auth_loc_reply(&num[3], parv[0], NULL, NULL, 0);
51 }
52
53
54 static void mode_a_join(struct Client* cptr, char *channel, int flags) {
55     struct Channel *chptr;
56     if (!(chptr = FindChannel(channel))) {
57         if (((channel[0] == '&') && !feature_bool(FEAT_LOCAL_CHANNELS)) || strlen(channel) > IRCD_MIN(CHANNELLEN, feature_int(FEAT_CHANNELLEN))) {
58          //we don't send an error message here - that would be very strange for the user, because they normaly don't know that mode +F is set
59         } else if ((chptr = get_channel(cptr, channel, CGT_CREATE))) {
60            struct JoinBuf create;
61            joinbuf_init(&create, cptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
62            joinbuf_join(&create, chptr, CHFL_CHANOP | CHFL_CHANNEL_MANAGER);
63                    do_names(cptr, chptr, NAMES_ALL|NAMES_EON);
64            joinbuf_flush(&create);
65                 }
66         } else {
67         struct JoinBuf join;
68         joinbuf_init(&join, cptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
69                 joinbuf_join(&join, chptr, flags);
70                 del_invite(cptr, chptr);
71         if (chptr->topic[0]) {
72             send_reply(cptr, RPL_TOPIC, chptr->chname, chptr->topic);
73             send_reply(cptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick, chptr->topic_time);
74         }
75         do_names(cptr, chptr, NAMES_ALL|NAMES_EON); /* send /names list */
76         joinbuf_flush(&join);
77         }
78 }
79
80 static void mode_a_check_altchan(struct Client* sptr, char *channel) {
81         struct Channel *chptrb;
82         if (!(chptrb = FindChannel(channel))) {
83       if (((channel[0] == '&') && !feature_bool(FEAT_LOCAL_CHANNELS)) || strlen(channel) > IRCD_MIN(CHANNELLEN, feature_int(FEAT_CHANNELLEN))) {
84         //we don't send an error message here - that would be very strange for the user, because they normaly don't know that mode +F is set
85       } else if ((chptrb = get_channel(sptr, channel, CGT_CREATE))) {
86             struct JoinBuf create;
87         joinbuf_init(&create, sptr, sptr, JOINBUF_TYPE_CREATE, 0, TStime());
88         joinbuf_join(&create, chptrb, CHFL_CHANOP | CHFL_CHANNEL_MANAGER);
89                 do_names(sptr, chptrb, NAMES_ALL|NAMES_EON);
90                 joinbuf_flush(&create);
91           }
92         } else {
93           //first of all check if we may even join this channel
94           int err2 = 0;
95           int flags = 0;
96           if (chptrb->users == 0 && !chptrb->mode.apass[0] && !(chptrb->mode.mode & MODE_PERSIST)) {
97         /* Joining a zombie channel (zannel): give ops and increment TS. */
98         flags = CHFL_CHANOP;
99         chptrb->creationtime++;
100       } else if (IsInvited(sptr, chptrb)) {
101          /* Invites and key=OVERRIDE bypass these other checks. */
102       } else if (chptrb->mode.mode & MODE_INVITEONLY)
103         err2 = ERR_INVITEONLYCHAN;
104       else if (chptrb->mode.limit && (chptrb->users >= chptrb->mode.limit))
105         err2 = ERR_CHANNELISFULL;
106       else if ((chptrb->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
107         err2 = ERR_NEEDREGGEDNICK;
108       else if (find_ban(sptr, chptrb->banlist))
109         err2 = ERR_BANNEDFROMCHAN;
110       else if (*chptrb->mode.key) //Fix this!
111         err2 = ERR_BADCHANNELKEY;
112           if(!err2) {
113             struct JoinBuf join;
114         joinbuf_init(&join, sptr, sptr, JOINBUF_TYPE_JOIN, 0, 0);
115             joinbuf_join(&join, chptrb, flags);
116             del_invite(sptr, chptrb);
117         if (chptrb->topic[0]) {
118           send_reply(sptr, RPL_TOPIC, chptrb->chname, chptrb->topic);
119           send_reply(sptr, RPL_TOPICWHOTIME, chptrb->chname, chptrb->topic_nick, chptrb->topic_time);
120         }
121         do_names(sptr, chptrb, NAMES_ALL|NAMES_EON); /* send /names list */
122                 joinbuf_flush(&join);
123           }
124    }
125 }
126
127 /** RELAY
128  * The RELAY command has a special purpose. It is always built the following way:
129  *  <sender> RELAY <destination> <command>[ <list of parameters>]
130  * The <sender> is a single numeric nick of a server or user. <destination> can be:
131  * - a numeric-nick of a server (2 characters long): eg., AD
132  * - a numeric-nick of a user (5 characters long): eg., ADAAB
133  * - a numeric-nick of an unregistered user (6 characters long): eg., !ADAAB
134  * <command> is a subcommdn of RELAY.
135  *
136  * If we receive such a message, we relay the message to the server of <destination>.
137  * If we are the target, we check <command> and call the related subcommand handler.
138  *
139  * Therefore, this interface can be used to relay messages through the network without
140  * specifying new commands.
141  */
142 /* ms_relay - server message handler
143  *
144  * parv[0] = sender prefix
145  * parv[1] = target
146  * parv[2] = subcommand
147  * parv[3-X] = NULL or a list of parameters.
148  */
149 signed int ms_relay(struct Client* cptr, struct Client* sptr, signed int parc, char* parv[]) {
150     struct Client *server;
151     unsigned int len, i;
152     char buf[3], *act, *m, buffer[513];
153
154     if(parc < 3) {
155         return protocol_violation(cptr, "Too few arguments for RELAY");
156     }
157
158     /* Check <destination>. */
159     len = strlen(parv[1]);
160     buf[2] = 0;
161     switch(len) {
162         case 2:
163             server = FindNServer(parv[1]);
164             break;
165         case 5:
166             buf[0] = parv[1][0];
167             buf[1] = parv[1][1];
168             server = FindNServer(buf);
169             break;
170         case 6:
171             buf[0] = parv[1][1];
172             buf[1] = parv[1][2];
173             server = FindNServer(buf);
174             break;
175         default:
176             /* Invalid destination. Ignore. */
177             return 0;
178     }
179
180     if(!server) return 0;
181     if(server != &me) {
182         if(parc > 3) {
183             act = buffer;
184             for(i = 3; i < (parc - 1); ++i) {
185                 m = parv[i];
186                 while((*act++ = *m++)) /* empty loop */ ;
187                 *(act - 1) = ' ';
188             }
189             m = parv[i];
190             *act++ = ':';
191             while((*act++ = *m++)) /* empty loop */ ;
192             sendcmdto_one(sptr, CMD_RELAY, server, "%s %s %s", parv[1], parv[2], buffer);
193         }
194         else sendcmdto_one(sptr, CMD_RELAY, server, "%s %s", parv[1], parv[2]);
195         return 0;
196     }
197
198     /* Call subcommand handler. */
199     if(strcmp("LR", parv[2]) == 0) loc_handler_LR(parv[1], &parv[3], parc - 3);
200     else if(strcmp("LA", parv[2]) == 0 && parc > 3) loc_handler_LA(parv[1], &parv[3], parc - 3);
201         else if(strcmp("SM", parv[2]) == 0 && parc > 3) {
202      #ifndef UNRESTRICTED_SERV
203          struct Client *acptr;
204          if(acptr = findNUser(parv[3])) {
205           if (IsChannelPrefix(*parv[4])) {
206        relay_channel_message(acptr, parv[4], parv[parc - 1], 1);
207       } else {
208            relay_private_message(acptr, parv[4], parv[parc - 1]);
209           }
210          }
211      #endif
212         } else if(strcmp("UC", parv[2]) == 0 && parc > 3) {
213          struct Client *acptr;
214          if(acptr = findNUser(parv[3])) {
215           send_reply(acptr, ERR_UNKNOWNCOMMAND, parv[4]);
216          }
217         } else if(strcmp("SI", parv[2]) == 0 && parc > 3) {
218      #ifndef UNRESTRICTED_SERV
219          struct Client *acptr;
220          if(acptr = findNUser(parv[3])) {
221           parse_simul_client(acptr, parv[parc - 1]);
222          }
223      #endif
224         } else if(strcmp("JAA", parv[2]) == 0 && parc > 2) {
225          struct Client *acptr;
226          if(acptr = findNUser(parv[1])) {
227           mode_a_join(acptr,parv[3],strtoul(parv[4], 0, 10));
228          }
229         } else if(strcmp("JAR", parv[2]) == 0 && parc > 2) {
230          struct Client *acptr;
231          struct Channel *chptr;
232          if(acptr = findNUser(parv[1])) {
233           if(IsChannelName(parv[3]) && strIsIrcCh(parv[3]) && 
234          (chptr = FindChannel(parv[3])) && chptr->mode.altchan && 
235              IsChannelName(chptr->mode.altchan) && strIsIrcCh(chptr->mode.altchan)) {
236        mode_a_check_altchan(acptr,chptr->mode.altchan);
237           }
238           send_reply(acptr, ERR_JOINACCESS, parv[3]);
239          }
240         }
241
242     return 0;
243 }
244