[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; |
---|
| 38 | myargc--; |
---|
| 39 | myargv++; |
---|
| 40 | while (myargc) { |
---|
| 41 | if (!strcmp(myargv[0], "-c")) { |
---|
| 42 | if (myargc<2) { |
---|
| 43 | badargs=1; |
---|
| 44 | break; |
---|
| 45 | } |
---|
| 46 | strcpy(z->class, 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 | strcpy(z->inst, 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 | strcpy(z->realm, myargv[1]); |
---|
| 63 | myargv+=2; |
---|
| 64 | myargc-=2; |
---|
| 65 | } else if (!strcmp(myargv[0], "-O")) { |
---|
| 66 | if (myargc<2) { |
---|
| 67 | badargs=1; |
---|
| 68 | break; |
---|
| 69 | } |
---|
| 70 | strcpy(z->opcode, myargv[1]); |
---|
| 71 | myargv+=2; |
---|
| 72 | myargc-=2; |
---|
| 73 | } else if (!strcmp(myargv[0], "-C")) { |
---|
| 74 | z->cc=1; |
---|
| 75 | myargv++; |
---|
| 76 | myargc--; |
---|
| 77 | } else if (!strcmp(myargv[0], "-n")) { |
---|
| 78 | z->noping=1; |
---|
| 79 | myargv++; |
---|
| 80 | myargc--; |
---|
| 81 | } else { |
---|
| 82 | /* anything unattached is a recipient */ |
---|
| 83 | owl_list_append_element(&(z->recips), strdup(myargv[0])); |
---|
| 84 | myargv++; |
---|
| 85 | myargc--; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | owl_parsefree(argv, argc); |
---|
| 90 | |
---|
| 91 | if (badargs) { |
---|
| 92 | return(-1); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | if (!strcasecmp(z->class, "message") && |
---|
| 96 | !strcasecmp(z->inst, "personal") && |
---|
| 97 | owl_list_get_size(&(z->recips))==0) { |
---|
| 98 | /* do the makemsg somewhere else */ |
---|
| 99 | owl_function_makemsg("You must specify a recipient"); |
---|
| 100 | return(-1); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | return(0); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void owl_zwrite_send_ping(owl_zwrite *z) { |
---|
| 107 | int i, j; |
---|
| 108 | char to[LINE]; |
---|
| 109 | |
---|
| 110 | if (z->noping) return; |
---|
| 111 | |
---|
| 112 | if (strcasecmp(z->class, "message") || |
---|
| 113 | strcasecmp(z->inst, "personal")) { |
---|
| 114 | return; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /* if there are no recipients we won't send a ping, which |
---|
| 118 | is what we want */ |
---|
| 119 | j=owl_list_get_size(&(z->recips)); |
---|
| 120 | for (i=0; i<j; i++) { |
---|
| 121 | if (strcmp(z->realm, "")) { |
---|
| 122 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 123 | } else { |
---|
| 124 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 125 | } |
---|
| 126 | send_ping(to); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | void owl_zwrite_send_message(owl_zwrite *z, char *msg) { |
---|
| 132 | int i, j; |
---|
| 133 | char to[LINE]; |
---|
| 134 | |
---|
| 135 | j=owl_list_get_size(&(z->recips)); |
---|
| 136 | if (j>0) { |
---|
| 137 | char *tmpmsg=NULL; |
---|
| 138 | char toline[LINE]; |
---|
| 139 | |
---|
| 140 | if (z->cc) { |
---|
| 141 | strcpy(toline, "CC: "); |
---|
| 142 | for (i=0; i<j; i++) { |
---|
| 143 | if (strcmp(z->realm, "")) { |
---|
| 144 | sprintf(toline, "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 145 | } else { |
---|
| 146 | sprintf(toline, "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i)); |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | tmpmsg=owl_malloc(strlen(msg)+strlen(toline)+30); |
---|
| 150 | sprintf(tmpmsg, "%s\n%s", toline, msg); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | for (i=0; i<j; i++) { |
---|
| 154 | if (strcmp(z->realm, "")) { |
---|
| 155 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 156 | } else { |
---|
| 157 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 158 | } |
---|
| 159 | if (z->cc) { |
---|
| 160 | send_zephyr(z->opcode, NULL, z->class, z->inst, to, tmpmsg); |
---|
| 161 | } else { |
---|
| 162 | send_zephyr(z->opcode, NULL, z->class, z->inst, to, msg); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | if (z->cc) { |
---|
| 166 | owl_free(tmpmsg); |
---|
| 167 | } |
---|
| 168 | } else { |
---|
| 169 | sprintf(to, "@%s", z->realm); |
---|
| 170 | send_zephyr(z->opcode, NULL, z->class, z->inst, to, msg); |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | char *owl_zwrite_get_class(owl_zwrite *z) { |
---|
| 175 | return(z->class); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | char *owl_zwrite_get_instance(owl_zwrite *z) { |
---|
| 179 | return(z->inst); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | char *owl_zwrite_get_realm(owl_zwrite *z) { |
---|
| 183 | return(z->realm); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) { |
---|
| 187 | int i, j; |
---|
| 188 | |
---|
| 189 | strcpy(buff, ""); |
---|
| 190 | j=owl_list_get_size(&(z->recips)); |
---|
| 191 | for (i=0; i<j; i++) { |
---|
| 192 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
| 193 | strcat(buff, " "); |
---|
| 194 | } |
---|
| 195 | buff[strlen(buff)-1]='\0'; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | int owl_zwrite_get_numrecips(owl_zwrite *z) { |
---|
| 199 | return(owl_list_get_size(&(z->recips))); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) { |
---|
| 203 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | int owl_zwrite_is_personal(owl_zwrite *z) { |
---|
| 207 | /* return true if at least one of the recipients is personal */ |
---|
| 208 | int i, j; |
---|
| 209 | char *foo; |
---|
| 210 | |
---|
| 211 | j=owl_list_get_size(&(z->recips)); |
---|
| 212 | for (i=0; i<j; i++) { |
---|
| 213 | foo=owl_list_get_element(&(z->recips), i); |
---|
| 214 | if (foo[0]!='@') return(1); |
---|
| 215 | } |
---|
| 216 | return(0); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | void owl_zwrite_free(owl_zwrite *z) { |
---|
| 221 | owl_list_free_all(&(z->recips), &owl_free); |
---|
| 222 | } |
---|