aboutsummaryrefslogtreecommitdiff
path: root/misc/poc/firma-0.1.6
blob: 239c9a585aa1af2ff8afb9f82525cddcc2ce6151 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
#
# firma v0.2: simple encrypted mailing list aliases
# feedback: rhatto@riseup.net | GPL
#
# list configuration is passed through a config file,
# where you put PARAMETER=value (whithout spaces)
#
# MAIL= path for mail program
# GPG= path for gnupg binary
# TMP= where you want the temp files
# LISTNAME= list email
# GPGDIR= gpg dir for the lists' keyring
# PASSWD= passwd for the lists' keyring
# FOOTER= message footer
# ALLOWSENDKEY = set to 1 if you want people automatically receive the list
#                key requesting through listname-request@example.tld
#                with subject: key
#
# design / todo:
#
# - list-request:
#     - key (allow send key)
#     - help
#     - subscribe: exchange pubkey
#     - unsubscribe
# - strings
# - check signatures
# - create list
# - archive (optional)
# - logfile (optional)
# - gpg --no-tty --display-charset --utf8-strings ?
#
# sintax: firma -c || firma config-file
#         -c: create a new list
#         config-file: parse the email from stdin
#         with the parameters specified in the
#         config-file 
#
# fix:
# 
# - special chars
# - id's recipient selection
# 

fuction _refresh_cache { 
  rm $1 $1.gpg
  touch $1; chmod 600 $1;
  touch $1.gpg; chmod 600 $TMP.gpg;
}

function _process_message {
  # get the headers
  FROM=$(grep -m 1 ^From: $1 | cut -f 2 -d :)
  DATE=$(grep -m 1 ^Date: $1)
  SUBJECT=$(grep -m 1 ^Subject: $1)

  # detect the encrypted message
  sed -n '/-----BEGIN PGP MESSAGE-----/,/-----END PGP MESSAGE-----/p' $1 >> $1.gpg

  # encrypting and sending for each recipient on the list
  for EMAIL in $($GPGLIST | grep pub | cut -d "<" -f 2 | sed -e 's/>//' | grep @ | grep -v $LISTNAME); do 

    echo "$PASSWD
    Message from: $FROM
    $SUBJECT
    $DATE

    $(echo "$PASSWD" | $GPGDECRYPT $1.gpg)

    ---
    $FOOTER 
    " | sed -e 's/=20$//' | $GPGENCRYPT $EMAIL | $MAIL -r $LISTNAME $EMAIL 
  done
}

function _process_request {

  # todo: support subjects like "key   ", etc
  FROM=$(grep -m 1 ^From: $1 | cut -f 2 -d :)
  REQUEST=$(grep -m 1 ^Subject: $1)
  if [[ $REQUEST == "key" ]]; then
    if [[ $ALLOWSENDKEY == 1 ]]; then  
      # send key to From: recipient
    else
      # dont send the key; return error message
    fi
  else if [[ $REQUEST == "subscribe" ]]; then
    # check if user put its pubkey and
    # ask the list for subscribe From: recipient
  else if [[ $REQUEST == "unsubscribe" ]]; then
    # unsubscribe and advise the list
  else
    # error message
  fi 

}

function _process {

  # eval the config file
  source $1

  GPGCOMMAND="$GPG -q --homedir $GPGDIR"
  GPGLIST="$GPGCOMMAND --list-keys" 
  GPGDECRYPT="$GPGCOMMAND --decrypt"
  GPGENCRYPT="$GPGCOMMAND --always-trust --hidden-recipient --textmode -e -s -a -r" 

  # clear the cache before read the message
  _refresh_cache $TMP

  # todo: use an array
  while read STDIN; do
    echo $STDIN >> $TMP
  done 

  # check with action is requested depending on the To: field
  TO=$(grep -m 1 ^To: $)
  if [[ $TO == $LISTNAME ]]; then _process_message $TMP;
  else _process_request $TMP;
  fi

  # clear after process
  _refresh_cache $TMP

}

function newlist {

  LISTHOME = 
  LISTNANE = 
  ...

  $GPGCOMMAND --gen-key

}

# check sintax
if [[ $1 = "-c" ]]; then
  _newlist;
else if [ -f $1 ];
  then _process $1;
else
  echo sintax: $0 [-c] [config-file];
fi

rm $TMP $TMP.gpg