1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Simple module to facilitate twitter functionality. * |
---|
5 | * * |
---|
6 | * Copyright 2009 Geert Mulders <g.c.w.m.mulders@gmail.com> * |
---|
7 | * * |
---|
8 | * This library is free software; you can redistribute it and/or * |
---|
9 | * modify it under the terms of the GNU Lesser General Public * |
---|
10 | * License as published by the Free Software Foundation, version * |
---|
11 | * 2.1. * |
---|
12 | * * |
---|
13 | * This library 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 GNU * |
---|
16 | * Lesser General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
21 | * * |
---|
22 | ****************************************************************************/ |
---|
23 | |
---|
24 | #include "nogaim.h" |
---|
25 | #include "twitter.h" |
---|
26 | #include "twitter_http.h" |
---|
27 | #include "twitter_lib.h" |
---|
28 | |
---|
29 | |
---|
30 | /** |
---|
31 | * Main loop function |
---|
32 | */ |
---|
33 | gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond) |
---|
34 | { |
---|
35 | struct im_connection *ic = data; |
---|
36 | |
---|
37 | // Check if we are still logged in... |
---|
38 | if (!g_slist_find( twitter_connections, ic )) |
---|
39 | return 0; |
---|
40 | |
---|
41 | // If the user uses multiple private message windows we need to get the |
---|
42 | // users buddies. |
---|
43 | if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "many") == 0) |
---|
44 | twitter_get_statuses_friends(ic, -1); |
---|
45 | |
---|
46 | // Do stuff.. |
---|
47 | twitter_get_home_timeline(ic, -1); |
---|
48 | |
---|
49 | // If we are still logged in run this function again after timeout. |
---|
50 | return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN; |
---|
51 | } |
---|
52 | |
---|
53 | static char *set_eval_mode( set_t *set, char *value ) |
---|
54 | { |
---|
55 | if( g_strcasecmp( value, "one" ) == 0 || |
---|
56 | g_strcasecmp( value, "many" ) == 0 || |
---|
57 | g_strcasecmp( value, "char" ) == 0 ) |
---|
58 | return value; |
---|
59 | else |
---|
60 | return NULL; |
---|
61 | } |
---|
62 | |
---|
63 | static void twitter_init( account_t *acc ) |
---|
64 | { |
---|
65 | set_t *s; |
---|
66 | |
---|
67 | s = set_add( &acc->set, "mode", "one", set_eval_mode, acc ); |
---|
68 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Login method. Since the twitter API works with seperate HTTP request we |
---|
73 | * only save the user and pass to the twitter_data object. |
---|
74 | */ |
---|
75 | static void twitter_login( account_t *acc ) |
---|
76 | { |
---|
77 | struct im_connection *ic = imcb_new( acc ); |
---|
78 | struct twitter_data *td = g_new0( struct twitter_data, 1 ); |
---|
79 | char name[strlen(acc->user)+9]; |
---|
80 | |
---|
81 | twitter_connections = g_slist_append( twitter_connections, ic ); |
---|
82 | |
---|
83 | td->user = acc->user; |
---|
84 | td->pass = acc->pass; |
---|
85 | td->home_timeline_id = 0; |
---|
86 | |
---|
87 | ic->proto_data = td; |
---|
88 | |
---|
89 | imcb_log( ic, "Connecting to Twitter" ); |
---|
90 | |
---|
91 | // Run this once. After this queue the main loop function. |
---|
92 | twitter_main_loop(ic, -1, 0); |
---|
93 | |
---|
94 | // Queue the main_loop |
---|
95 | // Save the return value, so we can remove the timeout on logout. |
---|
96 | td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic); |
---|
97 | |
---|
98 | sprintf( name, "twitter_%s", acc->user ); |
---|
99 | imcb_add_buddy( ic, name, NULL ); |
---|
100 | imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL ); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Logout method. Just free the twitter_data. |
---|
105 | */ |
---|
106 | static void twitter_logout( struct im_connection *ic ) |
---|
107 | { |
---|
108 | struct twitter_data *td = ic->proto_data; |
---|
109 | |
---|
110 | // Set the status to logged out. |
---|
111 | ic->flags = 0; |
---|
112 | |
---|
113 | // Remove the main_loop function from the function queue. |
---|
114 | b_event_remove(td->main_loop_id); |
---|
115 | |
---|
116 | if(td->home_timeline_gc) |
---|
117 | imcb_chat_free(td->home_timeline_gc); |
---|
118 | |
---|
119 | if( td ) |
---|
120 | { |
---|
121 | g_free( td ); |
---|
122 | } |
---|
123 | |
---|
124 | twitter_connections = g_slist_remove( twitter_connections, ic ); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * |
---|
129 | */ |
---|
130 | static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
131 | { |
---|
132 | if (g_strncasecmp(who, "twitter_", 8) == 0 && |
---|
133 | g_strcasecmp(who + 8, ic->acc->user) == 0) |
---|
134 | twitter_post_status(ic, message); |
---|
135 | else |
---|
136 | twitter_direct_messages_new(ic, who, message); |
---|
137 | |
---|
138 | return( 0 ); |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * |
---|
143 | */ |
---|
144 | static void twitter_set_my_name( struct im_connection *ic, char *info ) |
---|
145 | { |
---|
146 | } |
---|
147 | |
---|
148 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
149 | { |
---|
150 | } |
---|
151 | |
---|
152 | static void twitter_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
153 | { |
---|
154 | } |
---|
155 | |
---|
156 | static void twitter_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
157 | { |
---|
158 | } |
---|
159 | |
---|
160 | static void twitter_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
161 | { |
---|
162 | if( c && message ) |
---|
163 | twitter_post_status(c->ic, message); |
---|
164 | } |
---|
165 | |
---|
166 | static void twitter_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
167 | { |
---|
168 | } |
---|
169 | |
---|
170 | static void twitter_chat_leave( struct groupchat *c ) |
---|
171 | { |
---|
172 | struct twitter_data *td = c->ic->proto_data; |
---|
173 | |
---|
174 | if( c != td->home_timeline_gc ) |
---|
175 | return; /* WTF? */ |
---|
176 | |
---|
177 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
178 | tweets come in. */ |
---|
179 | imcb_chat_free(td->home_timeline_gc); |
---|
180 | td->home_timeline_gc = NULL; |
---|
181 | } |
---|
182 | |
---|
183 | static struct groupchat *twitter_chat_with( struct im_connection *ic, char *who ) |
---|
184 | { |
---|
185 | return NULL; |
---|
186 | } |
---|
187 | |
---|
188 | static void twitter_keepalive( struct im_connection *ic ) |
---|
189 | { |
---|
190 | } |
---|
191 | |
---|
192 | static void twitter_add_permit( struct im_connection *ic, char *who ) |
---|
193 | { |
---|
194 | } |
---|
195 | |
---|
196 | static void twitter_rem_permit( struct im_connection *ic, char *who ) |
---|
197 | { |
---|
198 | } |
---|
199 | |
---|
200 | static void twitter_add_deny( struct im_connection *ic, char *who ) |
---|
201 | { |
---|
202 | } |
---|
203 | |
---|
204 | static void twitter_rem_deny( struct im_connection *ic, char *who ) |
---|
205 | { |
---|
206 | } |
---|
207 | |
---|
208 | static int twitter_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
209 | { |
---|
210 | return( 1 ); |
---|
211 | } |
---|
212 | |
---|
213 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
214 | //{ |
---|
215 | // return value; |
---|
216 | //} |
---|
217 | |
---|
218 | void twitter_initmodule() |
---|
219 | { |
---|
220 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
221 | |
---|
222 | ret->name = "twitter"; |
---|
223 | ret->login = twitter_login; |
---|
224 | ret->init = twitter_init; |
---|
225 | ret->logout = twitter_logout; |
---|
226 | ret->buddy_msg = twitter_buddy_msg; |
---|
227 | ret->get_info = twitter_get_info; |
---|
228 | ret->set_my_name = twitter_set_my_name; |
---|
229 | ret->add_buddy = twitter_add_buddy; |
---|
230 | ret->remove_buddy = twitter_remove_buddy; |
---|
231 | ret->chat_msg = twitter_chat_msg; |
---|
232 | ret->chat_invite = twitter_chat_invite; |
---|
233 | ret->chat_leave = twitter_chat_leave; |
---|
234 | ret->chat_with = twitter_chat_with; |
---|
235 | ret->keepalive = twitter_keepalive; |
---|
236 | ret->add_permit = twitter_add_permit; |
---|
237 | ret->rem_permit = twitter_rem_permit; |
---|
238 | ret->add_deny = twitter_add_deny; |
---|
239 | ret->rem_deny = twitter_rem_deny; |
---|
240 | ret->send_typing = twitter_send_typing; |
---|
241 | ret->handle_cmp = g_strcasecmp; |
---|
242 | |
---|
243 | register_protocol(ret); |
---|
244 | |
---|
245 | // Initialise the twitter_connections GSList. |
---|
246 | twitter_connections = NULL; |
---|
247 | } |
---|
248 | |
---|