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