File: gnarwl.c

package info (click to toggle)
gnarwl 3.6.dfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 856 kB
  • ctags: 288
  • sloc: ansic: 1,163; makefile: 119; sh: 70; php: 44
file content (107 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#ifndef __LCLINT__
#include <syslog.h>
#endif

#include <conf.h>
#include "util.h"
#include "config.h"
#include "dbaccess.h"
#include "mailhandler.h"

/* 
 * We can't feed addresses from the "-a" switch directly to addAddr(),
 * as cfg.blacklist may not be set up, yet. So we cache them here.
 */
char** tmprecv=NULL;

void printUsage(void) {
  
  printf("\nGNARWL(v%s) - Gnu Neat Auto Reply With LDAP\n\n\
  Read email through stdin, reply to it, if necessary\n\
  Options:\n\n\
  \t-h\t\tprint help\n\
  \t-a <address>\tforce receiver address\n\
  \t-s <address>\tforce sender address\n\
  \t-c <cfgfile>\tuse configfile (default: %s)\n\
  \n",VERSION,CFGFILE);
  
  exit(EXIT_SUCCESS);
}

void tmpAddrStore(const char* adr) {
  int i=0;
  
  if(adr==NULL || adr[0]=='\0') return;
  
  while(tmprecv[i]!=NULL) {
    if(!strcasecmp(tmprecv[i],adr)) return;
    i++;
  }
  
  tmprecv=(char**)realloc(tmprecv,(i+2)*sizeof(char**));
  if(tmprecv==NULL) oom();
  tmprecv[i]=NULL;
  tmprecv[i+1]=NULL;
  cpyStr(&(tmprecv[i]),adr);
  cleanAddress(&(tmprecv[i]));
}


int main(int argc, char **argv) {
  int ch;
  char* cfg_file;
  char** rep;
  char* sndr=NULL;
  extern char** receivers;
  extern int verbose;
  
  cfg_file=CFGFILE;
  tmprecv=(char**)calloc(1,sizeof(char**));
  if (tmprecv==NULL) oom();
  
  while ((ch = getopt(argc, argv, "hc:a:s:")) != EOF) {
    switch((char)ch) {
      case 'h': printUsage(); break;
      case 'c': cfg_file=optarg;break;
      case 'a': tmpAddrStore(optarg);break;
      case 's': cpyStr(&sndr,optarg);cleanAddress(&sndr);break;
    }
  }
  
  openlog("gnarwl",LOG_PID,LOG_MAIL);
  readConf(cfg_file);
  
  if (receiveMail(tmprecv,sndr)==FALSE) return EXIT_SUCCESS;
  
  ch=0;
  dbConnect();

  while(receivers[ch]!=NULL) {
    int i=0;
    rep=dbQuery(receivers[ch]);
    while(rep[i]!=NULL) {
      if (dbCheck(sender,receivers[ch])) {
        translateString(&rep[i]);
        sendMail(receivers[ch],rep[i]);
        dbLock(sender,receivers[ch]);
      }
      else {
        if (verbose>=LVL_DEBUG) {
          syslog(LOG_MAIL|LOG_DEBUG,"DEBUG/MAIL blocked: %s & %s",sender,receivers[ch]);
        }
      }
      free(rep[i]);
      i++;
    }
    ch++;
  }

  dbDisconnect();
  closelog();
  return EXIT_SUCCESS;
}