comparison libpurple/protocols/msn/state.c @ 20394:4a099e4d0d09

propagate from branch 'im.pidgin.pidgin' (head 98b6b547b29ea1192b73cc4e1de1e674edef4328) to branch 'im.pidgin.rlaager.merging.msnp13-and-pidgin' (head 4d82c29e56bd33cd6f94302e343dfeb5d68ab3eb)
author Richard Laager <rlaager@wiktel.com>
date Sun, 15 Apr 2007 03:43:17 +0000
parents 32c366eeeb99
children 6f986caeab59 4ddc27c18781
comparison
equal deleted inserted replaced
20393:40a04930b233 20394:4a099e4d0d09
1 /**
2 * @file state.c State functions and definitions
3 *
4 * purple
5 *
6 * Purple is the legal property of its developers, whose names are too numerous
7 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * source distribution.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 #include "msn.h"
25 #include "state.h"
26
27 static const char *away_text[] =
28 {
29 N_("Available"),
30 N_("Available"),
31 N_("Busy"),
32 N_("Idle"),
33 N_("Be Right Back"),
34 N_("Away From Computer"),
35 N_("On The Phone"),
36 N_("Out To Lunch"),
37 N_("Available"),
38 N_("Available")
39 };
40
41 /* Local Function Prototype*/
42 static char *msn_build_psm(const char *psmstr,const char *mediastr,
43 const char *guidstr);
44
45 /*
46 * WLM media PSM info build prcedure
47 *
48 * Result can like:
49 * <CurrentMedia>\0Music\01\0{0} - {1}\0 Song Title\0Song Artist\0Song Album\0\0</CurrentMedia>\
50 * <CurrentMedia>\0Games\01\0Playing {0}\0Game Name\0</CurrentMedia>\
51 * <CurrentMedia>\0Office\01\0Office Message\0Office App Name\0</CurrentMedia>"
52 */
53 static char *
54 msn_build_psm(const char *psmstr,const char *mediastr, const char *guidstr)
55 {
56 xmlnode *dataNode,*psmNode,*mediaNode,*guidNode;
57 char *result;
58 int length;
59
60 dataNode = xmlnode_new("Data");
61
62 psmNode = xmlnode_new("PSM");
63 if(psmstr != NULL){
64 xmlnode_insert_data(psmNode,psmstr,strlen(psmstr));
65 }
66 xmlnode_insert_child(dataNode,psmNode);
67
68 mediaNode = xmlnode_new("CurrentMedia");
69 if(mediastr != NULL){
70 xmlnode_insert_data(psmNode,mediastr,strlen(mediastr));
71 }
72 xmlnode_insert_child(dataNode,mediaNode);
73
74 guidNode = xmlnode_new("MachineGuid");
75 if(guidstr != NULL){
76 xmlnode_insert_data(guidNode,guidstr,strlen(guidstr));
77 }
78 xmlnode_insert_child(dataNode,guidNode);
79
80 result = xmlnode_to_str(dataNode,&length);
81 return result;
82 }
83
84 /*get the PSM info from the XML string*/
85 char *
86 msn_get_psm(char *xml_str, gsize len)
87 {
88 xmlnode *payloadNode, *psmNode;
89 char *psm_str, *psm;
90
91 gaim_debug_info("Ma Yuan","msn get PSM\n");
92 payloadNode = xmlnode_from_str(xml_str, len);
93 if (!payloadNode){
94 gaim_debug_error("MaYuan","PSM XML parse Error!\n");
95 return NULL;
96 }
97 psmNode = xmlnode_get_child(payloadNode, "PSM");
98 if (psmNode == NULL){
99 gaim_debug_info("Ma Yuan","No PSM status Node");
100 g_free(payloadNode);
101 return NULL;
102 }
103 psm_str = xmlnode_get_data(psmNode);
104 psm = g_strdup(psm_str);
105
106 g_free(psmNode);
107 g_free(payloadNode);
108
109 return psm;
110 }
111
112 /* set the MSN's PSM info,Currently Read from the status Line
113 * Thanks for Cris Code
114 */
115 void
116 msn_set_psm(MsnSession *session)
117 {
118 GaimAccount *account = session->account;
119 GaimPresence *presence;
120 GaimStatus *status;
121 MsnCmdProc *cmdproc;
122 MsnTransaction *trans;
123 char *payload;
124 const char *statusline;
125
126 g_return_if_fail(session != NULL);
127 g_return_if_fail(session->notification != NULL);
128
129 cmdproc = session->notification->cmdproc;
130 /*prepare PSM info*/
131 if(session->psm){
132 g_free(session->psm);
133 }
134 /*Get the PSM string from Gaim's Status Line*/
135 presence = gaim_account_get_presence(account);
136 status = gaim_presence_get_active_status(presence);
137 statusline = gaim_status_get_attr_string(status, "message");
138 session ->psm = msn_build_psm(statusline, NULL, NULL);
139 payload = session->psm;
140
141 gaim_debug_info("MaYuan","UUX{%s}\n",payload);
142 trans = msn_transaction_new(cmdproc, "UUX","%d",strlen(payload));
143 msn_transaction_set_payload(trans, payload, strlen(payload));
144 msn_cmdproc_send_trans(cmdproc, trans);
145 }
146
147 void
148 msn_change_status(MsnSession *session)
149 {
150 PurpleAccount *account;
151 MsnCmdProc *cmdproc;
152 MsnUser *user;
153 MsnObject *msnobj;
154 const char *state_text;
155
156 g_return_if_fail(session != NULL);
157 g_return_if_fail(session->notification != NULL);
158
159 account = session->account;
160 cmdproc = session->notification->cmdproc;
161 user = session->user;
162 state_text = msn_state_get_text(msn_state_from_account(account));
163
164 /* If we're not logged in yet, don't send the status to the server,
165 * it will be sent when login completes
166 */
167 if (!session->logged_in)
168 return;
169
170 msnobj = msn_user_get_object(user);
171
172 if (msnobj == NULL){
173 msn_cmdproc_send(cmdproc, "CHG", "%s %d", state_text,
174 MSN_CLIENT_ID);
175 }else{
176 char *msnobj_str;
177
178 msnobj_str = msn_object_to_string(msnobj);
179
180 msn_cmdproc_send(cmdproc, "CHG", "%s %d %s", state_text,
181 MSN_CLIENT_ID, purple_url_encode(msnobj_str));
182
183 g_free(msnobj_str);
184 }
185 msn_set_psm(session);
186 }
187
188 const char *
189 msn_away_get_text(MsnAwayType type)
190 {
191 g_return_val_if_fail(type <= MSN_HIDDEN, NULL);
192
193 return _(away_text[type]);
194 }
195
196 const char *
197 msn_state_get_text(MsnAwayType state)
198 {
199 static char *status_text[] =
200 { "NLN", "NLN", "BSY", "IDL", "BRB", "AWY", "PHN", "LUN", "HDN", "HDN" };
201
202 return status_text[state];
203 }
204
205 MsnAwayType
206 msn_state_from_account(PurpleAccount *account)
207 {
208 MsnAwayType msnstatus;
209 PurplePresence *presence;
210 PurpleStatus *status;
211 const char *status_id;
212
213 presence = purple_account_get_presence(account);
214 status = purple_presence_get_active_status(presence);
215 status_id = purple_status_get_id(status);
216
217 if (!strcmp(status_id, "away"))
218 msnstatus = MSN_AWAY;
219 else if (!strcmp(status_id, "brb"))
220 msnstatus = MSN_BRB;
221 else if (!strcmp(status_id, "busy"))
222 msnstatus = MSN_BUSY;
223 else if (!strcmp(status_id, "phone"))
224 msnstatus = MSN_PHONE;
225 else if (!strcmp(status_id, "lunch"))
226 msnstatus = MSN_LUNCH;
227 else if (!strcmp(status_id, "invisible"))
228 msnstatus = MSN_HIDDEN;
229 else
230 msnstatus = MSN_ONLINE;
231
232 if ((msnstatus == MSN_ONLINE) && purple_presence_is_idle(presence))
233 msnstatus = MSN_IDLE;
234
235 return msnstatus;
236 }