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