comparison libpurple/protocols/myspace/zap.c @ 19432:210f792efd7c

In msimprpl, move zap-related code to a separate module.
author Jeffrey Connelly <jaconnel@calpoly.edu>
date Sun, 26 Aug 2007 06:30:41 +0000
parents
children 1b5e786d137a
comparison
equal deleted inserted replaced
19431:3b7539c7402e 19432:210f792efd7c
1 /* MySpaceIM Protocol Plugin - zap support
2 *
3 * Copyright (C) 2007, Jeff Connelly <jeff2@soc.pidgin.im>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include "myspace.h"
21 #include "zap.h"
22
23 static gboolean msim_send_zap(MsimSession *session, const gchar *username, guint code);
24 static void msim_send_zap_from_menu(PurpleBlistNode *node, gpointer zap_num_ptr);
25
26
27 /** Get zap types. */
28 GList *
29 msim_attention_types(PurpleAccount *acct)
30 {
31 static GList *types = NULL;
32 MsimAttentionType* attn;
33
34 if (!types) {
35 #define _MSIM_ADD_NEW_ATTENTION(icn, nme, incoming, outgoing) \
36 attn = g_new0(MsimAttentionType, 1); \
37 attn->icon_name = icn; \
38 attn->name = nme; \
39 attn->incoming_description = incoming; \
40 attn->outgoing_description = outgoing; \
41 types = g_list_append(types, attn);
42
43 /* TODO: icons for each zap */
44 _MSIM_ADD_NEW_ATTENTION(NULL, _("zap"), _("zapped"), _("Zapping"));
45 _MSIM_ADD_NEW_ATTENTION(NULL, _("whack"), _("whacked"), _("Whacking"));
46 _MSIM_ADD_NEW_ATTENTION(NULL, _("torch"), _("torched"), _("Torching"));
47 _MSIM_ADD_NEW_ATTENTION(NULL, _("smooch"), _("smooched"), _("Smooching"));
48 _MSIM_ADD_NEW_ATTENTION(NULL, _("hug"), _("hugged"), _("Hugging"));
49 _MSIM_ADD_NEW_ATTENTION(NULL, _("bslap"), _("bslapped"), _("Bslapping"));
50 _MSIM_ADD_NEW_ATTENTION(NULL, _("goose"), _("goosed"), _("Goosing"));
51 _MSIM_ADD_NEW_ATTENTION(NULL, _("hi-five"), _("hi-fived"), _("Hi-fiving"));
52 _MSIM_ADD_NEW_ATTENTION(NULL, _("punk"), _("punk'd"), _("Punking"));
53 _MSIM_ADD_NEW_ATTENTION(NULL, _("raspberry"), _("raspberried"), _("Raspberry'ing"));
54 }
55
56 return types;
57 }
58
59 /** Send a zap */
60 gboolean
61 msim_send_attention(PurpleConnection *gc, const gchar *username, guint code)
62 {
63 GList *types;
64 MsimSession *session;
65 MsimAttentionType *attn;
66 PurpleBuddy *buddy;
67
68 session = (MsimSession *)gc->proto_data;
69
70 /* Look for this attention type, by the code index given. */
71 types = msim_attention_types(gc->account);
72 attn = (MsimAttentionType *)g_list_nth_data(types, code);
73
74 if (!attn) {
75 purple_debug_info("msim_send_attention", "got invalid zap code %d\n", code);
76 return FALSE;
77 }
78
79 buddy = purple_find_buddy(session->account, username);
80 if (!buddy) {
81 return FALSE;
82 }
83
84 /* TODO: make use of the MsimAttentionType we found, instead of
85 * doing it all over in msim_send_zap_from_menu. */
86 msim_send_zap_from_menu(&buddy->node, GUINT_TO_POINTER(code));
87
88 return TRUE;
89 }
90
91 /** Send a zap to a user. */
92 static gboolean
93 msim_send_zap(MsimSession *session, const gchar *username, guint code)
94 {
95 gchar *zap_string;
96 #ifndef MSIM_USE_ATTENTION_API
97 gchar *zap_description;
98 #endif
99 GList *types;
100 MsimAttentionType *attn;
101 gboolean rc;
102
103 g_return_val_if_fail(session != NULL, FALSE);
104 g_return_val_if_fail(username != NULL, FALSE);
105
106 types = msim_attention_types(session->account);
107
108 attn = g_list_nth_data(types, code);
109 if (!attn) {
110 return FALSE;
111 }
112
113
114 #ifdef MSIM_USE_ATTENTION_API
115 serv_got_attention(session->gc, username, attn, FALSE);
116 #else
117 zap_description = g_strdup_printf("*** Attention: %s %s ***", attn->outgoing_description,
118 username);
119
120 serv_got_im(session->gc, username, zap_description,
121 PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_SYSTEM, time(NULL));
122
123 g_free(zap_description);
124 #endif
125
126 /* Construct and send the actual zap command. */
127 zap_string = g_strdup_printf("!!!ZAP_SEND!!!=RTE_BTN_ZAPS_%d", code);
128
129 if (!msim_send_bm(session, username, zap_string, MSIM_BM_ACTION)) {
130 purple_debug_info("msim_send_zap_from_menu", "msim_send_bm failed: zapping %s with %s",
131 username, zap_string);
132 rc = FALSE;
133 } else {
134 rc = TRUE;
135 }
136
137 g_free(zap_string);
138
139 return rc;
140
141 }
142
143 /** Zap someone. Callback from msim_blist_node_menu zap menu. */
144 static void
145 msim_send_zap_from_menu(PurpleBlistNode *node, gpointer zap_num_ptr)
146 {
147 PurpleBuddy *buddy;
148 PurpleAccount *account;
149 PurpleConnection *gc;
150 MsimSession *session;
151 guint zap;
152
153 if (!PURPLE_BLIST_NODE_IS_BUDDY(node)) {
154 /* Only know about buddies for now. */
155 return;
156 }
157
158 g_return_if_fail(PURPLE_BLIST_NODE_IS_BUDDY(node));
159
160 buddy = (PurpleBuddy *)node;
161
162 /* Find the session */
163 account = buddy->account;
164 gc = purple_account_get_connection(account);
165 session = (MsimSession *)gc->proto_data;
166
167 zap = GPOINTER_TO_INT(zap_num_ptr);
168
169 g_return_if_fail(msim_send_zap(session, buddy->name, zap));
170 }
171
172 /** Return menu, if any, for a buddy list node. */
173 GList *
174 msim_blist_node_menu(PurpleBlistNode *node)
175 {
176 GList *menu, *zap_menu;
177 GList *types;
178 PurpleMenuAction *act;
179 /* Warning: hardcoded to match that in msim_attention_types. */
180 const gchar *zap_names[10];
181 guint i;
182
183 if (!PURPLE_BLIST_NODE_IS_BUDDY(node)) {
184 /* Only know about buddies for now. */
185 return NULL;
186 }
187
188 /* Names from official client. */
189 types = msim_attention_types(NULL);
190 i = 0;
191 do
192 {
193 MsimAttentionType *attn;
194
195 attn = (MsimAttentionType *)types->data;
196 zap_names[i] = attn->name;
197 ++i;
198 } while ((types = g_list_next(types)));
199
200 menu = zap_menu = NULL;
201
202 /* TODO: get rid of once is accessible directly in GUI */
203 for (i = 0; i < sizeof(zap_names) / sizeof(zap_names[0]); ++i) {
204 act = purple_menu_action_new(zap_names[i], PURPLE_CALLBACK(msim_send_zap_from_menu),
205 GUINT_TO_POINTER(i), NULL);
206 zap_menu = g_list_append(zap_menu, act);
207 }
208
209 act = purple_menu_action_new(_("Zap"), NULL, NULL, zap_menu);
210 menu = g_list_append(menu, act);
211
212 return menu;
213 }
214
215 /** Process an incoming zap. */
216 gboolean
217 msim_incoming_zap(MsimSession *session, MsimMessage *msg)
218 {
219 gchar *msg_text, *username;
220 gint zap;
221 const gchar *zap_past_tense[10];
222 #ifdef MSIM_USE_ATTENTION_API
223 MsimAttentionType attn;
224 #else
225 gchar *zap_text;
226 #endif
227
228 zap_past_tense[0] = _("zapped");
229 zap_past_tense[1] = _("whacked");
230 zap_past_tense[2] = _("torched");
231 zap_past_tense[3] = _("smooched");
232 zap_past_tense[4] = _("hugged");
233 zap_past_tense[5] = _("bslapped");
234 zap_past_tense[6] = _("goosed");
235 zap_past_tense[7] = _("hi-fived");
236 zap_past_tense[8] = _("punk'd");
237 zap_past_tense[9] = _("raspberried");
238
239 msg_text = msim_msg_get_string(msg, "msg");
240 username = msim_msg_get_string(msg, "_username");
241
242 g_return_val_if_fail(msg_text != NULL, FALSE);
243 g_return_val_if_fail(username != NULL, FALSE);
244
245 g_return_val_if_fail(sscanf(msg_text, "!!!ZAP_SEND!!!=RTE_BTN_ZAPS_%d", &zap) == 1, FALSE);
246
247 zap = CLAMP(zap, 0, sizeof(zap_past_tense) / sizeof(zap_past_tense[0]));
248
249 /* TODO:ZAP: use msim_attention_types */
250 #ifdef MSIM_USE_ATTENTION_API
251 attn.incoming_description = zap_past_tense[zap];
252 attn.outgoing_description = NULL;
253 attn.icon_name = NULL; /* TODO: icon */
254
255 serv_got_attention(session->gc, username, &attn, TRUE);
256 #else
257 zap_text = g_strdup_printf(_("*** You have been %s! ***"), zap_past_tense[zap]);
258 serv_got_im(session->gc, username, zap_text,
259 PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_SYSTEM, time(NULL));
260 g_free(zap_text);
261 #endif
262
263 g_free(msg_text);
264 g_free(username);
265
266 return TRUE;
267 }
268
269