source: protocols/twitter/twitter.c @ 1014cab

Last change on this file since 1014cab was 1014cab, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-07T00:46:38Z

In groupchat mode, make contacts show up in the room instead of in &bitlbee.
And clean up the room when disabling the Twitter account.

  • Property mode set to 100644
File size: 6.5 KB
Line 
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 */
33gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond)
34{
35        struct im_connection *ic = data;
36        // Check if we are still logged in...
37        // We are logged in if the flag says so and the connection is still in the connections list.
38        if (!g_slist_find( twitter_connections, ic ) ||
39            (ic->flags & OPT_LOGGED_IN) != OPT_LOGGED_IN) 
40                return 0;
41
42        // If the user uses multiple private message windows we need to get the
43        // users buddies.
44        if (!set_getbool( &ic->acc->set, "use_groupchat" ))
45                twitter_get_statuses_friends(ic, -1);
46
47        // Do stuff..
48        twitter_get_home_timeline(ic, -1);
49
50        // If we are still logged in run this function again after timeout.
51        return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN;
52}
53
54
55static void twitter_init( account_t *acc )
56{
57        set_t *s;
58        s = set_add( &acc->set, "use_groupchat", "false", set_eval_bool, acc );
59        s->flags |= ACC_SET_OFFLINE_ONLY;
60}
61
62/**
63 * Login method. Since the twitter API works with seperate HTTP request we
64 * only save the user and pass to the twitter_data object.
65 */
66static void twitter_login( account_t *acc )
67{
68        struct im_connection *ic = imcb_new( acc );
69        struct twitter_data *td = g_new0( struct twitter_data, 1 );
70
71        twitter_connections = g_slist_append( twitter_connections, ic );
72
73        td->user = acc->user;
74        td->pass = acc->pass;
75        td->home_timeline_id = 0;
76
77        ic->proto_data = td;
78
79        imcb_log( ic, "Connecting to Twitter" );
80        imcb_connected(ic);
81
82        // Run this once. After this queue the main loop function.
83        twitter_main_loop(ic, -1, 0);
84
85        // Queue the main_loop
86        // Save the return value, so we can remove the timeout on logout.
87        td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic);
88}
89
90/**
91 * Logout method. Just free the twitter_data.
92 */
93static void twitter_logout( struct im_connection *ic )
94{
95        struct twitter_data *td = ic->proto_data;
96       
97        // Set the status to logged out.
98        ic->flags = 0;
99
100        // Remove the main_loop function from the function queue.
101        b_event_remove(td->main_loop_id);
102
103        imcb_chat_free(td->home_timeline_gc);
104
105        if( td )
106        {
107                g_free( td );
108        }
109
110        twitter_connections = g_slist_remove( twitter_connections, ic );
111}
112
113/**
114 *
115 */
116static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
117{
118        // Let's just update the status.
119//      if ( g_strcasecmp(who, ic->acc->user) == 0 )
120                twitter_post_status(ic, message);
121//      else
122//              twitter_direct_messages_new(ic, who, message);
123        return( 0 );
124}
125
126/**
127 *
128 */
129static GList *twitter_away_states( struct im_connection *ic )
130{
131        static GList *l = NULL;
132        return l;
133}
134
135static void twitter_set_away( struct im_connection *ic, char *state, char *message )
136{
137}
138
139static void twitter_set_my_name( struct im_connection *ic, char *info )
140{
141}
142
143static void twitter_get_info(struct im_connection *ic, char *who) 
144{
145}
146
147static void twitter_add_buddy( struct im_connection *ic, char *who, char *group )
148{
149}
150
151static void twitter_remove_buddy( struct im_connection *ic, char *who, char *group )
152{
153}
154
155static void twitter_chat_msg( struct groupchat *c, char *message, int flags )
156{
157        if( c && message )
158                twitter_post_status(c->ic, message);
159}
160
161static void twitter_chat_invite( struct groupchat *c, char *who, char *message )
162{
163}
164
165static void twitter_chat_leave( struct groupchat *c )
166{
167}
168
169static struct groupchat *twitter_chat_with( struct im_connection *ic, char *who )
170{
171        return NULL;
172}
173
174static void twitter_keepalive( struct im_connection *ic )
175{
176}
177
178static void twitter_add_permit( struct im_connection *ic, char *who )
179{
180}
181
182static void twitter_rem_permit( struct im_connection *ic, char *who )
183{
184}
185
186static void twitter_add_deny( struct im_connection *ic, char *who )
187{
188}
189
190static void twitter_rem_deny( struct im_connection *ic, char *who )
191{
192}
193
194static int twitter_send_typing( struct im_connection *ic, char *who, int typing )
195{
196        return( 1 );
197}
198
199//static char *twitter_set_display_name( set_t *set, char *value )
200//{
201//      return value;
202//}
203
204void twitter_initmodule()
205{
206        struct prpl *ret = g_new0(struct prpl, 1);
207       
208        ret->name = "twitter";
209        ret->login = twitter_login;
210        ret->init = twitter_init;
211        ret->logout = twitter_logout;
212        ret->buddy_msg = twitter_buddy_msg;
213        ret->away_states = twitter_away_states;
214        ret->set_away = twitter_set_away;
215        ret->get_info = twitter_get_info;
216        ret->set_my_name = twitter_set_my_name;
217        ret->add_buddy = twitter_add_buddy;
218        ret->remove_buddy = twitter_remove_buddy;
219        ret->chat_msg = twitter_chat_msg;
220        ret->chat_invite = twitter_chat_invite;
221        ret->chat_leave = twitter_chat_leave;
222        ret->chat_with = twitter_chat_with;
223        ret->keepalive = twitter_keepalive;
224        ret->add_permit = twitter_add_permit;
225        ret->rem_permit = twitter_rem_permit;
226        ret->add_deny = twitter_add_deny;
227        ret->rem_deny = twitter_rem_deny;
228        ret->send_typing = twitter_send_typing;
229        ret->handle_cmp = g_strcasecmp;
230
231        register_protocol(ret);
232
233        // Initialise the twitter_connections GSList.
234        twitter_connections = NULL;
235}
236
Note: See TracBrowser for help on using the repository browser.