[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 | |
---|
[e19eb97] | 7 | int owl_zwrite_create_from_line(owl_zwrite *z, const char *line) |
---|
[ce7db4d] | 8 | { |
---|
[a52d13a] | 9 | int argc, badargs, myargc, i, len; |
---|
[65b2173] | 10 | char **argv; |
---|
[e19eb97] | 11 | const char *const *myargv; |
---|
[a52d13a] | 12 | char *msg = NULL; |
---|
[7d4fbcd] | 13 | |
---|
| 14 | badargs=0; |
---|
| 15 | |
---|
[69f89c7] | 16 | /* start with null entries */ |
---|
[ce7db4d] | 17 | z->realm=NULL; |
---|
| 18 | z->class=NULL; |
---|
| 19 | z->inst=NULL; |
---|
| 20 | z->opcode=NULL; |
---|
| 21 | z->zsig=NULL; |
---|
| 22 | z->message=NULL; |
---|
[7d4fbcd] | 23 | z->cc=0; |
---|
| 24 | z->noping=0; |
---|
| 25 | owl_list_create(&(z->recips)); |
---|
[24ccc01] | 26 | z->zwriteline = owl_strdup(line); |
---|
[7d4fbcd] | 27 | |
---|
| 28 | /* parse the command line for options */ |
---|
[c529ac8] | 29 | argv=owl_parseline(line, &argc); |
---|
| 30 | myargv=strs(argv); |
---|
[7d4fbcd] | 31 | if (argc<0) { |
---|
[836ea3a3] | 32 | owl_function_error("Unbalanced quotes in zwrite"); |
---|
[7d4fbcd] | 33 | return(-1); |
---|
| 34 | } |
---|
| 35 | myargc=argc; |
---|
[e1c4636] | 36 | if (myargc && *(myargv[0])!='-') { |
---|
| 37 | myargc--; |
---|
| 38 | myargv++; |
---|
| 39 | } |
---|
[7d4fbcd] | 40 | while (myargc) { |
---|
| 41 | if (!strcmp(myargv[0], "-c")) { |
---|
| 42 | if (myargc<2) { |
---|
| 43 | badargs=1; |
---|
| 44 | break; |
---|
| 45 | } |
---|
[4b17a6c] | 46 | z->class=owl_validate_utf8(myargv[1]); |
---|
[7d4fbcd] | 47 | myargv+=2; |
---|
| 48 | myargc-=2; |
---|
| 49 | } else if (!strcmp(myargv[0], "-i")) { |
---|
| 50 | if (myargc<2) { |
---|
| 51 | badargs=1; |
---|
| 52 | break; |
---|
| 53 | } |
---|
[4b17a6c] | 54 | z->inst=owl_validate_utf8(myargv[1]); |
---|
[7d4fbcd] | 55 | myargv+=2; |
---|
| 56 | myargc-=2; |
---|
| 57 | } else if (!strcmp(myargv[0], "-r")) { |
---|
| 58 | if (myargc<2) { |
---|
| 59 | badargs=1; |
---|
| 60 | break; |
---|
| 61 | } |
---|
[4b17a6c] | 62 | z->realm=owl_validate_utf8(myargv[1]); |
---|
[ce7db4d] | 63 | myargv+=2; |
---|
| 64 | myargc-=2; |
---|
| 65 | } else if (!strcmp(myargv[0], "-s")) { |
---|
| 66 | if (myargc<2) { |
---|
| 67 | badargs=1; |
---|
| 68 | break; |
---|
| 69 | } |
---|
[4b17a6c] | 70 | z->zsig=owl_validate_utf8(myargv[1]); |
---|
[7d4fbcd] | 71 | myargv+=2; |
---|
| 72 | myargc-=2; |
---|
| 73 | } else if (!strcmp(myargv[0], "-O")) { |
---|
| 74 | if (myargc<2) { |
---|
| 75 | badargs=1; |
---|
| 76 | break; |
---|
| 77 | } |
---|
[4b17a6c] | 78 | z->opcode=owl_validate_utf8(myargv[1]); |
---|
[7d4fbcd] | 79 | myargv+=2; |
---|
| 80 | myargc-=2; |
---|
[ce7db4d] | 81 | } else if (!strcmp(myargv[0], "-m")) { |
---|
| 82 | if (myargc<2) { |
---|
| 83 | badargs=1; |
---|
| 84 | break; |
---|
| 85 | } |
---|
[69f89c7] | 86 | /* we must already have users or a class or an instance */ |
---|
[e016fc2] | 87 | if (owl_list_get_size(&(z->recips))<1 && (!z->class) && (!z->inst)) { |
---|
[ce7db4d] | 88 | badargs=1; |
---|
| 89 | break; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /* Once we have -m, gobble up everything else on the line */ |
---|
| 93 | myargv++; |
---|
| 94 | myargc--; |
---|
[a52d13a] | 95 | len = 0; |
---|
| 96 | for (i=0;i<myargc;i++) { |
---|
[96cdeaf] | 97 | len += strlen(myargv[i]) + 1; |
---|
[a52d13a] | 98 | } |
---|
[4db2355] | 99 | len++; /* NULL after the last trailing ' ' in the loop below. */ |
---|
[a52d13a] | 100 | msg = owl_malloc(len); |
---|
| 101 | msg[0] = '\0'; |
---|
[ce7db4d] | 102 | while (myargc) { |
---|
[a52d13a] | 103 | strcat(msg, myargv[0]); |
---|
| 104 | strcat(msg, " "); |
---|
| 105 | myargc--; |
---|
| 106 | myargv++; |
---|
[ce7db4d] | 107 | } |
---|
[a52d13a] | 108 | msg[strlen(msg)-1] = '\0'; |
---|
[ce7db4d] | 109 | break; |
---|
[7d4fbcd] | 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 */ |
---|
[4b17a6c] | 120 | owl_list_append_element(&(z->recips), owl_validate_utf8(myargv[0])); |
---|
[7d4fbcd] | 121 | myargv++; |
---|
| 122 | myargc--; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | owl_parsefree(argv, argc); |
---|
| 127 | |
---|
| 128 | if (badargs) { |
---|
| 129 | return(-1); |
---|
| 130 | } |
---|
| 131 | |
---|
[1fe100c] | 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 | |
---|
[ce7db4d] | 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 */ |
---|
[a52d13a] | 145 | |
---|
| 146 | if(msg) { |
---|
| 147 | owl_zwrite_set_message(z, msg); |
---|
| 148 | owl_free(msg); |
---|
| 149 | } |
---|
| 150 | |
---|
[3f3ee61] | 151 | return(0); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | void owl_zwrite_populate_zsig(owl_zwrite *z) |
---|
| 155 | { |
---|
[ce7db4d] | 156 | /* get a zsig, if not given */ |
---|
[de3f641] | 157 | if (z->zsig != NULL) |
---|
| 158 | return; |
---|
[ce7db4d] | 159 | |
---|
[de3f641] | 160 | z->zsig = owl_perlconfig_execute(owl_global_get_zsigfunc(&g)); |
---|
[7d4fbcd] | 161 | } |
---|
| 162 | |
---|
[a352029b] | 163 | void owl_zwrite_send_ping(const owl_zwrite *z) |
---|
[ce7db4d] | 164 | { |
---|
[7d4fbcd] | 165 | int i, j; |
---|
[44a61ac] | 166 | char *to; |
---|
[7d4fbcd] | 167 | |
---|
| 168 | if (z->noping) return; |
---|
| 169 | |
---|
[3ef779b] | 170 | if (strcasecmp(z->class, "message")) { |
---|
[7d4fbcd] | 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, "")) { |
---|
[e19eb97] | 179 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
[7d4fbcd] | 180 | } else { |
---|
[44a61ac] | 181 | to = owl_strdup(owl_list_get_element(&(z->recips), i)); |
---|
[7d4fbcd] | 182 | } |
---|
[3ef779b] | 183 | send_ping(to, z->class, z->inst); |
---|
[44a61ac] | 184 | owl_free(to); |
---|
[7d4fbcd] | 185 | } |
---|
| 186 | |
---|
| 187 | } |
---|
| 188 | |
---|
[e19eb97] | 189 | void owl_zwrite_set_message(owl_zwrite *z, const char *msg) |
---|
[ce7db4d] | 190 | { |
---|
[db2dd3d] | 191 | int i, j; |
---|
[3538bc8] | 192 | char *toline = NULL; |
---|
[a52d13a] | 193 | char *tmp = NULL, *tmp2; |
---|
[db2dd3d] | 194 | |
---|
[ce7db4d] | 195 | if (z->message) owl_free(z->message); |
---|
[db2dd3d] | 196 | |
---|
| 197 | j=owl_list_get_size(&(z->recips)); |
---|
| 198 | if (j>0 && z->cc) { |
---|
[3538bc8] | 199 | toline = owl_strdup( "CC: "); |
---|
[db2dd3d] | 200 | for (i=0; i<j; i++) { |
---|
[3538bc8] | 201 | tmp = toline; |
---|
[db2dd3d] | 202 | if (strcmp(z->realm, "")) { |
---|
[e19eb97] | 203 | toline = owl_sprintf( "%s%s@%s ", toline, (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
[db2dd3d] | 204 | } else { |
---|
[e19eb97] | 205 | toline = owl_sprintf( "%s%s ", toline, (const char *) owl_list_get_element(&(z->recips), i)); |
---|
[db2dd3d] | 206 | } |
---|
[3538bc8] | 207 | owl_free(tmp); |
---|
| 208 | tmp = NULL; |
---|
[db2dd3d] | 209 | } |
---|
[4b17a6c] | 210 | tmp = owl_validate_utf8(msg); |
---|
[a52d13a] | 211 | tmp2 = owl_text_expand_tabs(tmp); |
---|
| 212 | z->message=owl_sprintf("%s\n%s", toline, tmp2); |
---|
[3538bc8] | 213 | owl_free(toline); |
---|
[a52d13a] | 214 | owl_free(tmp); |
---|
| 215 | owl_free(tmp2); |
---|
[db2dd3d] | 216 | } else { |
---|
[a52d13a] | 217 | tmp=owl_validate_utf8(msg); |
---|
| 218 | z->message=owl_text_expand_tabs(tmp); |
---|
| 219 | owl_free(tmp); |
---|
[db2dd3d] | 220 | } |
---|
[ce7db4d] | 221 | } |
---|
| 222 | |
---|
[a352029b] | 223 | const char *owl_zwrite_get_message(const owl_zwrite *z) |
---|
[ce7db4d] | 224 | { |
---|
| 225 | if (z->message) return(z->message); |
---|
| 226 | return(""); |
---|
| 227 | } |
---|
| 228 | |
---|
[a352029b] | 229 | int owl_zwrite_is_message_set(const owl_zwrite *z) |
---|
[ce7db4d] | 230 | { |
---|
| 231 | if (z->message) return(1); |
---|
| 232 | return(0); |
---|
| 233 | } |
---|
| 234 | |
---|
[a352029b] | 235 | int owl_zwrite_send_message(const owl_zwrite *z) |
---|
[ce7db4d] | 236 | { |
---|
[7d4fbcd] | 237 | int i, j; |
---|
[823671c] | 238 | char *to = NULL; |
---|
[7d4fbcd] | 239 | |
---|
[ce7db4d] | 240 | if (z->message==NULL) return(-1); |
---|
| 241 | |
---|
[7d4fbcd] | 242 | j=owl_list_get_size(&(z->recips)); |
---|
| 243 | if (j>0) { |
---|
| 244 | for (i=0; i<j; i++) { |
---|
| 245 | if (strcmp(z->realm, "")) { |
---|
[e19eb97] | 246 | to = owl_sprintf("%s@%s", (const char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
[7d4fbcd] | 247 | } else { |
---|
[823671c] | 248 | to = owl_strdup( owl_list_get_element(&(z->recips), i)); |
---|
[7d4fbcd] | 249 | } |
---|
[db2dd3d] | 250 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
[823671c] | 251 | owl_free(to); |
---|
| 252 | to = NULL; |
---|
[7d4fbcd] | 253 | } |
---|
| 254 | } else { |
---|
[823671c] | 255 | to = owl_sprintf( "@%s", z->realm); |
---|
[ce7db4d] | 256 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, z->message); |
---|
| 257 | } |
---|
[823671c] | 258 | owl_free(to); |
---|
[ce7db4d] | 259 | return(0); |
---|
| 260 | } |
---|
| 261 | |
---|
[e19eb97] | 262 | int owl_zwrite_create_and_send_from_line(const char *cmd, const char *msg) |
---|
[ce7db4d] | 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); |
---|
[7d4fbcd] | 270 | } |
---|
[3f3ee61] | 271 | owl_zwrite_populate_zsig(&z); |
---|
[ce7db4d] | 272 | owl_zwrite_send_message(&z); |
---|
| 273 | owl_zwrite_free(&z); |
---|
| 274 | return(0); |
---|
[7d4fbcd] | 275 | } |
---|
| 276 | |
---|
[a352029b] | 277 | const char *owl_zwrite_get_class(const owl_zwrite *z) |
---|
[ce7db4d] | 278 | { |
---|
[7d4fbcd] | 279 | return(z->class); |
---|
| 280 | } |
---|
| 281 | |
---|
[a352029b] | 282 | const char *owl_zwrite_get_instance(const owl_zwrite *z) |
---|
[ce7db4d] | 283 | { |
---|
[7d4fbcd] | 284 | return(z->inst); |
---|
| 285 | } |
---|
| 286 | |
---|
[a352029b] | 287 | const char *owl_zwrite_get_opcode(const owl_zwrite *z) |
---|
[ce7db4d] | 288 | { |
---|
[4b464a4] | 289 | return(z->opcode); |
---|
| 290 | } |
---|
| 291 | |
---|
[e19eb97] | 292 | void owl_zwrite_set_opcode(owl_zwrite *z, const char *opcode) |
---|
[9ceee9d] | 293 | { |
---|
| 294 | if (z->opcode) owl_free(z->opcode); |
---|
[4b17a6c] | 295 | z->opcode=owl_validate_utf8(opcode); |
---|
[9ceee9d] | 296 | } |
---|
| 297 | |
---|
[a352029b] | 298 | const char *owl_zwrite_get_realm(const owl_zwrite *z) |
---|
[ce7db4d] | 299 | { |
---|
[7d4fbcd] | 300 | return(z->realm); |
---|
| 301 | } |
---|
| 302 | |
---|
[a352029b] | 303 | const char *owl_zwrite_get_zsig(const owl_zwrite *z) |
---|
[ce7db4d] | 304 | { |
---|
| 305 | if (z->zsig) return(z->zsig); |
---|
| 306 | return(""); |
---|
[56330ff] | 307 | } |
---|
| 308 | |
---|
[24ccc01] | 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 | |
---|
[a352029b] | 315 | void owl_zwrite_get_recipstr(const owl_zwrite *z, char *buff) |
---|
[ce7db4d] | 316 | { |
---|
[7d4fbcd] | 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 | |
---|
[a352029b] | 328 | int owl_zwrite_get_numrecips(const owl_zwrite *z) |
---|
[ce7db4d] | 329 | { |
---|
[7d4fbcd] | 330 | return(owl_list_get_size(&(z->recips))); |
---|
| 331 | } |
---|
| 332 | |
---|
[a352029b] | 333 | const char *owl_zwrite_get_recip_n(const owl_zwrite *z, int n) |
---|
[ce7db4d] | 334 | { |
---|
[7d4fbcd] | 335 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 336 | } |
---|
| 337 | |
---|
[a352029b] | 338 | int owl_zwrite_is_personal(const owl_zwrite *z) |
---|
[ce7db4d] | 339 | { |
---|
[7d4fbcd] | 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 | } |
---|
[3f3ee61] | 351 | |
---|
[ce7db4d] | 352 | void owl_zwrite_free(owl_zwrite *z) |
---|
| 353 | { |
---|
[7d4fbcd] | 354 | owl_list_free_all(&(z->recips), &owl_free); |
---|
[24ccc01] | 355 | if (z->zwriteline) owl_free(z->zwriteline); |
---|
[ce7db4d] | 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); |
---|
[7d4fbcd] | 362 | } |
---|