nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commands.c (1690B)


      1 #include "commands.h"
      2 
      3 #ifndef USING_SKALIBS
      4 #include "substdio.h"
      5 #include "stralloc.h"
      6 #else
      7 #include <skalibs/buffer.h>
      8 #include <skalibs/stralloc.h>
      9 #endif
     10 #include "str.h"
     11 #include "case.h"
     12 
     13 #ifdef USING_SKALIBS
     14 static stralloc cmd = STRALLOC_ZERO;
     15 #else
     16 static stralloc cmd = {0};
     17 #endif
     18 
     19 // XXX: not instantiable.
     20 
     21 #ifdef USING_SKALIBS
     22 int commands (
     23  buffer *bio,
     24  struct commands *c
     25 ) {
     26 #else
     27 int commands (
     28  substdio *ss,
     29  struct commands *c
     30 ) {
     31 #endif
     32   unsigned int i;
     33   char *arg;
     34 
     35   for (;;) {
     36 	/* This isn't very optimized for the modern world.
     37 	   It's basically a spinlock, only protected from
     38        going insane on your CPU by the fact that the
     39        socket is blocking.
     40 
     41        Even if we are only handling one socket in smtpd,
     42        that was 1997. This is 2021. We aren't doing that
     43        anymore. smtpd has to do iauth and, if it's running
     44        on the submission port, it has to do SASL too.
     45 
     46        Wait: I misinterpreted. It's totally fine on that
     47        front. But the way it re
     48         ~ Amelia */
     49     if (!stralloc_copys(&cmd,"")) return -1;
     50 
     51     for (;;) {
     52       int j;
     53       if (!stralloc_readyplus(&cmd,1)) return -1;
     54 #ifdef USING_SKALIBS
     55       j = buffer_get(bio,cmd.s + cmd.len,1);
     56 #else
     57       j = substdio_get(ss,cmd.s + cmd.len,1);
     58 #endif
     59       if (j != 1) return j;
     60       if (cmd.s[cmd.len] == '\n') break;
     61       ++cmd.len;
     62     }
     63 
     64     if (cmd.len > 0) if (cmd.s[cmd.len - 1] == '\r') --cmd.len;
     65 
     66     cmd.s[cmd.len] = 0;
     67 
     68     i = str_chr(cmd.s,' ');
     69     arg = cmd.s + i;
     70     while (*arg == ' ') ++arg;
     71     cmd.s[i] = 0;
     72 
     73     for (i = 0;c[i].text;++i) if (case_equals(c[i].text,cmd.s)) break;
     74     c[i].fun(arg);
     75     if (c[i].flush) c[i].flush();
     76   }
     77 }