5370
|
1 /**
|
|
2 * @file page.c Paging functions
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
|
6 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org>
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 */
|
|
22 #include "msn.h"
|
|
23 #include "page.h"
|
|
24
|
|
25 #define GET_NEXT(tmp) \
|
|
26 while (*(tmp) && *(tmp) != ' ' && *(tmp) != '\r') \
|
|
27 (tmp)++; \
|
|
28 if (*(tmp) != '\0') *(tmp)++ = '\0'; \
|
|
29 if (*(tmp) == '\n') *(tmp)++; \
|
|
30 while (*(tmp) && *(tmp) == ' ') \
|
|
31 (tmp)++
|
|
32
|
|
33 #define GET_NEXT_LINE(tmp) \
|
|
34 while (*(tmp) && *(tmp) != '\r') \
|
|
35 (tmp)++; \
|
|
36 if (*(tmp) != '\0') *(tmp)++ = '\0'; \
|
|
37 if (*(tmp) == '\n') *(tmp)++
|
|
38
|
|
39 /*
|
|
40 * "<TEXT>" == 6
|
|
41 * "</TEXT>" == 7
|
|
42 * ----
|
|
43 * 13
|
|
44 */
|
|
45 #define MSN_PAGE_BASE_SIZE 13
|
|
46
|
|
47 MsnPage *
|
|
48 msn_page_new(void)
|
|
49 {
|
|
50 MsnPage *page;
|
|
51
|
|
52 page = g_new0(MsnPage, 1);
|
|
53
|
|
54 page->size = MSN_PAGE_BASE_SIZE;
|
|
55
|
|
56 return page;
|
|
57 }
|
|
58
|
|
59 MsnPage *
|
|
60 msn_page_new_from_str(MsnSession *session, const char *str)
|
|
61 {
|
|
62 g_return_val_if_fail(str != NULL, NULL);
|
|
63
|
|
64 return NULL;
|
|
65 }
|
|
66
|
|
67 void
|
|
68 msn_page_destroy(MsnPage *page)
|
|
69 {
|
|
70 g_return_if_fail(page != NULL);
|
|
71
|
|
72 if (page->sender != NULL)
|
|
73 msn_user_unref(page->sender);
|
|
74
|
|
75 if (page->receiver != NULL)
|
|
76 msn_user_unref(page->receiver);
|
|
77
|
|
78 if (page->body != NULL)
|
|
79 g_free(page->body);
|
|
80
|
|
81 if (page->from_location != NULL)
|
|
82 g_free(page->from_location);
|
|
83
|
|
84 if (page->from_phone != NULL)
|
|
85 g_free(page->from_phone);
|
|
86
|
|
87 g_free(page);
|
|
88 }
|
|
89
|
|
90 char *
|
|
91 msn_page_build_string(const MsnPage *page)
|
|
92 {
|
|
93 char *page_start;
|
|
94 char *str;
|
|
95 char buf[MSN_BUF_LEN];
|
|
96 int len;
|
|
97
|
|
98 /*
|
|
99 * Okay, how we do things here is just bad. I don't like writing to
|
|
100 * a static buffer and then copying to the string. Unfortunately,
|
|
101 * just trying to append to the string is causing issues.. Such as
|
|
102 * the string you're appending to being erased. Ugh. So, this is
|
|
103 * good enough for now.
|
|
104 *
|
|
105 * -- ChipX86
|
|
106 */
|
|
107 g_return_val_if_fail(page != NULL, NULL);
|
|
108
|
|
109 if (msn_page_is_incoming(page)) {
|
|
110 /* We don't know this yet :) */
|
|
111 return NULL;
|
|
112 }
|
|
113 else {
|
|
114 MsnUser *receiver = msn_page_get_receiver(page);
|
|
115
|
|
116 g_snprintf(buf, sizeof(buf), "PAG %d %s %d\r\n",
|
|
117 msn_page_get_transaction_id(page),
|
|
118 msn_user_get_passport(receiver),
|
|
119 page->size);
|
|
120 }
|
|
121
|
|
122 len = strlen(buf) + page->size + 1;
|
|
123
|
|
124 str = g_new0(char, len);
|
|
125
|
|
126 g_strlcpy(str, buf, len);
|
|
127
|
|
128 page_start = str + strlen(str);
|
|
129
|
|
130 g_snprintf(buf, sizeof(buf), "<TEXT>%s</TEXT>", msn_page_get_body(page));
|
|
131
|
|
132 g_strlcat(str, buf, len);
|
|
133
|
|
134 if (page->size != strlen(page_start)) {
|
|
135 gaim_debug(GAIM_DEBUG_ERROR, "msn",
|
|
136 "Outgoing page size (%d) and string length (%d) "
|
|
137 "do not match!\n", page->size, strlen(page_start));
|
|
138 }
|
|
139
|
|
140 return str;
|
|
141 }
|
|
142
|
|
143 gboolean
|
|
144 msn_page_is_outgoing(const MsnPage *page)
|
|
145 {
|
|
146 g_return_val_if_fail(page != NULL, FALSE);
|
|
147
|
|
148 return !page->incoming;
|
|
149 }
|
|
150
|
|
151 gboolean
|
|
152 msn_page_is_incoming(const MsnPage *page)
|
|
153 {
|
|
154 g_return_val_if_fail(page != NULL, FALSE);
|
|
155
|
|
156 return page->incoming;
|
|
157 }
|
|
158
|
|
159 void
|
|
160 msn_page_set_sender(MsnPage *page, MsnUser *user)
|
|
161 {
|
|
162 g_return_if_fail(page != NULL);
|
|
163 g_return_if_fail(user != NULL);
|
|
164
|
|
165 page->sender = user;
|
|
166
|
|
167 msn_user_ref(page->sender);
|
|
168 }
|
|
169
|
|
170 MsnUser *
|
|
171 msn_page_get_sender(const MsnPage *page)
|
|
172 {
|
|
173 g_return_val_if_fail(page != NULL, NULL);
|
|
174
|
|
175 return page->sender;
|
|
176 }
|
|
177
|
|
178 void
|
|
179 msn_page_set_receiver(MsnPage *page, MsnUser *user)
|
|
180 {
|
|
181 g_return_if_fail(page != NULL);
|
|
182 g_return_if_fail(user != NULL);
|
|
183
|
|
184 page->receiver = user;
|
|
185
|
|
186 msn_user_ref(page->receiver);
|
|
187 }
|
|
188
|
|
189 MsnUser *
|
|
190 msn_page_get_receiver(const MsnPage *page)
|
|
191 {
|
|
192 g_return_val_if_fail(page != NULL, NULL);
|
|
193
|
|
194 return page->receiver;
|
|
195 }
|
|
196
|
|
197 void
|
|
198 msn_page_set_transaction_id(MsnPage *page, unsigned int tid)
|
|
199 {
|
|
200 g_return_if_fail(page != NULL);
|
|
201 g_return_if_fail(tid > 0);
|
|
202
|
|
203 page->trId = tid;
|
|
204 }
|
|
205
|
|
206 unsigned int
|
|
207 msn_page_get_transaction_id(const MsnPage *page)
|
|
208 {
|
|
209 g_return_val_if_fail(page != NULL, 0);
|
|
210
|
|
211 return page->trId;
|
|
212 }
|
|
213
|
|
214 void
|
|
215 msn_page_set_body(MsnPage *page, const char *body)
|
|
216 {
|
|
217 g_return_if_fail(page != NULL);
|
|
218 g_return_if_fail(body != NULL);
|
|
219
|
|
220 if (page->body != NULL) {
|
|
221 page->size -= strlen(page->body);
|
|
222 g_free(page->body);
|
|
223 }
|
|
224
|
|
225 page->body = g_strdup(body);
|
|
226
|
|
227 page->size += strlen(body);
|
|
228 }
|
|
229
|
|
230 const char *
|
|
231 msn_page_get_body(const MsnPage *page)
|
|
232 {
|
|
233 g_return_val_if_fail(page != NULL, NULL);
|
|
234
|
|
235 return page->body;
|
|
236 }
|
|
237
|