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