| 1 | #include <string.h> |
|---|
| 2 | #include <pwd.h> |
|---|
| 3 | #include <sys/types.h> |
|---|
| 4 | #include <unistd.h> |
|---|
| 5 | #include "owl.h" |
|---|
| 6 | |
|---|
| 7 | int owl_zwrite_create_from_line(owl_zwrite *z, const char *line) |
|---|
| 8 | { |
|---|
| 9 | int argc, badargs, myargc, i, len; |
|---|
| 10 | char **argv; |
|---|
| 11 | const char *const *myargv; |
|---|
| 12 | char *msg = NULL; |
|---|
| 13 | |
|---|
| 14 | badargs=0; |
|---|
| 15 | |
|---|
| 16 | /* start with null entries */ |
|---|
| 17 | z->realm=NULL; |
|---|
| 18 | z->class=NULL; |
|---|
| 19 | z->inst=NULL; |
|---|
| 20 | z->opcode=NULL; |
|---|
| 21 | z->zsig=NULL; |
|---|
| 22 | z->message=NULL; |
|---|
| 23 | z->cc=0; |
|---|
| 24 | z->noping=0; |
|---|
| 25 | owl_list_create(&(z->recips)); |
|---|
| 26 | z->zwriteline = owl_strdup(line); |
|---|
| 27 | |
|---|
| 28 | /* parse the command line for options */ |
|---|
| 29 | argv=owl_parseline(line, &argc); |
|---|
| 30 | myargv=strs(argv); |
|---|
| 31 | if (argc<0) { |
|---|
| 32 | owl_function_error("Unbalanced quotes in zwrite"); |
|---|
| 33 | return(-1); |
|---|
| 34 | } |
|---|
| 35 | myargc=argc; |
|---|
| 36 | if (myargc && *(myargv[0])!='-') { |
|---|
| 37 | myargc--; |
|---|
| 38 | myargv++; |
|---|
| 39 | } |
|---|
| 40 | while (myargc) { |
|---|
| 41 | if (!strcmp(myargv[0], "-c")) { |
|---|
| 42 | if (myargc<2) { |
|---|
| 43 | badargs=1; |
|---|
| 44 | break; |
|---|
| 45 | } |
|---|
| 46 | z->class=owl_validate_utf8(myargv[1]); |
|---|
| 47 | myargv+=2; |
|---|
| 48 | myargc-=2; |
|---|
| 49 | } else if (!strcmp(myargv[0], "-i")) { |
|---|
| 50 | if (myargc<2) { |
|---|
| 51 | badargs=1; |
|---|
| 52 | break; |
|---|
| 53 | } |
|---|
| 54 | z->inst=owl_validate_utf8(myargv[1]); |
|---|
| 55 | myargv+=2; |
|---|
| 56 | myargc-=2; |
|---|
| 57 | } else if (!strcmp(myargv[0], "-r")) { |
|---|
| 58 | if (myargc<2) { |
|---|
| 59 | badargs=1; |
|---|
| 60 | break; |
|---|
| 61 | } |
|---|
| 62 | z->realm=owl_validate_utf8(myargv[1]); |
|---|
| 63 | myargv+=2; |
|---|
| 64 | myargc-=2; |
|---|
| 65 | } else if (!strcmp(myargv[0], "-s")) { |
|---|
| 66 | if (myargc<2) { |
|---|
| 67 | badargs=1; |
|---|
| 68 | break; |
|---|
| 69 | } |
|---|
| 70 | z->zsig=owl_validate_utf8(myargv[1]); |
|---|
| 71 | myargv+=2; |
|---|
| 72 | myargc-=2; |
|---|
| 73 | } else if (!strcmp(myargv[0], "-O")) { |
|---|
| 74 | if (myargc<2) { |
|---|
| 75 | badargs=1; |
|---|
| 76 | break; |
|---|
| 77 | } |
|---|
| 78 | z->opcode=owl_validate_utf8(myargv[1]); |
|---|
| 79 | myargv+=2; |
|---|
| 80 | myargc-=2; |
|---|
| 81 | } else if (!strcmp(myargv[0], "-m")) { |
|---|
| 82 | if (myargc<2) { |
|---|
| 83 | badargs=1; |
|---|
| 84 | break; |
|---|
| 85 | } |
|---|
| 86 | /* we must already have users or a class or an instance */ |
|---|
| 87 | if (owl_list_get_size(&(z->recips))<1 && (!z->class) && (!z->inst)) { |
|---|
| 88 | badargs=1; |
|---|
| 89 | break; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /* Once we have -m, gobble up everything else on the line */ |
|---|
| 93 | myargv++; |
|---|
| 94 | myargc--; |
|---|
| 95 | len = 0; |
|---|
| 96 | for (i=0;i<myargc;i++) { |
|---|
| 97 | len += strlen(myargv[i]) + 1; |
|---|
| 98 | } |
|---|
| 99 | len++; /* NULL after the last trailing ' ' in the loop below. */ |
|---|
| 100 | msg = owl_malloc(len); |
|---|
| 101 | msg[0] = '\0'; |
|---|
| 102 | while (myargc) { |
|---|
| 103 | strcat(msg, myargv[0]); |
|---|
| 104 | strcat(msg, " "); |
|---|
| 105 | myargc--; |
|---|
| 106 | myargv++; |
|---|
| 107 | } |
|---|
| 108 | msg[strlen(msg)-1] = '\0'; |
|---|
| 109 | break; |
|---|
| 110 | } else if (!strcmp(myargv[0], "-C")) { |
|---|
| 111 | z->cc=1; |
|---|
| 112 | myargv++; |
|---|
| 113 | myargc--; |
|---|
| 114 | } else if (!strcmp(myargv[0], "-n")) { |
|---|
| 115 | z->noping=1; |
|---|
| 116 | myargv++; |
|---|
| 117 | myargc--; |
|---|
| 118 | } else { |
|---|
| 119 | /* anything unattached is a recipient */ |
|---|
| 120 | owl_list_append_element(&(z->recips), owl_validate_utf8(myargv[0])); |
|---|
| 121 | myargv++; |
|---|
| 122 | myargc--; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | owl_parsefree(argv, argc); |
|---|
| 127 | |
|---|
| 128 | if (badargs) { |
|---|
| 129 | return(-1); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | if (z->class == NULL && |
|---|
| 133 | z->inst == NULL && |
|---|
| 134 | owl_list_get_size(&(z->recips))==0) { |
|---|
| 135 | owl_function_error("You must specify a recipient for zwrite"); |
|---|
| 136 | return(-1); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /* now deal with defaults */ |
|---|
| 140 | if (z->class==NULL) z->class=owl_strdup("message"); |
|---|
| 141 | if (z->inst==NULL) z->inst=owl_strdup("personal"); |
|---|
| 142 | if (z->realm==NULL) z->realm=owl_strdup(""); |
|---|
| 143 | if (z->opcode==NULL) z->opcode=owl_strdup(""); |
|---|
| 144 | /* z->message is allowed to stay NULL */ |
|---|
| 145 | |
|---|
| 146 | if(msg) { |
|---|
| 147 | owl_zwrite_set_message(z, msg); |
|---|
| 148 | owl_free(msg); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | return(0); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void owl_zwrite_populate_zsig(owl_zwrite *z) |
|---|
| 155 | { |
|---|
| 156 | /* get a zsig, if not given */ |
|---|
| 157 | if (z->zsig != NULL) |
|---|
| 158 | return; |
|---|
| 159 | |
|---|
| 160 | z->zsig = owl_perlconfig_execute(owl_global_get_zsigfunc(&g)); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | void owl_zwrite_send_ping(const owl_zwrite *z) |
|---|
| 164 | { |
|---|
| 165 | int i, j; |
|---|
| 166 | char *to; |
|---|
| 167 | |
|---|
| 168 | if (z->noping) return; |
|---|
| 169 | |
|---|
| 170 | if (strcasecmp(z->class, "message")) { |
|---|
| 171 | return; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /* if there are no recipients we won't send a ping, which |
|---|
| 175 | is what we want */ |
|---|
| 176 | j=owl_list_get_size(&(z->recips)); |
|---|
| 177 | for (i=0; i<j; i++) { |
|---|
| 178 | if (strcmp(z->realm, "")) { |
|---|
| 179 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
|---|
| 180 | } else { |
|---|
| 181 | to = owl_strdup(owl_list_get_element(&(z->recips), i)); |
|---|
| 182 | } |
|---|
| 183 | send_ping(to, z->class, z->inst); |
|---|
| 184 | owl_free(to); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | void owl_zwrite_set_message(owl_zwrite *z, const char *msg) |
|---|
| 190 | { |
|---|
| 191 | int i, j; |
|---|
| 192 | char *toline = NULL; |
|---|
| 193 | char *tmp = NULL, *tmp2; |
|---|
| 194 | |
|---|
| 195 | if (z->message) owl_free(z->message); |
|---|
| 196 | |
|---|
| 197 | j=owl_list_get_size(&(z->recips)); |
|---|
| 198 | if (j>0 && z->cc) { |
|---|
| 199 | toline = owl_strdup( "CC: "); |
|---|
| 200 | for (i=0; i<j; i++) { |
|---|
| 201 | tmp = toline; |
|---|
| 202 | if (strcmp(z->realm, "")) { |
|---|
| 203 | toline = owl_sprintf( "%s%s@%s ", toline, (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
|---|
| 204 | } else { |
|---|
| 205 | toline = owl_sprintf( "%s%s ", toline, (const char *) owl_list_get_element(&(z->recips), i)); |
|---|
| 206 | } |
|---|
| 207 | owl_free(tmp); |
|---|
| 208 | tmp = NULL; |
|---|
| 209 | } |
|---|
| 210 | tmp = owl_validate_utf8(msg); |
|---|
| 211 | tmp2 = owl_text_expand_tabs(tmp); |
|---|
| 212 | z->message=owl_sprintf("%s\n%s", toline, tmp2); |
|---|
| 213 | owl_free(toline); |
|---|
| 214 | owl_free(tmp); |
|---|
| 215 | owl_free(tmp2); |
|---|
| 216 | } else { |
|---|
| 217 | tmp=owl_validate_utf8(msg); |
|---|
| 218 | z->message=owl_text_expand_tabs(tmp); |
|---|
| 219 | owl_free(tmp); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | const char *owl_zwrite_get_message(const owl_zwrite *z) |
|---|
| 224 | { |
|---|
| 225 | if (z->message) return(z->message); |
|---|
| 226 | return(""); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | int owl_zwrite_is_message_set(const owl_zwrite *z) |
|---|
| 230 | { |
|---|
| 231 | if (z->message) return(1); |
|---|
| 232 | return(0); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | int owl_zwrite_send_message(const owl_zwrite *z) |
|---|
| 236 | { |
|---|
| 237 | int i, j; |
|---|
| 238 | char *to = NULL; |
|---|
| 239 | |
|---|
| 240 | if (z->message==NULL) return(-1); |
|---|
| 241 | |
|---|
| 242 | j=owl_list_get_size(&(z->recips)); |
|---|
| 243 | if (j>0) { |
|---|
| 244 | for (i=0; i<j; i++) { |
|---|
| 245 | if (strcmp(z->realm, "")) { |
|---|
| 246 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
|---|
| 247 | } else { |
|---|
| 248 | to = owl_strdup( owl_list_get_element(&(z->recips), i)); |
|---|
| 249 | } |
|---|
| 250 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
|---|
| 251 | owl_free(to); |
|---|
| 252 | to = NULL; |
|---|
| 253 | } |
|---|
| 254 | } else { |
|---|
| 255 | to = owl_sprintf( "@%s", z->realm); |
|---|
| 256 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
|---|
| 257 | } |
|---|
| 258 | owl_free(to); |
|---|
| 259 | return(0); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg) |
|---|
| 263 | { |
|---|
| 264 | owl_zwrite z; |
|---|
| 265 | int rv; |
|---|
| 266 | rv=owl_zwrite_create_from_line(&z, cmd); |
|---|
| 267 | if (rv) return(rv); |
|---|
| 268 | if (!owl_zwrite_is_message_set(&z)) { |
|---|
| 269 | owl_zwrite_set_message(&z, msg); |
|---|
| 270 | } |
|---|
| 271 | owl_zwrite_populate_zsig(&z); |
|---|
| 272 | owl_zwrite_send_message(&z); |
|---|
| 273 | owl_zwrite_free(&z); |
|---|
| 274 | return(0); |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | const char *owl_zwrite_get_class(const owl_zwrite *z) |
|---|
| 278 | { |
|---|
| 279 | return(z->class); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | const char *owl_zwrite_get_instance(const owl_zwrite *z) |
|---|
| 283 | { |
|---|
| 284 | return(z->inst); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | const char *owl_zwrite_get_opcode(const owl_zwrite *z) |
|---|
| 288 | { |
|---|
| 289 | return(z->opcode); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode) |
|---|
| 293 | { |
|---|
| 294 | if (z->opcode) owl_free(z->opcode); |
|---|
| 295 | z->opcode=owl_validate_utf8(opcode); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | const char *owl_zwrite_get_realm(const owl_zwrite *z) |
|---|
| 299 | { |
|---|
| 300 | return(z->realm); |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | const char *owl_zwrite_get_zsig(const owl_zwrite *z) |
|---|
| 304 | { |
|---|
| 305 | if (z->zsig) return(z->zsig); |
|---|
| 306 | return(""); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | void owl_zwrite_set_zsig(owl_zwrite *z, const char *zsig) |
|---|
| 310 | { |
|---|
| 311 | if(z->zsig) owl_free(z->zsig); |
|---|
| 312 | z->zsig = owl_strdup(zsig); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | void owl_zwrite_get_recipstr(const owl_zwrite *z, char *buff) |
|---|
| 316 | { |
|---|
| 317 | int i, j; |
|---|
| 318 | |
|---|
| 319 | strcpy(buff, ""); |
|---|
| 320 | j=owl_list_get_size(&(z->recips)); |
|---|
| 321 | for (i=0; i<j; i++) { |
|---|
| 322 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
|---|
| 323 | strcat(buff, " "); |
|---|
| 324 | } |
|---|
| 325 | buff[strlen(buff)-1]='\0'; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | int owl_zwrite_get_numrecips(const owl_zwrite *z) |
|---|
| 329 | { |
|---|
| 330 | return(owl_list_get_size(&(z->recips))); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n) |
|---|
| 334 | { |
|---|
| 335 | return(owl_list_get_element(&(z->recips), n)); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | int owl_zwrite_is_personal(const owl_zwrite *z) |
|---|
| 339 | { |
|---|
| 340 | /* return true if at least one of the recipients is personal */ |
|---|
| 341 | int i, j; |
|---|
| 342 | char *foo; |
|---|
| 343 | |
|---|
| 344 | j=owl_list_get_size(&(z->recips)); |
|---|
| 345 | for (i=0; i<j; i++) { |
|---|
| 346 | foo=owl_list_get_element(&(z->recips), i); |
|---|
| 347 | if (foo[0]!='@') return(1); |
|---|
| 348 | } |
|---|
| 349 | return(0); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | void owl_zwrite_free(owl_zwrite *z) |
|---|
| 353 | { |
|---|
| 354 | owl_list_free_all(&(z->recips), &owl_free); |
|---|
| 355 | if (z->zwriteline) owl_free(z->zwriteline); |
|---|
| 356 | if (z->class) owl_free(z->class); |
|---|
| 357 | if (z->inst) owl_free(z->inst); |
|---|
| 358 | if (z->opcode) owl_free(z->opcode); |
|---|
| 359 | if (z->realm) owl_free(z->realm); |
|---|
| 360 | if (z->message) owl_free(z->message); |
|---|
| 361 | if (z->zsig) owl_free(z->zsig); |
|---|
| 362 | } |
|---|