[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 | |
---|
[1aee7d9] | 7 | static const char fileIdent[] = "$Id$"; |
---|
| 8 | |
---|
[e3869df] | 9 | Code_t ZResetAuthentication(); |
---|
| 10 | |
---|
[1c6c4d3] | 11 | int owl_zwrite_create_and_send_from_line(char *cmd, char *msg) { |
---|
| 12 | owl_zwrite z; |
---|
| 13 | int rv; |
---|
| 14 | rv = owl_zwrite_create_from_line(&z, cmd); |
---|
| 15 | if (rv) return(rv); |
---|
| 16 | owl_zwrite_send_message(&z, msg); |
---|
| 17 | owl_zwrite_free(&z); |
---|
| 18 | return(0); |
---|
| 19 | } |
---|
| 20 | |
---|
[7d4fbcd] | 21 | int owl_zwrite_create_from_line(owl_zwrite *z, char *line) { |
---|
| 22 | int argc, badargs, myargc; |
---|
| 23 | char **argv, **myargv; |
---|
[ced25d1] | 24 | char *zsigproc, *zsigowlvar, *zsigzvar, *ptr; |
---|
[56330ff] | 25 | struct passwd *pw; |
---|
[7d4fbcd] | 26 | |
---|
| 27 | badargs=0; |
---|
| 28 | |
---|
| 29 | /* set the defaults */ |
---|
| 30 | strcpy(z->realm, ""); |
---|
| 31 | strcpy(z->class, "message"); |
---|
| 32 | strcpy(z->inst, "personal"); |
---|
| 33 | strcpy(z->opcode, ""); |
---|
[56330ff] | 34 | z->zsig=owl_strdup(""); |
---|
[7d4fbcd] | 35 | z->cc=0; |
---|
| 36 | z->noping=0; |
---|
| 37 | owl_list_create(&(z->recips)); |
---|
| 38 | |
---|
| 39 | /* parse the command line for options */ |
---|
| 40 | argv=myargv=owl_parseline(line, &argc); |
---|
| 41 | if (argc<0) { |
---|
| 42 | owl_function_makemsg("Unbalanced quotes"); |
---|
| 43 | return(-1); |
---|
| 44 | } |
---|
| 45 | myargc=argc; |
---|
[e1c4636] | 46 | if (myargc && *(myargv[0])!='-') { |
---|
| 47 | myargc--; |
---|
| 48 | myargv++; |
---|
| 49 | } |
---|
[7d4fbcd] | 50 | while (myargc) { |
---|
| 51 | if (!strcmp(myargv[0], "-c")) { |
---|
| 52 | if (myargc<2) { |
---|
| 53 | badargs=1; |
---|
| 54 | break; |
---|
| 55 | } |
---|
| 56 | strcpy(z->class, myargv[1]); |
---|
| 57 | myargv+=2; |
---|
| 58 | myargc-=2; |
---|
| 59 | } else if (!strcmp(myargv[0], "-i")) { |
---|
| 60 | if (myargc<2) { |
---|
| 61 | badargs=1; |
---|
| 62 | break; |
---|
| 63 | } |
---|
| 64 | strcpy(z->inst, myargv[1]); |
---|
| 65 | myargv+=2; |
---|
| 66 | myargc-=2; |
---|
| 67 | } else if (!strcmp(myargv[0], "-r")) { |
---|
| 68 | if (myargc<2) { |
---|
| 69 | badargs=1; |
---|
| 70 | break; |
---|
| 71 | } |
---|
| 72 | strcpy(z->realm, myargv[1]); |
---|
| 73 | myargv+=2; |
---|
| 74 | myargc-=2; |
---|
| 75 | } else if (!strcmp(myargv[0], "-O")) { |
---|
| 76 | if (myargc<2) { |
---|
| 77 | badargs=1; |
---|
| 78 | break; |
---|
| 79 | } |
---|
| 80 | strcpy(z->opcode, myargv[1]); |
---|
| 81 | myargv+=2; |
---|
| 82 | myargc-=2; |
---|
| 83 | } else if (!strcmp(myargv[0], "-C")) { |
---|
| 84 | z->cc=1; |
---|
| 85 | myargv++; |
---|
| 86 | myargc--; |
---|
| 87 | } else if (!strcmp(myargv[0], "-n")) { |
---|
| 88 | z->noping=1; |
---|
| 89 | myargv++; |
---|
| 90 | myargc--; |
---|
| 91 | } else { |
---|
| 92 | /* anything unattached is a recipient */ |
---|
| 93 | owl_list_append_element(&(z->recips), strdup(myargv[0])); |
---|
| 94 | myargv++; |
---|
| 95 | myargc--; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | owl_parsefree(argv, argc); |
---|
| 100 | |
---|
| 101 | if (badargs) { |
---|
| 102 | return(-1); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | if (!strcasecmp(z->class, "message") && |
---|
| 106 | !strcasecmp(z->inst, "personal") && |
---|
| 107 | owl_list_get_size(&(z->recips))==0) { |
---|
| 108 | /* do the makemsg somewhere else */ |
---|
| 109 | owl_function_makemsg("You must specify a recipient"); |
---|
| 110 | return(-1); |
---|
| 111 | } |
---|
| 112 | |
---|
[56330ff] | 113 | /* set a zsig */ |
---|
[ced25d1] | 114 | zsigproc = owl_global_get_zsigproc(&g); |
---|
[56330ff] | 115 | zsigowlvar = owl_global_get_zsig(&g); |
---|
| 116 | zsigzvar = ZGetVariable("zwrite-signature"); |
---|
| 117 | |
---|
| 118 | if (zsigowlvar && *zsigowlvar) { |
---|
| 119 | owl_free(z->zsig); |
---|
| 120 | z->zsig=strdup(zsigowlvar); |
---|
[ced25d1] | 121 | } else if (zsigproc && *zsigproc) { |
---|
[56330ff] | 122 | FILE *file; |
---|
[ced25d1] | 123 | char buff[LINE], *openline; |
---|
[56330ff] | 124 | |
---|
[ced25d1] | 125 | /* simple hack for now to nuke stderr */ |
---|
| 126 | openline=owl_malloc(strlen(zsigproc)+40); |
---|
| 127 | strcpy(openline, zsigproc); |
---|
| 128 | strcat(openline, " 2> /dev/null"); |
---|
| 129 | file=popen(openline, "r"); |
---|
| 130 | owl_free(openline); |
---|
[56330ff] | 131 | if (!file) { |
---|
| 132 | if (zsigzvar && *zsigzvar) { |
---|
| 133 | owl_free(z->zsig); |
---|
| 134 | z->zsig=owl_strdup(zsigzvar); |
---|
| 135 | } |
---|
| 136 | } else { |
---|
| 137 | owl_free(z->zsig); |
---|
| 138 | z->zsig=owl_malloc(LINE); |
---|
| 139 | strcpy(z->zsig, ""); |
---|
| 140 | while (fgets(buff, LINE, file)) { /* wrong sizing */ |
---|
| 141 | strcat(z->zsig, buff); |
---|
| 142 | } |
---|
| 143 | pclose(file); |
---|
| 144 | if (z->zsig[strlen(z->zsig)-1]=='\n') { |
---|
| 145 | z->zsig[strlen(z->zsig)-1]='\0'; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | } else if (zsigzvar) { |
---|
| 149 | owl_free(z->zsig); |
---|
| 150 | z->zsig=owl_strdup(zsigzvar); |
---|
| 151 | } else if (((pw=getpwuid(getuid()))!=NULL) && (pw->pw_gecos)) { |
---|
| 152 | owl_free(z->zsig); |
---|
| 153 | z->zsig=strdup(pw->pw_gecos); |
---|
| 154 | ptr=strchr(z->zsig, ','); |
---|
| 155 | if (ptr) { |
---|
| 156 | ptr[0]='\0'; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
[7d4fbcd] | 160 | return(0); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void owl_zwrite_send_ping(owl_zwrite *z) { |
---|
| 164 | int i, j; |
---|
| 165 | char to[LINE]; |
---|
| 166 | |
---|
| 167 | if (z->noping) return; |
---|
| 168 | |
---|
| 169 | if (strcasecmp(z->class, "message") || |
---|
| 170 | strcasecmp(z->inst, "personal")) { |
---|
| 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 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 180 | } else { |
---|
| 181 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 182 | } |
---|
| 183 | send_ping(to); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void owl_zwrite_send_message(owl_zwrite *z, char *msg) { |
---|
| 189 | int i, j; |
---|
| 190 | char to[LINE]; |
---|
| 191 | |
---|
| 192 | j=owl_list_get_size(&(z->recips)); |
---|
| 193 | if (j>0) { |
---|
| 194 | char *tmpmsg=NULL; |
---|
| 195 | char toline[LINE]; |
---|
| 196 | |
---|
| 197 | if (z->cc) { |
---|
| 198 | strcpy(toline, "CC: "); |
---|
| 199 | for (i=0; i<j; i++) { |
---|
| 200 | if (strcmp(z->realm, "")) { |
---|
| 201 | sprintf(toline, "%s%s@%s ", toline, (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 202 | } else { |
---|
| 203 | sprintf(toline, "%s%s ", toline, (char *) owl_list_get_element(&(z->recips), i)); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | tmpmsg=owl_malloc(strlen(msg)+strlen(toline)+30); |
---|
| 207 | sprintf(tmpmsg, "%s\n%s", toline, msg); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | for (i=0; i<j; i++) { |
---|
| 211 | if (strcmp(z->realm, "")) { |
---|
| 212 | sprintf(to, "%s@%s", (char *) owl_list_get_element(&(z->recips), i), z->realm); |
---|
| 213 | } else { |
---|
| 214 | strcpy(to, owl_list_get_element(&(z->recips), i)); |
---|
| 215 | } |
---|
| 216 | if (z->cc) { |
---|
[56330ff] | 217 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, tmpmsg); |
---|
[7d4fbcd] | 218 | } else { |
---|
[56330ff] | 219 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, msg); |
---|
[7d4fbcd] | 220 | } |
---|
| 221 | } |
---|
| 222 | if (z->cc) { |
---|
| 223 | owl_free(tmpmsg); |
---|
| 224 | } |
---|
| 225 | } else { |
---|
| 226 | sprintf(to, "@%s", z->realm); |
---|
[c01e477] | 227 | send_zephyr(z->opcode, z->zsig, z->class, z->inst, to, msg); |
---|
[7d4fbcd] | 228 | } |
---|
[aecf3e6] | 229 | |
---|
| 230 | ZResetAuthentication(); |
---|
[7d4fbcd] | 231 | } |
---|
| 232 | |
---|
| 233 | char *owl_zwrite_get_class(owl_zwrite *z) { |
---|
| 234 | return(z->class); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | char *owl_zwrite_get_instance(owl_zwrite *z) { |
---|
| 238 | return(z->inst); |
---|
| 239 | } |
---|
| 240 | |
---|
[4b464a4] | 241 | char *owl_zwrite_get_opcode(owl_zwrite *z) { |
---|
| 242 | return(z->opcode); |
---|
| 243 | } |
---|
| 244 | |
---|
[7d4fbcd] | 245 | char *owl_zwrite_get_realm(owl_zwrite *z) { |
---|
| 246 | return(z->realm); |
---|
| 247 | } |
---|
| 248 | |
---|
[56330ff] | 249 | char *owl_zwrite_get_zsig(owl_zwrite *z) { |
---|
| 250 | return(z->zsig); |
---|
| 251 | } |
---|
| 252 | |
---|
[7d4fbcd] | 253 | void owl_zwrite_get_recipstr(owl_zwrite *z, char *buff) { |
---|
| 254 | int i, j; |
---|
| 255 | |
---|
| 256 | strcpy(buff, ""); |
---|
| 257 | j=owl_list_get_size(&(z->recips)); |
---|
| 258 | for (i=0; i<j; i++) { |
---|
| 259 | strcat(buff, owl_list_get_element(&(z->recips), i)); |
---|
| 260 | strcat(buff, " "); |
---|
| 261 | } |
---|
| 262 | buff[strlen(buff)-1]='\0'; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | int owl_zwrite_get_numrecips(owl_zwrite *z) { |
---|
| 266 | return(owl_list_get_size(&(z->recips))); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | char *owl_zwrite_get_recip_n(owl_zwrite *z, int n) { |
---|
| 270 | return(owl_list_get_element(&(z->recips), n)); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | int owl_zwrite_is_personal(owl_zwrite *z) { |
---|
| 274 | /* return true if at least one of the recipients is personal */ |
---|
| 275 | int i, j; |
---|
| 276 | char *foo; |
---|
| 277 | |
---|
| 278 | j=owl_list_get_size(&(z->recips)); |
---|
| 279 | for (i=0; i<j; i++) { |
---|
| 280 | foo=owl_list_get_element(&(z->recips), i); |
---|
| 281 | if (foo[0]!='@') return(1); |
---|
| 282 | } |
---|
| 283 | return(0); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | void owl_zwrite_free(owl_zwrite *z) { |
---|
| 287 | owl_list_free_all(&(z->recips), &owl_free); |
---|
[56330ff] | 288 | owl_free(z->zsig); |
---|
[7d4fbcd] | 289 | } |
---|