source: zwrite.c @ 776c4bb

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 776c4bb was d43edd2, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Death to RCS keywords. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 8.7 KB
RevLine 
[7d4fbcd]1#include <string.h>
[56330ff]2#include <pwd.h>
3#include <sys/types.h>
4#include <unistd.h>
[7d4fbcd]5#include "owl.h"
6
[ce7db4d]7int owl_zwrite_create_from_line(owl_zwrite *z, char *line)
8{
[a52d13a]9  int argc, badargs, myargc, i, len;
[7d4fbcd]10  char **argv, **myargv;
[a52d13a]11  char *msg = NULL;
[7d4fbcd]12
13  badargs=0;
14 
[69f89c7]15  /* start with null entries */
[ce7db4d]16  z->realm=NULL;
17  z->class=NULL;
18  z->inst=NULL;
19  z->opcode=NULL;
20  z->zsig=NULL;
21  z->message=NULL;
[7d4fbcd]22  z->cc=0;
23  z->noping=0;
24  owl_list_create(&(z->recips));
25
26  /* parse the command line for options */
27  argv=myargv=owl_parseline(line, &argc);
28  if (argc<0) {
[836ea3a3]29    owl_function_error("Unbalanced quotes in zwrite");
[7d4fbcd]30    return(-1);
31  }
32  myargc=argc;
[e1c4636]33  if (myargc && *(myargv[0])!='-') {
34    myargc--;
35    myargv++;
36  }
[7d4fbcd]37  while (myargc) {
38    if (!strcmp(myargv[0], "-c")) {
39      if (myargc<2) {
40        badargs=1;
41        break;
42      }
[4b17a6c]43      z->class=owl_validate_utf8(myargv[1]);
[7d4fbcd]44      myargv+=2;
45      myargc-=2;
46    } else if (!strcmp(myargv[0], "-i")) {
47      if (myargc<2) {
48        badargs=1;
49        break;
50      }
[4b17a6c]51      z->inst=owl_validate_utf8(myargv[1]);
[7d4fbcd]52      myargv+=2;
53      myargc-=2;
54    } else if (!strcmp(myargv[0], "-r")) {
55      if (myargc<2) {
56        badargs=1;
57        break;
58      }
[4b17a6c]59      z->realm=owl_validate_utf8(myargv[1]);
[ce7db4d]60      myargv+=2;
61      myargc-=2;
62    } else if (!strcmp(myargv[0], "-s")) {
63      if (myargc<2) {
64        badargs=1;
65        break;
66      }
[4b17a6c]67      z->zsig=owl_validate_utf8(myargv[1]);
[7d4fbcd]68      myargv+=2;
69      myargc-=2;
70    } else if (!strcmp(myargv[0], "-O")) {
71      if (myargc<2) {
72        badargs=1;
73        break;
74      }
[4b17a6c]75      z->opcode=owl_validate_utf8(myargv[1]);
[7d4fbcd]76      myargv+=2;
77      myargc-=2;
[ce7db4d]78    } else if (!strcmp(myargv[0], "-m")) {
79      if (myargc<2) {
80        badargs=1;
81        break;
82      }
[69f89c7]83      /* we must already have users or a class or an instance */
[e016fc2]84      if (owl_list_get_size(&(z->recips))<1 && (!z->class) && (!z->inst)) {
[ce7db4d]85        badargs=1;
86        break;
87      }
88
89      /* Once we have -m, gobble up everything else on the line */
90      myargv++;
91      myargc--;
[a52d13a]92      len = 0;
93      for (i=0;i<myargc;i++) {
94        len += strlen(myargv[i-1]) + 1;
95      }
96      msg = owl_malloc(len);
97      msg[0] = '\0';
[ce7db4d]98      while (myargc) {
[a52d13a]99        strcat(msg, myargv[0]);
100        strcat(msg, " ");
101        myargc--;
102        myargv++;
[ce7db4d]103      }
[a52d13a]104      msg[strlen(msg)-1] = '\0';
[ce7db4d]105      break;
[7d4fbcd]106    } else if (!strcmp(myargv[0], "-C")) {
107      z->cc=1;
108      myargv++;
109      myargc--;
110    } else if (!strcmp(myargv[0], "-n")) {
111      z->noping=1;
112      myargv++;
113      myargc--;
114    } else {
115      /* anything unattached is a recipient */
[4b17a6c]116      owl_list_append_element(&(z->recips), owl_validate_utf8(myargv[0]));
[7d4fbcd]117      myargv++;
118      myargc--;
119    }
120  }
121
122  owl_parsefree(argv, argc);
123
124  if (badargs) {
125    return(-1);
126  }
127
[1fe100c]128  if (z->class == NULL &&
129      z->inst == NULL &&
130      owl_list_get_size(&(z->recips))==0) {
131    owl_function_error("You must specify a recipient for zwrite");
132    return(-1);
133  }
134
[ce7db4d]135  /* now deal with defaults */
136  if (z->class==NULL) z->class=owl_strdup("message");
137  if (z->inst==NULL) z->inst=owl_strdup("personal");
138  if (z->realm==NULL) z->realm=owl_strdup("");
139  if (z->opcode==NULL) z->opcode=owl_strdup("");
140  /* z->message is allowed to stay NULL */
[a52d13a]141
142  if(msg) {
143    owl_zwrite_set_message(z, msg);
144    owl_free(msg);
145  }
146
[3f3ee61]147  return(0);
148}
149
150void owl_zwrite_populate_zsig(owl_zwrite *z)
151{
152  char *zsigproc, *zsigowlvar, *zsigzvar, *ptr;
153  struct passwd *pw;
154
[ce7db4d]155  /* get a zsig, if not given */
156  if (z->zsig==NULL) {
157    zsigproc = owl_global_get_zsigproc(&g);
158    zsigowlvar = owl_global_get_zsig(&g);
[09489b89]159    zsigzvar = owl_zephyr_get_variable("zwrite-signature");
[ce7db4d]160
161    if (zsigowlvar && *zsigowlvar) {
[4b17a6c]162      z->zsig=owl_validate_utf8(zsigowlvar);
[ce7db4d]163    } else if (zsigproc && *zsigproc) {
164      FILE *file;
165      char buff[LINE], *openline;
166     
167      /* simple hack for now to nuke stderr */
168      openline=owl_malloc(strlen(zsigproc)+40);
169      strcpy(openline, zsigproc);
[2a2bb60]170#if !(OWL_STDERR_REDIR)
[ce7db4d]171      strcat(openline, " 2> /dev/null");
[2a2bb60]172#endif
[ce7db4d]173      file=popen(openline, "r");
174      owl_free(openline);
175      if (!file) {
176        if (zsigzvar && *zsigzvar) {
[4b17a6c]177          z->zsig=owl_validate_utf8(zsigzvar);
[ce7db4d]178        }
179      } else {
180        z->zsig=owl_malloc(LINE*5);
181        strcpy(z->zsig, "");
182        while (fgets(buff, LINE, file)) { /* wrong sizing */
183          strcat(z->zsig, buff);
184        }
185        pclose(file);
186        if (z->zsig[strlen(z->zsig)-1]=='\n') {
187          z->zsig[strlen(z->zsig)-1]='\0';
188        }
[56330ff]189      }
[ce7db4d]190    } else if (zsigzvar) {
[4b17a6c]191      z->zsig=owl_validate_utf8(zsigzvar);
[ce7db4d]192    } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) {
[4b17a6c]193      z->zsig=owl_validate_utf8(pw->pw_gecos);
[ce7db4d]194      ptr=strchr(z->zsig, ',');
195      if (ptr) {
196        ptr[0]='\0';
[56330ff]197      }
198    }
199  }
[7d4fbcd]200}
201
[ce7db4d]202void owl_zwrite_send_ping(owl_zwrite *z)
203{
[7d4fbcd]204  int i, j;
[44a61ac]205  char *to;
[7d4fbcd]206
207  if (z->noping) return;
208 
[3ef779b]209  if (strcasecmp(z->class, "message")) {
[7d4fbcd]210    return;
211  }
212
213  /* if there are no recipients we won't send a ping, which
214     is what we want */
215  j=owl_list_get_size(&(z->recips));
216  for (i=0; i<j; i++) {
217    if (strcmp(z->realm, "")) {
[44a61ac]218      to = owl_sprintf("%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm);
[7d4fbcd]219    } else {
[44a61ac]220      to = owl_strdup(owl_list_get_element(&(z->recips), i));
[7d4fbcd]221    }
[3ef779b]222    send_ping(to, z->class, z->inst);
[44a61ac]223    owl_free(to);
[7d4fbcd]224  }
225
226}
227
[ce7db4d]228void owl_zwrite_set_message(owl_zwrite *z, char *msg)
229{
[db2dd3d]230  int i, j;
[3538bc8]231  char *toline = NULL;
[a52d13a]232  char *tmp = NULL, *tmp2;
[db2dd3d]233
[ce7db4d]234  if (z->message) owl_free(z->message);
[db2dd3d]235
236  j=owl_list_get_size(&(z->recips));
237  if (j>0 && z->cc) {
[3538bc8]238    toline = owl_strdup( "CC: ");
[db2dd3d]239    for (i=0; i<j; i++) {
[3538bc8]240      tmp = toline;
[db2dd3d]241      if (strcmp(z->realm, "")) {
[3538bc8]242        toline = owl_sprintf( "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm);
[db2dd3d]243      } else {
[3538bc8]244        toline = owl_sprintf( "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i));
[db2dd3d]245      }
[3538bc8]246      owl_free(tmp);
247      tmp = NULL;
[db2dd3d]248    }
[4b17a6c]249    tmp = owl_validate_utf8(msg);
[a52d13a]250    tmp2 = owl_text_expand_tabs(tmp);
251    z->message=owl_sprintf("%s\n%s", toline, tmp2);
[3538bc8]252    owl_free(toline);
[a52d13a]253    owl_free(tmp);
254    owl_free(tmp2);
[db2dd3d]255  } else {
[a52d13a]256    tmp=owl_validate_utf8(msg);
257    z->message=owl_text_expand_tabs(tmp);
258    owl_free(tmp);
[db2dd3d]259  }
[ce7db4d]260}
261
262char *owl_zwrite_get_message(owl_zwrite *z)
263{
264  if (z->message) return(z->message);
265  return("");
266}
267
268int owl_zwrite_is_message_set(owl_zwrite *z)
269{
270  if (z->message) return(1);
271  return(0);
272}
273
274int owl_zwrite_send_message(owl_zwrite *z)
275{
[7d4fbcd]276  int i, j;
[823671c]277  char *to = NULL;
[7d4fbcd]278
[ce7db4d]279  if (z->message==NULL) return(-1);
280
[7d4fbcd]281  j=owl_list_get_size(&(z->recips));
282  if (j>0) {
283    for (i=0; i<j; i++) {
284      if (strcmp(z->realm, "")) {
[823671c]285        to = owl_sprintf("%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm);
[7d4fbcd]286      } else {
[823671c]287        to = owl_strdup( owl_list_get_element(&(z->recips), i));
[7d4fbcd]288      }
[db2dd3d]289      send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
[823671c]290      owl_free(to);
291      to = NULL;
[7d4fbcd]292    }
293  } else {
[823671c]294    to = owl_sprintf( "@%s", z->realm);
[ce7db4d]295    send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
296  }
[823671c]297  owl_free(to);
[ce7db4d]298  return(0);
299}
300
301int owl_zwrite_create_and_send_from_line(char *cmd, char *msg)
302{
303  owl_zwrite z;
304  int rv;
305  rv=owl_zwrite_create_from_line(&z, cmd);
306  if (rv) return(rv);
307  if (!owl_zwrite_is_message_set(&z)) {
308    owl_zwrite_set_message(&z, msg);
[7d4fbcd]309  }
[3f3ee61]310  owl_zwrite_populate_zsig(&z);
[ce7db4d]311  owl_zwrite_send_message(&z);
312  owl_zwrite_free(&z);
313  return(0);
[7d4fbcd]314}
315
[ce7db4d]316char *owl_zwrite_get_class(owl_zwrite *z)
317{
[7d4fbcd]318  return(z->class);
319}
320
[ce7db4d]321char *owl_zwrite_get_instance(owl_zwrite *z)
322{
[7d4fbcd]323  return(z->inst);
324}
325
[ce7db4d]326char *owl_zwrite_get_opcode(owl_zwrite *z)
327{
[4b464a4]328  return(z->opcode);
329}
330
[9ceee9d]331void owl_zwrite_set_opcode(owl_zwrite *z, char *opcode)
332{
333  if (z->opcode) owl_free(z->opcode);
[4b17a6c]334  z->opcode=owl_validate_utf8(opcode);
[9ceee9d]335}
336
[ce7db4d]337char *owl_zwrite_get_realm(owl_zwrite *z)
338{
[7d4fbcd]339  return(z->realm);
340}
341
[ce7db4d]342char *owl_zwrite_get_zsig(owl_zwrite *z)
343{
344  if (z->zsig) return(z->zsig);
345  return("");
[56330ff]346}
347
[ce7db4d]348void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff)
349{
[7d4fbcd]350  int i, j;
351
352  strcpy(buff, "");
353  j=owl_list_get_size(&(z->recips));
354  for (i=0; i<j; i++) {
355    strcat(buff, owl_list_get_element(&(z->recips), i));
356    strcat(buff, " ");
357  }
358  buff[strlen(buff)-1]='\0';
359}
360
[ce7db4d]361int owl_zwrite_get_numrecips(owl_zwrite *z)
362{
[7d4fbcd]363  return(owl_list_get_size(&(z->recips)));
364}
365
[ce7db4d]366char *owl_zwrite_get_recip_n(owl_zwrite *z, int n)
367{
[7d4fbcd]368  return(owl_list_get_element(&(z->recips), n));
369}
370
[ce7db4d]371int owl_zwrite_is_personal(owl_zwrite *z)
372{
[7d4fbcd]373  /* return true if at least one of the recipients is personal */
374  int i, j;
375  char *foo;
376
377  j=owl_list_get_size(&(z->recips));
378  for (i=0; i<j; i++) {
379    foo=owl_list_get_element(&(z->recips), i);
380    if (foo[0]!='@') return(1);
381  }
382  return(0);
383}
[3f3ee61]384
[ce7db4d]385void owl_zwrite_free(owl_zwrite *z)
386{
[7d4fbcd]387  owl_list_free_all(&(z->recips), &owl_free);
[ce7db4d]388  if (z->class) owl_free(z->class);
389  if (z->inst) owl_free(z->inst);
390  if (z->opcode) owl_free(z->opcode);
391  if (z->realm) owl_free(z->realm);
392  if (z->message) owl_free(z->message);
393  if (z->zsig) owl_free(z->zsig);
[7d4fbcd]394}
Note: See TracBrowser for help on using the repository browser.