ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/send.c
(Generate patch)

Comparing ircd-hybrid/trunk/src/send.c (file contents):
Revision 1832 by michael, Fri Apr 19 19:16:09 2013 UTC vs.
Revision 3215 by michael, Tue Mar 25 19:23:15 2014 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  send.c: Functions for sending messages.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2002 by the past and present ircd coders, and others.
4 > *  Copyright (c) 1997-2014 ircd-hybrid development team
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 18 | Line 17
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19   *  USA
20 < *
21 < *  $Id$
20 > */
21 >
22 > /*! \file send.c
23 > * \brief Functions for sending messages.
24 > * \version $Id$
25   */
26  
27   #include "stdinc.h"
# Line 45 | Line 47 | static unsigned int current_serial = 0;
47  
48   /* send_format()
49   *
50 < * inputs       - buffer to format into
51 < *              - size of the buffer
50 > * inputs
51 > *              - buffer
52   *              - format pattern to use
53   *              - var args
54   * output       - number of bytes formatted output
55   * side effects - modifies sendbuf
56   */
57 < static inline int
58 < send_format(char *lsendbuf, int bufsize, const char *pattern, va_list args)
57 > static void
58 > send_format(struct dbuf_block *buffer, const char *pattern, va_list args)
59   {
58  int len;
59
60    /*
61     * from rfc1459
62     *
63     * IRC messages are always lines of characters terminated with a CR-LF
64     * (Carriage Return - Line Feed) pair, and these messages shall not
65 <   * exceed 512 characters in length,  counting all characters
65 >   * exceed 512 characters in length,  counting all characters
66     * including the trailing CR-LF.
67     * Thus, there are 510 characters maximum allowed
68     * for the command and its parameters.  There is no provision for
69     * continuation message lines.  See section 7 for more details about
70     * current implementations.
71     */
72 <  len = vsnprintf(lsendbuf, bufsize - 1, pattern, args);
73 <  if (len > bufsize - 2)
74 <    len = bufsize - 2;  /* required by some versions of vsnprintf */
75 <
76 <  lsendbuf[len++] = '\r';
77 <  lsendbuf[len++] = '\n';
78 <  return len;
72 >  dbuf_put_args(buffer, pattern, args);
73 >
74 >  if (buffer->size > sizeof(buffer->data) - 2)
75 >    buffer->size = sizeof(buffer->data) - 2;
76 >
77 >  buffer->data[buffer->size++] = '\r';
78 >  buffer->data[buffer->size++] = '\n';
79   }
80  
81   /*
# Line 84 | Line 84 | send_format(char *lsendbuf, int bufsize,
84   **      sendq.
85   */
86   static void
87 < send_message(struct Client *to, char *buf, int len)
87 > send_message(struct Client *to, struct dbuf_block *buf)
88   {
89    assert(!IsMe(to));
90    assert(to != &me);
91  
92 <  if (dbuf_length(&to->localClient->buf_sendq) + len > get_sendq(&to->localClient->confs))
92 >  if (dbuf_length(&to->localClient->buf_sendq) + buf->size > get_sendq(&to->localClient->confs))
93    {
94      if (IsServer(to))
95        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
96                             "Max SendQ limit exceeded for %s: %lu > %u",
97                             get_client_name(to, HIDE_IP),
98 <                           (unsigned long)(dbuf_length(&to->localClient->buf_sendq) + len),
98 >                           (unsigned long)(dbuf_length(&to->localClient->buf_sendq) + buf->size),
99                             get_sendq(&to->localClient->confs));
100      if (IsClient(to))
101        SetSendQExceeded(to);
102 +
103      dead_link_on_write(to, 0);
104      return;
105    }
106 <
107 <  dbuf_put(&to->localClient->buf_sendq, buf, len);
106 >
107 >  dbuf_add(&to->localClient->buf_sendq, buf);
108  
109    /*
110 <   ** Update statistics. The following is slightly incorrect
111 <   ** because it counts messages even if queued, but bytes
112 <   ** only really sent. Queued bytes get updated in SendQueued.
110 >   * Update statistics. The following is slightly incorrect because
111 >   * it counts messages even if queued, but bytes only really sent.
112 >   * Queued bytes get updated in send_queued_write().
113     */
114    ++to->localClient->send.messages;
115    ++me.localClient->send.messages;
116  
117 <  if (dbuf_length(&to->localClient->buf_sendq) >
117 <      (IsServer(to) ? (unsigned int) 1024 : (unsigned int) 4096))
118 <    send_queued_write(to);
117 >  send_queued_write(to);
118   }
119  
120   /* send_message_remote()
121   *
122   * inputs       - pointer to client from message is being sent
123   *              - pointer to client to send to
124 < *              - pointer to preformatted buffer
126 < *              - length of input buffer
124 > *              - pointer to buffer
125   * output       - none
126   * side effects - Despite the function name, this only sends to directly
127   *                connected clients.
128 < *
128 > *
129   */
130   static void
131 < send_message_remote(struct Client *to, struct Client *from,
134 <                    char *buf, int len)
131 > send_message_remote(struct Client *to, struct Client *from, struct dbuf_block *buf)
132   {
133 +  to = to->from;
134 +
135    if (!MyConnect(to))
136    {
137      sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
138 <                         "server send message to %s [%s] dropped from %s(Not local server)",
139 <                         to->name, to->from->name, from->name);
138 >                         "Server send message to %s [%s] dropped from %s(Not local server)",
139 >                         to->name, to->from->name, from->name);
140      return;
141    }
142  
# Line 160 | Line 159 | send_message_remote(struct Client *to, s
159                           from->name, from->username, from->host,
160                           to->from->name);
161  
162 <    sendto_server(NULL, CAP_TS6, NOCAPS,
162 >    sendto_server(NULL, NOCAPS, NOCAPS,
163                    ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
164 <                  me.id, to->name, me.name, to->name,
166 <                  to->username, to->host, to->from->name);
167 <    sendto_server(NULL, NOCAPS, CAP_TS6,
168 <                  ":%s KILL %s :%s (%s[%s@%s] Ghosted %s)",
169 <                  me.name, to->name, me.name, to->name,
164 >                  me.id, to->id, me.name, to->name,
165                    to->username, to->host, to->from->name);
166  
167      AddFlag(to, FLAGS_KILLED);
168  
169      if (IsClient(from))
170 <      sendto_one(from, ERR_GHOSTEDCLIENT,
171 <                 me.name, from->name, to->name, to->username,
177 <                 to->host, to->from);
178 <
179 <    exit_client(to, &me, "Ghosted client");
170 >      sendto_one_numeric(from, &me, ERR_GHOSTEDCLIENT, to->name,
171 >                         to->username, to->host, to->from);
172  
173 +    exit_client(to, "Ghosted client");
174      return;
175 <  }
175 >  }
176  
177 <  send_message(to, buf, len);
177 >  send_message(to, buf);
178   }
179  
180   /*
# Line 191 | Line 184 | send_message_remote(struct Client *to, s
184   void
185   sendq_unblocked(fde_t *fd, struct Client *client_p)
186   {
187 <  ClearSendqBlocked(client_p);
188 <  /* let send_queued_write be executed by send_queued_all */
196 <
197 < #ifdef HAVE_LIBCRYPTO
198 <  if (fd->flags.pending_read)
199 <  {
200 <    fd->flags.pending_read = 0;
201 <    read_packet(fd, client_p);
202 <  }
203 < #endif
187 >  assert(fd == &client_p->localClient->fd);
188 >  send_queued_write(client_p);
189   }
190  
191   /*
# Line 212 | Line 197 | sendq_unblocked(fde_t *fd, struct Client
197   void
198   send_queued_write(struct Client *to)
199   {
200 <  int retlen;
216 <  struct dbuf_block *first;
200 >  int retlen = 0;
201  
202    /*
203     ** Once socket is marked dead, we cannot start writing to it,
204     ** even if the error is removed...
205     */
206 <  if (IsDead(to) || IsSendqBlocked(to))
206 >  if (IsDead(to))
207      return;  /* no use calling send() now */
208  
209    /* Next, lets try to write some data */
226
210    if (dbuf_length(&to->localClient->buf_sendq))
211    {
212 <    do {
213 <      first = to->localClient->buf_sendq.blocks.head->data;
212 >    do
213 >    {
214 >      struct dbuf_block *first = to->localClient->buf_sendq.blocks.head->data;
215  
216   #ifdef HAVE_LIBCRYPTO
217        if (to->localClient->fd.ssl)
218        {
219 <        retlen = SSL_write(to->localClient->fd.ssl, first->data, first->size);
219 >        retlen = SSL_write(to->localClient->fd.ssl, first->data + to->localClient->buf_sendq.pos, first->size - to->localClient->buf_sendq.pos);
220  
221          /* translate openssl error codes, sigh */
222 <        if (retlen < 0)
223 <          switch (SSL_get_error(to->localClient->fd.ssl, retlen))
224 <          {
222 >        if (retlen < 0)
223 >        {
224 >          switch (SSL_get_error(to->localClient->fd.ssl, retlen))
225 >          {
226              case SSL_ERROR_WANT_READ:
227 <              return;  /* retry later, don't register for write events */
228 <
229 <            case SSL_ERROR_WANT_WRITE:
245 <              errno = EWOULDBLOCK;
227 >              return;  /* retry later, don't register for write events */
228 >            case SSL_ERROR_WANT_WRITE:
229 >              errno = EWOULDBLOCK;
230              case SSL_ERROR_SYSCALL:
231 <              break;
231 >              break;
232              case SSL_ERROR_SSL:
233                if (errno == EAGAIN)
234                  break;
235              default:
236 <              retlen = errno = 0;  /* either an SSL-specific error or EOF */
237 <          }
236 >              retlen = errno = 0;  /* either an SSL-specific error or EOF */
237 >          }
238 >        }
239        }
240        else
241   #endif
242 <        retlen = send(to->localClient->fd.fd, first->data, first->size, 0);
242 >        retlen = send(to->localClient->fd.fd, first->data + to->localClient->buf_sendq.pos, first->size - to->localClient->buf_sendq.pos, 0);
243  
244        if (retlen <= 0)
245          break;
# Line 269 | Line 254 | send_queued_write(struct Client *to)
254      if ((retlen < 0) && (ignoreErrno(errno)))
255      {
256        /* we have a non-fatal error, reschedule a write */
272      SetSendqBlocked(to);
257        comm_setselect(&to->localClient->fd, COMM_SELECT_WRITE,
258 <                     (PF *)sendq_unblocked, (void *)to, 0);
258 >                     (PF *)sendq_unblocked, to, 0);
259      }
260      else if (retlen <= 0)
261      {
# Line 296 | Line 280 | send_queued_all(void)
280     * a notice to opers, which is to be delivered by this function.
281     */
282    DLINK_FOREACH(ptr, serv_list.head)
283 <    send_queued_write((struct Client *) ptr->data);
283 >    send_queued_write(ptr->data);
284  
285    DLINK_FOREACH(ptr, unknown_list.head)
286 <    send_queued_write((struct Client *) ptr->data);
286 >    send_queued_write(ptr->data);
287  
288    DLINK_FOREACH(ptr, local_client_list.head)
289 <    send_queued_write((struct Client *) ptr->data);
289 >    send_queued_write(ptr->data);
290  
291    /* NOTE: This can still put clients on aborted_list; unfortunately,
292     * exit_aborted_clients takes precedence over send_queued_all,
# Line 323 | Line 307 | void
307   sendto_one(struct Client *to, const char *pattern, ...)
308   {
309    va_list args;
310 <  char buffer[IRCD_BUFSIZE];
327 <  int len;
310 >  struct dbuf_block *buffer = NULL;
311  
312 <  if (to->from != NULL)
313 <    to = to->from;
314 <  if (IsDead(to))
315 <    return; /* This socket has already been marked as dead */
312 >  if (IsDead(to->from))
313 >    return;  /* This socket has already been marked as dead */
314 >
315 >  buffer = dbuf_alloc();
316  
317    va_start(args, pattern);
318 <  len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
318 >  send_format(buffer, pattern, args);
319    va_end(args);
320  
321 <  send_message(to, buffer, len);
321 >  send_message(to->from, buffer);
322 >
323 >  dbuf_ref_free(buffer);
324 > }
325 >
326 > void
327 > sendto_one_numeric(struct Client *to, struct Client *from, enum irc_numerics numeric, ...)
328 > {
329 >  struct dbuf_block *buffer = NULL;
330 >  const char *dest = NULL;
331 >  va_list args;
332 >
333 >  if (IsDead(to->from))
334 >    return;
335 >
336 >  dest = ID_or_name(to, to);
337 >  if (EmptyString(dest))
338 >    dest = "*";
339 >
340 >  buffer = dbuf_alloc();
341 >
342 >  dbuf_put_fmt(buffer, ":%s %03d %s ", ID_or_name(from, to), numeric, dest);
343 >
344 >  va_start(args, numeric);
345 >  send_format(buffer, numeric_form(numeric), args);
346 >  va_end(args);
347 >
348 >  send_message(to->from, buffer);
349 >
350 >  dbuf_ref_free(buffer);
351 > }
352 >
353 > void
354 > sendto_one_notice(struct Client *to, struct Client *from, const char *pattern, ...)
355 > {
356 >  struct dbuf_block *buffer = NULL;
357 >  const char *dest = NULL;
358 >  va_list args;
359 >
360 >  if (IsDead(to->from))
361 >    return;
362 >
363 >  dest = ID_or_name(to, to);
364 >  if (EmptyString(dest))
365 >    dest = "*";
366 >
367 >  buffer = dbuf_alloc();
368 >
369 >  dbuf_put_fmt(buffer, ":%s NOTICE %s ", ID_or_name(from, to), dest);
370 >
371 >  va_start(args, pattern);
372 >  send_format(buffer, pattern, args);
373 >  va_end(args);
374 >
375 >  send_message(to->from, buffer);
376 >
377 >  dbuf_ref_free(buffer);
378   }
379  
380   /* sendto_channel_butone()
# Line 354 | Line 393 | sendto_channel_butone(struct Client *one
393                        struct Channel *chptr, unsigned int type,
394                        const char *pattern, ...)
395   {
396 <  va_list alocal, aremote, auid;
397 <  char local_buf[IRCD_BUFSIZE];
359 <  char remote_buf[IRCD_BUFSIZE];
360 <  char uid_buf[IRCD_BUFSIZE];
361 <  int local_len, remote_len, uid_len;
396 >  va_list alocal, aremote;
397 >  struct dbuf_block *local_buf, *remote_buf;
398    dlink_node *ptr = NULL, *ptr_next = NULL;
399  
400 +  local_buf = dbuf_alloc(), remote_buf = dbuf_alloc();
401 +
402    if (IsServer(from))
403 <    local_len = sprintf(local_buf, ":%s ",
366 <                        from->name);
403 >    dbuf_put_fmt(local_buf, ":%s ", from->name);
404    else
405 <    local_len = sprintf(local_buf, ":%s!%s@%s ",
406 <                        from->name, from->username, from->host);
407 <  remote_len = sprintf(remote_buf, ":%s ",
371 <                          from->name);
372 <  uid_len = sprintf(uid_buf, ":%s ",
373 <                    ID(from));
405 >    dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
406 >
407 >  dbuf_put_fmt(remote_buf, ":%s ", from->id);
408  
409    va_start(alocal, pattern);
410    va_start(aremote, pattern);
411 <  va_start(auid, pattern);
412 <  local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len,
413 <                           pattern, alocal);
380 <  remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len,
381 <                            pattern, aremote);
382 <  uid_len += send_format(&uid_buf[uid_len], IRCD_BUFSIZE - uid_len, pattern,
383 <                         auid);
384 <  va_end(auid);
411 >  send_format(local_buf, pattern, alocal);
412 >  send_format(remote_buf, pattern, aremote);
413 >
414    va_end(aremote);
415    va_end(alocal);
416  
# Line 394 | Line 423 | sendto_channel_butone(struct Client *one
423  
424      assert(IsClient(target_p));
425  
426 <    if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF) || target_p->from == one)
426 >    if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF) ||
427 >        (one && target_p->from == one->from))
428        continue;
429  
430      if (type != 0 && (ms->flags & type) == 0)
431        continue;
432  
433      if (MyConnect(target_p))
434 <    {
435 <      if (target_p->localClient->serial != current_serial)
436 <      {
437 <        send_message(target_p, local_buf, local_len);
408 <        target_p->localClient->serial = current_serial;
409 <      }
410 <    }
411 <    else
412 <    {
413 <      /* Now check whether a message has been sent to this
414 <       * remote link already
415 <       */
416 <      if (target_p->from->localClient->serial != current_serial)
417 <      {
418 <        if (IsCapable(target_p->from, CAP_TS6))
419 <          send_message_remote(target_p->from, from, uid_buf, uid_len);
420 <        else
421 <          send_message_remote(target_p->from, from, remote_buf, remote_len);
422 <        target_p->from->localClient->serial = current_serial;
423 <      }
424 <    }
434 >      send_message(target_p, local_buf);
435 >    else if (target_p->from->localClient->serial != current_serial)
436 >      send_message_remote(target_p->from, from, remote_buf);
437 >    target_p->from->localClient->serial = current_serial;
438    }
439 +
440 +  dbuf_ref_free(local_buf);
441 +  dbuf_ref_free(remote_buf);
442   }
443  
444   /* sendto_server()
445 < *
445 > *
446   * inputs       - pointer to client to NOT send to
447   *              - pointer to channel
448   *              - caps or'd together which must ALL be present
# Line 437 | Line 453 | sendto_channel_butone(struct Client *one
453   * side effects - Send a message to all connected servers, except the
454   *                client 'one' (if non-NULL), as long as the servers
455   *                support ALL capabs in 'caps', and NO capabs in 'nocaps'.
456 < *            
456 > *
457   * This function was written in an attempt to merge together the other
458   * billion sendto_*serv*() functions, which sprung up with capabs,
459   * lazylinks, uids, etc.
460   * -davidt
461   */
462 < void
462 > void
463   sendto_server(struct Client *one,
464                const unsigned int caps,
465                const unsigned int nocaps,
# Line 451 | Line 467 | sendto_server(struct Client *one,
467   {
468    va_list args;
469    dlink_node *ptr = NULL;
470 <  char buffer[IRCD_BUFSIZE];
471 <  int len = 0;
470 >  struct dbuf_block *buffer;
471 >
472 >  buffer = dbuf_alloc();
473  
474    va_start(args, format);
475 <  len = send_format(buffer, IRCD_BUFSIZE, format, args);
475 >  send_format(buffer, format, args);
476    va_end(args);
477  
478    DLINK_FOREACH(ptr, serv_list.head)
# Line 475 | Line 492 | sendto_server(struct Client *one,
492      if ((client_p->localClient->caps & nocaps) != 0)
493        continue;
494  
495 <    send_message(client_p, buffer, len);
495 >    send_message(client_p, buffer);
496    }
497 +
498 +  dbuf_ref_free(buffer);
499   }
500  
501   /* sendto_common_channels_local()
# Line 485 | Line 504 | sendto_server(struct Client *one,
504   *              - pattern to send
505   * output       - NONE
506   * side effects - Sends a message to all people on local server who are
507 < *                in same channel with user.
507 > *                in same channel with user.
508   *                used by m_nick.c and exit_one_client.
509   */
510   void
# Line 498 | Line 517 | sendto_common_channels_local(struct Clie
517    struct Channel *chptr;
518    struct Membership *ms;
519    struct Client *target_p;
520 <  char buffer[IRCD_BUFSIZE];
521 <  int len;
520 >  struct dbuf_block *buffer;
521 >
522 >  buffer = dbuf_alloc();
523  
524    va_start(args, pattern);
525 <  len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
525 >  send_format(buffer, pattern, args);
526    va_end(args);
527  
528    ++current_serial;
529  
530    DLINK_FOREACH(cptr, user->channel.head)
531    {
532 <    chptr = ((struct Membership *) cptr->data)->chptr;
532 >    chptr = ((struct Membership *)cptr->data)->chptr;
533      assert(chptr != NULL);
534  
535      DLINK_FOREACH(uptr, chptr->members.head)
# Line 526 | Line 546 | sendto_common_channels_local(struct Clie
546          continue;
547  
548        target_p->localClient->serial = current_serial;
549 <      send_message(target_p, buffer, len);
549 >      send_message(target_p, buffer);
550      }
551    }
552  
553    if (touser && MyConnect(user) && !IsDead(user) &&
554        user->localClient->serial != current_serial)
555 <    if (HasCap(target_p, cap) == cap)
556 <      send_message(user, buffer, len);
555 >    if (HasCap(user, cap) == cap)
556 >      send_message(user, buffer);
557 >
558 >  dbuf_ref_free(buffer);
559   }
560  
561   /* sendto_channel_local()
# Line 547 | Line 569 | sendto_common_channels_local(struct Clie
569   *                locally connected to this server.
570   */
571   void
572 < sendto_channel_local(int type, int nodeaf, struct Channel *chptr,
572 > sendto_channel_local(unsigned int type, int nodeaf, struct Channel *chptr,
573                       const char *pattern, ...)
574   {
575    va_list args;
576 <  char buffer[IRCD_BUFSIZE];
577 <  int len;
578 <  dlink_node *ptr;
579 <  struct Membership *ms;
558 <  struct Client *target_p;
576 >  dlink_node *ptr = NULL;
577 >  struct dbuf_block *buffer;
578 >
579 >  buffer = dbuf_alloc();
580  
581    va_start(args, pattern);
582 <  len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
582 >  send_format(buffer, pattern, args);
583    va_end(args);
584  
585    DLINK_FOREACH(ptr, chptr->members.head)
586    {
587 <    ms = ptr->data;
588 <    target_p = ms->client_p;
587 >    struct Membership *ms = ptr->data;
588 >    struct Client *target_p = ms->client_p;
589  
590      if (type != 0 && (ms->flags & type) == 0)
591        continue;
# Line 573 | Line 594 | sendto_channel_local(int type, int nodea
594          (nodeaf && HasUMode(target_p, UMODE_DEAF)))
595        continue;
596  
597 <    send_message(target_p, buffer, len);
597 >    send_message(target_p, buffer);
598    }
599 +
600 +  dbuf_ref_free(buffer);
601   }
602  
603   /* sendto_channel_local_butone()
# Line 589 | Line 612 | sendto_channel_local(int type, int nodea
612   *
613   * WARNING - +D clients are omitted
614   */
592 void      
593 sendto_channel_local_butone(struct Client *one, int type, unsigned int cap,
594                            struct Channel *chptr, const char *pattern, ...)
595 {
596  va_list args;
597  char buffer[IRCD_BUFSIZE];
598  int len;
599  struct Client *target_p;
600  struct Membership *ms;
601  dlink_node *ptr;
602
603  va_start(args, pattern);
604  len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
605  va_end(args);
606
607  DLINK_FOREACH(ptr, chptr->members.head)      
608  {  
609    ms = ptr->data;
610    target_p = ms->client_p;
611
612    if (type != 0 && (ms->flags & type) == 0)
613      continue;
614
615    if (!MyConnect(target_p) || target_p == one ||
616        IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF))
617      continue;
618
619    if (HasCap(target_p, cap) != cap)
620        continue;
621    send_message(target_p, buffer, len);
622  }
623 }
624
625
626 /* sendto_channel_remote()
627 *
628 * inputs       - Client not to send towards
629 *              - Client from whom message is from
630 *              - member status mask, e.g. CHFL_CHANOP | CHFL_VOICE
631 *              - pointer to channel to send to
632 *              - var args pattern
633 * output       - NONE
634 * side effects - Send a message to all members of a channel that are
635 *                remote to this server.
636 */
615   void
616 < sendto_channel_remote(struct Client *one, struct Client *from, int type,
617 <                      const unsigned int caps, const unsigned int nocaps,
640 <                      struct Channel *chptr, const char *pattern, ...)
616 > sendto_channel_local_butone(struct Client *one, unsigned int type, unsigned int cap,
617 >                            struct Channel *chptr, const char *pattern, ...)
618   {
619    va_list args;
620 <  char buffer[IRCD_BUFSIZE];
621 <  int len;
622 <  dlink_node *ptr;
623 <  struct Client *target_p;
647 <  struct Membership *ms;
620 >  dlink_node *ptr = NULL;
621 >  struct dbuf_block *buffer;
622 >
623 >  buffer = dbuf_alloc();
624  
625    va_start(args, pattern);
626 <  len = send_format(buffer, IRCD_BUFSIZE, pattern, args);
626 >  send_format(buffer, pattern, args);
627    va_end(args);
628  
653  ++current_serial;
654
629    DLINK_FOREACH(ptr, chptr->members.head)
630    {
631 <    ms = ptr->data;
632 <    target_p = ms->client_p;
631 >    struct Membership *ms = ptr->data;
632 >    struct Client *target_p = ms->client_p;
633  
634      if (type != 0 && (ms->flags & type) == 0)
635        continue;
636  
637 <    if (MyConnect(target_p))
637 >    if (!MyConnect(target_p) || (one && target_p == one->from))
638        continue;
665    target_p = target_p->from;
639  
640 <    if (target_p == one->from ||
668 <        ((target_p->from->localClient->caps & caps) != caps) ||
669 <        ((target_p->from->localClient->caps & nocaps) != 0))
640 >    if (IsDefunct(target_p) || HasUMode(target_p, UMODE_DEAF))
641        continue;
642 <    if (target_p->from->localClient->serial != current_serial)
643 <    {
644 <      send_message(target_p, buffer, len);
645 <      target_p->from->localClient->serial = current_serial;
646 <    }
647 <  }
642 >
643 >    if (HasCap(target_p, cap) != cap)
644 >      continue;
645 >
646 >    send_message(target_p, buffer);
647 >  }
648 >
649 >  dbuf_ref_free(buffer);
650   }
651  
652   /*
# Line 694 | Line 667 | sendto_channel_remote(struct Client *one
667   * side effects - NONE
668   */
669   static int
670 < match_it(const struct Client *one, const char *mask, int what)
670 > match_it(const struct Client *one, const char *mask, unsigned int what)
671   {
672    if (what == MATCH_HOST)
673      return !match(mask, one->host);
# Line 710 | Line 683 | match_it(const struct Client *one, const
683   * ugh. ONLY used by m_message.c to send an "oper magic" message. ugh.
684   */
685   void
686 < sendto_match_butone(struct Client *one, struct Client *from, char *mask,
686 > sendto_match_butone(struct Client *one, struct Client *from, const char *mask,
687                      int what, const char *pattern, ...)
688   {
689    va_list alocal, aremote;
690 <  struct Client *client_p;
691 <  dlink_node *ptr, *ptr_next;
692 <  char local_buf[IRCD_BUFSIZE], remote_buf[IRCD_BUFSIZE];
693 <  int local_len = sprintf(local_buf, ":%s!%s@%s ", from->name,
694 <                          from->username, from->host);
695 <  int remote_len = sprintf(remote_buf, ":%s ", from->name);
690 >  dlink_node *ptr = NULL, *ptr_next = NULL;
691 >  struct dbuf_block *local_buf, *remote_buf;
692 >
693 >  local_buf = dbuf_alloc(), remote_buf = dbuf_alloc();
694 >
695 >  dbuf_put_fmt(local_buf, ":%s!%s@%s ", from->name, from->username, from->host);
696 >  dbuf_put_fmt(remote_buf, ":%s ", from->id);
697  
698    va_start(alocal, pattern);
699    va_start(aremote, pattern);
700 <  local_len += send_format(&local_buf[local_len], IRCD_BUFSIZE - local_len,
701 <                           pattern, alocal);
728 <  remote_len += send_format(&remote_buf[remote_len], IRCD_BUFSIZE - remote_len,
729 <                            pattern, aremote);
700 >  send_format(local_buf, pattern, alocal);
701 >  send_format(remote_buf, pattern, aremote);
702    va_end(aremote);
703    va_end(alocal);
704  
705    /* scan the local clients */
706    DLINK_FOREACH(ptr, local_client_list.head)
707    {
708 <    client_p = ptr->data;
708 >    struct Client *client_p = ptr->data;
709  
710 <    if (client_p != one && !IsDefunct(client_p) &&
710 >    if ((!one || client_p != one->from) && !IsDefunct(client_p) &&
711          match_it(client_p, mask, what))
712 <      send_message(client_p, local_buf, local_len);
712 >      send_message(client_p, local_buf);
713    }
714  
715    /* Now scan servers */
716    DLINK_FOREACH_SAFE(ptr, ptr_next, serv_list.head)
717    {
718 <    client_p = ptr->data;
718 >    struct Client *client_p = ptr->data;
719  
720      /*
721       * The old code looped through every client on the
# Line 769 | Line 741 | sendto_match_butone(struct Client *one,
741       * server deal with it.
742       * -wnder
743       */
744 <    if (client_p != one && !IsDefunct(client_p))
745 <      send_message_remote(client_p, from, remote_buf, remote_len);
744 >    if ((!one || client_p != one->from) && !IsDefunct(client_p))
745 >      send_message_remote(client_p, from, remote_buf);
746    }
747 +
748 +  dbuf_ref_free(local_buf);
749 +  dbuf_ref_free(remote_buf);
750   }
751  
752   /* sendto_match_servs()
# Line 784 | Line 759 | sendto_match_butone(struct Client *one,
759   * side effects - data sent to servers matching with capab
760   */
761   void
762 < sendto_match_servs(struct Client *source_p, const char *mask, int cap,
762 > sendto_match_servs(struct Client *source_p, const char *mask, unsigned int cap,
763                     const char *pattern, ...)
764   {
765    va_list args;
766 <  struct Client *target_p;
767 <  dlink_node *ptr;
768 <  char buffer[IRCD_BUFSIZE];
769 <  int found = 0;
766 >  dlink_node *ptr = NULL;
767 >  struct dbuf_block *buff_suid;
768 >
769 >  buff_suid = dbuf_alloc();
770  
771    va_start(args, pattern);
772 <  vsnprintf(buffer, sizeof(buffer), pattern, args);
772 >  dbuf_put_fmt(buff_suid, ":%s ", source_p->id);
773 >  dbuf_put_args(buff_suid, pattern, args);
774    va_end(args);
775  
776    ++current_serial;
777  
778    DLINK_FOREACH(ptr, global_serv_list.head)
779    {
780 <    target_p = ptr->data;
780 >    struct Client *target_p = ptr->data;
781  
782      /* Do not attempt to send to ourselves, or the source */
783      if (IsMe(target_p) || target_p->from == source_p->from)
# Line 817 | Line 793 | sendto_match_servs(struct Client *source
793         * match() again, if !IsCapable()
794         */
795        target_p->from->localClient->serial = current_serial;
820      found++;
796  
797        if (!IsCapable(target_p->from, cap))
798          continue;
799  
800 <      sendto_anywhere(target_p, source_p, "%s", buffer);
800 >      send_message_remote(target_p->from, source_p, buff_suid);
801      }
802    }
803 +
804 +  dbuf_ref_free(buff_suid);
805   }
806  
807   /* sendto_anywhere()
# Line 838 | Line 815 | sendto_match_servs(struct Client *source
815   */
816   void
817   sendto_anywhere(struct Client *to, struct Client *from,
818 +                const char *command,
819                  const char *pattern, ...)
820   {
821    va_list args;
822 <  char buffer[IRCD_BUFSIZE];
845 <  int len;
846 <  struct Client *send_to = (to->from != NULL ? to->from : to);
822 >  struct dbuf_block *buffer = NULL;
823  
824 <  if (IsDead(send_to))
824 >  if (IsDead(to->from))
825      return;
826  
827 <  if (MyClient(to))
828 <  {
829 <    if (IsServer(from))
830 <    {
831 <      if (IsCapable(to, CAP_TS6) && HasID(from))
832 <        len = sprintf(buffer, ":%s ", from->id);
833 <      else
834 <        len = sprintf(buffer, ":%s ", from->name);
859 <    }
860 <    else
861 <      len = sprintf(buffer, ":%s!%s@%s ",
862 <                       from->name, from->username, from->host);
863 <  }
864 <  else len = sprintf(buffer, ":%s ", ID_or_name(from, send_to));
827 >  buffer = dbuf_alloc();
828 >
829 >  if (MyClient(to) && IsClient(from))
830 >    dbuf_put_fmt(buffer, ":%s!%s@%s %s %s ", from->name, from->username,
831 >                 from->host, command, to->name);
832 >  else
833 >    dbuf_put_fmt(buffer, ":%s %s %s ", ID_or_name(from, to),
834 >                 command, ID_or_name(to, to));
835  
836    va_start(args, pattern);
837 <  len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
837 >  send_format(buffer, pattern, args);
838    va_end(args);
839  
840 <  if(MyClient(to))
841 <    send_message(send_to, buffer, len);
840 >  if (MyClient(to))
841 >    send_message(to, buffer);
842    else
843 <    send_message_remote(send_to, from, buffer, len);
843 >    send_message_remote(to, from, buffer);
844 >
845 >  dbuf_ref_free(buffer);
846   }
847  
848   /* sendto_realops_flags()
# Line 886 | Line 858 | sendto_realops_flags(unsigned int flags,
858   {
859    const char *ntype = NULL;
860    dlink_node *ptr = NULL;
861 <  char nbuf[IRCD_BUFSIZE];
861 >  char nbuf[IRCD_BUFSIZE] = "";
862    va_list args;
863  
864    va_start(args, pattern);
# Line 918 | Line 890 | sendto_realops_flags(unsigned int flags,
890       * If we're sending it to admins, and they're not, skip.
891       */
892      if (((level == L_ADMIN) && !HasUMode(client_p, UMODE_ADMIN)) ||
893 <        ((level == L_OPER) && HasUMode(client_p, UMODE_ADMIN)))
893 >        ((level == L_OPER) && HasUMode(client_p, UMODE_ADMIN)))
894        continue;
895  
896      if (HasUMode(client_p, flags))
# Line 927 | Line 899 | sendto_realops_flags(unsigned int flags,
899    }
900   }
901  
930 /* sendto_wallops_flags()
931 *
932 * inputs       - flag types of messages to show to real opers
933 *              - client sending request
934 *              - var args input message
935 * output       - NONE
936 * side effects - Send a wallops to local opers
937 */
938 void
939 sendto_wallops_flags(unsigned int flags, struct Client *source_p,
940                     const char *pattern, ...)
941 {
942  dlink_node *ptr = NULL;
943  va_list args;
944  char buffer[IRCD_BUFSIZE];
945  int len;
946
947  if (IsClient(source_p))
948    len = sprintf(buffer, ":%s!%s@%s WALLOPS :",
949                  source_p->name, source_p->username, source_p->host);
950  else
951    len = sprintf(buffer, ":%s WALLOPS :", source_p->name);
952
953  va_start(args, pattern);
954  len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
955  va_end(args);
956
957  DLINK_FOREACH(ptr, oper_list.head)
958  {
959    struct Client *client_p = ptr->data;
960    assert(client_p->umodes & UMODE_OPER);
961
962    if (HasUMode(client_p, flags) && !IsDefunct(client_p))
963      send_message(client_p, buffer, len);
964  }
965 }
966
902   /* ts_warn()
903   *
904 < * inputs       - var args message
905 < * output       - NONE
906 < * side effects - Call sendto_realops_flags, with some flood checking
907 < *                (at most 5 warnings every 5 seconds)
904 > * inputs       - var args message
905 > * output       - NONE
906 > * side effects - Call sendto_realops_flags, with some flood checking
907 > *                (at most 5 warnings every 5 seconds)
908   */
909   void
910 < ts_warn(const char *pattern, ...)
910 > sendto_realops_flags_ratelimited(const char *pattern, ...)
911   {
912    va_list args;
913 <  char buffer[IRCD_BUFSIZE];
913 >  char buffer[IRCD_BUFSIZE] = "";
914    static time_t last = 0;
915    static int warnings = 0;
916  
# Line 1005 | Line 940 | ts_warn(const char *pattern, ...)
940    ilog(LOG_TYPE_IRCD, "%s", buffer);
941   }
942  
943 < /* kill_client()
943 > /* sendto_wallops_flags()
944   *
945 < * inputs       - client to send kill towards
946 < *              - pointer to client to kill
947 < *              - reason for kill
948 < * output       - NONE
949 < * side effects - NONE
945 > * inputs       - flag types of messages to show to real opers
946 > *              - client sending request
947 > *              - var args input message
948 > * output       - NONE
949 > * side effects - Send a wallops to local opers
950   */
951   void
952 < kill_client(struct Client *client_p, struct Client *diedie,
953 <            const char *pattern, ...)
952 > sendto_wallops_flags(unsigned int flags, struct Client *source_p,
953 >                     const char *pattern, ...)
954   {
955 +  dlink_node *ptr = NULL;
956    va_list args;
957 <  char buffer[IRCD_BUFSIZE];
1022 <  int len;
1023 <
1024 <  if (client_p->from != NULL)
1025 <    client_p = client_p->from;
1026 <  if (IsDead(client_p))
1027 <    return;
1028 <
1029 <  len = sprintf(buffer, ":%s KILL %s :", ID_or_name(&me, client_p->from),
1030 <                ID_or_name(diedie, client_p));
1031 <
1032 <  va_start(args, pattern);
1033 <  len += send_format(&buffer[len], IRCD_BUFSIZE - len, pattern, args);
1034 <  va_end(args);
1035 <
1036 <  send_message(client_p, buffer, len);
1037 < }
957 >  struct dbuf_block *buffer;
958  
959 < /* kill_client_ll_serv_butone()
1040 < *
1041 < * inputs       - pointer to client to not send to
1042 < *              - pointer to client to kill
1043 < * output       - NONE
1044 < * side effects - Send a KILL for the given client
1045 < *                message to all connected servers
1046 < *                except the client 'one'. Also deal with
1047 < *                client being unknown to leaf, as in lazylink...
1048 < */
1049 < void
1050 < kill_client_ll_serv_butone(struct Client *one, struct Client *source_p,
1051 <                           const char *pattern, ...)
1052 < {
1053 <  va_list args;
1054 <  int have_uid = 0;
1055 <  dlink_node *ptr = NULL;
1056 <  char buf_uid[IRCD_BUFSIZE], buf_nick[IRCD_BUFSIZE];
1057 <  int len_uid = 0, len_nick = 0;
959 >  buffer = dbuf_alloc();
960  
961 <  if (HasID(source_p))
962 <  {
963 <    have_uid = 1;
964 <    va_start(args, pattern);
1063 <    len_uid = sprintf(buf_uid, ":%s KILL %s :", me.id, ID(source_p));
1064 <    len_uid += send_format(&buf_uid[len_uid], IRCD_BUFSIZE - len_uid, pattern,
1065 <                           args);
1066 <    va_end(args);
1067 <  }
961 >  if (IsClient(source_p))
962 >    dbuf_put_fmt(buffer, ":%s!%s@%s WALLOPS :", source_p->name, source_p->username, source_p->host);
963 >  else
964 >    dbuf_put_fmt(buffer, ":%s WALLOPS :", source_p->name);
965  
966    va_start(args, pattern);
967 <  len_nick = sprintf(buf_nick, ":%s KILL %s :", me.name, source_p->name);
1071 <  len_nick += send_format(&buf_nick[len_nick], IRCD_BUFSIZE - len_nick, pattern,
1072 <                          args);
967 >  send_format(buffer, pattern, args);
968    va_end(args);
969  
970 <  DLINK_FOREACH(ptr, serv_list.head)
970 >  DLINK_FOREACH(ptr, oper_list.head)
971    {
972      struct Client *client_p = ptr->data;
973 +    assert(client_p->umodes & UMODE_OPER);
974  
975 <    if (one != NULL && (client_p == one->from))
976 <      continue;
1081 <    if (IsDefunct(client_p))
1082 <      continue;
1083 <
1084 <    if (have_uid && IsCapable(client_p, CAP_TS6))
1085 <      send_message(client_p, buf_uid, len_uid);
1086 <    else
1087 <      send_message(client_p, buf_nick, len_nick);
975 >    if (HasUMode(client_p, flags) && !IsDefunct(client_p))
976 >      send_message(client_p, buffer);
977    }
978 < }
978 >
979 >  dbuf_ref_free(buffer);
980 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)