source: protocols/twitter/twitter.c @ 3e69802

Last change on this file since 3e69802 was 3e69802, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-04-13T12:51:05Z

Use full name information of Twitter buddies.

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