[dab82f29] | 1 | /* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar |
---|
| 2 | * |
---|
| 3 | * This file is part of Owl. |
---|
| 4 | * |
---|
| 5 | * Owl is free software: you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * Owl is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with Owl. If not, see <http://www.gnu.org/licenses/>. |
---|
| 17 | * |
---|
| 18 | * --------------------------------------------------------------- |
---|
| 19 | * |
---|
| 20 | * As of Owl version 2.1.12 there are patches contributed by |
---|
| 21 | * developers of the the branched BarnOwl project, Copyright (c) |
---|
| 22 | * 2006-2008 The BarnOwl Developers. All rights reserved. |
---|
| 23 | */ |
---|
| 24 | |
---|
[7d4fbcd] | 25 | #include <string.h> |
---|
[56330ff] | 26 | #include <pwd.h> |
---|
| 27 | #include <sys/types.h> |
---|
| 28 | #include <unistd.h> |
---|
[7d4fbcd] | 29 | #include "owl.h" |
---|
| 30 | |
---|
[1aee7d9] | 31 | static const char fileIdent[] = "$Id$"; |
---|
| 32 | |
---|
[ce7db4d] | 33 | int owl_zwrite_create_from_line(owl_zwrite *z, char *line) |
---|
| 34 | { |
---|
[7d4fbcd] | 35 | int argc, badargs, myargc; |
---|
| 36 | char **argv, **myargv; |
---|
[ced25d1] | 37 | char *zsigproc, *zsigowlvar, *zsigzvar, *ptr; |
---|
[56330ff] | 38 | struct passwd *pw; |
---|
[7d4fbcd] | 39 | |
---|
| 40 | badargs=0; |
---|
| 41 | |
---|
[ce7db4d] | 42 | /* start with null entires */ |
---|
| 43 | z->realm=NULL; |
---|
| 44 | z->class=NULL; |
---|
| 45 | z->inst=NULL; |
---|
| 46 | z->opcode=NULL; |
---|
| 47 | z->zsig=NULL; |
---|
| 48 | z->message=NULL; |
---|
[7d4fbcd] | 49 | z->cc=0; |
---|
| 50 | z->noping=0; |
---|
| 51 | owl_list_create(&(z->recips)); |
---|
| 52 | |
---|
| 53 | /* parse the command line for options */ |
---|
| 54 | argv=myargv=owl_parseline(line, &argc); |
---|
| 55 | if (argc<0) { |
---|
[836ea3a3] | 56 | owl_function_error("Unbalanced quotes in zwrite"); |
---|
[7d4fbcd] | 57 | return(-1); |
---|
| 58 | } |
---|
| 59 | myargc=argc; |
---|
[e1c4636] | 60 | if (myargc && *(myargv[0])!='-') { |
---|
| 61 | myargc--; |
---|
| 62 | myargv++; |
---|
| 63 | } |
---|
[7d4fbcd] | 64 | while (myargc) { |
---|
| 65 | if (!strcmp(myargv[0], "-c")) { |
---|
| 66 | if (myargc<2) { |
---|
| 67 | badargs=1; |
---|
| 68 | break; |
---|
| 69 | } |
---|
[ce7db4d] | 70 | z->class=owl_strdup(myargv[1]); |
---|
[7d4fbcd] | 71 | myargv+=2; |
---|
| 72 | myargc-=2; |
---|
| 73 | } else if (!strcmp(myargv[0], "-i")) { |
---|
| 74 | if (myargc<2) { |
---|
| 75 | badargs=1; |
---|
| 76 | break; |
---|
| 77 | } |
---|
[ce7db4d] | 78 | z->inst=owl_strdup(myargv[1]); |
---|
[7d4fbcd] | 79 | myargv+=2; |
---|
| 80 | myargc-=2; |
---|
| 81 | } else if (!strcmp(myargv[0], "-r")) { |
---|
| 82 | if (myargc<2) { |
---|
| 83 | badargs=1; |
---|
| 84 | break; |
---|
| 85 | } |
---|
[ce7db4d] | 86 | z->realm=owl_strdup(myargv[1]); |
---|
| 87 | myargv+=2; |
---|
| 88 | myargc-=2; |
---|
| 89 | } else if (!strcmp(myargv[0], "-s")) { |
---|
| 90 | if (myargc<2) { |
---|
| 91 | badargs=1; |
---|
| 92 | break; |
---|
| 93 | } |
---|
| 94 | z->zsig=owl_strdup(myargv[1]); |
---|
[7d4fbcd] | 95 | myargv+=2; |
---|
| 96 | myargc-=2; |
---|
| 97 | } else if (!strcmp(myargv[0], "-O")) { |
---|
| 98 | if (myargc<2) { |
---|
| 99 | badargs=1; |
---|
| 100 | break; |
---|
| 101 | } |
---|
[ce7db4d] | 102 | z->opcode=owl_strdup(myargv[1]); |
---|
[7d4fbcd] | 103 | myargv+=2; |
---|
| 104 | myargc-=2; |
---|
[ce7db4d] | 105 | } else if (!strcmp(myargv[0], "-m")) { |
---|
| 106 | if (myargc<2) { |
---|
| 107 | badargs=1; |
---|
| 108 | break; |
---|
| 109 | } |
---|
[e016fc2] | 110 | /* we must already have users or a class or an instance*/ |
---|
| 111 | if (owl_list_get_size(&(z->recips))<1 && (!z->class) && (!z->inst)) { |
---|
[ce7db4d] | 112 | badargs=1; |
---|
| 113 | break; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /* Once we have -m, gobble up everything else on the line */ |
---|
| 117 | myargv++; |
---|
| 118 | myargc--; |
---|
| 119 | z->message=owl_strdup(""); |
---|
| 120 | while (myargc) { |
---|
| 121 | z->message=realloc(z->message, strlen(z->message)+strlen(myargv[0])+5); |
---|
| 122 | strcat(z->message, myargv[0]); |
---|
[ae0a5fc] | 123 | strcat(z->message, " "); |
---|
[ce7db4d] | 124 | myargc--; |
---|
| 125 | myargv++; |
---|
| 126 | } |
---|
[ae0a5fc] | 127 | z->message[strlen(z->message)-1]='\0'; /* remove last space */ |
---|
[ce7db4d] | 128 | break; |
---|
[7d4fbcd] | 129 | } else if (!strcmp(myargv[0], "-C")) { |
---|
| 130 | z->cc=1; |
---|
| 131 | myargv++; |
---|
| 132 | myargc--; |
---|
| 133 | } else if (!strcmp(myargv[0], "-n")) { |
---|
| 134 | z->noping=1; |
---|
| 135 | myargv++; |
---|
| 136 | myargc--; |
---|
| 137 | } else { |
---|
| 138 | /* anything unattached is a recipient */ |
---|
| 139 | owl_list_append_element(&(z->recips), strdup(myargv[0])); |
---|
| 140 | myargv++; |
---|
| 141 | myargc--; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | owl_parsefree(argv, argc); |
---|
| 146 | |
---|
| 147 | if (badargs) { |
---|
| 148 | return(-1); |
---|
| 149 | } |
---|
| 150 | |
---|
[ce7db4d] | 151 | /* now deal with defaults */ |
---|
| 152 | if (z->class==NULL) z->class=owl_strdup("message"); |
---|
| 153 | if (z->inst==NULL) z->inst=owl_strdup("personal"); |
---|
| 154 | if (z->realm==NULL) z->realm=owl_strdup(""); |
---|
| 155 | if (z->opcode==NULL) z->opcode=owl_strdup(""); |
---|
| 156 | /* z->message is allowed to stay NULL */ |
---|
| 157 | |
---|
[7d4fbcd] | 158 | if (!strcasecmp(z->class, "message") && |
---|
| 159 | !strcasecmp(z->inst, "personal") && |
---|
| 160 | owl_list_get_size(&(z->recips))==0) { |
---|
[836ea3a3] | 161 | owl_function_error("You must specify a recipient for zwrite"); |
---|
[7d4fbcd] | 162 | return(-1); |
---|
| 163 | } |
---|
| 164 | |
---|
[ce7db4d] | 165 | /* get a zsig, if not given */ |
---|
| 166 | if (z->zsig==NULL) { |
---|
| 167 | zsigproc = owl_global_get_zsigproc(&g); |
---|
| 168 | zsigowlvar = owl_global_get_zsig(&g); |
---|
[09489b89] | 169 | zsigzvar = owl_zephyr_get_variable("zwrite-signature"); |
---|
[ce7db4d] | 170 | |
---|
| 171 | if (zsigowlvar && *zsigowlvar) { |
---|
| 172 | z->zsig=owl_strdup(zsigowlvar); |
---|
| 173 | } else if (zsigproc && *zsigproc) { |
---|
| 174 | FILE *file; |
---|
| 175 | char buff[LINE], *openline; |
---|
| 176 | |
---|
| 177 | /* simple hack for now to nuke stderr */ |
---|
| 178 | openline=owl_malloc(strlen(zsigproc)+40); |
---|
| 179 | strcpy(openline, zsigproc); |
---|
[2a2bb60] | 180 | #if !(OWL_STDERR_REDIR) |
---|
[ce7db4d] | 181 | strcat(openline, " 2> /dev/null"); |
---|
[2a2bb60] | 182 | #endif |
---|
[ce7db4d] | 183 | file=popen(openline, "r"); |
---|
| 184 | owl_free(openline); |
---|
| 185 | if (!file) { |
---|
| 186 | if (zsigzvar && *zsigzvar) { |
---|
| 187 | z->zsig=owl_strdup(zsigzvar); |
---|
| 188 | } |
---|
| 189 | } else { |
---|
| 190 | z->zsig=owl_malloc(LINE*5); |
---|
| 191 | strcpy(z->zsig, ""); |
---|
| 192 | while (fgets(buff, LINE, file)) { /* wrong sizing */ |
---|
| 193 | strcat(z->zsig, buff); |
---|
| 194 | } |
---|
| 195 | pclose(file); |
---|
| 196 | if (z->zsig[strlen(z->zsig)-1]=='\n') { |
---|
| 197 | z->zsig[strlen(z->zsig)-1]='\0'; |
---|
| 198 | } |
---|
[56330ff] | 199 | } |
---|
[ce7db4d] | 200 | } else if (zsigzvar) { |
---|
| 201 | z->zsig=owl_strdup(zsigzvar); |
---|
| 202 | } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) { |
---|
| 203 | z->zsig=strdup(pw->pw_gecos); |
---|
| 204 | ptr=strchr(z->zsig, ','); |
---|
| 205 | if (ptr) { |
---|
| 206 | ptr[0]='\0'; |
---|
[56330ff] | 207 | } |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
[7d4fbcd] | 211 | return(0); |
---|
| 212 | } |
---|
| 213 | |
---|
[ce7db4d] | 214 | void owl_zwrite_send_ping(owl_zwrite *z) |
---|
| 215 | { |
---|
[7d4fbcd] | 216 | int i, j; |
---|
[dab82f29] | 217 | char *to; |
---|
[7d4fbcd] | 218 | |
---|
| 219 | if (z->noping) return; |
---|
| 220 | |
---|
| 221 | if (strcasecmp(z->class, "message") || |
---|
| 222 | strcasecmp(z->inst, "personal")) { |
---|
| 223 | return; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | /* if there are no recipients we won't send a ping, which |
---|
| 227 | is what we want */ |
---|
| 228 | j=owl_list_get_size(&(z->recips)); |
---|
| 229 | for (i=0; i<j; i++) { |
---|
| 230 | if (strcmp(z->realm, "")) { |
---|
[dab82f29] | 231 | to = owl_sprintf("%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
[7d4fbcd] | 232 | } else { |
---|
[dab82f29] | 233 | to = owl_strdup(owl_list_get_element(&(z->recips), i)); |
---|
[7d4fbcd] | 234 | } |
---|
| 235 | send_ping(to); |
---|
[dab82f29] | 236 | owl_free(to); |
---|
[7d4fbcd] | 237 | } |
---|
| 238 | |
---|
| 239 | } |
---|
| 240 | |
---|
[ce7db4d] | 241 | void owl_zwrite_set_message(owl_zwrite *z, char *msg) |
---|
| 242 | { |
---|
[db2dd3d] | 243 | int i, j; |
---|
[dab82f29] | 244 | char *toline = NULL; |
---|
| 245 | char *tmp = NULL; |
---|
| 246 | |
---|
[ce7db4d] | 247 | if (z->message) owl_free(z->message); |
---|
[db2dd3d] | 248 | |
---|
| 249 | j=owl_list_get_size(&(z->recips)); |
---|
| 250 | if (j>0 && z->cc) { |
---|
[dab82f29] | 251 | toline = owl_strdup( "CC: "); |
---|
[db2dd3d] | 252 | for (i=0; i<j; i++) { |
---|
[dab82f29] | 253 | tmp = toline; |
---|
[db2dd3d] | 254 | if (strcmp(z->realm, "")) { |
---|
[dab82f29] | 255 | toline = owl_sprintf( "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
[db2dd3d] | 256 | } else { |
---|
[dab82f29] | 257 | toline = owl_sprintf( "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i)); |
---|
[db2dd3d] | 258 | } |
---|
[dab82f29] | 259 | owl_free(tmp); |
---|
| 260 | tmp=NULL; |
---|
[db2dd3d] | 261 | } |
---|
| 262 | z->message=owl_sprintf("%s\n%s", toline, msg); |
---|
[dab82f29] | 263 | owl_free(toline); |
---|
[db2dd3d] | 264 | } else { |
---|
| 265 | z->message=owl_strdup(msg); |
---|
| 266 | } |
---|
[ce7db4d] | 267 | } |
---|
| 268 | |
---|
| 269 | char *owl_zwrite_get_message(owl_zwrite *z) |
---|
| 270 | { |
---|
| 271 | if (z->message) return(z->message); |
---|
| 272 | return(""); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | int owl_zwrite_is_message_set(owl_zwrite *z) |
---|
| 276 | { |
---|
| 277 | if (z->message) return(1); |
---|
| 278 | return(0); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | int owl_zwrite_send_message(owl_zwrite *z) |
---|
| 282 | { |
---|
[7d4fbcd] | 283 | int i, j; |
---|
[dab82f29] | 284 | char *to = NULL; |
---|
[7d4fbcd] | 285 | |
---|
[ce7db4d] | 286 | if (z->message==NULL) return(-1); |
---|
| 287 | |
---|
[7d4fbcd] | 288 | j=owl_list_get_size(&(z->recips)); |
---|
| 289 | if (j>0) { |
---|
| 290 | for (i=0; i<j; i++) { |
---|
| 291 | if (strcmp(z->realm, "")) { |
---|
[dab82f29] | 292 | to = owl_sprintf("%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
[7d4fbcd] | 293 | } else { |
---|
[dab82f29] | 294 | to = owl_strdup( owl_list_get_element(&(z->recips), i)); |
---|
[7d4fbcd] | 295 | } |
---|
[db2dd3d] | 296 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
[dab82f29] | 297 | owl_free(to); |
---|
| 298 | to = NULL; |
---|
[7d4fbcd] | 299 | } |
---|
| 300 | } else { |
---|
[dab82f29] | 301 | to = owl_sprintf( "@%s", z->realm); |
---|
[ce7db4d] | 302 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
| 303 | } |
---|
[dab82f29] | 304 | owl_free(to); |
---|
[ce7db4d] | 305 | return(0); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | int owl_zwrite_create_and_send_from_line(char *cmd, char *msg) |
---|
| 309 | { |
---|
| 310 | owl_zwrite z; |
---|
| 311 | int rv; |
---|
| 312 | rv=owl_zwrite_create_from_line(&z, cmd); |
---|
| 313 | if (rv) return(rv); |
---|
| 314 | if (!owl_zwrite_is_message_set(&z)) { |
---|
| 315 | owl_zwrite_set_message(&z, msg); |
---|
[7d4fbcd] | 316 | } |
---|
[ce7db4d] | 317 | owl_zwrite_send_message(&z); |
---|
| 318 | owl_zwrite_free(&z); |
---|
| 319 | return(0); |
---|
[7d4fbcd] | 320 | } |
---|
| 321 | |
---|
[ce7db4d] | 322 | char *owl_zwrite_get_class(owl_zwrite *z) |
---|
| 323 | { |
---|
[7d4fbcd] | 324 | return(z->class); |
---|
| 325 | } |
---|
| 326 | |
---|
[ce7db4d] | 327 | char *owl_zwrite_get_instance(owl_zwrite *z) |
---|
| 328 | { |
---|
[7d4fbcd] | 329 | return(z->inst); |
---|
| 330 | } |
---|
| 331 | |
---|
[ce7db4d] | 332 | char *owl_zwrite_get_opcode(owl_zwrite *z) |
---|
| 333 | { |
---|
[4b464a4] | 334 | return(z->opcode); |
---|
| 335 | } |
---|
| 336 | |
---|
[9ceee9d] | 337 | void owl_zwrite_set_opcode(owl_zwrite *z, char *opcode) |
---|
| 338 | { |
---|
| 339 | if (z->opcode) owl_free(z->opcode); |
---|
| 340 | z->opcode=owl_strdup(opcode); |
---|
| 341 | } |
---|
| 342 | |
---|
[ce7db4d] | 343 | char *owl_zwrite_get_realm(owl_zwrite *z) |
---|
| 344 | { |
---|
[7d4fbcd] | 345 | return(z->realm); |
---|
| 346 | } |
---|
| 347 | |
---|
[ce7db4d] | 348 | char *owl_zwrite_get_zsig(owl_zwrite *z) |
---|
| 349 | { |
---|
| 350 | if (z->zsig) return(z->zsig); |
---|
| 351 | return(""); |
---|
[56330ff] | 352 | } |
---|
| 353 | |
---|
[ce7db4d] | 354 | void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) |
---|
| 355 | { |
---|
[7d4fbcd] | 356 | int i, j; |
---|
| 357 | |
---|
| 358 | strcpy(buff, ""); |
---|
| 359 | j=owl_list_get_size(&(z->recips)); |
---|
| 360 | for (i=0; i<j; i++) { |
---|
| 361 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
| 362 | strcat(buff, " "); |
---|
| 363 | } |
---|
| 364 | buff[strlen(buff)-1]='\0'; |
---|
| 365 | } |
---|
| 366 | |
---|
[ce7db4d] | 367 | int owl_zwrite_get_numrecips(owl_zwrite *z) |
---|
| 368 | { |
---|
[7d4fbcd] | 369 | return(owl_list_get_size(&(z->recips))); |
---|
| 370 | } |
---|
| 371 | |
---|
[ce7db4d] | 372 | char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) |
---|
| 373 | { |
---|
[7d4fbcd] | 374 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 375 | } |
---|
| 376 | |
---|
[ce7db4d] | 377 | int owl_zwrite_is_personal(owl_zwrite *z) |
---|
| 378 | { |
---|
[7d4fbcd] | 379 | /* return true if at least one of the recipients is personal */ |
---|
| 380 | int i, j; |
---|
| 381 | char *foo; |
---|
| 382 | |
---|
| 383 | j=owl_list_get_size(&(z->recips)); |
---|
| 384 | for (i=0; i<j; i++) { |
---|
| 385 | foo=owl_list_get_element(&(z->recips), i); |
---|
| 386 | if (foo[0]!='@') return(1); |
---|
| 387 | } |
---|
| 388 | return(0); |
---|
| 389 | } |
---|
| 390 | |
---|
[ce7db4d] | 391 | void owl_zwrite_free(owl_zwrite *z) |
---|
| 392 | { |
---|
[7d4fbcd] | 393 | owl_list_free_all(&(z->recips), &owl_free); |
---|
[ce7db4d] | 394 | if (z->class) owl_free(z->class); |
---|
| 395 | if (z->inst) owl_free(z->inst); |
---|
| 396 | if (z->opcode) owl_free(z->opcode); |
---|
| 397 | if (z->realm) owl_free(z->realm); |
---|
| 398 | if (z->message) owl_free(z->message); |
---|
| 399 | if (z->zsig) owl_free(z->zsig); |
---|
[7d4fbcd] | 400 | } |
---|