[7d4fbcd] | 1 | #include <string.h> |
---|
| 2 | #include "owl.h" |
---|
| 3 | |
---|
[1aee7d9] | 4 | static const char fileIdent[] = "$Id$"; |
---|
| 5 | |
---|
[1c6c4d3] | 6 | int owl_zwrite_create_and_send_from_line(char *cmd, char *msg) { |
---|
| 7 | owl_zwrite z; |
---|
| 8 | int rv; |
---|
| 9 | rv = owl_zwrite_create_from_line(&z, cmd); |
---|
| 10 | if (rv) return(rv); |
---|
| 11 | owl_zwrite_send_message(&z, msg); |
---|
| 12 | owl_zwrite_free(&z); |
---|
| 13 | return(0); |
---|
| 14 | } |
---|
| 15 | |
---|
[7d4fbcd] | 16 | int owl_zwrite_create_from_line(owl_zwrite *z, char *line) { |
---|
| 17 | int argc, badargs, myargc; |
---|
| 18 | char **argv, **myargv; |
---|
| 19 | |
---|
| 20 | badargs=0; |
---|
| 21 | |
---|
| 22 | /* set the defaults */ |
---|
| 23 | strcpy(z->realm, ""); |
---|
| 24 | strcpy(z->class, "message"); |
---|
| 25 | strcpy(z->inst, "personal"); |
---|
| 26 | strcpy(z->opcode, ""); |
---|
| 27 | z->cc=0; |
---|
| 28 | z->noping=0; |
---|
| 29 | owl_list_create(&(z->recips)); |
---|
| 30 | |
---|
| 31 | /* parse the command line for options */ |
---|
| 32 | argv=myargv=owl_parseline(line, &argc); |
---|
| 33 | if (argc<0) { |
---|
| 34 | owl_function_makemsg("Unbalanced quotes"); |
---|
| 35 | return(-1); |
---|
| 36 | } |
---|
| 37 | myargc=argc; |
---|
[e1c4636] | 38 | if (myargc && *(myargv[0])!='-') { |
---|
| 39 | myargc--; |
---|
| 40 | myargv++; |
---|
| 41 | } |
---|
[7d4fbcd] | 42 | while (myargc) { |
---|
| 43 | if (!strcmp(myargv[0], "-c")) { |
---|
| 44 | if (myargc<2) { |
---|
| 45 | badargs=1; |
---|
| 46 | break; |
---|
| 47 | } |
---|
| 48 | strcpy(z->class, myargv[1]); |
---|
| 49 | myargv+=2; |
---|
| 50 | myargc-=2; |
---|
| 51 | } else if (!strcmp(myargv[0], "-i")) { |
---|
| 52 | if (myargc<2) { |
---|
| 53 | badargs=1; |
---|
| 54 | break; |
---|
| 55 | } |
---|
| 56 | strcpy(z->inst, myargv[1]); |
---|
| 57 | myargv+=2; |
---|
| 58 | myargc-=2; |
---|
| 59 | } else if (!strcmp(myargv[0], "-r")) { |
---|
| 60 | if (myargc<2) { |
---|
| 61 | badargs=1; |
---|
| 62 | break; |
---|
| 63 | } |
---|
| 64 | strcpy(z->realm, myargv[1]); |
---|
| 65 | myargv+=2; |
---|
| 66 | myargc-=2; |
---|
| 67 | } else if (!strcmp(myargv[0], "-O")) { |
---|
| 68 | if (myargc<2) { |
---|
| 69 | badargs=1; |
---|
| 70 | break; |
---|
| 71 | } |
---|
| 72 | strcpy(z->opcode, myargv[1]); |
---|
| 73 | myargv+=2; |
---|
| 74 | myargc-=2; |
---|
| 75 | } else if (!strcmp(myargv[0], "-C")) { |
---|
| 76 | z->cc=1; |
---|
| 77 | myargv++; |
---|
| 78 | myargc--; |
---|
| 79 | } else if (!strcmp(myargv[0], "-n")) { |
---|
| 80 | z->noping=1; |
---|
| 81 | myargv++; |
---|
| 82 | myargc--; |
---|
| 83 | } else { |
---|
| 84 | /* anything unattached is a recipient */ |
---|
| 85 | owl_list_append_element(&(z->recips), strdup(myargv[0])); |
---|
| 86 | myargv++; |
---|
| 87 | myargc--; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | owl_parsefree(argv, argc); |
---|
| 92 | |
---|
| 93 | if (badargs) { |
---|
| 94 | return(-1); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | if (!strcasecmp(z->class, "message") && |
---|
| 98 | !strcasecmp(z->inst, "personal") && |
---|
| 99 | owl_list_get_size(&(z->recips))==0) { |
---|
| 100 | /* do the makemsg somewhere else */ |
---|
| 101 | owl_function_makemsg("You must specify a recipient"); |
---|
| 102 | return(-1); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | return(0); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void owl_zwrite_send_ping(owl_zwrite *z) { |
---|
| 109 | int i, j; |
---|
| 110 | char to[LINE]; |
---|
| 111 | |
---|
| 112 | if (z->noping) return; |
---|
| 113 | |
---|
| 114 | if (strcasecmp(z->class, "message") || |
---|
| 115 | strcasecmp(z->inst, "personal")) { |
---|
| 116 | return; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | /* if there are no recipients we won't send a ping, which |
---|
| 120 | is what we want */ |
---|
| 121 | j=owl_list_get_size(&(z->recips)); |
---|
| 122 | for (i=0; i<j; i++) { |
---|
| 123 | if (strcmp(z->realm, "")) { |
---|
| 124 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 125 | } else { |
---|
| 126 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 127 | } |
---|
| 128 | send_ping(to); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void owl_zwrite_send_message(owl_zwrite *z, char *msg) { |
---|
| 134 | int i, j; |
---|
| 135 | char to[LINE]; |
---|
| 136 | |
---|
| 137 | j=owl_list_get_size(&(z->recips)); |
---|
| 138 | if (j>0) { |
---|
| 139 | char *tmpmsg=NULL; |
---|
| 140 | char toline[LINE]; |
---|
| 141 | |
---|
| 142 | if (z->cc) { |
---|
| 143 | strcpy(toline, "CC: "); |
---|
| 144 | for (i=0; i<j; i++) { |
---|
| 145 | if (strcmp(z->realm, "")) { |
---|
| 146 | sprintf(toline, "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 147 | } else { |
---|
| 148 | sprintf(toline, "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i)); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | tmpmsg=owl_malloc(strlen(msg)+strlen(toline)+30); |
---|
| 152 | sprintf(tmpmsg, "%s\n%s", toline, msg); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | for (i=0; i<j; i++) { |
---|
| 156 | if (strcmp(z->realm, "")) { |
---|
| 157 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 158 | } else { |
---|
| 159 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 160 | } |
---|
| 161 | if (z->cc) { |
---|
| 162 | send_zephyr(z->opcode, NULL, z->class, z->inst, to, tmpmsg); |
---|
| 163 | } else { |
---|
| 164 | send_zephyr(z->opcode, NULL, z->class, z->inst, to, msg); |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | if (z->cc) { |
---|
| 168 | owl_free(tmpmsg); |
---|
| 169 | } |
---|
| 170 | } else { |
---|
| 171 | sprintf(to, "@%s", z->realm); |
---|
| 172 | send_zephyr(z->opcode, NULL, z->class, z->inst, to, msg); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | char *owl_zwrite_get_class(owl_zwrite *z) { |
---|
| 177 | return(z->class); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | char *owl_zwrite_get_instance(owl_zwrite *z) { |
---|
| 181 | return(z->inst); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | char *owl_zwrite_get_realm(owl_zwrite *z) { |
---|
| 185 | return(z->realm); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) { |
---|
| 189 | int i, j; |
---|
| 190 | |
---|
| 191 | strcpy(buff, ""); |
---|
| 192 | j=owl_list_get_size(&(z->recips)); |
---|
| 193 | for (i=0; i<j; i++) { |
---|
| 194 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
| 195 | strcat(buff, " "); |
---|
| 196 | } |
---|
| 197 | buff[strlen(buff)-1]='\0'; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | int owl_zwrite_get_numrecips(owl_zwrite *z) { |
---|
| 201 | return(owl_list_get_size(&(z->recips))); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) { |
---|
| 205 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | int owl_zwrite_is_personal(owl_zwrite *z) { |
---|
| 209 | /* return true if at least one of the recipients is personal */ |
---|
| 210 | int i, j; |
---|
| 211 | char *foo; |
---|
| 212 | |
---|
| 213 | j=owl_list_get_size(&(z->recips)); |
---|
| 214 | for (i=0; i<j; i++) { |
---|
| 215 | foo=owl_list_get_element(&(z->recips), i); |
---|
| 216 | if (foo[0]!='@') return(1); |
---|
| 217 | } |
---|
| 218 | return(0); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | void owl_zwrite_free(owl_zwrite *z) { |
---|
| 223 | owl_list_free_all(&(z->recips), &owl_free); |
---|
| 224 | } |
---|