source: zwrite.c @ ef742ad

release-1.8
Last change on this file since ef742ad was ef742ad, checked in by David Benjamin <davidben@mit.edu>, 12 years ago
Don’t send public pings on zwrite '' or zwrite @REALM Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: David Benjamin <davidben@mit.edu>
  • Property mode set to 100644
File size: 9.1 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
[6829afc]7CALLER_OWN owl_zwrite *owl_zwrite_new(const char *line)
[987cf3f]8{
[96828e4]9  owl_zwrite *z = g_new(owl_zwrite, 1);
[987cf3f]10  if (owl_zwrite_create_from_line(z, line) < 0) {
11    owl_zwrite_delete(z);
12    return NULL;
13  }
14  return z;
15}
16
[d427f08]17G_GNUC_WARN_UNUSED_RESULT int owl_zwrite_create_from_line(owl_zwrite *z, const char *line)
[ce7db4d]18{
[c6b1782]19  int argc, badargs, myargc;
[65b2173]20  char **argv;
[e19eb97]21  const char *const *myargv;
[a52d13a]22  char *msg = NULL;
[7d4fbcd]23
24  badargs=0;
25 
[69f89c7]26  /* start with null entries */
[987cf3f]27  z->cmd=NULL;
[ce7db4d]28  z->realm=NULL;
29  z->class=NULL;
30  z->inst=NULL;
31  z->opcode=NULL;
32  z->zsig=NULL;
33  z->message=NULL;
[7d4fbcd]34  z->cc=0;
35  z->noping=0;
[12294d2]36  z->recips = g_ptr_array_new();
[d4927a7]37  z->zwriteline = g_strdup(line);
[7d4fbcd]38
39  /* parse the command line for options */
[c529ac8]40  argv=owl_parseline(line, &argc);
41  myargv=strs(argv);
[7d4fbcd]42  if (argc<0) {
[836ea3a3]43    owl_function_error("Unbalanced quotes in zwrite");
[7d4fbcd]44    return(-1);
45  }
46  myargc=argc;
[e1c4636]47  if (myargc && *(myargv[0])!='-') {
[d4927a7]48    z->cmd=g_strdup(myargv[0]);
[e1c4636]49    myargc--;
50    myargv++;
51  }
[7d4fbcd]52  while (myargc) {
53    if (!strcmp(myargv[0], "-c")) {
54      if (myargc<2) {
55        badargs=1;
56        break;
57      }
[4b17a6c]58      z->class=owl_validate_utf8(myargv[1]);
[7d4fbcd]59      myargv+=2;
60      myargc-=2;
61    } else if (!strcmp(myargv[0], "-i")) {
62      if (myargc<2) {
63        badargs=1;
64        break;
65      }
[4b17a6c]66      z->inst=owl_validate_utf8(myargv[1]);
[7d4fbcd]67      myargv+=2;
68      myargc-=2;
69    } else if (!strcmp(myargv[0], "-r")) {
70      if (myargc<2) {
71        badargs=1;
72        break;
73      }
[4b17a6c]74      z->realm=owl_validate_utf8(myargv[1]);
[ce7db4d]75      myargv+=2;
76      myargc-=2;
77    } else if (!strcmp(myargv[0], "-s")) {
78      if (myargc<2) {
79        badargs=1;
80        break;
81      }
[4b17a6c]82      z->zsig=owl_validate_utf8(myargv[1]);
[7d4fbcd]83      myargv+=2;
84      myargc-=2;
85    } else if (!strcmp(myargv[0], "-O")) {
86      if (myargc<2) {
87        badargs=1;
88        break;
89      }
[4b17a6c]90      z->opcode=owl_validate_utf8(myargv[1]);
[7d4fbcd]91      myargv+=2;
92      myargc-=2;
[ce7db4d]93    } else if (!strcmp(myargv[0], "-m")) {
94      if (myargc<2) {
95        badargs=1;
96        break;
97      }
[69f89c7]98      /* we must already have users or a class or an instance */
[12294d2]99      if (z->recips->len < 1 && (!z->class) && (!z->inst)) {
[ce7db4d]100        badargs=1;
101        break;
102      }
103
104      /* Once we have -m, gobble up everything else on the line */
105      myargv++;
106      myargc--;
[c6b1782]107      msg = g_strjoinv(" ", (char**)myargv);
[ce7db4d]108      break;
[7d4fbcd]109    } else if (!strcmp(myargv[0], "-C")) {
110      z->cc=1;
111      myargv++;
112      myargc--;
113    } else if (!strcmp(myargv[0], "-n")) {
114      z->noping=1;
115      myargv++;
116      myargc--;
117    } else {
118      /* anything unattached is a recipient */
[12294d2]119      g_ptr_array_add(z->recips, owl_validate_utf8(myargv[0]));
[7d4fbcd]120      myargv++;
121      myargc--;
122    }
123  }
124
[e56303f]125  g_strfreev(argv);
[7d4fbcd]126
127  if (badargs) {
128    return(-1);
129  }
130
[1fe100c]131  if (z->class == NULL &&
132      z->inst == NULL &&
[12294d2]133      z->recips->len == 0) {
[1fe100c]134    owl_function_error("You must specify a recipient for zwrite");
135    return(-1);
136  }
137
[ce7db4d]138  /* now deal with defaults */
[d4927a7]139  if (z->class==NULL) z->class=g_strdup("message");
140  if (z->inst==NULL) z->inst=g_strdup("personal");
141  if (z->realm==NULL) z->realm=g_strdup("");
142  if (z->opcode==NULL) z->opcode=g_strdup("");
[ce7db4d]143  /* z->message is allowed to stay NULL */
[a52d13a]144
145  if(msg) {
146    owl_zwrite_set_message(z, msg);
[ddbbcffa]147    g_free(msg);
[a52d13a]148  }
149
[3f3ee61]150  return(0);
151}
152
153void owl_zwrite_populate_zsig(owl_zwrite *z)
154{
[ce7db4d]155  /* get a zsig, if not given */
[de3f641]156  if (z->zsig != NULL)
157    return;
[ce7db4d]158
[de3f641]159  z->zsig = owl_perlconfig_execute(owl_global_get_zsigfunc(&g));
[7d4fbcd]160}
161
[a352029b]162void owl_zwrite_send_ping(const owl_zwrite *z)
[ce7db4d]163{
[12294d2]164  int i;
[44a61ac]165  char *to;
[7d4fbcd]166
167  if (z->noping) return;
168 
[3ef779b]169  if (strcasecmp(z->class, "message")) {
[7d4fbcd]170    return;
171  }
172
[12294d2]173  for (i = 0; i < z->recips->len; i++) {
[3f52e14]174    to = owl_zwrite_get_recip_n_with_realm(z, i);
[ef742ad]175    if (owl_zwrite_recip_is_personal(to))
176      send_ping(to, z->class, z->inst);
[ddbbcffa]177    g_free(to);
[7d4fbcd]178  }
179
180}
181
[7bfc613]182/* Set the message with no post-processing*/
183void owl_zwrite_set_message_raw(owl_zwrite *z, const char *msg)
184{
[3b8a563]185  g_free(z->message);
[7bfc613]186  z->message = owl_validate_utf8(msg);
187}
188
[e19eb97]189void owl_zwrite_set_message(owl_zwrite *z, const char *msg)
[ce7db4d]190{
[12294d2]191  int i;
[3f52e14]192  GString *message;
[a52d13a]193  char *tmp = NULL, *tmp2;
[db2dd3d]194
[3b8a563]195  g_free(z->message);
[db2dd3d]196
[12294d2]197  if (z->recips->len > 0 && z->cc) {
[3f52e14]198    message = g_string_new("CC: ");
[12294d2]199    for (i = 0; i < z->recips->len; i++) {
[3f52e14]200      tmp = owl_zwrite_get_recip_n_with_realm(z, i);
201      g_string_append_printf(message, "%s ", tmp);
[ddbbcffa]202      g_free(tmp);
[3538bc8]203      tmp = NULL;
[db2dd3d]204    }
[4b17a6c]205    tmp = owl_validate_utf8(msg);
[a52d13a]206    tmp2 = owl_text_expand_tabs(tmp);
[3f52e14]207    g_string_append_printf(message, "\n%s", tmp2);
208    z->message = g_string_free(message, false);
[ddbbcffa]209    g_free(tmp);
210    g_free(tmp2);
[db2dd3d]211  } else {
[a52d13a]212    tmp=owl_validate_utf8(msg);
213    z->message=owl_text_expand_tabs(tmp);
[ddbbcffa]214    g_free(tmp);
[db2dd3d]215  }
[ce7db4d]216}
217
[a352029b]218const char *owl_zwrite_get_message(const owl_zwrite *z)
[ce7db4d]219{
220  if (z->message) return(z->message);
221  return("");
222}
223
[a352029b]224int owl_zwrite_is_message_set(const owl_zwrite *z)
[ce7db4d]225{
226  if (z->message) return(1);
227  return(0);
228}
229
[a352029b]230int owl_zwrite_send_message(const owl_zwrite *z)
[ce7db4d]231{
[12294d2]232  int i, ret = 0;
[823671c]233  char *to = NULL;
[7d4fbcd]234
[ce7db4d]235  if (z->message==NULL) return(-1);
236
[12294d2]237  if (z->recips->len > 0) {
238    for (i = 0; i < z->recips->len; i++) {
[3f52e14]239      to = owl_zwrite_get_recip_n_with_realm(z, i);
[0743696]240      ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
241      /* Abort on the first error, to match the zwrite binary. */
242      if (ret != 0)
243        break;
[ddbbcffa]244      g_free(to);
[823671c]245      to = NULL;
[7d4fbcd]246    }
247  } else {
[3472845]248    to = g_strdup_printf( "@%s", z->realm);
[0743696]249    ret = send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message);
[ce7db4d]250  }
[ddbbcffa]251  g_free(to);
[0743696]252  return ret;
[ce7db4d]253}
254
[e19eb97]255int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg)
[ce7db4d]256{
257  owl_zwrite z;
258  int rv;
259  rv=owl_zwrite_create_from_line(&z, cmd);
260  if (rv) return(rv);
261  if (!owl_zwrite_is_message_set(&z)) {
262    owl_zwrite_set_message(&z, msg);
[7d4fbcd]263  }
[3f3ee61]264  owl_zwrite_populate_zsig(&z);
[ce7db4d]265  owl_zwrite_send_message(&z);
[c230bc1]266  owl_zwrite_cleanup(&z);
[ce7db4d]267  return(0);
[7d4fbcd]268}
269
[a352029b]270const char *owl_zwrite_get_class(const owl_zwrite *z)
[ce7db4d]271{
[7d4fbcd]272  return(z->class);
273}
274
[a352029b]275const char *owl_zwrite_get_instance(const owl_zwrite *z)
[ce7db4d]276{
[7d4fbcd]277  return(z->inst);
278}
279
[a352029b]280const char *owl_zwrite_get_opcode(const owl_zwrite *z)
[ce7db4d]281{
[4b464a4]282  return(z->opcode);
283}
284
[e19eb97]285void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode)
[9ceee9d]286{
[3b8a563]287  g_free(z->opcode);
[4b17a6c]288  z->opcode=owl_validate_utf8(opcode);
[9ceee9d]289}
290
[a352029b]291const char *owl_zwrite_get_realm(const owl_zwrite *z)
[ce7db4d]292{
[7d4fbcd]293  return(z->realm);
294}
295
[a352029b]296const char *owl_zwrite_get_zsig(const owl_zwrite *z)
[ce7db4d]297{
298  if (z->zsig) return(z->zsig);
299  return("");
[56330ff]300}
301
[24ccc01]302void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig)
303{
[3b8a563]304  g_free(z->zsig);
[d4927a7]305  z->zsig = g_strdup(zsig);
[24ccc01]306}
307
[a352029b]308int owl_zwrite_get_numrecips(const owl_zwrite *z)
[ce7db4d]309{
[12294d2]310  return z->recips->len;
[7d4fbcd]311}
312
[a352029b]313const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n)
[ce7db4d]314{
[12294d2]315  return z->recips->pdata[n];
[7d4fbcd]316}
317
[3f52e14]318/* Caller must free the result. */
[6829afc]319CALLER_OWN char *owl_zwrite_get_recip_n_with_realm(const owl_zwrite *z, int n)
[3f52e14]320{
321  if (z->realm[0]) {
322    return g_strdup_printf("%s@%s", owl_zwrite_get_recip_n(z, n), z->realm);
323  } else {
324    return g_strdup(owl_zwrite_get_recip_n(z, n));
325  }
326}
327
[3965ec3d]328bool owl_zwrite_recip_is_personal(const char *recipient)
329{
330  return recipient[0] && recipient[0] != '@';
331}
332
[18250fa]333bool owl_zwrite_is_personal(const owl_zwrite *z)
[ce7db4d]334{
[7d4fbcd]335  /* return true if at least one of the recipients is personal */
[12294d2]336  int i;
[7d4fbcd]337
[18250fa]338  for (i = 0; i < z->recips->len; i++)
339    if (owl_zwrite_recip_is_personal(z->recips->pdata[i]))
340      return true;
341  return false;
[7d4fbcd]342}
[3f3ee61]343
[987cf3f]344void owl_zwrite_delete(owl_zwrite *z)
345{
346  owl_zwrite_cleanup(z);
[ddbbcffa]347  g_free(z);
[987cf3f]348}
349
[c230bc1]350void owl_zwrite_cleanup(owl_zwrite *z)
[ce7db4d]351{
[3cdd6d2]352  owl_ptr_array_free(z->recips, g_free);
[3b8a563]353  g_free(z->cmd);
354  g_free(z->zwriteline);
355  g_free(z->class);
356  g_free(z->inst);
357  g_free(z->opcode);
358  g_free(z->realm);
359  g_free(z->message);
360  g_free(z->zsig);
[7d4fbcd]361}
[719119de]362
363/*
364 * Returns a zwrite line suitable for replying, specifically the
365 * message field is stripped out. Result should be freed with
[ddbbcffa]366 * g_free.
[a5b5d00]367 *
368 * If not a CC, only the recip_index'th user will be replied to.
[719119de]369 */
[6829afc]370CALLER_OWN char *owl_zwrite_get_replyline(const owl_zwrite *z, int recip_index)
[719119de]371{
372  /* Match ordering in zwrite help. */
373  GString *buf = g_string_new("");
374  int i;
375
376  /* Disturbingly, it is apparently possible to z->cmd to be null if
377   * owl_zwrite_create_from_line got something starting with -. And we
378   * can't kill it because this is exported to perl. */
379  owl_string_append_quoted_arg(buf, z->cmd ? z->cmd : "zwrite");
380  if (z->noping) {
381    g_string_append(buf, " -n");
382  }
383  if (z->cc) {
384    g_string_append(buf, " -C");
385  }
386  if (strcmp(z->class, "message")) {
387    g_string_append(buf, " -c ");
388    owl_string_append_quoted_arg(buf, z->class);
389  }
390  if (strcmp(z->inst, "personal")) {
391    g_string_append(buf, " -i ");
392    owl_string_append_quoted_arg(buf, z->inst);
393  }
394  if (z->realm && z->realm[0] != '\0') {
395    g_string_append(buf, " -r ");
396    owl_string_append_quoted_arg(buf, z->realm);
397  }
398  if (z->opcode && z->opcode[0] != '\0') {
399    g_string_append(buf, " -O ");
400    owl_string_append_quoted_arg(buf, z->opcode);
401  }
[a5b5d00]402  if (z->cc) {
[12294d2]403    for (i = 0; i < z->recips->len; i++) {
[a5b5d00]404      g_string_append_c(buf, ' ');
[12294d2]405      owl_string_append_quoted_arg(buf, z->recips->pdata[i]);
[a5b5d00]406    }
[12294d2]407  } else if (recip_index < z->recips->len) {
[719119de]408    g_string_append_c(buf, ' ');
[12294d2]409    owl_string_append_quoted_arg(buf, z->recips->pdata[recip_index]);
[719119de]410  }
411
412  return g_string_free(buf, false);
413}
Note: See TracBrowser for help on using the repository browser.