├── purple2compat ├── xfer.h ├── plugins.h ├── glibcompat.h ├── internal.h ├── image-store.h ├── circularbuffer.h ├── image.h ├── purple-socket.h ├── purple-socket.c └── http.h ├── icons ├── 16 │ ├── skype.png │ └── teams.png ├── 22 │ ├── skype.png │ └── teams.png └── 48 │ ├── skype.png │ └── teams.png ├── libjson-glib-1.0.dll ├── glibcompat.h ├── teams_login.h ├── teams_util.h ├── README.md ├── teams_connection.h ├── teams_contacts.h ├── teams_messages.h ├── Makefile ├── libteams.h ├── teams_connection.c ├── teams_util.c ├── purplecompat.h ├── libteams.c ├── gpl3.txt └── teams_login.c /purple2compat/xfer.h: -------------------------------------------------------------------------------- 1 | #include "ft.h" 2 | -------------------------------------------------------------------------------- /purple2compat/plugins.h: -------------------------------------------------------------------------------- 1 | #include "plugin.h" 2 | -------------------------------------------------------------------------------- /icons/16/skype.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RaymiiOrg/purple-teams/master/icons/16/skype.png -------------------------------------------------------------------------------- /icons/16/teams.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RaymiiOrg/purple-teams/master/icons/16/teams.png -------------------------------------------------------------------------------- /icons/22/skype.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RaymiiOrg/purple-teams/master/icons/22/skype.png -------------------------------------------------------------------------------- /icons/22/teams.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RaymiiOrg/purple-teams/master/icons/22/teams.png -------------------------------------------------------------------------------- /icons/48/skype.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RaymiiOrg/purple-teams/master/icons/48/skype.png -------------------------------------------------------------------------------- /icons/48/teams.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RaymiiOrg/purple-teams/master/icons/48/teams.png -------------------------------------------------------------------------------- /libjson-glib-1.0.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RaymiiOrg/purple-teams/master/libjson-glib-1.0.dll -------------------------------------------------------------------------------- /purple2compat/glibcompat.h: -------------------------------------------------------------------------------- 1 | 2 | #if !GLIB_CHECK_VERSION(2, 68, 0) 3 | #define g_memdup2(mem,size) g_memdup((mem),(size)) 4 | #endif -------------------------------------------------------------------------------- /purple2compat/internal.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #ifdef _WIN32 6 | #include "win32/win32dep.h" 7 | #endif 8 | 9 | #define purple_proxy_info_get_proxy_type purple_proxy_info_get_type 10 | #define purple_connection_is_disconnecting(c) FALSE 11 | 12 | #ifndef N_ 13 | # define N_(a) (a) 14 | #endif 15 | 16 | #ifndef _ 17 | # define _(a) (a) 18 | #endif 19 | -------------------------------------------------------------------------------- /purple2compat/image-store.h: -------------------------------------------------------------------------------- 1 | #ifndef _IMAGE_STORE_H_ 2 | #define _IMAGE_STORE_H_ 3 | 4 | #include "imgstore.h" 5 | #include "image.h" 6 | 7 | 8 | #define purple_image_store_add(img) purple_imgstore_add_with_id( \ 9 | g_memdup2(purple_imgstore_get_data(img), purple_imgstore_get_size(img)), \ 10 | purple_imgstore_get_size(img), purple_imgstore_get_filename(img)) 11 | #define purple_image_store_get purple_imgstore_find_by_id 12 | 13 | 14 | #endif /*_IMAGE_STORE_H_*/ 15 | -------------------------------------------------------------------------------- /glibcompat.h: -------------------------------------------------------------------------------- 1 | #ifndef _GLIBCOMPAT_H_ 2 | #define _GLIBCOMPAT_H_ 3 | 4 | #if !GLIB_CHECK_VERSION(2, 68, 0) 5 | #define g_memdup2(mem, size) g_memdup((mem), (size)) 6 | #endif /* 2.68.0 */ 7 | 8 | #if !GLIB_CHECK_VERSION(2, 32, 0) 9 | #define g_hash_table_contains(hash_table, key) g_hash_table_lookup_extended((hash_table), (key), NULL, NULL) 10 | #endif /* 2.32.0 */ 11 | 12 | #if !GLIB_CHECK_VERSION(2, 28, 0) 13 | gint64 14 | g_get_real_time() 15 | { 16 | GTimeVal val; 17 | 18 | g_get_current_time (&val); 19 | 20 | return (((gint64) val.tv_sec) * 1000000) + val.tv_usec; 21 | } 22 | #endif /* 2.28.0 */ 23 | 24 | #endif /*_GLIBCOMPAT_H_*/ -------------------------------------------------------------------------------- /purple2compat/circularbuffer.h: -------------------------------------------------------------------------------- 1 | #ifndef _CIRCULARBUFFER_H_ 2 | #define _CIRCULARBUFFER_H_ 3 | 4 | #include "circbuffer.h" 5 | 6 | #define PurpleCircularBuffer PurpleCircBuffer 7 | #define purple_circular_buffer_new purple_circ_buffer_new 8 | #define purple_circular_buffer_destroy purple_circ_buffer_destroy 9 | #define purple_circular_buffer_append purple_circ_buffer_append 10 | #define purple_circular_buffer_get_max_read purple_circ_buffer_get_max_read 11 | #define purple_circular_buffer_mark_read purple_circ_buffer_mark_read 12 | #define purple_circular_buffer_get_output(buf) ((const gchar *) (buf)->outptr) 13 | #define purple_circular_buffer_get_used(buf) ((buf)->bufused) 14 | 15 | #endif /*_CIRCULARBUFFER_H_*/ 16 | -------------------------------------------------------------------------------- /purple2compat/image.h: -------------------------------------------------------------------------------- 1 | #ifndef _IMAGE_H_ 2 | #define _IMAGE_H_ 3 | 4 | #include "imgstore.h" 5 | 6 | #define PurpleImage PurpleStoredImage 7 | #define purple_image_new_from_file(p, e) purple_imgstore_new_from_file(p) 8 | #define purple_image_new_from_data(d, l) purple_imgstore_add(d, l, NULL) 9 | #define purple_image_get_path purple_imgstore_get_filename 10 | #define purple_image_get_data_size purple_imgstore_get_size 11 | #define purple_image_get_data purple_imgstore_get_data 12 | #define purple_image_get_extension purple_imgstore_get_extension 13 | #define purple_image_ref purple_imgstore_ref 14 | #define purple_image_unref purple_imgstore_unref 15 | 16 | 17 | static inline const gchar * 18 | purple_image_get_mimetype(PurpleImage *image) 19 | { 20 | const gchar *extension = purple_image_get_extension(image); 21 | 22 | if (purple_strequal(extension, "jpg") || purple_strequal(extension, "jpeg")) { 23 | return "image/jpeg"; 24 | } else if (purple_strequal(extension, "png")) { 25 | return "image/png"; 26 | } else if (purple_strequal(extension, "gif")) { 27 | return "image/gif"; 28 | } 29 | return "image/*"; 30 | } 31 | 32 | #endif /* _IMAGE_H_ */ 33 | -------------------------------------------------------------------------------- /teams_login.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2020 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #ifndef TEAMS_LOGIN_H 20 | #define TEAMS_LOGIN_H 21 | 22 | #include "libteams.h" 23 | #include "teams_connection.h" 24 | 25 | #include 26 | 27 | void teams_logout(TeamsAccount *sa); 28 | void teams_begin_web_login(TeamsAccount *sa); 29 | void teams_begin_oauth_login(TeamsAccount *sa); 30 | void teams_refresh_token_login(TeamsAccount *sa); 31 | void teams_begin_soapy_login(TeamsAccount *sa); 32 | 33 | gboolean teams_oauth_refresh_token(TeamsAccount *sa); 34 | void teams_do_web_auth(TeamsAccount *sa); 35 | 36 | #endif /* TEAMS_LOGIN_H */ 37 | -------------------------------------------------------------------------------- /teams_util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2020 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #include "libteams.h" 20 | 21 | gchar *teams_string_get_chunk(const gchar *haystack, gsize len, const gchar *start, const gchar *end); 22 | 23 | gchar *teams_jsonobj_to_string(JsonObject *jsonobj); 24 | gchar *teams_jsonarr_to_string(JsonArray *jsonarr); 25 | JsonObject *json_decode_object(const gchar *data, gssize len); 26 | JsonArray *json_decode_array(const gchar *data, gssize len); 27 | 28 | const gchar *teams_contact_url_to_name(const gchar *url); 29 | const gchar *teams_thread_url_to_name(const gchar *url); 30 | 31 | gchar *teams_hmac_sha256(gchar *input); 32 | 33 | gint64 teams_get_js_time(); 34 | 35 | PurpleAccount *find_acct(const char *prpl, const char *acct_id); 36 | 37 | const gchar *teams_user_url_prefix(const gchar *who); 38 | const gchar *teams_strip_user_prefix(const gchar *who); 39 | 40 | PurpleGroup *teams_get_blist_group(TeamsAccount *sa); 41 | gboolean teams_is_user_self(TeamsAccount *sa, const gchar *username); 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MS Teams Plugin for Pidgin 2 | ========================== 3 | 4 | Hey there, I'm building a new plugin for Teams, based on the old skype4pidgin plugin. At the moment it's primarily focused at "Teams for Work/School", but let me know in [this issue](https://github.com/EionRobb/purple-teams/issues/16) if you're interested in "Teams for Personal" 5 | Totally a work in progress, but it's sending and receiving messages, so good enough to ship! 6 | 7 | Building 8 | ======== 9 | You'll need libjson-glib-dev, libpurple-dev and glib-2.0-dev packages from your distro, then simply 10 | ``` 11 | git clone https://github.com/EionRobb/purple-teams 12 | cd purple-teams 13 | make 14 | sudo make install 15 | ``` 16 | to install. 17 | 18 | Windows Users 19 | ============= 20 | Download from [here](https://eion.robbmob.com/libteams.dll), copy to `C:\Program Files (x86)\Pidgin\plugins\`. You may also need [libjson-glib](https://eion.robbmob.com/libjson-glib-1.0.dll) in your `C:\Program Files (x86)\Pidgin\` folder (not the plugins folder) 21 | 22 | General Login 23 | ============= 24 | Usernames in purple-teams don't mean too much, they just need to be unique if you plan on signing into multiple servers/tenants at the same time. Leave the 'tenant' field on the Advanced tab blank, unless you're following the below 25 | 26 | Logging in as a guest 27 | ===================== 28 | If you're like me and need to connect to multiple servers/tenants, then you'll need to set the 'Tenant' on the Advanced tab of the connection. These are generally in the form of 'fancynamegoeshere.onmicrosoft.com' you can leave off the '.onmicrosoft.com' bit and the plugin should add that on automatically. Hopefully in the future I come up with an easier way to work that out. 29 | 30 | You can also use a GUID/ID, eg using https://www.whatismytenantid.com -------------------------------------------------------------------------------- /teams_connection.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2020 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #ifndef TEAMS_CONNECTION_H 20 | #define TEAMS_CONNECTION_H 21 | 22 | #include "libteams.h" 23 | 24 | typedef void (*TeamsProxyCallbackFunc)(TeamsAccount *sa, JsonNode *node, gpointer user_data); 25 | typedef void (*TeamsProxyCallbackErrorFunc)(TeamsAccount *sa, const gchar *data, gssize data_len, gpointer user_data); 26 | 27 | /* 28 | * This is a bitmask. 29 | */ 30 | typedef enum 31 | { 32 | TEAMS_METHOD_GET = 0x0001, 33 | TEAMS_METHOD_POST = 0x0002, 34 | TEAMS_METHOD_PUT = 0x0004, 35 | TEAMS_METHOD_DELETE = 0x0008, 36 | TEAMS_METHOD_SSL = 0x1000, 37 | } TeamsMethod; 38 | 39 | typedef struct _TeamsConnection TeamsConnection; 40 | struct _TeamsConnection { 41 | TeamsAccount *sa; 42 | gchar *url; 43 | TeamsProxyCallbackFunc callback; 44 | gpointer user_data; 45 | PurpleHttpConnection *http_conn; 46 | TeamsProxyCallbackErrorFunc error_callback; 47 | }; 48 | 49 | TeamsConnection *teams_post_or_get(TeamsAccount *sa, TeamsMethod method, 50 | const gchar *host, const gchar *url, const gchar *postdata, 51 | TeamsProxyCallbackFunc callback_func, gpointer user_data, 52 | gboolean keepalive); 53 | 54 | void teams_update_cookies(TeamsAccount *sa, const gchar *headers); 55 | gchar *teams_cookies_to_string(TeamsAccount *sa); 56 | 57 | //TeamsConnection *teams_fetch_url_request(); 58 | 59 | #endif /* TEAMS_CONNECTION_H */ 60 | -------------------------------------------------------------------------------- /teams_contacts.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2015 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #ifndef TEAMS_CONTACTS_H 20 | #define TEAMS_CONTACTS_H 21 | 22 | #include "libteams.h" 23 | 24 | void teams_get_icon(PurpleBuddy *buddy); 25 | void teams_download_uri_to_conv(TeamsAccount *sa, const gchar *uri, PurpleConversation *conv, time_t ts, const gchar* from); 26 | void teams_download_video_message(TeamsAccount *sa, const gchar *sid, PurpleConversation *conv); 27 | void teams_download_moji_to_conv(TeamsAccount *sa, const gchar *text, const gchar *url_thumbnail, PurpleConversation *conv, time_t ts, const gchar* from); 28 | void teams_present_uri_as_filetransfer(TeamsAccount *sa, const gchar *uri, const gchar *from); 29 | 30 | PurpleXfer *teams_new_xfer( 31 | #if PURPLE_VERSION_CHECK(3, 0, 0) 32 | PurpleProtocolXfer *prplxfer, 33 | #endif 34 | PurpleConnection *pc, const char *who); 35 | 36 | void teams_send_file( 37 | #if PURPLE_VERSION_CHECK(3, 0, 0) 38 | PurpleProtocolXfer *prplxfer, 39 | #endif 40 | PurpleConnection *pc, const gchar *who, const gchar *filename); 41 | 42 | gboolean teams_can_receive_file( 43 | #if PURPLE_VERSION_CHECK(3, 0, 0) 44 | PurpleProtocolXfer *prplxfer, 45 | #endif 46 | PurpleConnection *pc, const gchar *who); 47 | 48 | void teams_search_users(PurpleProtocolAction *action); 49 | 50 | void teams_received_contacts(TeamsAccount *sa, PurpleXmlNode *contacts); 51 | 52 | void teams_get_friend_profiles(TeamsAccount *sa, GSList *contacts); 53 | void teams_get_friend_profile(TeamsAccount *sa, const gchar *who); 54 | 55 | void teams_get_friend_list(TeamsAccount *sa); 56 | void teams_get_info(PurpleConnection *pc, const gchar *username); 57 | void teams_get_self_details(TeamsAccount *sa); 58 | 59 | void teams_buddy_remove(PurpleConnection *pc, PurpleBuddy *buddy, PurpleGroup *group); 60 | void teams_add_buddy_with_invite(PurpleConnection *pc, PurpleBuddy *buddy, PurpleGroup *group, const char* message); 61 | void teams_add_buddy(PurpleConnection *pc, PurpleBuddy *buddy, PurpleGroup *group); 62 | void teams_buddy_block(PurpleConnection *pc, const char *name); 63 | void teams_buddy_unblock(PurpleConnection *pc, const char *name); 64 | 65 | gboolean teams_check_authrequests(TeamsAccount *sa); 66 | 67 | void teams_set_mood_message(TeamsAccount *sa, const gchar *mood); 68 | 69 | #endif /* TEAMS_CONTACTS_H */ 70 | -------------------------------------------------------------------------------- /teams_messages.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2020 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #ifndef TEAMS_MESSAGES_H 20 | #define TEAMS_MESSAGES_H 21 | 22 | #include "libteams.h" 23 | 24 | gint teams_send_im(PurpleConnection *pc, 25 | #if PURPLE_VERSION_CHECK(3, 0, 0) 26 | PurpleMessage *msg 27 | #else 28 | const gchar *who, const gchar *message, PurpleMessageFlags flags 29 | #endif 30 | ); 31 | 32 | gint teams_chat_send(PurpleConnection *pc, gint id, 33 | #if PURPLE_VERSION_CHECK(3, 0, 0) 34 | PurpleMessage *msg 35 | #else 36 | const gchar *message, PurpleMessageFlags flags 37 | #endif 38 | ); 39 | 40 | void teams_set_idle(PurpleConnection *pc, int time); 41 | void teams_set_status(PurpleAccount *account, PurpleStatus *status); 42 | guint teams_conv_send_typing(PurpleConversation *conv, PurpleIMTypingState state); 43 | guint teams_send_typing(PurpleConnection *pc, const gchar *name, PurpleIMTypingState state); 44 | void teams_poll(TeamsAccount *sa); 45 | void teams_get_registration_token(TeamsAccount *sa); 46 | void teams_subscribe(TeamsAccount *sa); 47 | void teams_get_vdms_token(TeamsAccount *sa); 48 | void teams_chat_kick(PurpleConnection *pc, int id, const char *who); 49 | void teams_chat_invite(PurpleConnection *pc, int id, const char *message, const char *who); 50 | void teams_initiate_chat(TeamsAccount *sa, const gchar *who, gboolean one_to_one, const gchar *initial_message); 51 | void teams_initiate_chat_from_node(PurpleBlistNode *node, gpointer userdata); 52 | PurpleRoomlist *teams_roomlist_get_list(PurpleConnection *pc); 53 | void teams_chat_set_topic(PurpleConnection *pc, int id, const char *topic); 54 | 55 | void teams_subscribe_to_contact_status(TeamsAccount *sa, GSList *contacts); 56 | void teams_unsubscribe_from_contact_status(TeamsAccount *sa, const gchar *who); 57 | void teams_get_conversation_history_since(TeamsAccount *sa, const gchar *convname, gint since); 58 | void teams_get_conversation_history(TeamsAccount *sa, const gchar *convname); 59 | void teams_get_thread_users(TeamsAccount *sa, const gchar *convname); 60 | void teams_get_all_conversations_since(TeamsAccount *sa, gint since); 61 | void skype_web_get_offline_history(TeamsAccount *sa); 62 | void teams_mark_conv_seen(PurpleConversation *conv, PurpleConversationUpdateType type); 63 | 64 | void teams_gather_self_properties(TeamsAccount *sa); 65 | 66 | #endif /* TEAMS_MESSAGES_H */ 67 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | PIDGIN_TREE_TOP ?= ../pidgin-2.10.11 3 | PIDGIN3_TREE_TOP ?= ../pidgin-main 4 | LIBPURPLE_DIR ?= $(PIDGIN_TREE_TOP)/libpurple 5 | WIN32_DEV_TOP ?= $(PIDGIN_TREE_TOP)/../win32-dev 6 | 7 | WIN32_CC ?= $(WIN32_DEV_TOP)/mingw-4.7.2/bin/gcc 8 | 9 | PKG_CONFIG ?= pkg-config 10 | DIR_PERM = 0755 11 | LIB_PERM = 0755 12 | FILE_PERM = 0644 13 | MAKENSIS ?= makensis 14 | 15 | CFLAGS ?= -O2 -g -pipe 16 | LDFLAGS ?= 17 | 18 | # Do some nasty OS and purple version detection 19 | ifeq ($(OS),Windows_NT) 20 | #only defined on 64-bit windows 21 | PROGFILES32 = ${ProgramFiles(x86)} 22 | ifndef PROGFILES32 23 | PROGFILES32 = $(PROGRAMFILES) 24 | endif 25 | TEAMS_TARGET = libteams.dll 26 | TEAMS_DEST = "$(PROGFILES32)/Pidgin/plugins" 27 | TEAMS_ICONS_DEST = "$(PROGFILES32)/Pidgin/pixmaps/pidgin/protocols" 28 | TEAMS_THEME_DEST = "$(PROGFILES32)/Pidgin/pixmaps/pidgin/emotes/teams" 29 | MAKENSIS = "$(PROGFILES32)/NSIS/makensis.exe" 30 | else 31 | 32 | UNAME_S := $(shell uname -s) 33 | 34 | #.. There are special flags we need for OSX 35 | ifeq ($(UNAME_S), Darwin) 36 | # 37 | #.. /opt/local/include and subdirs are included here to ensure this compiles 38 | # for folks using Macports. I believe Homebrew uses /usr/local/include 39 | # so things should "just work". You *must* make sure your packages are 40 | # all up to date or you will most likely get compilation errors. 41 | # 42 | INCLUDES = -I/opt/local/include -lz $(OS) 43 | 44 | CC = gcc 45 | else 46 | INCLUDES = 47 | CC ?= gcc 48 | endif 49 | 50 | ifeq ($(shell $(PKG_CONFIG) --exists purple-3 2>/dev/null && echo "true"),) 51 | ifeq ($(shell $(PKG_CONFIG) --exists purple 2>/dev/null && echo "true"),) 52 | TEAMS_TARGET = FAILNOPURPLE 53 | TEAMS_DEST = 54 | TEAMS_ICONS_DEST = 55 | TEAMS_THEME_DEST = 56 | else 57 | TEAMS_TARGET = libteams.so 58 | TEAMS_DEST = $(DESTDIR)`$(PKG_CONFIG) --variable=plugindir purple` 59 | TEAMS_ICONS_DEST = $(DESTDIR)`$(PKG_CONFIG) --variable=datadir purple`/pixmaps/pidgin/protocols 60 | TEAMS_THEME_DEST = $(DESTDIR)`$(PKG_CONFIG) --variable=datadir purple`/pixmaps/pidgin/emotes/skype 61 | endif 62 | else 63 | TEAMS_TARGET = libteams3.so 64 | TEAMS_DEST = $(DESTDIR)`$(PKG_CONFIG) --variable=plugindir purple-3` 65 | TEAMS_ICONS_DEST = $(DESTDIR)`$(PKG_CONFIG) --variable=datadir purple-3`/pixmaps/pidgin/protocols 66 | TEAMS_THEME_DEST = $(DESTDIR)`$(PKG_CONFIG) --variable=datadir purple-3`/pixmaps/pidgin/emotes/skype 67 | endif 68 | endif 69 | 70 | WIN32_CFLAGS = -I$(WIN32_DEV_TOP)/glib-2.28.8/include -I$(WIN32_DEV_TOP)/glib-2.28.8/include/glib-2.0 -I$(WIN32_DEV_TOP)/glib-2.28.8/lib/glib-2.0/include -I$(WIN32_DEV_TOP)/json-glib-0.14/include/json-glib-1.0 -DENABLE_NLS -DPACKAGE_VERSION='"$(PLUGIN_VERSION)"' -Wall -Wextra -Werror -Wno-deprecated-declarations -Wno-unused-parameter -fno-strict-aliasing -Wformat -Wno-sign-compare 71 | WIN32_LDFLAGS = -L$(WIN32_DEV_TOP)/glib-2.28.8/lib -L$(PROTOBUF_C_DIR)/bin -L$(WIN32_DEV_TOP)/json-glib-0.14/lib -lpurple -lintl -lglib-2.0 -lgobject-2.0 -ljson-glib-1.0 -g -ggdb -static-libgcc -lz 72 | WIN32_PIDGIN2_CFLAGS = -I$(PIDGIN_TREE_TOP)/libpurple -I$(PIDGIN_TREE_TOP) $(WIN32_CFLAGS) 73 | WIN32_PIDGIN3_CFLAGS = -I$(PIDGIN3_TREE_TOP)/libpurple -I$(PIDGIN3_TREE_TOP) -I$(WIN32_DEV_TOP)/gplugin-dev/gplugin $(WIN32_CFLAGS) 74 | WIN32_PIDGIN2_LDFLAGS = -L$(PIDGIN_TREE_TOP)/libpurple $(WIN32_LDFLAGS) 75 | WIN32_PIDGIN3_LDFLAGS = -L$(PIDGIN3_TREE_TOP)/libpurple -L$(WIN32_DEV_TOP)/gplugin-dev/gplugin $(WIN32_LDFLAGS) -lgplugin 76 | 77 | C_FILES = \ 78 | teams_connection.c \ 79 | teams_contacts.c \ 80 | teams_login.c \ 81 | teams_messages.c \ 82 | teams_util.c \ 83 | libteams.c 84 | PURPLE_COMPAT_FILES := purple2compat/http.c purple2compat/purple-socket.c 85 | PURPLE_C_FILES := libteams.c $(C_FILES) 86 | 87 | 88 | 89 | .PHONY: all install FAILNOPURPLE clean translations 90 | 91 | all: $(TEAMS_TARGET) 92 | 93 | libteams.so: $(PURPLE_C_FILES) $(PURPLE_COMPAT_FILES) 94 | $(CC) -fPIC $(CFLAGS) -shared -o $@ $^ $(LDFLAGS) $(PROTOBUF_OPTS) `$(PKG_CONFIG) purple glib-2.0 json-glib-1.0 zlib --libs --cflags` $(INCLUDES) -Ipurple2compat -g -ggdb 95 | 96 | libteams3.so: $(PURPLE_C_FILES) 97 | $(CC) -fPIC $(CFLAGS) -shared -o $@ $^ $(LDFLAGS) $(PROTOBUF_OPTS) `$(PKG_CONFIG) purple-3 glib-2.0 json-glib-1.0 zlib --libs --cflags` $(INCLUDES) -g -ggdb 98 | 99 | libteams.dll: $(PURPLE_C_FILES) $(PURPLE_COMPAT_FILES) 100 | $(WIN32_CC) -shared -o $@ $^ $(WIN32_PIDGIN2_CFLAGS) $(WIN32_PIDGIN2_LDFLAGS) -Ipurple2compat 101 | 102 | libteams3.dll: $(PURPLE_C_FILES) 103 | $(WIN32_CC) -shared -o $@ $^ $(WIN32_PIDGIN3_CFLAGS) $(WIN32_PIDGIN3_LDFLAGS) 104 | 105 | install: $(TEAMS_TARGET) install-icons 106 | mkdir -m $(DIR_PERM) -p $(TEAMS_DEST) 107 | install -m $(LIB_PERM) -p $(TEAMS_TARGET) $(TEAMS_DEST) 108 | 109 | install-icons: icons/16/teams.png icons/22/teams.png icons/48/teams.png 110 | mkdir -m $(DIR_PERM) -p $(TEAMS_ICONS_DEST)/16 111 | mkdir -m $(DIR_PERM) -p $(TEAMS_ICONS_DEST)/22 112 | mkdir -m $(DIR_PERM) -p $(TEAMS_ICONS_DEST)/48 113 | install -m $(FILE_PERM) -p icons/16/teams.png $(TEAMS_ICONS_DEST)/16/teams.png 114 | install -m $(FILE_PERM) -p icons/22/teams.png $(TEAMS_ICONS_DEST)/22/teams.png 115 | install -m $(FILE_PERM) -p icons/48/teams.png $(TEAMS_ICONS_DEST)/48/teams.png 116 | 117 | installer: pidgin-teams.nsi libteams.dll 118 | $(MAKENSIS) "/DPIDGIN_VARIANT"="Pidgin" "/DPRODUCT_NAME"="pidgin-teams" "/DINSTALLER_NAME"="pidgin-teams-installer" "/DJSON_GLIB_DLL"="libjson-glib-1.0.dll" pidgin-teams.nsi 119 | 120 | translations: po/purple-teams.pot 121 | 122 | po/purple-teams.pot: 123 | xgettext $^ -k_ --no-location -o $@ 124 | 125 | po/%.po: po/purple-teams.pot 126 | msgmerge $@ po/purple-teams.pot > tmp-$* 127 | mv -f tmp-$* $@ 128 | 129 | po/%.mo: po/%.po 130 | msgfmt -o $@ $^ 131 | 132 | %-locale-install: po/%.mo 133 | install -D -m $(FILE_PERM) -p po/$(*F).mo $(LOCALEDIR)/$(*F)/LC_MESSAGES/purple-teams.mo 134 | 135 | FAILNOPURPLE: 136 | echo "You need libpurple development headers installed to be able to compile this plugin" 137 | 138 | clean: 139 | rm -f $(TEAMS_TARGET) 140 | 141 | -------------------------------------------------------------------------------- /purple2compat/purple-socket.h: -------------------------------------------------------------------------------- 1 | /* purple 2 | * 3 | * Purple is the legal property of its developers, whose names are too numerous 4 | * to list here. Please refer to the COPYRIGHT file distributed with this 5 | * source distribution. 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA 20 | */ 21 | 22 | #ifndef _PURPLE_SOCKET_H_ 23 | #define _PURPLE_SOCKET_H_ 24 | /** 25 | * SECTION:purple-socket 26 | * @section_id: libpurple-purple-socket 27 | * @short_description: purple-socket.h 28 | * @title: Generic Sockets 29 | */ 30 | 31 | #include "connection.h" 32 | 33 | /** 34 | * PurpleSocket: 35 | * 36 | * A structure holding all resources needed for the TCP connection. 37 | */ 38 | typedef struct _PurpleSocket PurpleSocket; 39 | 40 | /** 41 | * PurpleSocketConnectCb: 42 | * @ps: The socket. 43 | * @error: Error message, or NULL if connection was successful. 44 | * @user_data: The user data passed with callback function. 45 | * 46 | * A callback fired after (successfully or not) establishing a connection. 47 | */ 48 | typedef void (*PurpleSocketConnectCb)(PurpleSocket *ps, const gchar *error, 49 | gpointer user_data); 50 | 51 | /** 52 | * purple_socket_new: 53 | * @gc: The connection for which the socket is needed, or NULL. 54 | * 55 | * Creates new, disconnected socket. 56 | * 57 | * Passing a PurpleConnection allows for proper proxy handling. 58 | * 59 | * Returns: The new socket struct. 60 | */ 61 | PurpleSocket * 62 | purple_socket_new(PurpleConnection *gc); 63 | 64 | /** 65 | * purple_socket_get_connection: 66 | * @ps: The socket. 67 | * 68 | * Gets PurpleConnection tied with specified socket. 69 | * 70 | * Returns: The PurpleConnection object. 71 | */ 72 | PurpleConnection * 73 | purple_socket_get_connection(PurpleSocket *ps); 74 | 75 | /** 76 | * purple_socket_set_tls: 77 | * @ps: The socket. 78 | * @is_tls: TRUE, if TLS should be handled transparently, FALSE otherwise. 79 | * 80 | * Determines, if socket should handle TLS. 81 | */ 82 | void 83 | purple_socket_set_tls(PurpleSocket *ps, gboolean is_tls); 84 | 85 | /** 86 | * purple_socket_set_host: 87 | * @ps: The socket. 88 | * @host: The connection host. 89 | * 90 | * Sets connection host. 91 | */ 92 | void 93 | purple_socket_set_host(PurpleSocket *ps, const gchar *host); 94 | 95 | /** 96 | * purple_socket_set_port: 97 | * @ps: The socket. 98 | * @port: The connection port. 99 | * 100 | * Sets connection port. 101 | */ 102 | void 103 | purple_socket_set_port(PurpleSocket *ps, int port); 104 | 105 | /** 106 | * purple_socket_connect: 107 | * @ps: The socket. 108 | * @cb: The function to call after establishing a connection, or on 109 | * error. 110 | * @user_data: The user data to be passed to callback function. 111 | * 112 | * Establishes a connection. 113 | * 114 | * Returns: TRUE on success (this doesn't mean it's connected yet), FALSE 115 | * otherwise. 116 | */ 117 | gboolean 118 | purple_socket_connect(PurpleSocket *ps, PurpleSocketConnectCb cb, 119 | gpointer user_data); 120 | 121 | /** 122 | * purple_socket_read: 123 | * @ps: The socket. 124 | * @buf: The buffer to write data to. 125 | * @len: The buffer size. 126 | * 127 | * Reads incoming data from socket. 128 | * 129 | * This function deals with TLS, if the socket is configured to do it. 130 | * 131 | * Returns: Amount of data written, or -1 on error (errno will be also be set). 132 | */ 133 | gssize 134 | purple_socket_read(PurpleSocket *ps, guchar *buf, size_t len); 135 | 136 | /** 137 | * purple_socket_write: 138 | * @ps: The socket. 139 | * @buf: The buffer to read data from. 140 | * @len: The amount of data to read and send. 141 | * 142 | * Sends data through socket. 143 | * 144 | * This function deals with TLS, if the socket is configured to do it. 145 | * 146 | * Returns: Amount of data sent, or -1 on error (errno will albo be set). 147 | */ 148 | gssize 149 | purple_socket_write(PurpleSocket *ps, const guchar *buf, size_t len); 150 | 151 | /** 152 | * purple_socket_watch: 153 | * @ps: The socket. 154 | * @cond: The condition type. 155 | * @func: The callback function for data, or NULL to remove any 156 | * existing callbacks. 157 | * @user_data: The user data to be passed to callback function. 158 | * 159 | * Adds an input handler for the socket. 160 | * 161 | * If the specified socket had input handler already registered, it will be 162 | * removed. To remove any input handlers, pass a NULL handler function. 163 | */ 164 | void 165 | purple_socket_watch(PurpleSocket *ps, PurpleInputCondition cond, 166 | PurpleInputFunction func, gpointer user_data); 167 | 168 | /** 169 | * purple_socket_get_fd: 170 | * @ps: The socket 171 | * 172 | * Gets underlying file descriptor for socket. 173 | * 174 | * It's not meant to read/write data (use purple_socket_read/ 175 | * purple_socket_write), rather for watching for changes with select(). 176 | * 177 | * Returns: The file descriptor, or -1 on error. 178 | */ 179 | int 180 | purple_socket_get_fd(PurpleSocket *ps); 181 | 182 | /** 183 | * purple_socket_set_data: 184 | * @ps: The socket. 185 | * @key: The unique key. 186 | * @data: The data to assign, or NULL to remove. 187 | * 188 | * Sets extra data for a socket. 189 | */ 190 | void 191 | purple_socket_set_data(PurpleSocket *ps, const gchar *key, gpointer data); 192 | 193 | /** 194 | * purple_socket_get_data: 195 | * @ps: The socket. 196 | * @key: The unique key. 197 | * 198 | * Returns extra data in a socket. 199 | * 200 | * Returns: The data associated with the key. 201 | */ 202 | gpointer 203 | purple_socket_get_data(PurpleSocket *ps, const gchar *key); 204 | 205 | /** 206 | * purple_socket_destroy: 207 | * @ps: The socket. 208 | * 209 | * Destroys the socket, closes connection and frees all resources. 210 | * 211 | * If file descriptor for the socket was extracted with purple_socket_get_fd and 212 | * added to event loop, it have to be removed prior this. 213 | */ 214 | void 215 | purple_socket_destroy(PurpleSocket *ps); 216 | 217 | #endif /* _PURPLE_SOCKET_H_ */ 218 | -------------------------------------------------------------------------------- /libteams.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2020 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #ifndef LIBTEAMS_H 20 | #define LIBTEAMS_H 21 | 22 | #ifndef PURPLE_PLUGINS 23 | # define PURPLE_PLUGINS 24 | #endif 25 | 26 | /* Maximum number of simultaneous connections to a server */ 27 | #define TEAMS_MAX_CONNECTIONS 16 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #ifdef __GNUC__ 37 | #include 38 | #include 39 | #endif 40 | 41 | #ifndef G_GNUC_NULL_TERMINATED 42 | # if __GNUC__ >= 4 43 | # define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) 44 | # else 45 | # define G_GNUC_NULL_TERMINATED 46 | # endif /* __GNUC__ >= 4 */ 47 | #endif /* G_GNUC_NULL_TERMINATED */ 48 | 49 | // workaround for MinGW32 which doesn't support "%zu"; see also https://stackoverflow.com/a/44383330 50 | #ifdef _WIN32 51 | # ifdef _WIN64 52 | # define PRI_SIZET PRIu64 53 | # else 54 | # define PRI_SIZET PRIu32 55 | # endif 56 | #else 57 | # define PRI_SIZET "zu" 58 | #endif 59 | 60 | #include "purplecompat.h" 61 | #include "glibcompat.h" 62 | 63 | #ifdef _WIN32 64 | # include 65 | # define dlopen(a,b) GetModuleHandleA(a) 66 | # define RTLD_LAZY 0x0001 67 | # define dlsym(a,b) GetProcAddress(a,b) 68 | # define dlclose(a) FreeLibrary(a) 69 | #else 70 | # include 71 | # include 72 | # include 73 | # include 74 | #endif 75 | 76 | #include 77 | 78 | #define json_object_get_int_member(JSON_OBJECT, MEMBER) \ 79 | ((JSON_OBJECT) && json_object_has_member((JSON_OBJECT), (MEMBER)) ? json_object_get_int_member((JSON_OBJECT), (MEMBER)) : 0) 80 | #define json_object_get_string_member(JSON_OBJECT, MEMBER) \ 81 | ((JSON_OBJECT) && json_object_has_member((JSON_OBJECT), (MEMBER)) ? json_object_get_string_member((JSON_OBJECT), (MEMBER)) : NULL) 82 | #define json_object_get_array_member(JSON_OBJECT, MEMBER) \ 83 | ((JSON_OBJECT) && json_object_has_member((JSON_OBJECT), (MEMBER)) ? json_object_get_array_member((JSON_OBJECT), (MEMBER)) : NULL) 84 | #define json_object_get_object_member(JSON_OBJECT, MEMBER) \ 85 | ((JSON_OBJECT) && json_object_has_member((JSON_OBJECT), (MEMBER)) ? json_object_get_object_member((JSON_OBJECT), (MEMBER)) : NULL) 86 | #define json_object_get_boolean_member(JSON_OBJECT, MEMBER) \ 87 | ((JSON_OBJECT) && json_object_has_member((JSON_OBJECT), (MEMBER)) ? json_object_get_boolean_member((JSON_OBJECT), (MEMBER)) : FALSE) 88 | #define json_array_get_length(JSON_ARRAY) \ 89 | ((JSON_ARRAY) ? json_array_get_length(JSON_ARRAY) : 0) 90 | #define json_node_get_array(JSON_NODE) \ 91 | ((JSON_NODE) && JSON_NODE_TYPE(JSON_NODE) == (JSON_NODE_ARRAY) ? json_node_get_array(JSON_NODE) : NULL) 92 | 93 | #include "accountopt.h" 94 | #include "core.h" 95 | #include "cmds.h" 96 | #include "connection.h" 97 | #include "debug.h" 98 | #include "http.h" 99 | #include "image.h" 100 | #include "image-store.h" 101 | #include "plugins.h" 102 | #include "proxy.h" 103 | #include "request.h" 104 | #include "roomlist.h" 105 | #include "savedstatuses.h" 106 | #include "sslconn.h" 107 | #include "util.h" 108 | #include "version.h" 109 | 110 | 111 | #if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 12 112 | # define atoll(a) g_ascii_strtoll(a, NULL, 0) 113 | #endif 114 | 115 | 116 | #define TEAMS_MAX_MSG_RETRY 2 117 | 118 | #define TEAMS_PLUGIN_ID "prpl-eionrobb-msteams" 119 | #define TEAMS_PLUGIN_VERSION "1.0" 120 | 121 | #define TEAMS_LOCKANDKEY_APPID "msmsgs@msnmsgr.com" 122 | #define TEAMS_LOCKANDKEY_SECRET "Q1P7W2E4J9R8U3S5" 123 | 124 | #define TEAMS_DEFAULT_MESSAGES_HOST "apac.notifications.teams.microsoft.com" 125 | #define TEAMS_PRESENCE_HOST "presence.teams.microsoft.com" 126 | 127 | #define TEAMS_CONTACTS_HOST "apac.ng.msg.teams.microsoft.com" 128 | #define TEAMS_NEW_CONTACTS_HOST "contacts.skype.com" 129 | #define TEAMS_LOGIN_HOST "login.skype.com" 130 | #define TEAMS_VIDEOMAIL_HOST "vm.skype.com" 131 | #define TEAMS_XFER_HOST "api.asm.skype.com" 132 | #define TEAMS_GRAPH_HOST "skypegraph.skype.com" 133 | #define TEAMS_STATIC_HOST "static.asm.skype.com" 134 | #define TEAMS_STATIC_CDN_HOST "static-asm.secure.skypeassets.com" 135 | #define TEAMS_DEFAULT_CONTACT_SUGGESTIONS_HOST "peoplerecommendations.skype.com" 136 | 137 | #define TEAMS_VDMS_TTL 300 138 | 139 | #define TEAMS_CLIENTINFO_NAME "skypeteams" 140 | #define TEAMS_CLIENTINFO_VERSION "1415/1.0.0.2021062127" 141 | 142 | 143 | #define TEAMS_BUDDY_IS_MSN(a) G_UNLIKELY((a) != NULL && strchr((a), '@') != NULL) 144 | #define TEAMS_BUDDY_IS_PHONE(a) G_UNLIKELY((a) != NULL && *(a) == '+') 145 | #define TEAMS_BUDDY_IS_S4B(a) G_UNLIKELY((a) != NULL && g_str_has_prefix((a), "2:")) 146 | #define TEAMS_BUDDY_IS_BOT(a) G_UNLIKELY((a) != NULL && g_str_has_prefix((a), "28:")) 147 | #define TEAMS_BUDDY_IS_NOTIFICATIONS(a) G_UNLIKELY((a) != NULL && g_str_has_prefix((a), "48:")) 148 | #define TEAMS_BUDDY_IS_SKYPE(a) G_UNLIKELY((a) != NULL && g_str_has_prefix((a), "8:") && !g_str_has_prefix((a), "8:orgid:")) 149 | #define TEAMS_BUDDY_IS_TEAMS(a) G_LIKELY((a) != NULL && g_str_has_prefix((a), "8:orgid:")) 150 | 151 | typedef struct _TeamsAccount TeamsAccount; 152 | typedef struct _TeamsBuddy TeamsBuddy; 153 | 154 | typedef void (*TeamsFunc)(TeamsAccount *swa); 155 | 156 | struct _TeamsAccount { 157 | gchar *username; 158 | gchar *primary_member_name; 159 | gchar *self_display_name; 160 | 161 | PurpleAccount *account; 162 | PurpleConnection *pc; 163 | PurpleHttpKeepalivePool *keepalive_pool; 164 | PurpleHttpConnectionSet *conns; 165 | PurpleHttpCookieJar *cookie_jar; 166 | gchar *messages_host; 167 | 168 | GHashTable *sent_messages_hash; 169 | guint poll_timeout; 170 | guint watchdog_timeout; 171 | 172 | guint authcheck_timeout; 173 | time_t last_authrequest; 174 | 175 | //old skypeweb 176 | gchar *skype_token; 177 | gchar *registration_token; 178 | gchar *vdms_token; 179 | gchar *endpoint; 180 | gint registration_expiry; 181 | gint vdms_expiry; 182 | gchar *region; 183 | 184 | //teams 185 | gchar *id_token; 186 | gchar *refresh_token; 187 | gchar *messages_cursor; 188 | gchar *tenant; 189 | GHashTable *buddy_to_chat_lookup; 190 | GHashTable *chat_to_buddy_lookup; 191 | gint refresh_token_timeout; 192 | gchar *csa_access_token; 193 | gchar *presence_access_token; 194 | struct _TeamsConnection *poll_conn; 195 | guint friend_list_poll_timeout; 196 | }; 197 | 198 | struct _TeamsBuddy { 199 | TeamsAccount *sa; 200 | PurpleBuddy *buddy; 201 | 202 | /** Contact info */ 203 | gchar *skypename; 204 | gchar *fullname; 205 | gchar *display_name; 206 | gboolean authorized; 207 | gboolean blocked; 208 | 209 | /** Profile info */ 210 | gchar *avatar_url; 211 | gchar *mood; 212 | }; 213 | 214 | void teams_buddy_free(PurpleBuddy *buddy); 215 | 216 | void teams_do_all_the_things(TeamsAccount *sa); 217 | 218 | #endif /* LIBTEAMS_H */ 219 | -------------------------------------------------------------------------------- /teams_connection.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2020 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #include "teams_connection.h" 20 | 21 | #include "http.h" 22 | 23 | static void 24 | teams_destroy_connection(TeamsConnection *conn) 25 | { 26 | g_free(conn->url); 27 | g_free(conn); 28 | } 29 | 30 | static void 31 | teams_post_or_get_cb(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 32 | { 33 | TeamsConnection *conn = user_data; 34 | const gchar *data; 35 | gsize len; 36 | 37 | data = purple_http_response_get_data(response, &len); 38 | 39 | if (conn->callback != NULL) { 40 | if (!len) 41 | { 42 | purple_debug_info("teams", "No data in response\n"); 43 | conn->callback(conn->sa, NULL, conn->user_data); 44 | } else { 45 | JsonParser *parser = json_parser_new(); 46 | if (!json_parser_load_from_data(parser, data, len, NULL)) 47 | { 48 | if (conn->error_callback != NULL) { 49 | conn->error_callback(conn->sa, data, len, conn->user_data); 50 | } else { 51 | purple_debug_error("teams", "Error parsing response: %s\n", data); 52 | } 53 | } else { 54 | JsonNode *root = json_parser_get_root(parser); 55 | 56 | purple_debug_info("teams", "executing callback for %s\n", conn->url); 57 | conn->callback(conn->sa, root, conn->user_data); 58 | } 59 | g_object_unref(parser); 60 | } 61 | } 62 | 63 | teams_destroy_connection(conn); 64 | } 65 | 66 | TeamsConnection *teams_post_or_get(TeamsAccount *sa, TeamsMethod method, 67 | const gchar *host, const gchar *url, const gchar *postdata, 68 | TeamsProxyCallbackFunc callback_func, gpointer user_data, 69 | gboolean keepalive) 70 | { 71 | TeamsConnection *conn; 72 | PurpleHttpRequest *request; 73 | const gchar* const *languages; 74 | gchar *language_names; 75 | gchar *real_url; 76 | 77 | g_return_val_if_fail(host != NULL, NULL); 78 | g_return_val_if_fail(url != NULL, NULL); 79 | g_return_val_if_fail(sa && sa->conns != NULL, NULL); 80 | 81 | real_url = g_strdup_printf("%s://%s%s", method & TEAMS_METHOD_SSL ? "https" : "http", host, url); 82 | 83 | purple_debug_info("teams", "Fetching url %s\n", real_url); 84 | 85 | request = purple_http_request_new(real_url); 86 | if (method & TEAMS_METHOD_POST) { 87 | purple_http_request_set_method(request, "POST"); 88 | } else if (method & TEAMS_METHOD_PUT) { 89 | purple_http_request_set_method(request, "PUT"); 90 | } else if (method & TEAMS_METHOD_DELETE) { 91 | purple_http_request_set_method(request, "DELETE"); 92 | } 93 | if (keepalive) { 94 | purple_http_request_set_keepalive_pool(request, sa->keepalive_pool); 95 | } 96 | 97 | purple_http_request_set_max_redirects(request, 0); 98 | purple_http_request_set_timeout(request, 120); 99 | 100 | if (method & (TEAMS_METHOD_POST | TEAMS_METHOD_PUT)) { 101 | if (postdata && (postdata[0] == '[' || postdata[0] == '{')) { 102 | purple_http_request_header_set(request, "Content-Type", "application/json"); // hax 103 | } else { 104 | purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded"); 105 | } 106 | if (postdata && *postdata) { 107 | purple_http_request_set_contents(request, postdata, strlen(postdata)); 108 | } 109 | 110 | //Zero-length PUT's dont get the content-length header set 111 | if ((method & TEAMS_METHOD_PUT) && (!postdata || !*postdata)) { 112 | purple_http_request_header_set(request, "Content-Length", "0"); 113 | } 114 | } 115 | 116 | purple_http_request_header_set(request, "BehaviorOverride", "redirectAs404"); 117 | 118 | if (g_str_equal(host, TEAMS_CONTACTS_HOST) || g_str_equal(host, TEAMS_VIDEOMAIL_HOST) || g_str_equal(host, TEAMS_NEW_CONTACTS_HOST)) { 119 | purple_http_request_header_set(request, "X-Skypetoken", sa->skype_token); 120 | purple_http_request_header_set(request, "X-Stratus-Caller", TEAMS_CLIENTINFO_NAME); 121 | purple_http_request_header_set(request, "X-Stratus-Request", "abcd1234"); 122 | purple_http_request_header_set(request, "Origin", "https://teams.microsoft.com"); 123 | purple_http_request_header_set(request, "Referer", "https://teams.microsoft.com/"); 124 | purple_http_request_header_set(request, "Accept", "application/json; ver=1.0;"); 125 | 126 | } else if (g_str_equal(host, TEAMS_GRAPH_HOST)) { 127 | purple_http_request_header_set(request, "X-Skypetoken", sa->skype_token); 128 | purple_http_request_header_set(request, "Accept", "application/json"); 129 | 130 | } else if (g_str_equal(host, sa->messages_host)) { 131 | purple_http_request_header_set_printf(request, "Authentication", "skypetoken=%s", sa->skype_token); 132 | purple_http_request_header_set(request, "Referer", "https://teams.microsoft.com/"); 133 | purple_http_request_header_set(request, "Accept", "application/json; ver=1.0"); 134 | purple_http_request_header_set(request, "ClientInfo", "os=windows; osVer=10; proc=x86; lcid=en-us; deviceType=1; country=n/a; clientName=" TEAMS_CLIENTINFO_NAME "; clientVer=" TEAMS_CLIENTINFO_VERSION); 135 | 136 | } else if (g_str_equal(host, TEAMS_DEFAULT_CONTACT_SUGGESTIONS_HOST)) { 137 | purple_http_request_header_set(request, "X-RecommenderServiceSettings", "{\"experiment\":\"default\",\"recommend\":\"true\"}"); 138 | purple_http_request_header_set(request, "X-ECS-ETag", TEAMS_CLIENTINFO_NAME); 139 | purple_http_request_header_set(request, "X-Skypetoken", sa->skype_token); 140 | purple_http_request_header_set(request, "Accept", "application/json"); 141 | purple_http_request_header_set(request, "X-Skype-Client", TEAMS_CLIENTINFO_VERSION); 142 | 143 | } else if (g_str_equal(host, TEAMS_PRESENCE_HOST)) { 144 | purple_http_request_header_set_printf(request, "Authorization", "Bearer %s", sa->presence_access_token); 145 | purple_http_request_header_set(request, "Accept", "application/json"); 146 | 147 | } else if (g_str_equal(host, "teams.microsoft.com")) { 148 | if (strstr(url, "/api/csa/") == url) { 149 | purple_http_request_header_set_printf(request, "Authorization", "Bearer %s", sa->csa_access_token); 150 | } else { 151 | purple_http_request_header_set_printf(request, "Authorization", "Bearer %s", sa->id_token); 152 | } 153 | purple_http_request_header_set(request, "X-Skypetoken", sa->skype_token); 154 | purple_http_request_header_set(request, "Accept", "application/json"); 155 | 156 | } else { 157 | purple_http_request_header_set(request, "Accept", "*/*"); 158 | purple_http_request_set_cookie_jar(request, sa->cookie_jar); 159 | } 160 | 161 | /* Tell the server what language we accept, so that we get error messages in our language (rather than our IP's) */ 162 | languages = g_get_language_names(); 163 | language_names = g_strjoinv(", ", (gchar **)languages); 164 | purple_util_chrreplace(language_names, '_', '-'); 165 | purple_http_request_header_set(request, "Accept-Language", language_names); 166 | g_free(language_names); 167 | 168 | conn = g_new0(TeamsConnection, 1); 169 | conn->sa = sa; 170 | conn->user_data = user_data; 171 | conn->url = real_url; 172 | conn->callback = callback_func; 173 | 174 | conn->http_conn = purple_http_request(sa->pc, request, teams_post_or_get_cb, conn); 175 | if (conn->http_conn != NULL) { 176 | purple_http_connection_set_add(sa->conns, conn->http_conn); 177 | } 178 | 179 | purple_http_request_unref(request); 180 | 181 | return conn; 182 | } 183 | -------------------------------------------------------------------------------- /purple2compat/purple-socket.c: -------------------------------------------------------------------------------- 1 | /* purple 2 | * 3 | * Purple is the legal property of its developers, whose names are too numerous 4 | * to list here. Please refer to the COPYRIGHT file distributed with this 5 | * source distribution. 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA 20 | */ 21 | 22 | #include "purple-socket.h" 23 | 24 | #ifndef _WIN32 25 | #include 26 | #include 27 | #endif 28 | 29 | #include "internal.h" 30 | 31 | #include "debug.h" 32 | #include "proxy.h" 33 | #include "sslconn.h" 34 | 35 | typedef enum { 36 | PURPLE_SOCKET_STATE_DISCONNECTED = 0, 37 | PURPLE_SOCKET_STATE_CONNECTING, 38 | PURPLE_SOCKET_STATE_CONNECTED, 39 | PURPLE_SOCKET_STATE_ERROR 40 | } PurpleSocketState; 41 | 42 | struct _PurpleSocket 43 | { 44 | PurpleConnection *gc; 45 | gchar *host; 46 | int port; 47 | gboolean is_tls; 48 | GHashTable *data; 49 | 50 | PurpleSocketState state; 51 | 52 | PurpleSslConnection *tls_connection; 53 | PurpleProxyConnectData *raw_connection; 54 | int fd; 55 | guint inpa; 56 | 57 | PurpleSocketConnectCb cb; 58 | gpointer cb_data; 59 | }; 60 | 61 | static GHashTable *handles = NULL; 62 | 63 | static void 64 | handle_add(PurpleSocket *ps) 65 | { 66 | PurpleConnection *gc = ps->gc; 67 | GSList *l; 68 | 69 | l = g_hash_table_lookup(handles, gc); 70 | l = g_slist_prepend(l, ps); 71 | g_hash_table_insert(handles, gc, l); 72 | } 73 | 74 | static void 75 | handle_remove(PurpleSocket *ps) 76 | { 77 | PurpleConnection *gc = ps->gc; 78 | GSList *l; 79 | 80 | l = g_hash_table_lookup(handles, gc); 81 | if (l != NULL) { 82 | l = g_slist_remove(l, ps); 83 | g_hash_table_insert(handles, gc, l); 84 | } 85 | } 86 | 87 | void 88 | _purple_socket_init(void) 89 | { 90 | handles = g_hash_table_new(g_direct_hash, g_direct_equal); 91 | } 92 | 93 | void 94 | _purple_socket_uninit(void) 95 | { 96 | g_hash_table_destroy(handles); 97 | handles = NULL; 98 | } 99 | 100 | PurpleSocket * 101 | purple_socket_new(PurpleConnection *gc) 102 | { 103 | PurpleSocket *ps = g_new0(PurpleSocket, 1); 104 | 105 | ps->gc = gc; 106 | ps->fd = -1; 107 | ps->port = -1; 108 | ps->data = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); 109 | 110 | handle_add(ps); 111 | 112 | return ps; 113 | } 114 | 115 | PurpleConnection * 116 | purple_socket_get_connection(PurpleSocket *ps) 117 | { 118 | g_return_val_if_fail(ps != NULL, NULL); 119 | 120 | return ps->gc; 121 | } 122 | 123 | static gboolean 124 | purple_socket_check_state(PurpleSocket *ps, PurpleSocketState wanted_state) 125 | { 126 | g_return_val_if_fail(ps != NULL, FALSE); 127 | 128 | if (ps->state == wanted_state) 129 | return TRUE; 130 | 131 | purple_debug_error("socket", "invalid state: %d (should be: %d)", 132 | ps->state, wanted_state); 133 | ps->state = PURPLE_SOCKET_STATE_ERROR; 134 | return FALSE; 135 | } 136 | 137 | void 138 | purple_socket_set_tls(PurpleSocket *ps, gboolean is_tls) 139 | { 140 | g_return_if_fail(ps != NULL); 141 | 142 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_DISCONNECTED)) 143 | return; 144 | 145 | ps->is_tls = is_tls; 146 | } 147 | 148 | void 149 | purple_socket_set_host(PurpleSocket *ps, const gchar *host) 150 | { 151 | g_return_if_fail(ps != NULL); 152 | 153 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_DISCONNECTED)) 154 | return; 155 | 156 | g_free(ps->host); 157 | ps->host = g_strdup(host); 158 | } 159 | 160 | void 161 | purple_socket_set_port(PurpleSocket *ps, int port) 162 | { 163 | g_return_if_fail(ps != NULL); 164 | g_return_if_fail(port >= 0); 165 | g_return_if_fail(port <= 65535); 166 | 167 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_DISCONNECTED)) 168 | return; 169 | 170 | ps->port = port; 171 | } 172 | 173 | static void 174 | _purple_socket_connected_raw(gpointer _ps, gint fd, const gchar *error_message) 175 | { 176 | PurpleSocket *ps = _ps; 177 | 178 | ps->raw_connection = NULL; 179 | 180 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTING)) { 181 | if (fd > 0) 182 | close(fd); 183 | ps->cb(ps, _("Invalid socket state"), ps->cb_data); 184 | return; 185 | } 186 | 187 | if (fd <= 0 || error_message != NULL) { 188 | if (error_message == NULL) 189 | error_message = _("Unknown error"); 190 | ps->fd = -1; 191 | ps->state = PURPLE_SOCKET_STATE_ERROR; 192 | ps->cb(ps, error_message, ps->cb_data); 193 | return; 194 | } 195 | 196 | ps->state = PURPLE_SOCKET_STATE_CONNECTED; 197 | ps->fd = fd; 198 | ps->cb(ps, NULL, ps->cb_data); 199 | } 200 | 201 | static void 202 | _purple_socket_connected_tls(gpointer _ps, PurpleSslConnection *tls_connection, 203 | PurpleInputCondition cond) 204 | { 205 | PurpleSocket *ps = _ps; 206 | 207 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTING)) { 208 | purple_ssl_close(tls_connection); 209 | ps->tls_connection = NULL; 210 | ps->cb(ps, _("Invalid socket state"), ps->cb_data); 211 | return; 212 | } 213 | 214 | if (ps->tls_connection->fd <= 0) { 215 | ps->state = PURPLE_SOCKET_STATE_ERROR; 216 | purple_ssl_close(tls_connection); 217 | ps->tls_connection = NULL; 218 | ps->cb(ps, _("Invalid file descriptor"), ps->cb_data); 219 | return; 220 | } 221 | 222 | ps->state = PURPLE_SOCKET_STATE_CONNECTED; 223 | ps->fd = ps->tls_connection->fd; 224 | ps->cb(ps, NULL, ps->cb_data); 225 | } 226 | 227 | static void 228 | _purple_socket_connected_tls_error(PurpleSslConnection *ssl_connection, 229 | PurpleSslErrorType error, gpointer _ps) 230 | { 231 | PurpleSocket *ps = _ps; 232 | 233 | ps->state = PURPLE_SOCKET_STATE_ERROR; 234 | ps->tls_connection = NULL; 235 | ps->cb(ps, purple_ssl_strerror(error), ps->cb_data); 236 | } 237 | 238 | gboolean 239 | purple_socket_connect(PurpleSocket *ps, PurpleSocketConnectCb cb, 240 | gpointer user_data) 241 | { 242 | PurpleAccount *account = NULL; 243 | 244 | g_return_val_if_fail(ps != NULL, FALSE); 245 | 246 | if (ps->gc && purple_connection_is_disconnecting(ps->gc)) { 247 | purple_debug_error("socket", "connection is being destroyed"); 248 | ps->state = PURPLE_SOCKET_STATE_ERROR; 249 | return FALSE; 250 | } 251 | 252 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_DISCONNECTED)) 253 | return FALSE; 254 | ps->state = PURPLE_SOCKET_STATE_CONNECTING; 255 | 256 | if (ps->host == NULL || ps->port < 0) { 257 | purple_debug_error("socket", "Host or port is not specified"); 258 | ps->state = PURPLE_SOCKET_STATE_ERROR; 259 | return FALSE; 260 | } 261 | 262 | if (ps->gc != NULL) 263 | account = purple_connection_get_account(ps->gc); 264 | 265 | ps->cb = cb; 266 | ps->cb_data = user_data; 267 | 268 | if (ps->is_tls) { 269 | ps->tls_connection = purple_ssl_connect(account, ps->host, 270 | ps->port, _purple_socket_connected_tls, 271 | _purple_socket_connected_tls_error, ps); 272 | } else { 273 | ps->raw_connection = purple_proxy_connect(ps->gc, account, 274 | ps->host, ps->port, _purple_socket_connected_raw, ps); 275 | } 276 | 277 | if (ps->tls_connection == NULL && 278 | ps->raw_connection == NULL) 279 | { 280 | ps->state = PURPLE_SOCKET_STATE_ERROR; 281 | return FALSE; 282 | } 283 | 284 | return TRUE; 285 | } 286 | 287 | gssize 288 | purple_socket_read(PurpleSocket *ps, guchar *buf, size_t len) 289 | { 290 | g_return_val_if_fail(ps != NULL, -1); 291 | g_return_val_if_fail(buf != NULL, -1); 292 | 293 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTED)) 294 | return -1; 295 | 296 | if (ps->is_tls) 297 | return purple_ssl_read(ps->tls_connection, buf, len); 298 | else 299 | return read(ps->fd, buf, len); 300 | } 301 | 302 | gssize 303 | purple_socket_write(PurpleSocket *ps, const guchar *buf, size_t len) 304 | { 305 | g_return_val_if_fail(ps != NULL, -1); 306 | g_return_val_if_fail(buf != NULL, -1); 307 | 308 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTED)) 309 | return -1; 310 | 311 | if (ps->is_tls) 312 | return purple_ssl_write(ps->tls_connection, buf, len); 313 | else 314 | return write(ps->fd, buf, len); 315 | } 316 | 317 | void 318 | purple_socket_watch(PurpleSocket *ps, PurpleInputCondition cond, 319 | PurpleInputFunction func, gpointer user_data) 320 | { 321 | g_return_if_fail(ps != NULL); 322 | 323 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTED)) 324 | return; 325 | 326 | if (ps->inpa > 0) 327 | purple_input_remove(ps->inpa); 328 | ps->inpa = 0; 329 | 330 | g_return_if_fail(ps->fd > 0); 331 | 332 | if (func != NULL) 333 | ps->inpa = purple_input_add(ps->fd, cond, func, user_data); 334 | } 335 | 336 | int 337 | purple_socket_get_fd(PurpleSocket *ps) 338 | { 339 | g_return_val_if_fail(ps != NULL, -1); 340 | 341 | if (!purple_socket_check_state(ps, PURPLE_SOCKET_STATE_CONNECTED)) 342 | return -1; 343 | 344 | g_return_val_if_fail(ps->fd > 0, -1); 345 | 346 | return ps->fd; 347 | } 348 | 349 | void 350 | purple_socket_set_data(PurpleSocket *ps, const gchar *key, gpointer data) 351 | { 352 | g_return_if_fail(ps != NULL); 353 | g_return_if_fail(key != NULL); 354 | 355 | if (data == NULL) 356 | g_hash_table_remove(ps->data, key); 357 | else 358 | g_hash_table_insert(ps->data, g_strdup(key), data); 359 | } 360 | 361 | gpointer 362 | purple_socket_get_data(PurpleSocket *ps, const gchar *key) 363 | { 364 | g_return_val_if_fail(ps != NULL, NULL); 365 | g_return_val_if_fail(key != NULL, NULL); 366 | 367 | return g_hash_table_lookup(ps->data, key); 368 | } 369 | 370 | static void 371 | purple_socket_cancel(PurpleSocket *ps) 372 | { 373 | if (ps->inpa > 0) 374 | purple_input_remove(ps->inpa); 375 | ps->inpa = 0; 376 | 377 | if (ps->tls_connection != NULL) { 378 | purple_ssl_close(ps->tls_connection); 379 | ps->fd = -1; 380 | } 381 | ps->tls_connection = NULL; 382 | 383 | if (ps->raw_connection != NULL) 384 | purple_proxy_connect_cancel(ps->raw_connection); 385 | ps->raw_connection = NULL; 386 | 387 | if (ps->fd > 0) 388 | close(ps->fd); 389 | ps->fd = 0; 390 | } 391 | 392 | void 393 | purple_socket_destroy(PurpleSocket *ps) 394 | { 395 | if (ps == NULL) 396 | return; 397 | 398 | handle_remove(ps); 399 | 400 | purple_socket_cancel(ps); 401 | 402 | g_free(ps->host); 403 | g_hash_table_destroy(ps->data); 404 | g_free(ps); 405 | } 406 | 407 | void 408 | _purple_socket_cancel_with_connection(PurpleConnection *gc) 409 | { 410 | GSList *it; 411 | 412 | it = g_hash_table_lookup(handles, gc); 413 | for (; it; it = g_slist_next(it)) { 414 | PurpleSocket *ps = it->data; 415 | purple_socket_cancel(ps); 416 | } 417 | } 418 | -------------------------------------------------------------------------------- /teams_util.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2020 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #include "teams_util.h" 20 | 21 | gchar * 22 | teams_string_get_chunk(const gchar *haystack, gsize len, const gchar *start, const gchar *end) 23 | { 24 | const gchar *chunk_start, *chunk_end; 25 | g_return_val_if_fail(haystack && start, NULL); 26 | 27 | if (len > 0) { 28 | chunk_start = g_strstr_len(haystack, len, start); 29 | } else { 30 | chunk_start = strstr(haystack, start); 31 | } 32 | g_return_val_if_fail(chunk_start, NULL); 33 | chunk_start += strlen(start); 34 | 35 | if (end != NULL) { 36 | if (len > 0) { 37 | chunk_end = g_strstr_len(chunk_start, len - (chunk_start - haystack), end); 38 | } else { 39 | chunk_end = strstr(chunk_start, end); 40 | } 41 | g_return_val_if_fail(chunk_end, NULL); 42 | 43 | return g_strndup(chunk_start, chunk_end - chunk_start); 44 | } else { 45 | return g_strdup(chunk_start); 46 | } 47 | } 48 | 49 | gchar * 50 | teams_jsonobj_to_string(JsonObject *jsonobj) 51 | { 52 | JsonGenerator *generator; 53 | JsonNode *root; 54 | gchar *string; 55 | 56 | root = json_node_new(JSON_NODE_OBJECT); 57 | json_node_set_object(root, jsonobj); 58 | 59 | generator = json_generator_new(); 60 | json_generator_set_root(generator, root); 61 | 62 | string = json_generator_to_data(generator, NULL); 63 | 64 | g_object_unref(generator); 65 | json_node_free(root); 66 | 67 | return string; 68 | } 69 | 70 | gchar * 71 | teams_jsonarr_to_string(JsonArray *jsonarr) 72 | { 73 | JsonGenerator *generator; 74 | JsonNode *root; 75 | gchar *string; 76 | 77 | root = json_node_new(JSON_NODE_ARRAY); 78 | json_node_set_array(root, jsonarr); 79 | 80 | generator = json_generator_new(); 81 | json_generator_set_root(generator, root); 82 | 83 | string = json_generator_to_data(generator, NULL); 84 | 85 | g_object_unref(generator); 86 | json_node_free(root); 87 | 88 | return string; 89 | } 90 | 91 | JsonNode * 92 | json_decode(const gchar *data, gssize len) 93 | { 94 | JsonParser *parser = json_parser_new(); 95 | JsonNode *root = NULL; 96 | 97 | if (!data || !json_parser_load_from_data(parser, data, len, NULL)) 98 | { 99 | purple_debug_error("teams", "Error parsing JSON: %s\n", data ? data : "(null)"); 100 | } else { 101 | root = json_parser_get_root(parser); 102 | if (root != NULL) { 103 | root = json_node_copy(root); 104 | } 105 | } 106 | g_object_unref(parser); 107 | 108 | return root; 109 | } 110 | 111 | JsonObject * 112 | json_decode_object(const gchar *data, gssize len) 113 | { 114 | JsonNode *root = json_decode(data, len); 115 | JsonObject *ret; 116 | 117 | g_return_val_if_fail(root, NULL); 118 | 119 | if (!JSON_NODE_HOLDS_OBJECT(root)) { 120 | // That ain't my thumb, neither! 121 | json_node_free(root); 122 | return NULL; 123 | } 124 | 125 | ret = json_node_dup_object(root); 126 | 127 | json_node_free(root); 128 | 129 | return ret; 130 | } 131 | 132 | JsonArray * 133 | json_decode_array(const gchar *data, gssize len) 134 | { 135 | JsonNode *root = json_decode(data, len); 136 | JsonArray *ret; 137 | 138 | g_return_val_if_fail(root, NULL); 139 | 140 | if (!JSON_NODE_HOLDS_ARRAY(root)) { 141 | json_node_free(root); 142 | return NULL; 143 | } 144 | 145 | ret = json_node_dup_array(root); 146 | 147 | json_node_free(root); 148 | 149 | return ret; 150 | } 151 | 152 | /** turn https://bay-client-s.gateway.messenger.live.com/v1/users/ME/contacts/8:eionrobb 153 | or https://bay-client-s.gateway.messenger.live.com/v1/users/8:eionrobb/presenceDocs/messagingService 154 | into eionrobb 155 | */ 156 | const gchar * 157 | teams_contact_url_to_name(const gchar *url) 158 | { 159 | static gchar *tempname = NULL; 160 | const gchar *start, *end; 161 | 162 | g_return_val_if_fail(url != NULL, NULL); 163 | 164 | // Strip the numeric prefix off these ones 165 | start = g_strrstr(url, "/8:"); 166 | if (!start) start = g_strrstr(url, "/1:"); 167 | if (!start) start = g_strrstr(url, "/4:"); 168 | if (start) start = start + 2; 169 | 170 | // Keep the prefix on these ones 171 | if (!start) start = g_strrstr(url, "/2:"); 172 | if (!start) start = g_strrstr(url, "/28:"); 173 | if (!start) start = g_strrstr(url, "/48:"); 174 | if (start) start = start + 1; 175 | if (!start) return NULL; 176 | 177 | if ((end = strchr(start, '/'))) { 178 | g_free(tempname); 179 | tempname = g_strndup(start, end - start); 180 | return tempname; 181 | } 182 | 183 | g_free(tempname); 184 | tempname = g_strdup(start); 185 | return tempname; 186 | } 187 | 188 | /** turn https://bay-client-s.gateway.messenger.live.com/v1/users/ME/conversations/19:blah@thread.skype 189 | into 19:blah@thread.skype 190 | */ 191 | const gchar * 192 | teams_thread_url_to_name(const gchar *url) 193 | { 194 | static gchar *tempname = NULL; 195 | const gchar *start, *end; 196 | 197 | start = g_strrstr(url, "/19:"); 198 | if (!start) return NULL; 199 | start = start + 1; 200 | 201 | if ((end = strchr(start, '/'))) { 202 | g_free(tempname); 203 | tempname = g_strndup(start, end - start); 204 | return tempname; 205 | } 206 | 207 | return start; 208 | } 209 | 210 | /** Blatantly stolen from MSN prpl, with super-secret SHA256 change! */ 211 | #define BUFSIZE 256 212 | char * 213 | teams_hmac_sha256(char *input) 214 | { 215 | GChecksum *hash; 216 | const guchar productKey[] = TEAMS_LOCKANDKEY_SECRET; 217 | const guchar productID[] = TEAMS_LOCKANDKEY_APPID; 218 | const char hexChars[] = "0123456789abcdef"; 219 | char buf[BUFSIZE]; 220 | unsigned char sha256Hash[32]; 221 | gsize sha256HashLen = sizeof(sha256Hash); 222 | unsigned char *newHash; 223 | unsigned int *sha256Parts; 224 | unsigned int *chlStringParts; 225 | unsigned int newHashParts[5]; 226 | gchar *output; 227 | 228 | long long nHigh = 0, nLow = 0; 229 | 230 | int len; 231 | int i; 232 | 233 | hash = g_checksum_new(G_CHECKSUM_SHA256); 234 | g_checksum_update(hash, (guchar *)input, strlen(input)); 235 | g_checksum_update(hash, productKey, sizeof(productKey) - 1); 236 | g_checksum_get_digest(hash, (guchar *)sha256Hash, &sha256HashLen); 237 | g_checksum_free(hash); 238 | 239 | /* Split it into four integers */ 240 | sha256Parts = (unsigned int *)sha256Hash; 241 | for (i = 0; i < 4; i++) { 242 | /* adjust endianess */ 243 | sha256Parts[i] = GUINT_TO_LE(sha256Parts[i]); 244 | 245 | /* & each integer with 0x7FFFFFFF */ 246 | /* and save one unmodified array for later */ 247 | newHashParts[i] = sha256Parts[i]; 248 | sha256Parts[i] &= 0x7FFFFFFF; 249 | } 250 | 251 | /* make a new string and pad with '0' to length that's a multiple of 8 */ 252 | snprintf(buf, BUFSIZE - 5, "%s%s", input, productID); 253 | len = strlen(buf); 254 | if ((len % 8) != 0) { 255 | int fix = 8 - (len % 8); 256 | memset(&buf[len], '0', fix); 257 | buf[len + fix] = '\0'; 258 | len += fix; 259 | } 260 | 261 | /* split into integers */ 262 | chlStringParts = (unsigned int *)buf; 263 | 264 | /* this is magic */ 265 | for (i = 0; i < (len / 4); i += 2) { 266 | long long temp; 267 | 268 | chlStringParts[i] = GUINT_TO_LE(chlStringParts[i]); 269 | chlStringParts[i + 1] = GUINT_TO_LE(chlStringParts[i + 1]); 270 | 271 | temp = (0x0E79A9C1 * (long long)chlStringParts[i]) % 0x7FFFFFFF; 272 | temp = (sha256Parts[0] * (temp + nLow) + sha256Parts[1]) % 0x7FFFFFFF; 273 | nHigh += temp; 274 | 275 | temp = ((long long)chlStringParts[i + 1] + temp) % 0x7FFFFFFF; 276 | nLow = (sha256Parts[2] * temp + sha256Parts[3]) % 0x7FFFFFFF; 277 | nHigh += nLow; 278 | } 279 | nLow = (nLow + sha256Parts[1]) % 0x7FFFFFFF; 280 | nHigh = (nHigh + sha256Parts[3]) % 0x7FFFFFFF; 281 | 282 | newHashParts[0] ^= nLow; 283 | newHashParts[1] ^= nHigh; 284 | newHashParts[2] ^= nLow; 285 | newHashParts[3] ^= nHigh; 286 | 287 | /* adjust endianness */ 288 | for(i = 0; i < 4; i++) 289 | newHashParts[i] = GUINT_TO_LE(newHashParts[i]); 290 | 291 | /* make a string of the parts */ 292 | newHash = (unsigned char *)newHashParts; 293 | 294 | /* convert to hexadecimal */ 295 | output = g_new0(gchar, 33); 296 | for (i = 0; i < 16; i++) 297 | { 298 | output[i * 2] = hexChars[(newHash[i] >> 4) & 0xF]; 299 | output[(i * 2) + 1] = hexChars[newHash[i] & 0xF]; 300 | } 301 | output[32] = '\0'; 302 | 303 | return output; 304 | } 305 | 306 | gint64 307 | teams_get_js_time() 308 | { 309 | #if GLIB_CHECK_VERSION(2, 28, 0) 310 | return (g_get_real_time() / 1000); 311 | #else 312 | GTimeVal val; 313 | 314 | g_get_current_time (&val); 315 | 316 | return (((gint64) val.tv_sec) * 1000) + (val.tv_usec / 1000); 317 | #endif 318 | } 319 | 320 | /* copied from oscar.c to be libpurple 2.1 compatible */ 321 | PurpleAccount * 322 | find_acct(const char *prpl, const char *acct_id) 323 | { 324 | PurpleAccount *acct = NULL; 325 | 326 | /* If we have a specific acct, use it */ 327 | if (acct_id && *acct_id) { 328 | acct = purple_accounts_find(acct_id, prpl); 329 | if (acct && !purple_account_is_connected(acct)) 330 | acct = NULL; 331 | } else { /* Otherwise find an active account for the protocol */ 332 | GList *l = purple_accounts_get_all(); 333 | while (l) { 334 | if (!strcmp(prpl, purple_account_get_protocol_id(l->data)) 335 | && purple_account_is_connected(l->data)) { 336 | acct = l->data; 337 | break; 338 | } 339 | l = l->next; 340 | } 341 | } 342 | 343 | return acct; 344 | } 345 | 346 | const gchar * 347 | teams_user_url_prefix(const gchar *who) 348 | { 349 | if(TEAMS_BUDDY_IS_S4B(who) || TEAMS_BUDDY_IS_BOT(who) || TEAMS_BUDDY_IS_NOTIFICATIONS(who)) { 350 | return ""; // already has a prefix 351 | } else if (TEAMS_BUDDY_IS_MSN(who)) { 352 | return "1:"; 353 | } else if(TEAMS_BUDDY_IS_PHONE(who)) { 354 | return "4:"; 355 | } else { 356 | return "8:"; 357 | } 358 | } 359 | 360 | const gchar * 361 | teams_strip_user_prefix(const gchar *who) 362 | { 363 | if (who[1] == ':') { 364 | if (who[0] != '2') { 365 | return who + 2; 366 | } 367 | } 368 | 369 | return who; 370 | } 371 | 372 | PurpleGroup * 373 | teams_get_blist_group(TeamsAccount *sa) 374 | { 375 | PurpleGroup *group; 376 | gchar *group_name; 377 | 378 | if (purple_account_get_string(sa->account, "group_name", NULL)) { 379 | group_name = g_strdup(purple_account_get_string(sa->account, "group_name", NULL)); 380 | } else if (!sa->tenant || !*sa->tenant) { 381 | group_name = g_strdup("Teams"); 382 | } else { 383 | //TODO nicer name 384 | group_name = g_strdup_printf("Teams - %s", sa->tenant); 385 | } 386 | 387 | group = purple_blist_find_group(group_name); 388 | if (!group) 389 | { 390 | group = purple_group_new(group_name); 391 | purple_blist_add_group(group, NULL); 392 | } 393 | 394 | g_free(group_name); 395 | 396 | return group; 397 | } 398 | 399 | gboolean 400 | teams_is_user_self(TeamsAccount *sa, const gchar *username) { 401 | if (!username || *username == 0) { 402 | return FALSE; 403 | } 404 | 405 | if (sa->username) { 406 | if (g_str_equal(username, sa->username)) { 407 | return TRUE; 408 | } 409 | } 410 | 411 | if (sa->primary_member_name) { 412 | if (g_str_equal(username, sa->primary_member_name)) { 413 | return TRUE; 414 | } 415 | } 416 | 417 | return !g_ascii_strcasecmp(username, purple_account_get_username(sa->account)); 418 | } 419 | -------------------------------------------------------------------------------- /purplecompat.h: -------------------------------------------------------------------------------- 1 | #ifndef _PURPLECOMPAT_H_ 2 | #define _PURPLECOMPAT_H_ 3 | 4 | #include 5 | #include "version.h" 6 | 7 | #if PURPLE_VERSION_CHECK(3, 0, 0) 8 | #include 9 | 10 | #define purple_conversation_set_data(conv, key, value) g_object_set_data(G_OBJECT(conv), key, value) 11 | #define purple_conversation_get_data(conv, key) g_object_get_data(G_OBJECT(conv), key) 12 | 13 | #define purple_xfer_ref g_object_ref 14 | 15 | #define purple_xfer_unref g_object_unref 16 | #define purple_circular_buffer_destroy g_object_unref 17 | #define purple_hash_destroy g_object_unref 18 | #define purple_message_destroy g_object_unref 19 | #define purple_buddy_destroy g_object_unref 20 | 21 | #define PURPLE_TYPE_STRING G_TYPE_STRING 22 | 23 | #define purple_protocol_action_get_connection(action) ((action)->connection) 24 | 25 | #define purple_chat_user_set_alias(cb, alias) g_object_set((cb), "alias", g_strdup(alias), NULL) 26 | #define purple_chat_get_alias(chat) g_object_get_data(G_OBJECT(chat), "alias") 27 | 28 | //TODO remove this when dx adds this to the PurpleMessageFlags enum 29 | #define PURPLE_MESSAGE_REMOTE_SEND 0x10000 30 | 31 | #else /*!PURPLE_VERSION_CHECK(3, 0, 0)*/ 32 | 33 | #include "connection.h" 34 | 35 | #define purple_blist_find_buddy purple_find_buddy 36 | #define purple_blist_find_group purple_find_group 37 | #define purple_blist_find_buddies purple_find_buddies 38 | #define PURPLE_IS_BUDDY PURPLE_BLIST_NODE_IS_BUDDY 39 | #define PURPLE_IS_CHAT PURPLE_BLIST_NODE_IS_CHAT 40 | #define purple_chat_get_name_only purple_chat_get_name 41 | #define purple_chat_set_alias purple_blist_alias_chat 42 | #define purple_chat_get_alias(chat) ((chat)->alias) 43 | #define purple_buddy_set_server_alias purple_blist_server_alias_buddy 44 | #define purple_buddy_get_local_alias purple_buddy_get_local_buddy_alias 45 | static inline void 46 | purple_blist_node_set_transient(PurpleBlistNode *node, gboolean transient) 47 | { 48 | PurpleBlistNodeFlags old_flags = purple_blist_node_get_flags(node); 49 | PurpleBlistNodeFlags new_flags; 50 | 51 | if (transient) 52 | new_flags = old_flags | PURPLE_BLIST_NODE_FLAG_NO_SAVE; 53 | else 54 | new_flags = old_flags & ~PURPLE_BLIST_NODE_FLAG_NO_SAVE; 55 | 56 | purple_blist_node_set_flags(node, new_flags); 57 | } 58 | 59 | #define PURPLE_CMD_FLAG_PROTOCOL_ONLY PURPLE_CMD_FLAG_PRPL_ONLY 60 | 61 | #define PURPLE_TYPE_CONNECTION purple_value_new(PURPLE_TYPE_SUBTYPE, PURPLE_SUBTYPE_CONNECTION) 62 | #define PURPLE_IS_CONNECTION PURPLE_CONNECTION_IS_VALID 63 | 64 | #define PURPLE_CONNECTION_DISCONNECTED PURPLE_DISCONNECTED 65 | #define PURPLE_CONNECTION_DISCONNECTING 4 66 | #define PURPLE_CONNECTION_CONNECTING PURPLE_CONNECTING 67 | #define PURPLE_CONNECTION_CONNECTED PURPLE_CONNECTED 68 | #define PURPLE_CONNECTION_FLAG_HTML PURPLE_CONNECTION_HTML 69 | #define PURPLE_CONNECTION_FLAG_NO_BGCOLOR PURPLE_CONNECTION_NO_BGCOLOR 70 | #define PURPLE_CONNECTION_FLAG_NO_FONTSIZE PURPLE_CONNECTION_NO_FONTSIZE 71 | #define PURPLE_CONNECTION_FLAG_NO_IMAGES PURPLE_CONNECTION_NO_IMAGES 72 | 73 | #define purple_request_cpar_from_connection(a) purple_connection_get_account(a), NULL, NULL 74 | #define purple_connection_get_protocol purple_connection_get_prpl 75 | #define purple_connection_error purple_connection_error_reason 76 | #define purple_connection_is_disconnecting(c) (purple_connection_get_state(c) == PURPLE_DISCONNECTED || purple_connection_get_state(c) == PURPLE_CONNECTION_DISCONNECTING) 77 | #define purple_connection_set_flags(pc, f) ((pc)->flags = (f)) 78 | #define purple_connection_get_flags(pc) ((pc)->flags) 79 | 80 | #define PurpleConversationUpdateType PurpleConvUpdateType 81 | #define PURPLE_CONVERSATION_UPDATE_TOPIC PURPLE_CONV_UPDATE_TOPIC 82 | #define PURPLE_CONVERSATION_UPDATE_UNSEEN PURPLE_CONV_UPDATE_UNSEEN 83 | #define PurpleChatConversation PurpleConvChat 84 | #define PurpleIMConversation PurpleConvIm 85 | #define purple_conversations_find_chat_with_account(id, account) \ 86 | PURPLE_CONV_CHAT(purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, id, account)) 87 | #define purple_conversations_find_chat(pc, id) PURPLE_CONV_CHAT(purple_find_chat(pc, id)) 88 | #define purple_conversations_get_all purple_get_conversations 89 | #define purple_conversation_get_connection purple_conversation_get_gc 90 | #define purple_conversation_present_error purple_conv_present_error 91 | #define purple_chat_conversation_get_id purple_conv_chat_get_id 92 | #define purple_chat_conversation_get_topic purple_conv_chat_get_topic 93 | #define purple_chat_conversation_set_topic purple_conv_chat_set_topic 94 | #define purple_conversations_find_im_with_account(name, account) \ 95 | PURPLE_CONV_IM(purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, name, account)) 96 | #define purple_im_conversation_new(account, from) PURPLE_CONV_IM(purple_conversation_new(PURPLE_CONV_TYPE_IM, account, from)) 97 | #define PURPLE_CONVERSATION(chatorim) ((chatorim) == NULL ? NULL : (chatorim)->conv) 98 | #define PURPLE_IM_CONVERSATION(conv) PURPLE_CONV_IM(conv) 99 | #define PURPLE_CHAT_CONVERSATION(conv) PURPLE_CONV_CHAT(conv) 100 | #define PURPLE_IS_IM_CONVERSATION(conv) (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) 101 | #define PURPLE_IS_CHAT_CONVERSATION(conv) (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) 102 | #define purple_chat_conversation_add_user purple_conv_chat_add_user 103 | #define purple_chat_conversation_has_left purple_conv_chat_has_left 104 | #define purple_chat_conversation_leave purple_conv_chat_left 105 | #define purple_chat_conversation_remove_user purple_conv_chat_remove_user 106 | #define purple_chat_conversation_clear_users purple_conv_chat_clear_users 107 | 108 | #define PurpleMessage PurpleConvMessage 109 | #define purple_message_get_contents(msg) ((msg)->what) 110 | #define purple_message_set_time(msg, time) ((msg)->when = (time)) 111 | #define purple_conversation_write_message(conv, msg) purple_conversation_write((conv), (msg)->who, (msg)->what, (msg)->flags, (msg)->when) 112 | static inline PurpleMessage * 113 | purple_message_new_outgoing(const gchar *who, const gchar *contents, PurpleMessageFlags flags) 114 | { 115 | PurpleMessage *message = g_new0(PurpleMessage, 1); 116 | 117 | message->who = g_strdup(who); 118 | message->what = g_strdup(contents); 119 | message->flags = flags | PURPLE_MESSAGE_SEND; 120 | message->when = time(NULL); 121 | 122 | return message; 123 | } 124 | static inline PurpleMessage * 125 | purple_message_new_incoming(const gchar *who, const gchar *contents, PurpleMessageFlags flags, guint64 timestamp) 126 | { 127 | PurpleMessage *message = g_new0(PurpleMessage, 1); 128 | 129 | message->who = g_strdup(who); 130 | message->what = g_strdup(contents); 131 | message->flags = flags | PURPLE_MESSAGE_RECV; 132 | message->when = timestamp; 133 | 134 | return message; 135 | } 136 | static inline PurpleMessage * 137 | purple_message_new_system(const gchar *contents, PurpleMessageFlags flags) 138 | { 139 | PurpleMessage *message = g_new0(PurpleMessage, 1); 140 | 141 | message->what = g_strdup(contents); 142 | message->flags = flags | PURPLE_MESSAGE_SYSTEM; 143 | message->when = time(NULL); 144 | 145 | return message; 146 | } 147 | static inline void 148 | purple_message_destroy(PurpleMessage *message) 149 | { 150 | g_free(message->who); 151 | g_free(message->what); 152 | g_free(message); 153 | } 154 | #if !PURPLE_VERSION_CHECK(2, 12, 0) 155 | # define PURPLE_MESSAGE_REMOTE_SEND 0x10000 156 | #endif 157 | 158 | #define purple_conversation_write_system_message(conv, msg, flags) purple_conversation_write((conv), NULL, (msg), (flags) | PURPLE_MESSAGE_SYSTEM, time(NULL)); 159 | 160 | #define PurpleProtocolChatEntry struct proto_chat_entry 161 | #define PurpleChatUserFlags PurpleConvChatBuddyFlags 162 | #define PURPLE_CHAT_USER_NONE PURPLE_CBFLAGS_NONE 163 | #define PURPLE_CHAT_USER_OP PURPLE_CBFLAGS_OP 164 | #define PURPLE_CHAT_USER_FOUNDER PURPLE_CBFLAGS_FOUNDER 165 | #define PURPLE_CHAT_USER_TYPING PURPLE_CBFLAGS_TYPING 166 | #define PURPLE_CHAT_USER_AWAY PURPLE_CBFLAGS_AWAY 167 | #define PURPLE_CHAT_USER_HALFOP PURPLE_CBFLAGS_HALFOP 168 | #define PURPLE_CHAT_USER_VOICE PURPLE_CBFLAGS_VOICE 169 | #define PURPLE_CHAT_USER_TYPING PURPLE_CBFLAGS_TYPING 170 | #define PurpleChatUser PurpleConvChatBuddy 171 | 172 | static inline PurpleChatUser * 173 | purple_chat_conversation_find_user(PurpleChatConversation *chat, const char *name) 174 | { 175 | PurpleChatUser *cb = purple_conv_chat_cb_find(chat, name); 176 | 177 | if (cb != NULL) { 178 | g_dataset_set_data(cb, "chat", chat); 179 | } 180 | 181 | return cb; 182 | } 183 | #define purple_chat_user_get_flags(cb) purple_conv_chat_user_get_flags(g_dataset_get_data((cb), "chat"), (cb)->name) 184 | #define purple_chat_user_set_flags(cb, f) purple_conv_chat_user_set_flags(g_dataset_get_data((cb), "chat"), (cb)->name, (f)) 185 | static inline void 186 | purple_chat_user_set_alias(PurpleChatUser *cb, const gchar *alias) 187 | { 188 | g_free(cb->alias); 189 | cb->alias = g_strdup(alias); 190 | } 191 | 192 | #define PurpleIMTypingState PurpleTypingState 193 | #define PURPLE_IM_NOT_TYPING PURPLE_NOT_TYPING 194 | #define PURPLE_IM_TYPING PURPLE_TYPING 195 | #define PURPLE_IM_TYPED PURPLE_TYPED 196 | 197 | #define purple_media_set_protocol_data purple_media_set_prpl_data 198 | #if PURPLE_VERSION_CHECK(2, 10, 12) 199 | // Handle ABI breakage 200 | # define PURPLE_MEDIA_NETWORK_PROTOCOL_TCP PURPLE_MEDIA_NETWORK_PROTOCOL_TCP_PASSIVE 201 | #endif 202 | 203 | #undef purple_notify_error 204 | #define purple_notify_error(handle, title, primary, secondary, cpar) \ 205 | purple_notify_message((handle), PURPLE_NOTIFY_MSG_ERROR, (title), \ 206 | (primary), (secondary), NULL, NULL) 207 | #undef purple_notify_warning 208 | #define purple_notify_warning(handle, title, primary, secondary, cpar) \ 209 | purple_notify_message((handle), PURPLE_NOTIFY_MSG_WARNING, (title), \ 210 | (primary), (secondary), NULL, NULL) 211 | #define purple_notify_user_info_add_pair_html purple_notify_user_info_add_pair 212 | 213 | #define PurpleProtocolAction PurplePluginAction 214 | #define purple_protocol_action_get_connection(action) ((PurpleConnection *) (action)->context) 215 | #define purple_protocol_action_new purple_plugin_action_new 216 | #define purple_protocol_get_id purple_plugin_get_id 217 | 218 | #define purple_protocol_got_user_status purple_prpl_got_user_status 219 | #define purple_protocol_got_user_idle purple_prpl_got_user_idle 220 | 221 | #define purple_account_privacy_deny_add purple_privacy_deny_add 222 | #define purple_account_privacy_deny_remove purple_privacy_deny_remove 223 | #define purple_account_set_password(account, password, dummy1, dummy2) \ 224 | purple_account_set_password(account, password); 225 | #define purple_account_set_private_alias purple_account_set_alias 226 | #define purple_account_get_private_alias purple_account_get_alias 227 | 228 | #define purple_proxy_info_get_proxy_type purple_proxy_info_get_type 229 | 230 | #define purple_serv_got_im serv_got_im 231 | #define purple_serv_got_typing serv_got_typing 232 | #define purple_serv_got_alias serv_got_alias 233 | #define purple_serv_got_chat_in serv_got_chat_in 234 | #define purple_serv_got_chat_left serv_got_chat_left 235 | #define purple_serv_got_joined_chat(pc, id, name) PURPLE_CONV_CHAT(serv_got_joined_chat(pc, id, name)) 236 | 237 | #define purple_status_get_status_type purple_status_get_type 238 | 239 | #define PurpleXmlNode xmlnode 240 | #define purple_xmlnode_new xmlnode_new 241 | #define purple_xmlnode_new_child xmlnode_new_child 242 | #define purple_xmlnode_from_str xmlnode_from_str 243 | #define purple_xmlnode_to_str xmlnode_to_str 244 | #define purple_xmlnode_get_child xmlnode_get_child 245 | #define purple_xmlnode_get_next_twin xmlnode_get_next_twin 246 | #define purple_xmlnode_get_data xmlnode_get_data 247 | #define purple_xmlnode_get_attrib xmlnode_get_attrib 248 | #define purple_xmlnode_set_attrib xmlnode_set_attrib 249 | #define purple_xmlnode_insert_data xmlnode_insert_data 250 | #define purple_xmlnode_free xmlnode_free 251 | #define purple_xmlnode_get_name(n) ((n) && (n)->name ? (n)->name : NULL) 252 | 253 | #define purple_xfer_set_protocol_data(xfer, proto_data) ((xfer)->data = (proto_data)) 254 | #define purple_xfer_get_protocol_data(xfer) ((xfer)->data) 255 | #define purple_xfer_get_xfer_type purple_xfer_get_type 256 | #define purple_xfer_protocol_ready purple_xfer_prpl_ready 257 | #if !PURPLE_VERSION_CHECK(2, 10, 12) && !FEDORA 258 | static inline gboolean 259 | purple_xfer_write_file(PurpleXfer *xfer, const guchar *buffer, gsize size) { 260 | PurpleXferUiOps *ui_ops = purple_xfer_get_ui_ops(xfer); 261 | purple_xfer_set_bytes_sent(xfer, purple_xfer_get_bytes_sent(xfer) + 262 | (ui_ops && ui_ops->ui_write ? ui_ops->ui_write(xfer, buffer, size) : fwrite(buffer, 1, size, xfer->dest_fp))); 263 | return TRUE; 264 | } 265 | static inline gssize 266 | purple_xfer_read_file(PurpleXfer *xfer, guchar *buffer, gsize size) { 267 | PurpleXferUiOps *ui_ops = purple_xfer_get_ui_ops(xfer); 268 | gssize got_len = (ui_ops && ui_ops->ui_read ? ui_ops->ui_read(xfer, &buffer, size) : fread(buffer, 1, size, xfer->dest_fp)); 269 | purple_xfer_set_bytes_sent(xfer, purple_xfer_get_bytes_sent(xfer) + got_len); 270 | return got_len; 271 | } 272 | #endif 273 | #define PURPLE_XFER_TYPE_RECEIVE PURPLE_XFER_RECEIVE 274 | #define PURPLE_XFER_TYPE_SEND PURPLE_XFER_SEND 275 | 276 | // Kinda gross, since we can technically use the glib mainloop from purple2 277 | #define g_timeout_add_seconds purple_timeout_add_seconds 278 | #define g_timeout_add purple_timeout_add 279 | #define g_source_remove purple_timeout_remove 280 | 281 | #endif 282 | 283 | #endif /*_PURPLECOMPAT_H_*/ 284 | -------------------------------------------------------------------------------- /purple2compat/http.h: -------------------------------------------------------------------------------- 1 | /* purple 2 | * 3 | * Purple is the legal property of its developers, whose names are too numerous 4 | * to list here. Please refer to the COPYRIGHT file distributed with this 5 | * source distribution. 6 | * 7 | * This program is free software; you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation; either version 2 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA 20 | */ 21 | 22 | #ifndef _PURPLE_HTTP_H_ 23 | #define _PURPLE_HTTP_H_ 24 | /** 25 | * SECTION:http 26 | * @section_id: libpurple-http 27 | * @short_description: http.h 28 | * @title: HTTP API 29 | */ 30 | 31 | #include 32 | 33 | #include "connection.h" 34 | 35 | /** 36 | * PurpleHttpRequest: 37 | * 38 | * A structure containing all data required to generate a single HTTP request. 39 | */ 40 | typedef struct _PurpleHttpRequest PurpleHttpRequest; 41 | 42 | /** 43 | * PurpleHttpConnection: 44 | * 45 | * A representation of actually running HTTP request. Can be used to cancel the 46 | * request. 47 | */ 48 | typedef struct _PurpleHttpConnection PurpleHttpConnection; 49 | 50 | /** 51 | * PurpleHttpResponse: 52 | * 53 | * All information got with response for HTTP request. 54 | */ 55 | typedef struct _PurpleHttpResponse PurpleHttpResponse; 56 | 57 | /** 58 | * PurpleHttpURL: 59 | * 60 | * Parsed representation for the URL. 61 | */ 62 | typedef struct _PurpleHttpURL PurpleHttpURL; 63 | 64 | /** 65 | * PurpleHttpCookieJar: 66 | * 67 | * A collection of cookies, got from HTTP response or provided for HTTP 68 | * request. 69 | */ 70 | typedef struct _PurpleHttpCookieJar PurpleHttpCookieJar; 71 | 72 | /** 73 | * PurpleHttpKeepalivePool: 74 | * 75 | * A pool of TCP connections for HTTP Keep-Alive session. 76 | */ 77 | typedef struct _PurpleHttpKeepalivePool PurpleHttpKeepalivePool; 78 | 79 | /** 80 | * PurpleHttpConnectionSet: 81 | * 82 | * A set of running HTTP requests. Can be used to cancel all of them at once. 83 | */ 84 | typedef struct _PurpleHttpConnectionSet PurpleHttpConnectionSet; 85 | 86 | /** 87 | * PurpleHttpCallback: 88 | * 89 | * A callback called after performing (successfully or not) HTTP request. 90 | */ 91 | typedef void (*PurpleHttpCallback)(PurpleHttpConnection *http_conn, 92 | PurpleHttpResponse *response, gpointer user_data); 93 | 94 | /** 95 | * PurpleHttpContentReaderCb: 96 | * 97 | * A callback called after storing data requested by PurpleHttpContentReader. 98 | */ 99 | typedef void (*PurpleHttpContentReaderCb)(PurpleHttpConnection *http_conn, 100 | gboolean success, gboolean eof, size_t stored); 101 | 102 | /** 103 | * PurpleHttpContentReader: 104 | * @http_conn: Connection, which requests data. 105 | * @buffer: Buffer to store data to (with offset ignored). 106 | * @offset: Position, from where to read data. 107 | * @length: Length of data to read. 108 | * @user_data: The user data passed with callback function. 109 | * @cb: The function to call after storing data to buffer. 110 | * 111 | * A callback for getting large request contents (ie. from file stored on 112 | * disk). 113 | */ 114 | typedef void (*PurpleHttpContentReader)(PurpleHttpConnection *http_conn, 115 | gchar *buffer, size_t offset, size_t length, gpointer user_data, 116 | PurpleHttpContentReaderCb cb); 117 | 118 | /** 119 | * PurpleHttpContentWriter: 120 | * @http_conn: Connection, which requests data. 121 | * @response: Response at point got so far (may change later). 122 | * @buffer: Buffer to read data from (with offset ignored). 123 | * @offset: Position of data got (its value is offset + length of 124 | * previous call), can be safely ignored. 125 | * @length: Length of data read. 126 | * @user_data: The user data passed with callback function. 127 | * 128 | * A callback for writing large response contents. 129 | * 130 | * Returns: TRUE, if succeeded, FALSE otherwise. 131 | */ 132 | typedef gboolean (*PurpleHttpContentWriter)(PurpleHttpConnection *http_conn, 133 | PurpleHttpResponse *response, const gchar *buffer, size_t offset, 134 | size_t length, gpointer user_data); 135 | 136 | /** 137 | * PurpleHttpProgressWatcher: 138 | * @http_conn: The HTTP Connection. 139 | * @reading_state: FALSE, is we are sending the request, TRUE, when reading 140 | * the response. 141 | * @processed: The amount of data already processed. 142 | * @total: Total amount of data (in current state). 143 | * @user_data: The user data passed with callback function. 144 | * 145 | * A callback for watching HTTP connection progress. 146 | */ 147 | typedef void (*PurpleHttpProgressWatcher)(PurpleHttpConnection *http_conn, 148 | gboolean reading_state, int processed, int total, gpointer user_data); 149 | 150 | G_BEGIN_DECLS 151 | 152 | /**************************************************************************/ 153 | /* Performing HTTP requests */ 154 | /**************************************************************************/ 155 | 156 | /** 157 | * purple_http_get: 158 | * @gc: The connection for which the request is needed, or NULL. 159 | * @callback: (scope call): The callback function. 160 | * @user_data: The user data to pass to the callback function. 161 | * @url: The URL. 162 | * 163 | * Fetches the data from a URL with GET request, and passes it to a callback 164 | * function. 165 | * 166 | * Returns: The HTTP connection struct. 167 | */ 168 | PurpleHttpConnection * purple_http_get(PurpleConnection *gc, 169 | PurpleHttpCallback callback, gpointer user_data, const gchar *url); 170 | 171 | /** 172 | * purple_http_get_printf: 173 | * @gc: The connection for which the request is needed, or NULL. 174 | * @callback: (scope call): The callback function. 175 | * @user_data: The user data to pass to the callback function. 176 | * @format: The format string. 177 | * 178 | * Constructs a URL and fetches the data from it with GET request, then passes 179 | * it to a callback function. 180 | * 181 | * Returns: The HTTP connection struct. 182 | */ 183 | PurpleHttpConnection * purple_http_get_printf(PurpleConnection *gc, 184 | PurpleHttpCallback callback, gpointer user_data, 185 | const gchar *format, ...) G_GNUC_PRINTF(4, 5); 186 | 187 | /** 188 | * purple_http_request: 189 | * @gc: The connection for which the request is needed, or NULL. 190 | * @request: The request. 191 | * @callback: (scope call): The callback function. 192 | * @user_data: The user data to pass to the callback function. 193 | * 194 | * Fetches an HTTP request and passes the response to a callback function. 195 | * Provided request struct can be shared by multiple http requests but can not 196 | * be modified when any of these is running. 197 | * 198 | * Returns: The HTTP connection struct. 199 | */ 200 | PurpleHttpConnection * purple_http_request(PurpleConnection *gc, 201 | PurpleHttpRequest *request, PurpleHttpCallback callback, 202 | gpointer user_data); 203 | 204 | /**************************************************************************/ 205 | /* HTTP connection API */ 206 | /**************************************************************************/ 207 | 208 | /** 209 | * purple_http_conn_cancel: 210 | * @http_conn: The data returned when you initiated the HTTP request. 211 | * 212 | * Cancel a pending HTTP request. 213 | */ 214 | void purple_http_conn_cancel(PurpleHttpConnection *http_conn); 215 | 216 | /** 217 | * purple_http_conn_cancel_all: 218 | * @gc: The handle. 219 | * 220 | * Cancels all HTTP connections associated with the specified handle. 221 | */ 222 | void purple_http_conn_cancel_all(PurpleConnection *gc); 223 | 224 | /** 225 | * purple_http_conn_is_running: 226 | * @http_conn: The HTTP connection (may be invalid pointer). 227 | * 228 | * Checks, if provided HTTP request is running. 229 | * 230 | * Returns: TRUE, if provided connection is currently running. 231 | */ 232 | gboolean purple_http_conn_is_running(PurpleHttpConnection *http_conn); 233 | 234 | /** 235 | * purple_http_conn_get_request: 236 | * @http_conn: The HTTP connection. 237 | * 238 | * Gets PurpleHttpRequest used for specified HTTP connection. 239 | * 240 | * Returns: The PurpleHttpRequest object. 241 | */ 242 | PurpleHttpRequest * purple_http_conn_get_request( 243 | PurpleHttpConnection *http_conn); 244 | 245 | /** 246 | * purple_http_conn_get_cookie_jar: 247 | * @http_conn: The HTTP connection. 248 | * 249 | * Gets cookie jar used within connection. 250 | * 251 | * Returns: The cookie jar. 252 | */ 253 | PurpleHttpCookieJar * purple_http_conn_get_cookie_jar( 254 | PurpleHttpConnection *http_conn); 255 | 256 | /** 257 | * purple_http_conn_get_purple_connection: 258 | * @http_conn: The HTTP connection. 259 | * 260 | * Gets PurpleConnection tied with specified HTTP connection. 261 | * 262 | * Returns: The PurpleConnection object. 263 | */ 264 | PurpleConnection * purple_http_conn_get_purple_connection( 265 | PurpleHttpConnection *http_conn); 266 | 267 | /** 268 | * purple_http_conn_set_progress_watcher: 269 | * @http_conn: The HTTP connection. 270 | * @watcher: (scope call): The watcher. 271 | * @user_data: The user data to pass to the callback function. 272 | * @interval_threshold: Minimum interval (in microseconds) of calls to 273 | * watcher, or -1 for default. 274 | * 275 | * Sets the watcher, called after writing or reading data to/from HTTP stream. 276 | * May be used for updating transfer progress gauge. 277 | */ 278 | void purple_http_conn_set_progress_watcher(PurpleHttpConnection *http_conn, 279 | PurpleHttpProgressWatcher watcher, gpointer user_data, 280 | gint interval_threshold); 281 | 282 | 283 | /**************************************************************************/ 284 | /* URL processing API */ 285 | /**************************************************************************/ 286 | 287 | /** 288 | * purple_http_url_parse: 289 | * @url: The URL to parse. 290 | * 291 | * Parses a URL. 292 | * 293 | * The returned data must be freed with purple_http_url_free. 294 | * 295 | * Returns: The parsed url or NULL, if the URL is invalid. 296 | */ 297 | PurpleHttpURL * 298 | purple_http_url_parse(const char *url); 299 | 300 | /** 301 | * purple_http_url_free: 302 | * @parsed_url: The parsed URL struct, or NULL. 303 | * 304 | * Frees the parsed URL struct. 305 | */ 306 | void 307 | purple_http_url_free(PurpleHttpURL *parsed_url); 308 | 309 | /** 310 | * purple_http_url_relative: 311 | * @base_url: The base URL. The result is stored here. 312 | * @relative_url: The relative URL. 313 | * 314 | * Converts the base URL to the absolute form of the provided relative URL. 315 | * 316 | * Example: "https://example.com/path/to/file.html" + "subdir/other-file.html" = 317 | * "https://example.com/path/to/subdir/another-file.html" 318 | */ 319 | void 320 | purple_http_url_relative(PurpleHttpURL *base_url, PurpleHttpURL *relative_url); 321 | 322 | /** 323 | * purple_http_url_print: 324 | * @parsed_url: The URL struct. 325 | * 326 | * Converts the URL struct to the printable form. The result may not be a valid 327 | * URL (in cases, when the struct doesn't have all fields filled properly). 328 | * 329 | * The result must be g_free'd. 330 | * 331 | * Returns: The printable form of the URL. 332 | */ 333 | gchar * 334 | purple_http_url_print(PurpleHttpURL *parsed_url); 335 | 336 | /** 337 | * purple_http_url_get_protocol: 338 | * @parsed_url: The URL struct. 339 | * 340 | * Gets the protocol part of URL. 341 | * 342 | * Returns: The protocol. 343 | */ 344 | const gchar * 345 | purple_http_url_get_protocol(const PurpleHttpURL *parsed_url); 346 | 347 | /** 348 | * purple_http_url_get_username: 349 | * @parsed_url: The URL struct. 350 | * 351 | * Gets the username part of URL. 352 | * 353 | * Returns: The username. 354 | */ 355 | const gchar * 356 | purple_http_url_get_username(const PurpleHttpURL *parsed_url); 357 | 358 | /** 359 | * purple_http_url_get_password: 360 | * @parsed_url: The URL struct. 361 | * 362 | * Gets the password part of URL. 363 | * 364 | * Returns: The password. 365 | */ 366 | const gchar * 367 | purple_http_url_get_password(const PurpleHttpURL *parsed_url); 368 | 369 | /** 370 | * purple_http_url_get_host: 371 | * @parsed_url: The URL struct. 372 | * 373 | * Gets the hostname part of URL. 374 | * 375 | * Returns: The hostname. 376 | */ 377 | const gchar * 378 | purple_http_url_get_host(const PurpleHttpURL *parsed_url); 379 | 380 | /** 381 | * purple_http_url_get_port: 382 | * @parsed_url: The URL struct. 383 | * 384 | * Gets the port part of URL. 385 | * 386 | * Returns: The port number. 387 | */ 388 | int 389 | purple_http_url_get_port(const PurpleHttpURL *parsed_url); 390 | 391 | /** 392 | * purple_http_url_get_path: 393 | * @parsed_url: The URL struct. 394 | * 395 | * Gets the path part of URL. 396 | * 397 | * Returns: The path. 398 | */ 399 | const gchar * 400 | purple_http_url_get_path(const PurpleHttpURL *parsed_url); 401 | 402 | /** 403 | * purple_http_url_get_fragment: 404 | * @parsed_url: The URL struct. 405 | * 406 | * Gets the fragment part of URL. 407 | * 408 | * Returns: The fragment. 409 | */ 410 | const gchar * 411 | purple_http_url_get_fragment(const PurpleHttpURL *parsed_url); 412 | 413 | 414 | /**************************************************************************/ 415 | /* Cookie jar API */ 416 | /**************************************************************************/ 417 | 418 | /** 419 | * purple_http_cookie_jar_new: 420 | * 421 | * Creates new cookie jar, 422 | * 423 | * Returns: empty cookie jar. 424 | */ 425 | PurpleHttpCookieJar * purple_http_cookie_jar_new(void); 426 | 427 | /** 428 | * purple_http_cookie_jar_ref: 429 | * @cookie_jar: The cookie jar. 430 | * 431 | * Increment the reference count. 432 | */ 433 | void purple_http_cookie_jar_ref(PurpleHttpCookieJar *cookie_jar); 434 | 435 | /** 436 | * purple_http_cookie_jar_unref: 437 | * @cookie_jar: The cookie jar. 438 | * 439 | * Decrement the reference count. 440 | * 441 | * If the reference count reaches zero, the cookie jar will be freed. 442 | * 443 | * Returns: @cookie_jar or %NULL if the reference count reached zero. 444 | */ 445 | PurpleHttpCookieJar * purple_http_cookie_jar_unref( 446 | PurpleHttpCookieJar *cookie_jar); 447 | 448 | /** 449 | * purple_http_cookie_jar_set: 450 | * @cookie_jar: The cookie jar. 451 | * @name: Cookie name. 452 | * @value: Cookie contents. 453 | * 454 | * Sets the cookie. 455 | */ 456 | void purple_http_cookie_jar_set(PurpleHttpCookieJar *cookie_jar, 457 | const gchar *name, const gchar *value); 458 | 459 | /** 460 | * purple_http_cookie_jar_get: 461 | * @cookie_jar: The cookie jar. 462 | * @name: Cookie name. 463 | * 464 | * Gets the cookie. 465 | * 466 | * The result must be g_free'd. 467 | * 468 | * Returns: Cookie contents, or NULL, if cookie doesn't exists. 469 | */ 470 | gchar * purple_http_cookie_jar_get(PurpleHttpCookieJar *cookie_jar, 471 | const gchar *name); 472 | 473 | /** 474 | * purple_http_cookie_jar_is_empty: 475 | * @cookie_jar: The cookie jar. 476 | * 477 | * Checks, if the cookie jar contains any cookies. 478 | * 479 | * Returns: TRUE, if cookie jar contains any cookie, FALSE otherwise. 480 | */ 481 | gboolean purple_http_cookie_jar_is_empty(PurpleHttpCookieJar *cookie_jar); 482 | 483 | 484 | /**************************************************************************/ 485 | /* HTTP Request API */ 486 | /**************************************************************************/ 487 | 488 | /** 489 | * purple_http_request_new: 490 | * @url: The URL to request for, or NULL to leave empty (to be set with 491 | * purple_http_request_set_url). 492 | * 493 | * Creates the new instance of HTTP request configuration. 494 | * 495 | * Returns: The new instance of HTTP request struct. 496 | */ 497 | PurpleHttpRequest * purple_http_request_new(const gchar *url); 498 | 499 | /** 500 | * purple_http_request_ref: 501 | * @request: The request. 502 | * 503 | * Increment the reference count. 504 | */ 505 | void purple_http_request_ref(PurpleHttpRequest *request); 506 | 507 | /** 508 | * purple_http_request_unref: 509 | * @request: The request. 510 | * 511 | * Decrement the reference count. 512 | * 513 | * If the reference count reaches zero, the http request struct will be freed. 514 | * 515 | * Returns: @request or %NULL if the reference count reached zero. 516 | */ 517 | PurpleHttpRequest * purple_http_request_unref(PurpleHttpRequest *request); 518 | 519 | /** 520 | * purple_http_request_set_url: 521 | * @request: The request. 522 | * @url: The url. 523 | * 524 | * Sets URL for HTTP request. 525 | */ 526 | void purple_http_request_set_url(PurpleHttpRequest *request, const gchar *url); 527 | 528 | /** 529 | * purple_http_request_set_url_printf: 530 | * @request: The request. 531 | * @format: The format string. 532 | * 533 | * Constructs and sets a URL for HTTP request. 534 | */ 535 | void purple_http_request_set_url_printf(PurpleHttpRequest *request, 536 | const gchar *format, ...) G_GNUC_PRINTF(2, 3); 537 | 538 | /** 539 | * purple_http_request_get_url: 540 | * @request: The request. 541 | * 542 | * Gets URL set for the HTTP request. 543 | * 544 | * Returns: URL set for this request. 545 | */ 546 | const gchar * purple_http_request_get_url(PurpleHttpRequest *request); 547 | 548 | /** 549 | * purple_http_request_set_method: 550 | * @request: The request. 551 | * @method: The method, or NULL for default. 552 | * 553 | * Sets custom HTTP method used for the request. 554 | */ 555 | void purple_http_request_set_method(PurpleHttpRequest *request, 556 | const gchar *method); 557 | 558 | /** 559 | * purple_http_request_get_method: 560 | * @request: The request. 561 | * 562 | * Gets HTTP method set for the request. 563 | * 564 | * Returns: The method. 565 | */ 566 | const gchar * purple_http_request_get_method(PurpleHttpRequest *request); 567 | 568 | /** 569 | * purple_http_request_set_keepalive_pool: 570 | * @request: The request. 571 | * @pool: The new KeepAlive pool, or NULL to reset. 572 | * 573 | * Sets HTTP KeepAlive connections pool for the request. 574 | * 575 | * It increases pool's reference count. 576 | */ 577 | void 578 | purple_http_request_set_keepalive_pool(PurpleHttpRequest *request, 579 | PurpleHttpKeepalivePool *pool); 580 | 581 | /** 582 | * purple_http_request_get_keepalive_pool: 583 | * @request: The request. 584 | * 585 | * Gets HTTP KeepAlive connections pool associated with the request. 586 | * 587 | * It doesn't affect pool's reference count. 588 | * 589 | * Returns: The KeepAlive pool, used for the request. 590 | */ 591 | PurpleHttpKeepalivePool * 592 | purple_http_request_get_keepalive_pool(PurpleHttpRequest *request); 593 | 594 | /** 595 | * purple_http_request_set_contents: 596 | * @request: The request. 597 | * @contents: The contents. 598 | * @length: The length of contents (-1 if it's a NULL-terminated string) 599 | * 600 | * Sets contents of HTTP request (for example, POST data). 601 | */ 602 | void purple_http_request_set_contents(PurpleHttpRequest *request, 603 | const gchar *contents, gsize length); 604 | 605 | /** 606 | * purple_http_request_get_contents: 607 | * @request: The request. 608 | * 609 | * Gets HTTP postdata set for the request. 610 | * 611 | * Returns: The contents. 612 | */ 613 | const gchar * purple_http_request_get_contents(PurpleHttpRequest *request); 614 | /** 615 | * purple_http_request_set_contents_reader: 616 | * @request: The request. 617 | * @reader: (scope call): The reader callback. 618 | * @contents_length: The size of all contents. 619 | * @user_data: The user data to pass to the callback function. 620 | * 621 | * Sets contents reader for HTTP request, used mainly for possible large 622 | * uploads. 623 | */ 624 | void purple_http_request_set_contents_reader(PurpleHttpRequest *request, 625 | PurpleHttpContentReader reader, gsize contents_length, gpointer user_data); 626 | 627 | /** 628 | * purple_http_request_set_response_writer: 629 | * @request: The request. 630 | * @writer: (scope call): The writer callback, or %NULL to remove existing. 631 | * @user_data: The user data to pass to the callback function. 632 | * 633 | * Set contents writer for HTTP response. 634 | */ 635 | void purple_http_request_set_response_writer(PurpleHttpRequest *request, 636 | PurpleHttpContentWriter writer, gpointer user_data); 637 | 638 | /** 639 | * purple_http_request_set_timeout: 640 | * @request: The request. 641 | * @timeout: Time (in seconds) after that timeout will be cancelled, 642 | * -1 for infinite time. 643 | * 644 | * Set maximum amount of time, that request is allowed to run. 645 | */ 646 | void purple_http_request_set_timeout(PurpleHttpRequest *request, int timeout); 647 | 648 | /** 649 | * purple_http_request_get_timeout: 650 | * @request: The request. 651 | * 652 | * Get maximum amount of time, that request is allowed to run. 653 | * 654 | * Returns: Timeout currently set (-1 for infinite). 655 | */ 656 | int purple_http_request_get_timeout(PurpleHttpRequest *request); 657 | 658 | /** 659 | * purple_http_request_set_max_redirects: 660 | * @request: The request. 661 | * @max_redirects: Maximum amount of redirects, or -1 for unlimited. 662 | * 663 | * Sets maximum amount of redirects. 664 | */ 665 | void purple_http_request_set_max_redirects(PurpleHttpRequest *request, 666 | int max_redirects); 667 | 668 | /** 669 | * purple_http_request_get_max_redirects: 670 | * @request: The request. 671 | * 672 | * Gets maximum amount of redirects. 673 | * 674 | * Returns: Current maximum amount of redirects (-1 for unlimited). 675 | */ 676 | int purple_http_request_get_max_redirects(PurpleHttpRequest *request); 677 | 678 | /** 679 | * purple_http_request_set_cookie_jar: 680 | * @request: The request. 681 | * @cookie_jar: The cookie jar. 682 | * 683 | * Sets cookie jar used for the request. 684 | */ 685 | void purple_http_request_set_cookie_jar(PurpleHttpRequest *request, 686 | PurpleHttpCookieJar *cookie_jar); 687 | 688 | /** 689 | * purple_http_request_get_cookie_jar: 690 | * @request: The request. 691 | * 692 | * Gets cookie jar used for the request. 693 | * 694 | * Returns: The cookie jar. 695 | */ 696 | PurpleHttpCookieJar * purple_http_request_get_cookie_jar( 697 | PurpleHttpRequest *request); 698 | 699 | /** 700 | * purple_http_request_set_http11: 701 | * @request: The request. 702 | * @http11: TRUE for HTTP/1.1, FALSE for HTTP/1.0. 703 | * 704 | * Sets HTTP version to use. 705 | */ 706 | void purple_http_request_set_http11(PurpleHttpRequest *request, 707 | gboolean http11); 708 | 709 | /** 710 | * purple_http_request_is_http11: 711 | * @request: The request. 712 | * 713 | * Gets used HTTP version. 714 | * 715 | * Returns: TRUE, if we use HTTP/1.1, FALSE for HTTP/1.0. 716 | */ 717 | gboolean purple_http_request_is_http11(PurpleHttpRequest *request); 718 | 719 | /** 720 | * purple_http_request_set_max_len: 721 | * @request: The request. 722 | * @max_len: Maximum length of response to read (-1 for the maximum 723 | * supported amount). 724 | * 725 | * Sets maximum length of response content to read. 726 | * 727 | * Headers length doesn't count here. 728 | * 729 | */ 730 | void purple_http_request_set_max_len(PurpleHttpRequest *request, int max_len); 731 | 732 | /** 733 | * purple_http_request_get_max_len: 734 | * @request: The request. 735 | * 736 | * Gets maximum length of response content to read. 737 | * 738 | * Returns: Maximum length of response to read, or -1 if unlimited. 739 | */ 740 | int purple_http_request_get_max_len(PurpleHttpRequest *request); 741 | 742 | /** 743 | * purple_http_request_header_set: 744 | * @request: The request. 745 | * @key: A header to be set. 746 | * @value: A value to set, or NULL to remove specified header. 747 | * 748 | * Sets (replaces, if exists) specified HTTP request header with provided value. 749 | * 750 | * See purple_http_request_header_add(). 751 | */ 752 | void purple_http_request_header_set(PurpleHttpRequest *request, 753 | const gchar *key, const gchar *value); 754 | 755 | /** 756 | * purple_http_request_header_set_printf: 757 | * @request: The request. 758 | * @key: A header to be set. 759 | * @format: The format string. 760 | * 761 | * Constructs and sets (replaces, if exists) specified HTTP request header. 762 | */ 763 | void purple_http_request_header_set_printf(PurpleHttpRequest *request, 764 | const gchar *key, const gchar *format, ...) G_GNUC_PRINTF(3, 4); 765 | 766 | /** 767 | * purple_http_request_header_add: 768 | * @key: A header to be set. 769 | * @value: A value to set. 770 | * 771 | * Adds (without replacing, if exists) an HTTP request header. 772 | * 773 | * See purple_http_request_header_set(). 774 | */ 775 | void purple_http_request_header_add(PurpleHttpRequest *request, 776 | const gchar *key, const gchar *value); 777 | 778 | 779 | /**************************************************************************/ 780 | /* HTTP Keep-Alive pool API */ 781 | /**************************************************************************/ 782 | 783 | /** 784 | * purple_http_keepalive_pool_new: 785 | * 786 | * Creates a new HTTP Keep-Alive pool. 787 | */ 788 | PurpleHttpKeepalivePool * 789 | purple_http_keepalive_pool_new(void); 790 | 791 | /** 792 | * purple_http_keepalive_pool_ref: 793 | * @pool: The HTTP Keep-Alive pool. 794 | * 795 | * Increment the reference count. 796 | */ 797 | void 798 | purple_http_keepalive_pool_ref(PurpleHttpKeepalivePool *pool); 799 | 800 | /** 801 | * purple_http_keepalive_pool_unref: 802 | * @pool: The HTTP Keep-Alive pool. 803 | * 804 | * Decrement the reference count. 805 | * 806 | * If the reference count reaches zero, the pool will be freed and all 807 | * connections will be closed. 808 | * 809 | * Returns: @pool or %NULL if the reference count reached zero. 810 | */ 811 | PurpleHttpKeepalivePool * 812 | purple_http_keepalive_pool_unref(PurpleHttpKeepalivePool *pool); 813 | 814 | /** 815 | * purple_http_keepalive_pool_set_limit_per_host: 816 | * @pool: The HTTP Keep-Alive pool. 817 | * @limit: The new limit, 0 for unlimited. 818 | * 819 | * Sets maximum allowed number of connections to specific host-triple (is_ssl + 820 | * hostname + port). 821 | */ 822 | void 823 | purple_http_keepalive_pool_set_limit_per_host(PurpleHttpKeepalivePool *pool, 824 | guint limit); 825 | 826 | /** 827 | * purple_http_keepalive_pool_get_limit_per_host: 828 | * @pool: The HTTP Keep-Alive pool. 829 | * 830 | * Gets maximum allowed number of connections to specific host-triple (is_ssl + 831 | * hostname + port). 832 | * 833 | * Returns: The limit. 834 | */ 835 | guint 836 | purple_http_keepalive_pool_get_limit_per_host(PurpleHttpKeepalivePool *pool); 837 | 838 | 839 | /**************************************************************************/ 840 | /* HTTP connection set API */ 841 | /**************************************************************************/ 842 | 843 | PurpleHttpConnectionSet * 844 | purple_http_connection_set_new(void); 845 | 846 | void 847 | purple_http_connection_set_destroy(PurpleHttpConnectionSet *set); 848 | 849 | void 850 | purple_http_connection_set_add(PurpleHttpConnectionSet *set, 851 | PurpleHttpConnection *http_conn); 852 | 853 | 854 | /**************************************************************************/ 855 | /* HTTP response API */ 856 | /**************************************************************************/ 857 | 858 | /** 859 | * purple_http_response_is_successful: 860 | * @response: The response. 861 | * 862 | * Checks, if HTTP request was performed successfully. 863 | * 864 | * Returns: TRUE, if request was performed successfully. 865 | */ 866 | gboolean purple_http_response_is_successful(PurpleHttpResponse *response); 867 | 868 | /** 869 | * purple_http_response_get_code: 870 | * @response: The response. 871 | * 872 | * Gets HTTP response code. 873 | * 874 | * Returns: HTTP response code. 875 | */ 876 | int purple_http_response_get_code(PurpleHttpResponse *response); 877 | 878 | /** 879 | * purple_http_response_get_error: 880 | * @response: The response. 881 | * 882 | * Gets error description. 883 | * 884 | * Returns: Localized error description or NULL, if there was no error. 885 | */ 886 | const gchar * purple_http_response_get_error(PurpleHttpResponse *response); 887 | 888 | /** 889 | * purple_http_response_get_data_len: 890 | * @response: The response. 891 | * 892 | * Gets HTTP response data length. 893 | * 894 | * Returns: Data length; 895 | */ 896 | gsize purple_http_response_get_data_len(PurpleHttpResponse *response); 897 | 898 | /** 899 | * purple_http_response_get_data: 900 | * @response: The response. 901 | * @len: Return address for the size of the data. Can be NULL. 902 | * 903 | * Gets HTTP response data. 904 | * 905 | * Response data is not written, if writer callback was set for request. 906 | * 907 | * Returns: The data. 908 | */ 909 | const gchar * purple_http_response_get_data(PurpleHttpResponse *response, size_t *len); 910 | 911 | /** 912 | * purple_http_response_get_all_headers: 913 | * @response: The response. 914 | * 915 | * Gets all headers got with response. 916 | * 917 | * Returns: GList of PurpleKeyValuePair, which keys are header field 918 | * names (gchar*) and values are its contents (gchar*). 919 | */ 920 | const GList * purple_http_response_get_all_headers(PurpleHttpResponse *response); 921 | 922 | /** 923 | * purple_http_response_get_headers_by_name: 924 | * @response: The response. 925 | * @name: The name of header field. 926 | * 927 | * Gets all headers with specified name got with response. 928 | * 929 | * Returns: GList of header field records contents (gchar*). 930 | */ 931 | const GList * purple_http_response_get_headers_by_name( 932 | PurpleHttpResponse *response, const gchar *name); 933 | 934 | /** 935 | * purple_http_response_get_header: 936 | * @response: The response. 937 | * @name: The name of header field. 938 | * 939 | * Gets one header contents with specified name got with response. 940 | * 941 | * To get all headers with the same name, use 942 | * purple_http_response_get_headers_by_name instead. 943 | * 944 | * Returns: Header field contents or NULL, if there is no such one. 945 | */ 946 | const gchar * purple_http_response_get_header(PurpleHttpResponse *response, 947 | const gchar *name); 948 | 949 | 950 | /**************************************************************************/ 951 | /* HTTP Subsystem */ 952 | /**************************************************************************/ 953 | 954 | /** 955 | * purple_http_init: 956 | * 957 | * Initializes the http subsystem. 958 | */ 959 | void purple_http_init(void); 960 | 961 | /** 962 | * purple_http_uninit: 963 | * 964 | * Uninitializes the http subsystem. 965 | */ 966 | void purple_http_uninit(void); 967 | 968 | G_END_DECLS 969 | 970 | #endif /* _PURPLE_HTTP_H_ */ 971 | -------------------------------------------------------------------------------- /libteams.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2020 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #include "libteams.h" 20 | #include "teams_connection.h" 21 | #include "teams_contacts.h" 22 | #include "teams_login.h" 23 | #include "teams_messages.h" 24 | #include "teams_util.h" 25 | 26 | void 27 | teams_do_all_the_things(TeamsAccount *sa) 28 | { 29 | // teams_get_vdms_token(sa); 30 | 31 | if (!sa->username) { 32 | teams_get_self_details(sa); 33 | } else 34 | if (sa->endpoint) { 35 | teams_get_self_details(sa); 36 | 37 | if (sa->authcheck_timeout) 38 | g_source_remove(sa->authcheck_timeout); 39 | teams_check_authrequests(sa); 40 | sa->authcheck_timeout = g_timeout_add_seconds(120, (GSourceFunc)teams_check_authrequests, sa); 41 | purple_connection_set_state(sa->pc, PURPLE_CONNECTION_CONNECTED); 42 | 43 | teams_get_friend_list(sa); 44 | //TODO remove me when switching to websocket 45 | sa->friend_list_poll_timeout = g_timeout_add_seconds(300, (GSourceFunc)teams_get_friend_list, sa); 46 | teams_poll(sa); 47 | 48 | skype_web_get_offline_history(sa); 49 | 50 | teams_set_status(sa->account, purple_account_get_active_status(sa->account)); 51 | } else { 52 | //Too soon! 53 | teams_subscribe(sa); 54 | } 55 | 56 | 57 | } 58 | 59 | 60 | /******************************************************************************/ 61 | /* PRPL functions */ 62 | /******************************************************************************/ 63 | 64 | static const char * 65 | teams_list_icon(PurpleAccount *account, PurpleBuddy *buddy) 66 | { 67 | if (buddy != NULL) { 68 | const gchar *buddy_name = purple_buddy_get_name(buddy); 69 | if (buddy_name != NULL) { 70 | if (TEAMS_BUDDY_IS_MSN(buddy_name)) { 71 | return "msn"; 72 | } else if (TEAMS_BUDDY_IS_SKYPE(buddy_name)) { 73 | return "skype"; 74 | } 75 | } 76 | } 77 | 78 | return "teams"; 79 | } 80 | 81 | static gchar * 82 | teams_status_text(PurpleBuddy *buddy) 83 | { 84 | TeamsBuddy *sbuddy = purple_buddy_get_protocol_data(buddy); 85 | 86 | if (sbuddy && sbuddy->mood && *(sbuddy->mood)) 87 | { 88 | gchar *stripped = purple_markup_strip_html(sbuddy->mood); 89 | gchar *escaped = g_markup_printf_escaped("%s", stripped); 90 | 91 | g_free(stripped); 92 | 93 | return escaped; 94 | } 95 | 96 | return NULL; 97 | } 98 | 99 | void 100 | teams_tooltip_text(PurpleBuddy *buddy, PurpleNotifyUserInfo *user_info, gboolean full) 101 | { 102 | TeamsBuddy *sbuddy = purple_buddy_get_protocol_data(buddy); 103 | 104 | if (sbuddy) 105 | { 106 | PurplePresence *presence; 107 | PurpleStatus *status; 108 | TeamsBuddy *sbuddy = purple_buddy_get_protocol_data(buddy); 109 | 110 | presence = purple_buddy_get_presence(buddy); 111 | status = purple_presence_get_active_status(presence); 112 | purple_notify_user_info_add_pair_html(user_info, _("Status"), purple_status_get_name(status)); 113 | if (sbuddy->mood && *sbuddy->mood) { 114 | gchar *stripped = purple_markup_strip_html(sbuddy->mood); 115 | gchar *escaped = g_markup_printf_escaped("%s", stripped); 116 | 117 | purple_notify_user_info_add_pair_html(user_info, _("Message"), escaped); 118 | 119 | g_free(stripped); 120 | g_free(escaped); 121 | } 122 | 123 | if (sbuddy->display_name && *sbuddy->display_name) { 124 | gchar *escaped = g_markup_printf_escaped("%s", sbuddy->display_name); 125 | purple_notify_user_info_add_pair_html(user_info, "Alias", escaped); 126 | g_free(escaped); 127 | } 128 | if (sbuddy->fullname && *sbuddy->fullname) { 129 | gchar *escaped = g_markup_printf_escaped("%s", sbuddy->fullname); 130 | purple_notify_user_info_add_pair_html(user_info, "Full Name", escaped); 131 | g_free(escaped); 132 | } 133 | } 134 | } 135 | 136 | const gchar * 137 | teams_list_emblem(PurpleBuddy *buddy) 138 | { 139 | if (buddy != NULL) { 140 | //TeamsBuddy *sbuddy = purple_buddy_get_protocol_data(buddy); 141 | const gchar *buddy_name = purple_buddy_get_name(buddy); 142 | 143 | if (buddy_name && TEAMS_BUDDY_IS_BOT(buddy_name)) { 144 | return "bot"; 145 | } 146 | } 147 | return NULL; 148 | } 149 | 150 | GList * 151 | teams_status_types(PurpleAccount *account) 152 | { 153 | GList *types = NULL; 154 | PurpleStatusType *status; 155 | 156 | status = purple_status_type_new_full(PURPLE_STATUS_OFFLINE, NULL, NULL, FALSE, FALSE, FALSE); 157 | types = g_list_append(types, status); 158 | 159 | status = purple_status_type_new_with_attrs(PURPLE_STATUS_AVAILABLE, "Available", _("Available"), TRUE, TRUE, FALSE, "message", "Mood", purple_value_new(PURPLE_TYPE_STRING), NULL); 160 | types = g_list_append(types, status); 161 | status = purple_status_type_new_with_attrs(PURPLE_STATUS_AWAY, "Away", _("Away"), TRUE, TRUE, FALSE, "message", "Mood", purple_value_new(PURPLE_TYPE_STRING), NULL); 162 | types = g_list_append(types, status); 163 | status = purple_status_type_new_with_attrs(PURPLE_STATUS_EXTENDED_AWAY, "BeRightBack", _("Be Right Back"), TRUE, TRUE, FALSE, "message", "Mood", purple_value_new(PURPLE_TYPE_STRING), NULL); 164 | types = g_list_append(types, status); 165 | status = purple_status_type_new_with_attrs(PURPLE_STATUS_UNAVAILABLE, "Busy", _("Busy"), TRUE, TRUE, FALSE, "message", "Mood", purple_value_new(PURPLE_TYPE_STRING), NULL); 166 | types = g_list_append(types, status); 167 | status = purple_status_type_new_with_attrs(PURPLE_STATUS_UNAVAILABLE, "DoNotDisturb", _("Do Not Disturb"), TRUE, TRUE, FALSE, "message", "Mood", purple_value_new(PURPLE_TYPE_STRING), NULL); 168 | types = g_list_append(types, status); 169 | // status = purple_status_type_new_with_attrs(PURPLE_STATUS_INVISIBLE, "Offline", _("Appear offline"), TRUE, TRUE, FALSE, "message", "Mood", purple_value_new(PURPLE_TYPE_STRING), NULL); 170 | // types = g_list_append(types, status); 171 | status = purple_status_type_new_with_attrs(PURPLE_STATUS_OFFLINE, "Offline", _("Offline"), TRUE, TRUE, FALSE, "message", "Mood", purple_value_new(PURPLE_TYPE_STRING), NULL); 172 | types = g_list_append(types, status); 173 | 174 | return types; 175 | } 176 | 177 | 178 | static GList * 179 | teams_chat_info(PurpleConnection *gc) 180 | { 181 | GList *m = NULL; 182 | PurpleProtocolChatEntry *pce; 183 | 184 | pce = g_new0(PurpleProtocolChatEntry, 1); 185 | pce->label = _("Teams Name"); 186 | pce->identifier = "chatname"; 187 | pce->required = TRUE; 188 | m = g_list_append(m, pce); 189 | 190 | /*pce = g_new0(PurpleProtocolChatEntry, 1); 191 | pce->label = _("Password"); 192 | pce->identifier = "password"; 193 | pce->required = FALSE; 194 | m = g_list_append(m, pce);*/ 195 | 196 | return m; 197 | } 198 | 199 | static GHashTable * 200 | teams_chat_info_defaults(PurpleConnection *gc, const char *chatname) 201 | { 202 | GHashTable *defaults; 203 | defaults = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free); 204 | if (chatname != NULL) 205 | { 206 | g_hash_table_insert(defaults, "chatname", g_strdup(chatname)); 207 | } 208 | return defaults; 209 | } 210 | 211 | static gchar * 212 | teams_get_chat_name(GHashTable *data) 213 | { 214 | gchar *temp; 215 | 216 | if (data == NULL) 217 | return NULL; 218 | 219 | temp = g_hash_table_lookup(data, "chatname"); 220 | 221 | if (temp == NULL) 222 | return NULL; 223 | 224 | return g_strdup(temp); 225 | } 226 | 227 | 228 | static void 229 | teams_join_chat(PurpleConnection *pc, GHashTable *data) 230 | { 231 | TeamsAccount *sa = purple_connection_get_protocol_data(pc); 232 | gchar *chatname; 233 | gchar *post; 234 | GString *url; 235 | PurpleChatConversation *chatconv; 236 | 237 | chatname = (gchar *)g_hash_table_lookup(data, "chatname"); 238 | if (chatname == NULL) 239 | { 240 | return; 241 | } 242 | 243 | chatconv = purple_conversations_find_chat_with_account(chatname, sa->account); 244 | if (chatconv != NULL && !purple_chat_conversation_has_left(chatconv)) { 245 | purple_conversation_present(PURPLE_CONVERSATION(chatconv)); 246 | return; 247 | } 248 | 249 | url = g_string_new("/v1/threads/"); 250 | g_string_append_printf(url, "%s", purple_url_encode(chatname)); 251 | g_string_append(url, "/members/"); 252 | g_string_append_printf(url, "8:%s", purple_url_encode(sa->username)); 253 | 254 | /* Specifying the role does not seem to be required and often result in a users role being 255 | * downgraded from admin to user 256 | * post = "{\"role\":\"User\"}"; */ 257 | post = "{}"; 258 | 259 | //TODO find new endpoint 260 | //teams_post_or_get(sa, TEAMS_METHOD_PUT | TEAMS_METHOD_SSL, sa->messages_host, url->str, post, NULL, NULL, TRUE); 261 | (void) post; 262 | 263 | g_string_free(url, TRUE); 264 | 265 | teams_get_conversation_history(sa, chatname); 266 | teams_get_thread_users(sa, chatname); 267 | 268 | chatconv = purple_serv_got_joined_chat(pc, g_str_hash(chatname), chatname); 269 | purple_conversation_set_data(PURPLE_CONVERSATION(chatconv), "chatname", g_strdup(chatname)); 270 | 271 | purple_conversation_present(PURPLE_CONVERSATION(chatconv)); 272 | } 273 | 274 | void 275 | teams_buddy_free(PurpleBuddy *buddy) 276 | { 277 | TeamsBuddy *sbuddy = purple_buddy_get_protocol_data(buddy); 278 | if (sbuddy != NULL) 279 | { 280 | purple_buddy_set_protocol_data(buddy, NULL); 281 | 282 | g_free(sbuddy->skypename); 283 | g_free(sbuddy->fullname); 284 | g_free(sbuddy->display_name); 285 | g_free(sbuddy->avatar_url); 286 | g_free(sbuddy->mood); 287 | 288 | g_free(sbuddy); 289 | } 290 | } 291 | 292 | void 293 | teams_fake_group_buddy(PurpleConnection *pc, const char *who, const char *old_group, const char *new_group) 294 | { 295 | // Do nothing to stop the remove+add behaviour 296 | } 297 | void 298 | teams_fake_group_rename(PurpleConnection *pc, const char *old_name, PurpleGroup *group, GList *moved_buddies) 299 | { 300 | // Do nothing to stop the remove+add behaviour 301 | } 302 | 303 | static GList * 304 | teams_node_menu(PurpleBlistNode *node) 305 | { 306 | GList *m = NULL; 307 | PurpleMenuAction *act; 308 | PurpleBuddy *buddy; 309 | TeamsAccount *sa = NULL; 310 | 311 | if(PURPLE_IS_BUDDY(node)) 312 | { 313 | buddy = PURPLE_BUDDY(node); 314 | if (purple_buddy_get_protocol_data(buddy)) { 315 | TeamsBuddy *sbuddy = purple_buddy_get_protocol_data(buddy); 316 | sa = sbuddy->sa; 317 | } 318 | if (sa == NULL) { 319 | PurpleConnection *pc = purple_account_get_connection(purple_buddy_get_account(buddy)); 320 | sa = purple_connection_get_protocol_data(pc); 321 | } 322 | 323 | if (sa != NULL) { 324 | act = purple_menu_action_new(_("Initiate _Chat"), 325 | PURPLE_CALLBACK(teams_initiate_chat_from_node), 326 | sa, NULL); 327 | m = g_list_append(m, act); 328 | } 329 | } 330 | 331 | return m; 332 | } 333 | 334 | static gulong conversation_updated_signal = 0; 335 | static gulong chat_conversation_typing_signal = 0; 336 | 337 | static void 338 | teams_login(PurpleAccount *account) 339 | { 340 | PurpleConnection *pc = purple_account_get_connection(account); 341 | TeamsAccount *sa = g_new0(TeamsAccount, 1); 342 | PurpleConnectionFlags flags; 343 | const gchar *password = purple_connection_get_password(pc); 344 | 345 | purple_connection_set_protocol_data(pc, sa); 346 | 347 | flags = purple_connection_get_flags(pc); 348 | flags |= PURPLE_CONNECTION_FLAG_HTML | PURPLE_CONNECTION_FLAG_NO_BGCOLOR | PURPLE_CONNECTION_FLAG_NO_FONTSIZE; 349 | purple_connection_set_flags(pc, flags); 350 | 351 | if (!TEAMS_BUDDY_IS_MSN(purple_account_get_username(account))) { 352 | sa->username = g_ascii_strdown(purple_account_get_username(account), -1); 353 | } 354 | sa->account = account; 355 | sa->pc = pc; 356 | sa->cookie_jar = purple_http_cookie_jar_new(); 357 | sa->sent_messages_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); 358 | sa->buddy_to_chat_lookup = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); 359 | sa->chat_to_buddy_lookup = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); 360 | sa->messages_host = g_strdup(TEAMS_DEFAULT_MESSAGES_HOST); 361 | sa->keepalive_pool = purple_http_keepalive_pool_new(); 362 | purple_http_keepalive_pool_set_limit_per_host(sa->keepalive_pool, TEAMS_MAX_CONNECTIONS); 363 | sa->conns = purple_http_connection_set_new(); 364 | 365 | const gchar *tenant = purple_account_get_string(account, "tenant", NULL); 366 | if (tenant != NULL) { 367 | sa->tenant = g_strdup(tenant); 368 | } 369 | 370 | //teams_begin_soapy_login(sa); 371 | 372 | if (password && *password) { 373 | sa->refresh_token = g_strdup(password); 374 | purple_connection_update_progress(pc, _("Authenticating"), 1, 3); 375 | teams_oauth_refresh_token(sa); 376 | } else { 377 | teams_do_web_auth(sa); 378 | } 379 | 380 | if (!conversation_updated_signal) { 381 | conversation_updated_signal = purple_signal_connect(purple_conversations_get_handle(), "conversation-updated", purple_connection_get_protocol(pc), PURPLE_CALLBACK(teams_mark_conv_seen), NULL); 382 | } 383 | if (!chat_conversation_typing_signal) { 384 | chat_conversation_typing_signal = purple_signal_connect(purple_conversations_get_handle(), "chat-conversation-typing", purple_connection_get_protocol(pc), PURPLE_CALLBACK(teams_conv_send_typing), NULL); 385 | } 386 | } 387 | 388 | static void 389 | teams_close(PurpleConnection *pc) 390 | { 391 | TeamsAccount *sa; 392 | GSList *buddies; 393 | 394 | g_return_if_fail(pc != NULL); 395 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 396 | purple_connection_set_state(pc, PURPLE_CONNECTION_DISCONNECTING); 397 | #endif 398 | 399 | sa = purple_connection_get_protocol_data(pc); 400 | g_return_if_fail(sa != NULL); 401 | 402 | g_source_remove(sa->friend_list_poll_timeout); 403 | g_source_remove(sa->authcheck_timeout); 404 | g_source_remove(sa->poll_timeout); 405 | g_source_remove(sa->watchdog_timeout); 406 | g_source_remove(sa->refresh_token_timeout); 407 | 408 | teams_logout(sa); 409 | 410 | purple_debug_info("teams", "destroying incomplete connections\n"); 411 | 412 | purple_http_connection_set_destroy(sa->conns); 413 | sa->conns = NULL; 414 | purple_http_conn_cancel_all(pc); 415 | purple_http_keepalive_pool_unref(sa->keepalive_pool); 416 | purple_http_cookie_jar_unref(sa->cookie_jar); 417 | 418 | buddies = purple_blist_find_buddies(sa->account, NULL); 419 | while (buddies != NULL) { 420 | PurpleBuddy *buddy = buddies->data; 421 | teams_buddy_free(buddy); 422 | purple_buddy_set_protocol_data(buddy, NULL); 423 | buddies = g_slist_delete_link(buddies, buddies); 424 | } 425 | 426 | g_hash_table_destroy(sa->sent_messages_hash); 427 | g_hash_table_destroy(sa->buddy_to_chat_lookup); 428 | g_hash_table_destroy(sa->chat_to_buddy_lookup); 429 | 430 | g_free(sa->csa_access_token); 431 | g_free(sa->presence_access_token); 432 | g_free(sa->id_token); 433 | g_free(sa->refresh_token); 434 | g_free(sa->region); 435 | g_free(sa->messages_cursor); 436 | g_free(sa->tenant); 437 | 438 | g_free(sa->vdms_token); 439 | g_free(sa->messages_host); 440 | g_free(sa->skype_token); 441 | g_free(sa->registration_token); 442 | g_free(sa->endpoint); 443 | g_free(sa->primary_member_name); 444 | g_free(sa->self_display_name); 445 | g_free(sa->username); 446 | g_free(sa); 447 | } 448 | 449 | gboolean 450 | teams_offline_message(const PurpleBuddy *buddy) 451 | { 452 | return TRUE; 453 | } 454 | 455 | static PurpleCmdRet 456 | teams_cmd_list(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data) 457 | { 458 | purple_roomlist_show_with_account(purple_conversation_get_account(conv)); 459 | 460 | return PURPLE_CMD_RET_OK; 461 | } 462 | 463 | static PurpleCmdRet 464 | teams_cmd_leave(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data) 465 | { 466 | PurpleConnection *pc = NULL; 467 | int id = -1; 468 | TeamsAccount *sa; 469 | 470 | pc = purple_conversation_get_connection(conv); 471 | id = purple_chat_conversation_get_id(PURPLE_CHAT_CONVERSATION(conv)); 472 | 473 | if (pc == NULL || id == -1) 474 | return PURPLE_CMD_RET_FAILED; 475 | 476 | sa = purple_connection_get_protocol_data(pc); 477 | if (sa == NULL) 478 | return PURPLE_CMD_RET_FAILED; 479 | 480 | teams_chat_kick(pc, id, sa->username); 481 | 482 | return PURPLE_CMD_RET_OK; 483 | } 484 | 485 | static PurpleCmdRet 486 | teams_cmd_kick(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data) 487 | { 488 | PurpleConnection *pc = NULL; 489 | int id = -1; 490 | 491 | pc = purple_conversation_get_connection(conv); 492 | id = purple_chat_conversation_get_id(PURPLE_CHAT_CONVERSATION(conv)); 493 | 494 | if (pc == NULL || id == -1) 495 | return PURPLE_CMD_RET_FAILED; 496 | 497 | teams_chat_kick(pc, id, args[0]); 498 | 499 | return PURPLE_CMD_RET_OK; 500 | } 501 | 502 | static PurpleCmdRet 503 | teams_cmd_invite(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data) 504 | { 505 | PurpleConnection *pc = NULL; 506 | int id = -1; 507 | 508 | pc = purple_conversation_get_connection(conv); 509 | id = purple_chat_conversation_get_id(PURPLE_CHAT_CONVERSATION(conv)); 510 | 511 | if (pc == NULL || id == -1) 512 | return PURPLE_CMD_RET_FAILED; 513 | 514 | teams_chat_invite(pc, id, NULL, args[0]); 515 | 516 | return PURPLE_CMD_RET_OK; 517 | } 518 | 519 | static PurpleCmdRet 520 | teams_cmd_topic(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data) 521 | { 522 | PurpleConnection *pc = NULL; 523 | PurpleChatConversation *chat; 524 | int id = -1; 525 | 526 | pc = purple_conversation_get_connection(conv); 527 | chat = PURPLE_CHAT_CONVERSATION(conv); 528 | id = purple_chat_conversation_get_id(chat); 529 | 530 | if (pc == NULL || id == -1) 531 | return PURPLE_CMD_RET_FAILED; 532 | 533 | if (!args || !args[0]) { 534 | gchar *buf; 535 | const gchar *topic = purple_chat_conversation_get_topic(chat); 536 | 537 | if (topic) { 538 | gchar *tmp, *tmp2; 539 | tmp = g_markup_escape_text(topic, -1); 540 | tmp2 = purple_markup_linkify(tmp); 541 | buf = g_strdup_printf(_("current topic is: %s"), tmp2); 542 | g_free(tmp); 543 | g_free(tmp2); 544 | } else { 545 | buf = g_strdup(_("No topic is set")); 546 | } 547 | 548 | purple_conversation_write_system_message(conv, buf, PURPLE_MESSAGE_NO_LOG); 549 | 550 | g_free(buf); 551 | return PURPLE_CMD_RET_OK; 552 | } 553 | 554 | teams_chat_set_topic(pc, id, args[0]); 555 | 556 | return PURPLE_CMD_RET_OK; 557 | } 558 | 559 | static PurpleCmdRet 560 | teams_cmd_call(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data) 561 | { 562 | PurpleConnection *pc = NULL; 563 | TeamsAccount *sa; 564 | const gchar *convname = NULL; 565 | 566 | pc = purple_conversation_get_connection(conv); 567 | if (pc == NULL) 568 | return PURPLE_CMD_RET_FAILED; 569 | sa = purple_connection_get_protocol_data(pc); 570 | 571 | convname = purple_conversation_get_data(conv, "chatname"); 572 | if (!convname) { 573 | convname = purple_conversation_get_name(conv); 574 | 575 | if (PURPLE_IS_IM_CONVERSATION(conv)) { 576 | if (!TEAMS_BUDDY_IS_NOTIFICATIONS(convname)) { 577 | convname = g_hash_table_lookup(sa->buddy_to_chat_lookup, convname); 578 | } 579 | } 580 | } 581 | 582 | if (!convname) 583 | return PURPLE_CMD_RET_FAILED; 584 | 585 | gchar *message = g_strconcat("https://teams.microsoft.com/l/meetup-join/", purple_url_encode(convname), "/0", NULL); 586 | 587 | PurpleMessage *msg = purple_message_new_system(message, 0); 588 | purple_conversation_write_message(conv, msg); 589 | purple_message_destroy(msg); 590 | 591 | g_free(message); 592 | 593 | return PURPLE_CMD_RET_OK; 594 | } 595 | 596 | /******************************************************************************/ 597 | /* Plugin functions */ 598 | /******************************************************************************/ 599 | 600 | static gboolean 601 | teams_uri_handler(const char *proto, const char *cmd, GHashTable *params) 602 | { 603 | //PurpleAccount *account; 604 | //PurpleConnection *pc; 605 | 606 | if (!g_str_equal(proto, "msteams")) 607 | return FALSE; 608 | 609 | // msteams:/1/channel/19%3a.....%40thread.skype/{nameofchannel}?groupId={groupId}&tenantId={tenantId} 610 | 611 | //we don't know how to handle this 612 | return FALSE; 613 | } 614 | 615 | #if PURPLE_VERSION_CHECK(3, 0, 0) 616 | typedef struct _TeamsProtocol 617 | { 618 | PurpleProtocol parent; 619 | } TeamsProtocol; 620 | 621 | typedef struct _TeamsProtocolClass 622 | { 623 | PurpleProtocolClass parent_class; 624 | } TeamsProtocolClass; 625 | 626 | G_MODULE_EXPORT GType teams_protocol_get_type(void); 627 | #define TEAMS_TYPE_PROTOCOL (teams_protocol_get_type()) 628 | #define TEAMS_PROTOCOL(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), TEAMS_TYPE_PROTOCOL, TeamsProtocol)) 629 | #define TEAMS_PROTOCOL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), TEAMS_TYPE_PROTOCOL, TeamsProtocolClass)) 630 | #define TEAMS_IS_PROTOCOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), TEAMS_TYPE_PROTOCOL)) 631 | #define TEAMS_IS_PROTOCOL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), TEAMS_TYPE_PROTOCOL)) 632 | #define TEAMS_PROTOCOL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), TEAMS_TYPE_PROTOCOL, TeamsProtocolClass)) 633 | 634 | static PurpleProtocol *teams_protocol; 635 | #else 636 | 637 | // Normally set in core.c in purple3 638 | void _purple_socket_init(void); 639 | void _purple_socket_uninit(void); 640 | 641 | #endif 642 | 643 | 644 | static gboolean 645 | plugin_load(PurplePlugin *plugin 646 | #if PURPLE_VERSION_CHECK(3, 0, 0) 647 | , GError **error 648 | #endif 649 | ) 650 | { 651 | 652 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 653 | _purple_socket_init(); 654 | purple_http_init(); 655 | #endif 656 | 657 | 658 | //leave 659 | purple_cmd_register("leave", "", PURPLE_CMD_P_PLUGIN, PURPLE_CMD_FLAG_CHAT | 660 | PURPLE_CMD_FLAG_PROTOCOL_ONLY | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, 661 | TEAMS_PLUGIN_ID, teams_cmd_leave, 662 | _("leave: Leave the group chat"), NULL); 663 | //kick 664 | purple_cmd_register("kick", "s", PURPLE_CMD_P_PLUGIN, PURPLE_CMD_FLAG_CHAT | 665 | PURPLE_CMD_FLAG_PROTOCOL_ONLY, 666 | TEAMS_PLUGIN_ID, teams_cmd_kick, 667 | _("kick <user>: Kick a user from the group chat."), 668 | NULL); 669 | //add 670 | purple_cmd_register("add", "s", PURPLE_CMD_P_PLUGIN, PURPLE_CMD_FLAG_CHAT | 671 | PURPLE_CMD_FLAG_PROTOCOL_ONLY, 672 | TEAMS_PLUGIN_ID, teams_cmd_invite, 673 | _("add <user>: Add a user to the group chat."), 674 | NULL); 675 | //topic 676 | purple_cmd_register("topic", "s", PURPLE_CMD_P_PLUGIN, PURPLE_CMD_FLAG_CHAT | 677 | PURPLE_CMD_FLAG_PROTOCOL_ONLY | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, 678 | TEAMS_PLUGIN_ID, teams_cmd_topic, 679 | _("topic [<new topic>]: View or change the topic"), 680 | NULL); 681 | /* 682 | //call, as in call person 683 | //kickban 684 | purple_cmd_register("kickban", "s", PURPLE_CMD_P_PLUGIN, PURPLE_CMD_FLAG_CHAT | 685 | PURPLE_CMD_FLAG_PROTOCOL_ONLY, 686 | TEAMS_PLUGIN_ID, teams_cmd_kickban, 687 | _("kickban <user> [room]: Kick and ban a user from the room."), 688 | NULL); 689 | //setrole 690 | purple_cmd_register("setrole", "ss", PURPLE_CMD_P_PLUGIN, PURPLE_CMD_FLAG_CHAT | 691 | PURPLE_CMD_FLAG_PROTOCOL_ONLY, 692 | TEAMS_PLUGIN_ID, teams_cmd_setrole, 693 | _("setrole <user> <MASTER | USER | ADMIN>: Change the role of a user."), 694 | NULL); 695 | */ 696 | 697 | purple_cmd_register("list", "", PURPLE_CMD_P_PLUGIN, PURPLE_CMD_FLAG_CHAT | 698 | PURPLE_CMD_FLAG_PROTOCOL_ONLY | PURPLE_CMD_FLAG_IM, 699 | TEAMS_PLUGIN_ID, teams_cmd_list, 700 | _("list: Display a list of multi-chat group chats you are in."), 701 | NULL); 702 | 703 | purple_cmd_register("call", "", PURPLE_CMD_P_PLUGIN, PURPLE_CMD_FLAG_CHAT | 704 | PURPLE_CMD_FLAG_PROTOCOL_ONLY | PURPLE_CMD_FLAG_IM, 705 | TEAMS_PLUGIN_ID, teams_cmd_call, 706 | _("call: Create a URL link to start a voice/video call in the browser."), 707 | NULL); 708 | 709 | purple_signal_connect(purple_get_core(), "uri-handler", plugin, PURPLE_CALLBACK(teams_uri_handler), NULL); 710 | 711 | return TRUE; 712 | } 713 | 714 | static gboolean 715 | plugin_unload(PurplePlugin *plugin 716 | #if PURPLE_VERSION_CHECK(3, 0, 0) 717 | , GError **error 718 | #endif 719 | ) 720 | { 721 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 722 | _purple_socket_uninit(); 723 | purple_http_uninit(); 724 | #endif 725 | purple_signals_disconnect_by_handle(plugin); 726 | 727 | return TRUE; 728 | } 729 | 730 | static GList * 731 | teams_actions( 732 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 733 | PurplePlugin *plugin, gpointer context 734 | #else 735 | PurpleConnection *pc 736 | #endif 737 | ) 738 | { 739 | GList *m = NULL; 740 | PurpleProtocolAction *act; 741 | 742 | act = purple_protocol_action_new(_("Search for friends..."), teams_search_users); 743 | m = g_list_append(m, act); 744 | 745 | return m; 746 | } 747 | 748 | #if !PURPLE_VERSION_CHECK(2, 8, 0) 749 | # define OPT_PROTO_INVITE_MESSAGE 0x00000800 750 | #endif 751 | 752 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 753 | static void 754 | plugin_init(PurplePlugin *plugin) 755 | { 756 | PurplePluginInfo *info = g_new0(PurplePluginInfo, 1); 757 | PurplePluginProtocolInfo *prpl_info = g_new0(PurplePluginProtocolInfo, 1); 758 | #endif 759 | 760 | #if PURPLE_VERSION_CHECK(3, 0, 0) 761 | static void 762 | teams_protocol_init(PurpleProtocol *prpl_info) 763 | { 764 | PurpleProtocol *info = prpl_info; 765 | #endif 766 | PurpleAccountOption *opt; 767 | PurpleBuddyIconSpec icon_spec = {"jpeg", 0, 0, 96, 96, 0, PURPLE_ICON_SCALE_DISPLAY}; 768 | 769 | //PurpleProtocol 770 | info->id = TEAMS_PLUGIN_ID; 771 | info->name = "Teams"; 772 | prpl_info->options = OPT_PROTO_NO_PASSWORD | OPT_PROTO_CHAT_TOPIC /*| OPT_PROTO_INVITE_MESSAGE*/ | OPT_PROTO_IM_IMAGE; 773 | 774 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 775 | # define TEAMS_PRPL_APPEND_ACCOUNT_OPTION(opt) prpl_info->protocol_options = g_list_append(prpl_info->protocol_options, (opt)); 776 | prpl_info->icon_spec = icon_spec; 777 | #else 778 | # define TEAMS_PRPL_APPEND_ACCOUNT_OPTION(opt) prpl_info->account_options = g_list_append(prpl_info->account_options, (opt)); 779 | prpl_info->icon_spec = &icon_spec; 780 | #endif 781 | 782 | opt = purple_account_option_string_new(_("Tenant"), "tenant", ""); 783 | TEAMS_PRPL_APPEND_ACCOUNT_OPTION(opt); 784 | 785 | opt = purple_account_option_bool_new(_("Set global status"), "set-global-status", TRUE); 786 | TEAMS_PRPL_APPEND_ACCOUNT_OPTION(opt); 787 | 788 | opt = purple_account_option_bool_new(_("Collapse Teams threads into a single chat window"), "should_collapse_threads", TRUE); 789 | TEAMS_PRPL_APPEND_ACCOUNT_OPTION(opt); 790 | 791 | #undef TEAMS_PRPL_APPEND_ACCOUNT_OPTION 792 | 793 | #if PURPLE_VERSION_CHECK(3, 0, 0) 794 | } 795 | 796 | static void 797 | teams_protocol_class_init(PurpleProtocolClass *prpl_info) 798 | { 799 | #endif 800 | //PurpleProtocolClass 801 | prpl_info->login = teams_login; 802 | prpl_info->close = teams_close; 803 | prpl_info->status_types = teams_status_types; 804 | prpl_info->list_icon = teams_list_icon; 805 | #if PURPLE_VERSION_CHECK(3, 0, 0) 806 | } 807 | 808 | static void 809 | teams_protocol_client_iface_init(PurpleProtocolClientIface *prpl_info) 810 | { 811 | PurpleProtocolClientIface *info = prpl_info; 812 | #endif 813 | 814 | //PurpleProtocolClientIface 815 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 816 | info->actions = teams_actions; 817 | #else 818 | info->get_actions = teams_actions; 819 | #endif 820 | prpl_info->list_emblem = teams_list_emblem; 821 | prpl_info->status_text = teams_status_text; 822 | prpl_info->tooltip_text = teams_tooltip_text; 823 | prpl_info->blist_node_menu = teams_node_menu; 824 | prpl_info->buddy_free = teams_buddy_free; 825 | prpl_info->normalize = purple_normalize_nocase; 826 | prpl_info->offline_message = teams_offline_message; 827 | prpl_info->get_account_text_table = NULL; // teams_get_account_text_table; 828 | #if PURPLE_VERSION_CHECK(3, 0, 0) 829 | } 830 | 831 | static void 832 | teams_protocol_server_iface_init(PurpleProtocolServerIface *prpl_info) 833 | { 834 | #endif 835 | 836 | //PurpleProtocolServerIface 837 | prpl_info->get_info = teams_get_info; 838 | prpl_info->set_status = teams_set_status; 839 | prpl_info->set_idle = teams_set_idle; 840 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 841 | prpl_info->add_buddy = teams_add_buddy; 842 | #else 843 | prpl_info->add_buddy = teams_add_buddy_with_invite; 844 | #endif 845 | prpl_info->remove_buddy = teams_buddy_remove; 846 | prpl_info->group_buddy = teams_fake_group_buddy; 847 | prpl_info->rename_group = teams_fake_group_rename; 848 | #if PURPLE_VERSION_CHECK(3, 0, 0) 849 | } 850 | 851 | static void 852 | teams_protocol_im_iface_init(PurpleProtocolIMIface *prpl_info) 853 | { 854 | #endif 855 | 856 | //PurpleProtocolIMIface 857 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 858 | prpl_info->send_im = teams_send_im; 859 | #else 860 | prpl_info->send = teams_send_im; 861 | #endif 862 | prpl_info->send_typing = teams_send_typing; 863 | #if PURPLE_VERSION_CHECK(3, 0, 0) 864 | } 865 | 866 | static void 867 | teams_protocol_chat_iface_init(PurpleProtocolChatIface *prpl_info) 868 | { 869 | #endif 870 | 871 | //PurpleProtocolChatIface 872 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 873 | prpl_info->chat_info = teams_chat_info; 874 | prpl_info->chat_info_defaults = teams_chat_info_defaults; 875 | prpl_info->join_chat = teams_join_chat; 876 | prpl_info->get_chat_name = teams_get_chat_name; 877 | prpl_info->chat_invite = teams_chat_invite; 878 | prpl_info->chat_leave = NULL; //teams_chat_fake_leave; 879 | prpl_info->chat_send = teams_chat_send; 880 | prpl_info->set_chat_topic = teams_chat_set_topic; 881 | #else 882 | prpl_info->info = teams_chat_info; 883 | prpl_info->info_defaults = teams_chat_info_defaults; 884 | prpl_info->join = teams_join_chat; 885 | prpl_info->get_name = teams_get_chat_name; 886 | prpl_info->invite = teams_chat_invite; 887 | prpl_info->leave = NULL; //teams_chat_fake_leave; 888 | prpl_info->send = teams_chat_send; 889 | prpl_info->set_topic = teams_chat_set_topic; 890 | #endif 891 | #if PURPLE_VERSION_CHECK(3, 0, 0) 892 | } 893 | 894 | static void 895 | teams_protocol_privacy_iface_init(PurpleProtocolPrivacyIface *prpl_info) 896 | { 897 | #endif 898 | 899 | //PurpleProtocolPrivacyIface 900 | prpl_info->add_deny = teams_buddy_block; 901 | prpl_info->rem_deny = teams_buddy_unblock; 902 | #if PURPLE_VERSION_CHECK(3, 0, 0) 903 | } 904 | 905 | static void 906 | teams_protocol_xfer_iface_init(PurpleProtocolXferInterface *prpl_info) 907 | { 908 | #endif 909 | 910 | //PurpleProtocolXferInterface 911 | prpl_info->new_xfer = teams_new_xfer; 912 | prpl_info->send_file = teams_send_file; 913 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 914 | prpl_info->can_receive_file = teams_can_receive_file; 915 | #else 916 | prpl_info->can_receive = teams_can_receive_file; 917 | #endif 918 | 919 | #if PURPLE_VERSION_CHECK(3, 0, 0) 920 | } 921 | 922 | static void 923 | teams_protocol_roomlist_iface_init(PurpleProtocolRoomlistIface *prpl_info) 924 | { 925 | #endif 926 | 927 | //PurpleProtocolRoomlistIface 928 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 929 | prpl_info->roomlist_get_list = teams_roomlist_get_list; 930 | #else 931 | prpl_info->get_list = teams_roomlist_get_list; 932 | #endif 933 | 934 | #if !PURPLE_VERSION_CHECK(3, 0, 0) 935 | // Plugin info 936 | info->magic = PURPLE_PLUGIN_MAGIC; 937 | info->major_version = 2; 938 | info->minor_version = MIN(PURPLE_MINOR_VERSION, 8); 939 | info->type = PURPLE_PLUGIN_PROTOCOL; 940 | info->priority = PURPLE_PRIORITY_DEFAULT; 941 | info->version = TEAMS_PLUGIN_VERSION; 942 | info->summary = N_("Teams Protocol Plugin"); 943 | info->description = N_("Teams Protocol Plugin"); 944 | info->author = "Eion Robb "; 945 | info->homepage = "http://github.com/EionRobb/purple-teams"; 946 | info->load = plugin_load; 947 | info->unload = plugin_unload; 948 | info->extra_info = prpl_info; 949 | 950 | // Protocol info 951 | #if PURPLE_MINOR_VERSION >= 5 952 | prpl_info->struct_size = sizeof(PurplePluginProtocolInfo); 953 | #endif 954 | #if PURPLE_MINOR_VERSION >= 8 955 | prpl_info->add_buddy_with_invite = teams_add_buddy_with_invite; 956 | #endif 957 | 958 | plugin->info = info; 959 | #endif 960 | 961 | } 962 | 963 | #if PURPLE_VERSION_CHECK(3, 0, 0) 964 | 965 | 966 | PURPLE_DEFINE_TYPE_EXTENDED( 967 | TeamsProtocol, teams_protocol, PURPLE_TYPE_PROTOCOL, 0, 968 | 969 | PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_CLIENT_IFACE, 970 | teams_protocol_client_iface_init) 971 | 972 | PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_SERVER_IFACE, 973 | teams_protocol_server_iface_init) 974 | 975 | PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_IM_IFACE, 976 | teams_protocol_im_iface_init) 977 | 978 | PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_CHAT_IFACE, 979 | teams_protocol_chat_iface_init) 980 | 981 | PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_PRIVACY_IFACE, 982 | teams_protocol_privacy_iface_init) 983 | 984 | PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_ROOMLIST_IFACE, 985 | teams_protocol_roomlist_iface_init) 986 | 987 | PURPLE_IMPLEMENT_INTERFACE_STATIC(PURPLE_TYPE_PROTOCOL_XFER, 988 | teams_protocol_xfer_iface_init) 989 | ); 990 | 991 | static gboolean 992 | libpurple3_plugin_load(PurplePlugin *plugin, GError **error) 993 | { 994 | teams_protocol_register_type(plugin); 995 | teams_protocol = purple_protocols_add(TEAMS_TYPE_PROTOCOL, error); 996 | if (!teams_protocol) 997 | return FALSE; 998 | 999 | return plugin_load(plugin, error); 1000 | } 1001 | 1002 | static gboolean 1003 | libpurple3_plugin_unload(PurplePlugin *plugin, GError **error) 1004 | { 1005 | if (!plugin_unload(plugin, error)) 1006 | return FALSE; 1007 | 1008 | if (!purple_protocols_remove(teams_protocol, error)) 1009 | return FALSE; 1010 | 1011 | return TRUE; 1012 | } 1013 | 1014 | static PurplePluginInfo * 1015 | plugin_query(GError **error) 1016 | { 1017 | return purple_plugin_info_new( 1018 | "id", TEAMS_PLUGIN_ID, 1019 | "name", "Teams Protocol", 1020 | "version", TEAMS_PLUGIN_VERSION, 1021 | "category", N_("Protocol"), 1022 | "summary", N_("Teams Protocol Plugin"), 1023 | "description", N_("Teams Protocol Plugin"), 1024 | "website", "http://github.com/EionRobb/purple-teams", 1025 | "abi-version", PURPLE_ABI_VERSION, 1026 | "flags", PURPLE_PLUGIN_INFO_FLAGS_INTERNAL | 1027 | PURPLE_PLUGIN_INFO_FLAGS_AUTO_LOAD, 1028 | NULL 1029 | ); 1030 | } 1031 | 1032 | 1033 | PURPLE_PLUGIN_INIT(teams, plugin_query, libpurple3_plugin_load, libpurple3_plugin_unload); 1034 | #else 1035 | 1036 | static PurplePluginInfo aLovelyBunchOfCoconuts; 1037 | PURPLE_INIT_PLUGIN(teams, plugin_init, aLovelyBunchOfCoconuts); 1038 | #endif 1039 | 1040 | -------------------------------------------------------------------------------- /gpl3.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {{ project }} Copyright (C) {{ year }} {{ organization }} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . -------------------------------------------------------------------------------- /teams_login.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Teams Plugin for libpurple/Pidgin 3 | * Copyright (c) 2014-2020 Eion Robb 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 3 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, see . 17 | */ 18 | 19 | #include "teams_login.h" 20 | #include "teams_util.h" 21 | 22 | 23 | #define TEAMS_GUID_REGEX_PATTERN "^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$" 24 | 25 | static void 26 | teams_login_did_auth(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 27 | { 28 | gchar *refresh_token = NULL; 29 | TeamsAccount *sa = user_data; 30 | const gchar *data; 31 | gsize len; 32 | 33 | data = purple_http_response_get_data(response, &len); 34 | 35 | if (data != NULL) { 36 | refresh_token = teams_string_get_chunk(data, len, "=\"skypetoken\" value=\"", "\""); 37 | } else { 38 | purple_connection_error(sa->pc, 39 | PURPLE_CONNECTION_ERROR_NETWORK_ERROR, 40 | _("Failed getting Skype Token, please try logging in via browser first")); 41 | return; 42 | } 43 | 44 | if (refresh_token == NULL) { 45 | purple_account_set_string(sa->account, "refresh-token", NULL); 46 | if (g_strstr_len(data, len, "recaptcha_response_field")) { 47 | purple_connection_error(sa->pc, 48 | PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, 49 | _("Captcha required.\nTry logging into web.skype.com and try again.")); 50 | return; 51 | } else { 52 | purple_debug_info("teams", "login response was %s\r\n", data); 53 | purple_connection_error(sa->pc, 54 | PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, 55 | _("Failed getting Skype Token, please try logging in via browser first")); 56 | return; 57 | } 58 | } 59 | 60 | sa->skype_token = refresh_token; 61 | 62 | if (purple_account_get_remember_password(sa->account)) { 63 | purple_account_set_string(sa->account, "refresh-token", purple_http_cookie_jar_get(sa->cookie_jar, "refresh-token")); 64 | } 65 | 66 | teams_do_all_the_things(sa); 67 | } 68 | 69 | static void 70 | teams_login_got_pie(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 71 | { 72 | TeamsAccount *sa = user_data; 73 | PurpleAccount *account = sa->account; 74 | gchar *pie; 75 | gchar *etm; 76 | const gchar *login_url = "https://" TEAMS_LOGIN_HOST "/login?client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com"; 77 | GString *postdata; 78 | struct timeval tv; 79 | struct timezone tz; 80 | gint tzhours, tzminutes; 81 | int tmplen; 82 | PurpleHttpRequest *request; 83 | const gchar *data; 84 | gsize len; 85 | 86 | if (!purple_http_response_is_successful(response)) { 87 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, purple_http_response_get_error(response)); 88 | return; 89 | } 90 | 91 | data = purple_http_response_get_data(response, &len); 92 | 93 | gettimeofday(&tv, &tz); 94 | (void) tv; 95 | tzminutes = tz.tz_minuteswest; 96 | if (tzminutes < 0) tzminutes = -tzminutes; 97 | tzhours = tzminutes / 60; 98 | tzminutes -= tzhours * 60; 99 | 100 | pie = teams_string_get_chunk(data, len, "=\"pie\" value=\"", "\""); 101 | if (!pie) { 102 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Failed getting PIE value, please try logging in via browser first")); 103 | return; 104 | } 105 | 106 | etm = teams_string_get_chunk(data, len, "=\"etm\" value=\"", "\""); 107 | if (!etm) { 108 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Failed getting ETM value, please try logging in via browser first")); 109 | return; 110 | } 111 | 112 | 113 | postdata = g_string_new(""); 114 | g_string_append_printf(postdata, "username=%s&", purple_url_encode(purple_account_get_username(account))); 115 | g_string_append_printf(postdata, "password=%s&", purple_url_encode(purple_connection_get_password(sa->pc))); 116 | g_string_append_printf(postdata, "timezone_field=%c|%d|%d&", (tz.tz_minuteswest < 0 ? '+' : '-'), tzhours, tzminutes); 117 | g_string_append_printf(postdata, "pie=%s&", purple_url_encode(pie)); 118 | g_string_append_printf(postdata, "etm=%s&", purple_url_encode(etm)); 119 | g_string_append_printf(postdata, "js_time=%" G_GINT64_FORMAT "&", teams_get_js_time()); 120 | g_string_append(postdata, "client_id=578134&"); 121 | g_string_append(postdata, "redirect_uri=https://web.skype.com/"); 122 | 123 | tmplen = postdata->len; 124 | if (postdata->len > INT_MAX) tmplen = INT_MAX; 125 | 126 | request = purple_http_request_new(login_url); 127 | purple_http_request_set_method(request, "POST"); 128 | purple_http_request_set_cookie_jar(request, sa->cookie_jar); 129 | purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 130 | purple_http_request_header_set(request, "Accept", "*/*"); 131 | purple_http_request_header_set(request, "BehaviorOverride", "redirectAs404"); 132 | purple_http_request_set_contents(request, postdata->str, tmplen); 133 | purple_http_request(sa->pc, request, teams_login_did_auth, sa); 134 | purple_http_request_unref(request); 135 | 136 | g_string_free(postdata, TRUE); 137 | g_free(pie); 138 | g_free(etm); 139 | 140 | purple_connection_update_progress(sa->pc, _("Authenticating"), 2, 4); 141 | } 142 | 143 | void 144 | teams_begin_web_login(TeamsAccount *sa) 145 | { 146 | const gchar *login_url = "https://" TEAMS_LOGIN_HOST "/login?method=skype&client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com"; 147 | 148 | purple_http_get(sa->pc, teams_login_got_pie, sa, login_url); 149 | 150 | purple_connection_set_state(sa->pc, PURPLE_CONNECTION_CONNECTING); 151 | purple_connection_update_progress(sa->pc, _("Connecting"), 1, 4); 152 | } 153 | 154 | static void 155 | teams_login_got_t(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 156 | { 157 | TeamsAccount *sa = user_data; 158 | const gchar *login_url = "https://" TEAMS_LOGIN_HOST "/login/microsoft"; 159 | PurpleHttpRequest *request; 160 | GString *postdata; 161 | gchar *magic_t_value; // T is for tasty 162 | gchar *error_code; 163 | gchar *error_text; 164 | int tmplen; 165 | const gchar *data; 166 | gsize len; 167 | 168 | data = purple_http_response_get_data(response, &len); 169 | 170 | // 171 | error_text = teams_string_get_chunk(data, len, ",sErrTxt:'", "',Am:'"); 172 | error_code = teams_string_get_chunk(data, len, ",sErrorCode:'", "',Ag:"); 173 | magic_t_value = teams_string_get_chunk(data, len, "=\"t\" value=\"", "\""); 174 | 175 | if (!magic_t_value) { 176 | //No Magic T???? Maybe it be the mighty 2fa-beast 177 | 178 | if (FALSE) 179 | /*if (g_strnstr(data, len, "Set-Cookie: LOpt=0;"))*/ { 180 | //XX - Would this be better retrieved with JSON decoding the "var ServerData = {...}" code? 181 | // 182 | gchar *session_state = teams_string_get_chunk(data, len, ":'https://login.live.com/GetSessionState.srf?", "',"); 183 | if (session_state) { 184 | //These two appear to have different object keys each request :( 185 | /* 186 | gchar *PPFT = teams_string_get_chunk(data, len, ",sFT:'", "',"); 187 | gchar *SLK = teams_string_get_chunk(data, len, ",aB:'", "',"); 188 | gchar *ppauth_cookie = teams_string_get_chunk(data, len, "Set-Cookie: PPAuth=", ";"); 189 | gchar *mspok_cookie = teams_string_get_chunk(data, len, "Set-Cookie: MSPOK=", "; domain="); 190 | */ 191 | 192 | //Poll https://login.live.com/GetSessionState.srv?{session_state} to retrieve GIF(!!) of 2fa status 193 | //1x1 size GIF means pending, 2x2 rejected, 1x2 approved 194 | //Then re-request the MagicT, if approved with a slightly different GET parameters 195 | //purpose=eOTT_OneTimePassword&PPFT={ppft}&login={email}&SLK={slk} 196 | return; 197 | } 198 | } 199 | 200 | if (error_text) { 201 | GString *new_error; 202 | new_error = g_string_new(""); 203 | g_string_append_printf(new_error, "%s: ", error_code); 204 | g_string_append_printf(new_error, "%s", error_text); 205 | 206 | gchar *error_msg = g_string_free(new_error, FALSE); 207 | 208 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, error_msg); 209 | g_free (error_msg); 210 | return; 211 | } 212 | 213 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Failed getting Magic T value, please try logging in via browser first")); 214 | 215 | return; 216 | } 217 | 218 | // postdata: t=...&oauthPartner=999&client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com 219 | postdata = g_string_new(""); 220 | g_string_append_printf(postdata, "t=%s&", purple_url_encode(magic_t_value)); 221 | g_string_append(postdata, "site_name=lw.skype.com&"); 222 | g_string_append(postdata, "oauthPartner=999&"); 223 | g_string_append(postdata, "client_id=578134&"); 224 | g_string_append(postdata, "redirect_uri=https%3A%2F%2Fweb.skype.com"); 225 | 226 | tmplen = postdata->len; 227 | if (postdata->len > INT_MAX) tmplen = INT_MAX; 228 | 229 | // post the t to https://login.skype.com/login/oauth?client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com 230 | 231 | request = purple_http_request_new(login_url); 232 | purple_http_request_set_method(request, "POST"); 233 | purple_http_request_set_cookie_jar(request, sa->cookie_jar); 234 | purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 235 | purple_http_request_header_set(request, "Accept", "*/*"); 236 | purple_http_request_header_set(request, "BehaviorOverride", "redirectAs404"); 237 | purple_http_request_set_contents(request, postdata->str, tmplen); 238 | purple_http_request_set_max_redirects(request, 0); 239 | purple_http_request(sa->pc, request, teams_login_did_auth, sa); 240 | purple_http_request_unref(request); 241 | 242 | g_string_free(postdata, TRUE); 243 | g_free(magic_t_value); 244 | 245 | purple_connection_update_progress(sa->pc, _("Verifying"), 3, 4); 246 | } 247 | 248 | static void 249 | teams_login_got_opid(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 250 | { 251 | TeamsAccount *sa = user_data; 252 | const gchar *live_login_url = "https://login.live.com" "/ppsecure/post.srf?wa=wsignin1.0&wp=MBI_SSL&wreply=https%3A%2F%2Flw.skype.com%2Flogin%2Foauth%2Fproxy%3Fsite_name%3Dlw.skype.com"; 253 | gchar *ppft; 254 | gchar *opid; 255 | GString *postdata; 256 | PurpleHttpRequest *request; 257 | int tmplen; 258 | const gchar *data; 259 | gsize len; 260 | 261 | data = purple_http_response_get_data(response, &len); 262 | 263 | ppft = teams_string_get_chunk(data, len, ",sFT:'", "',"); 264 | if (!ppft) { 265 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Failed getting PPFT value, please try logging in via browser first")); 266 | return; 267 | } 268 | opid = teams_string_get_chunk(data, len, "&opid=", "'"); 269 | if (!opid) { 270 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Failed getting opid value, try using 'Alternate Auth Method' settings")); 271 | return; 272 | } 273 | postdata = g_string_new(""); 274 | g_string_append_printf(postdata, "opid=%s&", purple_url_encode(opid)); 275 | g_string_append(postdata, "site_name=lw.skype.com&"); 276 | g_string_append(postdata, "oauthPartner=999&"); 277 | g_string_append(postdata, "client_id=578134&"); 278 | g_string_append(postdata, "redirect_uri=https%3A%2F%2Fweb.skype.com&"); 279 | g_string_append_printf(postdata, "PPFT=%s&", purple_url_encode(ppft)); 280 | g_string_append(postdata, "type=28&"); 281 | 282 | tmplen = postdata->len; 283 | if (postdata->len > INT_MAX) tmplen = INT_MAX; 284 | 285 | request = purple_http_request_new(live_login_url); 286 | purple_http_request_set_method(request, "POST"); 287 | purple_http_request_set_cookie_jar(request, sa->cookie_jar); 288 | purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 289 | purple_http_request_header_set(request, "Accept", "*/*"); 290 | purple_http_request_set_contents(request, postdata->str, tmplen); 291 | purple_http_request(sa->pc, request, teams_login_got_t, sa); 292 | purple_http_request_unref(request); 293 | 294 | g_string_free(postdata, TRUE); 295 | 296 | g_free(ppft); 297 | g_free(opid); 298 | 299 | purple_connection_update_progress(sa->pc, _("Authenticating"), 2, 4); 300 | } 301 | 302 | static void 303 | teams_login_got_ppft(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 304 | { 305 | TeamsAccount *sa = user_data; 306 | const gchar *live_login_url = "https://login.live.com" "/ppsecure/post.srf?wa=wsignin1.0&wp=MBI_SSL&wreply=https%3A%2F%2Flw.skype.com%2Flogin%2Foauth%2Fproxy%3Fsite_name%3Dlw.skype.com"; 307 | gchar *cktst_cookie; 308 | gchar *ppft; 309 | GString *postdata; 310 | PurpleHttpRequest *request; 311 | int tmplen; 312 | const gchar *data; 313 | gsize len; 314 | 315 | data = purple_http_response_get_data(response, &len); 316 | 317 | // 318 | ppft = teams_string_get_chunk(data, len, "name=\"PPFT\" id=\"i0327\" value=\"", "\""); 319 | if (!ppft) { 320 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Failed getting PPFT value, please try logging in via browser first")); 321 | return; 322 | } 323 | // CkTst=G + timestamp e.g. G1422309314913 324 | cktst_cookie = g_strdup_printf("G%" G_GINT64_FORMAT, teams_get_js_time()); 325 | purple_http_cookie_jar_set(sa->cookie_jar, "CkTst", cktst_cookie); 326 | 327 | // postdata: login={username}&passwd={password}&PPFT={ppft value} 328 | postdata = g_string_new(""); 329 | g_string_append_printf(postdata, "login=%s&", purple_url_encode(purple_account_get_username(sa->account))); 330 | g_string_append_printf(postdata, "passwd=%s&", purple_url_encode(purple_connection_get_password(sa->pc))); 331 | g_string_append_printf(postdata, "PPFT=%s&", purple_url_encode(ppft)); 332 | g_string_append(postdata, "loginoptions=3&"); 333 | 334 | tmplen = postdata->len; 335 | if (postdata->len > INT_MAX) tmplen = INT_MAX; 336 | 337 | // POST to https://login.live.com/ppsecure/post.srf?wa=wsignin1.0&wreply=https%3A%2F%2Fsecure.skype.com%2Flogin%2Foauth%2Fproxy%3Fclient_id%3D578134%26redirect_uri%3Dhttps%253A%252F%252Fweb.skype.com 338 | 339 | request = purple_http_request_new(live_login_url); 340 | purple_http_request_set_method(request, "POST"); 341 | purple_http_request_set_cookie_jar(request, sa->cookie_jar); 342 | purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 343 | purple_http_request_header_set(request, "Accept", "*/*"); 344 | purple_http_request_set_contents(request, postdata->str, tmplen); 345 | purple_http_request(sa->pc, request, teams_login_got_opid, sa); 346 | purple_http_request_unref(request); 347 | 348 | g_string_free(postdata, TRUE); 349 | 350 | g_free(cktst_cookie); 351 | g_free(ppft); 352 | 353 | purple_connection_update_progress(sa->pc, _("Authenticating"), 2, 4); 354 | } 355 | 356 | void 357 | teams_begin_oauth_login(TeamsAccount *sa) 358 | { 359 | const gchar *login_url = "https://" TEAMS_LOGIN_HOST "/login/oauth/microsoft?client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com"; 360 | PurpleHttpRequest *request; 361 | 362 | request = purple_http_request_new(login_url); 363 | purple_http_request_set_cookie_jar(request, sa->cookie_jar); 364 | purple_http_request(sa->pc, request, teams_login_got_ppft, sa); 365 | purple_http_request_unref(request); 366 | 367 | purple_connection_set_state(sa->pc, PURPLE_CONNECTION_CONNECTING); 368 | purple_connection_update_progress(sa->pc, _("Connecting"), 1, 4); 369 | } 370 | 371 | void 372 | teams_logout(TeamsAccount *sa) 373 | { 374 | teams_post_or_get(sa, TEAMS_METHOD_GET | TEAMS_METHOD_SSL, TEAMS_LOGIN_HOST, "/logout", NULL, NULL, NULL, TRUE); 375 | } 376 | 377 | 378 | 379 | typedef struct { 380 | gpointer unused1; 381 | gpointer unused2; 382 | gpointer unused3; 383 | gpointer unused4; 384 | gpointer unused5; 385 | int unused6; 386 | int unused7; 387 | int unused8; 388 | int unused9; 389 | 390 | gpointer set; 391 | } bitlbee_account_t; 392 | 393 | typedef struct { 394 | bitlbee_account_t *acc; 395 | } bitlbee_im_connection; 396 | 397 | static gpointer bitlbee_module; 398 | static bitlbee_im_connection *(*bitlbee_purple_ic_by_pa)(PurpleAccount *); 399 | static int (*bitlbee_set_setstr)(gpointer *, const char *, const char *); 400 | static gboolean bitlbee_password_funcs_loaded = FALSE; 401 | 402 | #ifdef _WIN32 403 | #ifndef dlerror 404 | static gchar *last_dlopen_error = NULL; 405 | # define dlerror() (g_free(last_dlopen_error),last_dlopen_error=g_win32_error_message(GetLastError())) 406 | #endif 407 | #endif 408 | 409 | static void 410 | save_bitlbee_password(PurpleAccount *account, const gchar *password) 411 | { 412 | bitlbee_account_t *acc; 413 | bitlbee_im_connection *imconn; 414 | 415 | gboolean result = GPOINTER_TO_INT(purple_signal_emit_return_1(purple_accounts_get_handle(), "bitlbee-set-account-password", account, password)); 416 | 417 | if (result) { 418 | return; 419 | } 420 | 421 | if (bitlbee_password_funcs_loaded == FALSE) { 422 | bitlbee_module = dlopen(NULL, RTLD_LAZY); 423 | if (bitlbee_module == NULL) { 424 | purple_debug_error("googlechat", "Couldn't acquire address of bitlbee handle: %s\n", dlerror()); 425 | g_return_if_fail(bitlbee_module); 426 | } 427 | 428 | bitlbee_purple_ic_by_pa = (gpointer) dlsym(bitlbee_module, "purple_ic_by_pa"); 429 | bitlbee_set_setstr = (gpointer) dlsym(bitlbee_module, "set_setstr"); 430 | 431 | bitlbee_password_funcs_loaded = TRUE; 432 | } 433 | 434 | imconn = bitlbee_purple_ic_by_pa(account); 435 | acc = imconn->acc; 436 | bitlbee_set_setstr(&acc->set, "password", password ? password : ""); 437 | } 438 | 439 | 440 | 441 | static void 442 | teams_save_refresh_token_password(PurpleAccount *account, const gchar *password) 443 | { 444 | purple_account_set_password(account, password, NULL, NULL); 445 | 446 | if (g_strcmp0(purple_core_get_ui(), "BitlBee") == 0) { 447 | save_bitlbee_password(account, password); 448 | } 449 | } 450 | 451 | void 452 | teams_refresh_token_login(TeamsAccount *sa) 453 | { 454 | PurpleAccount *account = sa->account; 455 | const gchar *login_url = "https://" TEAMS_LOGIN_HOST "/login?client_id=578134&redirect_uri=https%3A%2F%2Fweb.skype.com"; 456 | PurpleHttpRequest *request; 457 | 458 | request = purple_http_request_new(login_url); 459 | purple_http_request_set_method(request, "GET"); 460 | purple_http_request_set_keepalive_pool(request, sa->keepalive_pool); 461 | purple_http_request_header_set(request, "Accept", "*/*"); 462 | purple_http_request_header_set(request, "BehaviorOverride", "redirectAs404"); 463 | purple_http_request_header_set_printf(request, "Cookie", "refresh-token=%s", purple_account_get_string(account, "refresh-token", "")); 464 | purple_http_request(sa->pc, request, teams_login_did_auth, sa); 465 | purple_http_request_unref(request); 466 | 467 | purple_connection_update_progress(sa->pc, _("Authenticating"), 2, 4); 468 | } 469 | 470 | 471 | static void 472 | teams_login_did_got_api_skypetoken(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 473 | { 474 | TeamsAccount *sa = user_data; 475 | const gchar *data; 476 | gsize len; 477 | JsonObject *obj, *tokens; 478 | gchar *error = NULL; 479 | PurpleConnectionError error_type = PURPLE_CONNECTION_ERROR_NETWORK_ERROR; 480 | 481 | data = purple_http_response_get_data(response, &len); 482 | 483 | //purple_debug_misc("teams", "Full skypetoken response: %s\n", data); 484 | 485 | obj = json_decode_object(data, len); 486 | 487 | if (!json_object_has_member(obj, "tokens")) { 488 | JsonObject *status = json_object_get_object_member(obj, "status"); 489 | 490 | if (status) { 491 | //{"status":{"code":40120,"text":"Authentication failed. Bad username or password."}} 492 | error = g_strdup_printf(_("Login error: %s (code %" G_GINT64_FORMAT ")"), 493 | json_object_get_string_member(status, "text"), 494 | json_object_get_int_member(status, "code") 495 | ); 496 | error_type = PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED; 497 | 498 | } else { 499 | //{"errorCode":"UserLicenseNotPresentForbidden","message":"User Login. Teams is disabled in user licenses"} 500 | error = g_strdup_printf(_("Login error: %s (code %" G_GINT64_FORMAT ")"), 501 | json_object_get_string_member(obj, "message"), 502 | json_object_get_int_member(obj, "errorCode") 503 | ); 504 | error_type = PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED; 505 | 506 | } 507 | goto fail; 508 | } 509 | 510 | tokens = json_object_get_object_member(obj, "tokens"); 511 | 512 | if (sa->skype_token) g_free(sa->skype_token); 513 | sa->skype_token = g_strdup(json_object_get_string_member(tokens, "skypeToken")); 514 | 515 | gint64 expiresIn = json_object_get_int_member(tokens, "expiresIn"); 516 | if (sa->refresh_token_timeout) 517 | g_source_remove(sa->refresh_token_timeout); 518 | sa->refresh_token_timeout = g_timeout_add_seconds(expiresIn - 5, (GSourceFunc)teams_oauth_refresh_token, sa); 519 | //set_timeout 520 | 521 | if (sa->region) g_free(sa->region); 522 | sa->region = g_strdup(json_object_get_string_member(obj, "region")); 523 | 524 | teams_do_all_the_things(sa); 525 | 526 | return; 527 | fail: 528 | purple_connection_error(sa->pc, error_type, 529 | error ? error : _("Failed getting Skype Token (alt)")); 530 | 531 | g_free(error); 532 | } 533 | 534 | static void 535 | teams_login_get_api_skypetoken(TeamsAccount *sa, const gchar *url, const gchar *username, const gchar *password) 536 | { 537 | PurpleHttpRequest *request; 538 | JsonObject *obj = NULL; 539 | gchar *postdata = NULL; 540 | 541 | if (url == NULL) { 542 | url = "https://teams.microsoft.com/api/authsvc/v1.0/authz"; 543 | } 544 | 545 | request = purple_http_request_new(url); 546 | purple_http_request_set_method(request, "POST"); 547 | 548 | obj = json_object_new(); 549 | 550 | if (username) { 551 | json_object_set_string_member(obj, "username", username); 552 | json_object_set_string_member(obj, "passwordHash", password); 553 | json_object_set_string_member(obj, "scopes", "client"); 554 | postdata = teams_jsonobj_to_string(obj); 555 | purple_http_request_set_contents(request, postdata ? postdata : "", -1); 556 | purple_http_request_header_set(request, "Content-Type", "application/json"); 557 | } else { 558 | purple_http_request_header_set_printf(request, "Authorization", "Bearer %s", password); 559 | } 560 | 561 | purple_http_request_header_set(request, "Accept", "application/json; ver=1.0"); 562 | purple_http_request(sa->pc, request, teams_login_did_got_api_skypetoken, sa); 563 | purple_http_request_unref(request); 564 | 565 | g_free(postdata); 566 | json_object_unref(obj); 567 | } 568 | 569 | static void 570 | teams_login_soap_got_token(TeamsAccount *sa, gchar *token) 571 | { 572 | const gchar *login_url = NULL;//"https://edge.skype.com/rps/v1/rps/skypetoken"; //TODO equiv? 573 | 574 | teams_login_get_api_skypetoken(sa, login_url, NULL, token); 575 | } 576 | 577 | static void 578 | teams_login_did_soap(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 579 | { 580 | TeamsAccount *sa = user_data; 581 | const gchar *data; 582 | gsize len; 583 | PurpleXmlNode *envelope, *main_node, *node, *fault; 584 | gchar *token; 585 | const char *error = NULL; 586 | 587 | data = purple_http_response_get_data(response, &len); 588 | envelope = purple_xmlnode_from_str(data, len); 589 | 590 | if (!data) { 591 | error = _("Error parsing SOAP response"); 592 | goto fail; 593 | } 594 | 595 | main_node = purple_xmlnode_get_child(envelope, "Body/RequestSecurityTokenResponseCollection/RequestSecurityTokenResponse"); 596 | 597 | if ((fault = purple_xmlnode_get_child(envelope, "Fault")) || 598 | (main_node && (fault = purple_xmlnode_get_child(main_node, "Fault")))) { 599 | gchar *code, *string, *error_; 600 | 601 | code = purple_xmlnode_get_data(purple_xmlnode_get_child(fault, "faultcode")); 602 | string = purple_xmlnode_get_data(purple_xmlnode_get_child(fault, "faultstring")); 603 | 604 | if (purple_strequal(code, "wsse:FailedAuthentication")) { 605 | error_ = g_strdup_printf(_("Login error: Bad username or password (%s)"), string); 606 | } else { 607 | error_ = g_strdup_printf(_("Login error: %s - %s"), code, string); 608 | } 609 | 610 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, error_); 611 | 612 | g_free(code); 613 | g_free(string); 614 | g_free(error_); 615 | goto fail; 616 | } 617 | 618 | node = purple_xmlnode_get_child(main_node, "RequestedSecurityToken/BinarySecurityToken"); 619 | 620 | if (!node) { 621 | error = _("Error getting BinarySecurityToken"); 622 | goto fail; 623 | } 624 | 625 | token = purple_xmlnode_get_data(node); 626 | teams_login_soap_got_token(sa, token); 627 | g_free(token); 628 | 629 | fail: 630 | if (error) { 631 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, error); 632 | } 633 | purple_xmlnode_free(envelope); 634 | return; 635 | } 636 | 637 | #define SIMPLE_OBJECT_ACCESS_PROTOCOL \ 638 | "\n" \ 644 | "
\n" \ 645 | " \n" \ 646 | " \n" \ 647 | " %s\n" \ 648 | " %s\n" \ 649 | " \n" \ 650 | " \n" \ 651 | "
\n" \ 652 | " \n" \ 653 | " \n" \ 654 | " \n" \ 655 | " http://schemas.xmlsoap.org/ws/2004/04/security/trust/Issue\n" \ 656 | " \n" \ 657 | " \n" \ 658 | " https://teams.events.data.microsoft.com/OneCollector/1.0/\n" \ 659 | " \n" \ 660 | " \n" \ 661 | " \n" \ 662 | " \n" \ 663 | " \n" \ 664 | " \n" \ 665 | "
" \ 666 | 667 | void 668 | teams_begin_soapy_login(TeamsAccount *sa) 669 | { 670 | PurpleAccount *account = sa->account; 671 | const gchar *login_url = "https://login.live.com/RST.srf"; 672 | const gchar *template = SIMPLE_OBJECT_ACCESS_PROTOCOL; 673 | gchar *postdata; 674 | PurpleHttpRequest *request; 675 | 676 | postdata = g_markup_printf_escaped(template, 677 | purple_account_get_username(account), 678 | purple_connection_get_password(sa->pc) 679 | ); 680 | 681 | request = purple_http_request_new(login_url); 682 | purple_http_request_set_method(request, "POST"); 683 | purple_http_request_set_contents(request, postdata, -1); 684 | purple_http_request_header_set(request, "Accept", "*/*"); 685 | purple_http_request_header_set(request, "Content-Type", "text/xml; charset=UTF-8"); 686 | purple_http_request(sa->pc, request, teams_login_did_soap, sa); 687 | purple_http_request_unref(request); 688 | 689 | purple_connection_update_progress(sa->pc, _("Authenticating"), 2, 4); 690 | 691 | g_free(postdata); 692 | } 693 | 694 | #define TEAMS_OAUTH_CLIENT_ID "1fec8e78-bce4-4aaf-ab1b-5451cc387264" 695 | #define TEAMS_OAUTH_RESOURCE "https://api.spaces.skype.com" 696 | #define TEAMS_OAUTH_REDIRECT_URI "https://login.microsoftonline.com/common/oauth2/nativeclient" 697 | 698 | 699 | static void 700 | teams_oauth_with_code_cb(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 701 | { 702 | TeamsAccount *sa = user_data; 703 | JsonObject *obj; 704 | const gchar *raw_response; 705 | gsize response_len; 706 | PurpleAccount *account = sa->account; 707 | 708 | raw_response = purple_http_response_get_data(response, &response_len); 709 | obj = json_decode_object(raw_response, response_len); 710 | 711 | if (purple_http_response_is_successful(response) && obj) 712 | { 713 | gchar *id_token = g_strdup(json_object_get_string_member(obj, "access_token")); 714 | if (sa->id_token) { 715 | g_free(sa->id_token); 716 | } 717 | sa->id_token = id_token; 718 | if (json_object_has_member(obj, "refresh_token")) { 719 | sa->refresh_token = g_strdup(json_object_get_string_member(obj, "refresh_token")); 720 | 721 | purple_account_set_remember_password(account, TRUE); 722 | teams_save_refresh_token_password(account, sa->refresh_token); 723 | } 724 | 725 | teams_login_get_api_skypetoken(sa, NULL, NULL, sa->id_token); 726 | } else { 727 | if (obj != NULL) { 728 | if (json_object_has_member(obj, "error")) { 729 | const gchar *error = json_object_get_string_member(obj, "error"); 730 | if (g_strcmp0(error, "invalid_grant") == 0 || g_strcmp0(error, "interaction_required") == 0) { 731 | teams_save_refresh_token_password(sa->account, NULL); 732 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, 733 | json_object_get_string_member(obj, "error_description")); 734 | } else { 735 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, 736 | json_object_get_string_member(obj, "error_description")); 737 | } 738 | } else { 739 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, 740 | _("Invalid response")); 741 | } 742 | } 743 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, 744 | _("Invalid response")); 745 | } 746 | 747 | json_object_unref(obj); 748 | } 749 | 750 | static void 751 | teams_presence_oauth_cb(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 752 | { 753 | TeamsAccount *sa = user_data; 754 | JsonObject *obj; 755 | const gchar *raw_response; 756 | gsize response_len; 757 | 758 | raw_response = purple_http_response_get_data(response, &response_len); 759 | obj = json_decode_object(raw_response, response_len); 760 | 761 | if (purple_http_response_is_successful(response) && obj) 762 | { 763 | gchar *presence_access_token = g_strdup(json_object_get_string_member(obj, "access_token")); 764 | if (sa->presence_access_token) { 765 | g_free(sa->presence_access_token); 766 | } 767 | sa->presence_access_token = presence_access_token; 768 | } 769 | } 770 | 771 | static void 772 | teams_csa_oauth_cb(PurpleHttpConnection *http_conn, PurpleHttpResponse *response, gpointer user_data) 773 | { 774 | TeamsAccount *sa = user_data; 775 | JsonObject *obj; 776 | const gchar *raw_response; 777 | gsize response_len; 778 | 779 | raw_response = purple_http_response_get_data(response, &response_len); 780 | obj = json_decode_object(raw_response, response_len); 781 | 782 | if (purple_http_response_is_successful(response) && obj) 783 | { 784 | gchar *csa_access_token = g_strdup(json_object_get_string_member(obj, "access_token")); 785 | if (sa->csa_access_token) { 786 | g_free(sa->csa_access_token); 787 | } 788 | sa->csa_access_token = csa_access_token; 789 | } 790 | } 791 | 792 | void 793 | teams_oauth_refresh_token_for_resource(TeamsAccount *sa, const gchar *resource, PurpleHttpCallback callback) { 794 | 795 | PurpleHttpRequest *request; 796 | PurpleConnection *pc; 797 | GString *postdata; 798 | gchar *tenant_host; 799 | gchar *auth_url; 800 | 801 | pc = sa->pc; 802 | if (!PURPLE_IS_CONNECTION(pc)) { 803 | return; 804 | } 805 | 806 | postdata = g_string_new(NULL); 807 | g_string_append_printf(postdata, "resource=%s&", purple_url_encode(resource)); 808 | g_string_append_printf(postdata, "client_id=%s&", purple_url_encode(TEAMS_OAUTH_CLIENT_ID)); 809 | g_string_append(postdata, "grant_type=refresh_token&"); 810 | g_string_append_printf(postdata, "refresh_token=%s&", purple_url_encode(sa->refresh_token)); 811 | 812 | if (sa->tenant && *sa->tenant) { 813 | if (strchr(sa->tenant, '.')) { 814 | // Likely a FQDN 815 | tenant_host = g_strdup(sa->tenant); 816 | } else if (g_regex_match_simple(TEAMS_GUID_REGEX_PATTERN, sa->tenant, 0, 0)) { 817 | tenant_host = g_strdup(sa->tenant); 818 | } else { 819 | tenant_host = g_strconcat(sa->tenant, ".onmicrosoft.com", NULL); 820 | } 821 | 822 | } else { 823 | tenant_host = g_strdup("Common"); 824 | } 825 | 826 | auth_url = g_strconcat("https://login.microsoftonline.com/", purple_url_encode(tenant_host), "/oauth2/token", NULL); 827 | 828 | request = purple_http_request_new(auth_url); 829 | purple_http_request_set_cookie_jar(request, sa->cookie_jar); 830 | purple_http_request_set_method(request, "POST"); 831 | purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded"); 832 | purple_http_request_set_contents(request, postdata->str, postdata->len); 833 | 834 | purple_http_request(pc, request, callback, sa); 835 | purple_http_request_unref(request); 836 | 837 | g_string_free(postdata, TRUE); 838 | 839 | g_free(auth_url); 840 | return; 841 | } 842 | 843 | gboolean 844 | teams_oauth_refresh_token(TeamsAccount *sa) 845 | { 846 | teams_oauth_refresh_token_for_resource(sa, "https://api.spaces.skype.com", teams_oauth_with_code_cb); 847 | teams_oauth_refresh_token_for_resource(sa, "https://presence.teams.microsoft.com", teams_presence_oauth_cb); 848 | teams_oauth_refresh_token_for_resource(sa, "https://chatsvcagg.teams.microsoft.com", teams_csa_oauth_cb); 849 | 850 | // For working with purple_timeout_add() 851 | return FALSE; 852 | } 853 | 854 | void 855 | teams_oauth_with_code(TeamsAccount *sa, const gchar *auth_code) 856 | { 857 | PurpleHttpRequest *request; 858 | PurpleConnection *pc = sa->pc; 859 | GString *postdata; 860 | gchar *tenant_host; 861 | gchar *auth_url; 862 | 863 | if (strstr(auth_code, "nativeclient")) { 864 | gchar *tmp = strchr(auth_code, '?'); 865 | if (tmp == NULL) { 866 | //todo error 867 | return; 868 | } 869 | auth_code = tmp + 1; 870 | 871 | tmp = strstr(auth_code, "code="); 872 | if (tmp == NULL) { 873 | //todo error 874 | return; 875 | } 876 | auth_code = tmp + 5; 877 | 878 | tmp = strchr(auth_code, '&'); 879 | if (tmp != NULL) { 880 | *tmp = '\0'; 881 | } 882 | 883 | auth_code = purple_url_decode(auth_code); 884 | } 885 | 886 | postdata = g_string_new(NULL); 887 | g_string_append(postdata, "resource=https%3A%2F%2Fapi.spaces.skype.com&"); 888 | g_string_append_printf(postdata, "client_id=%s&", purple_url_encode(TEAMS_OAUTH_CLIENT_ID)); 889 | g_string_append(postdata, "grant_type=authorization_code&"); 890 | g_string_append_printf(postdata, "code=%s&", purple_url_encode(auth_code)); 891 | g_string_append_printf(postdata, "redirect_uri=%s&", purple_url_encode(TEAMS_OAUTH_REDIRECT_URI)); 892 | 893 | if (sa->tenant && *sa->tenant) { 894 | if (strchr(sa->tenant, '.')) { 895 | // Likely a FQDN 896 | tenant_host = g_strdup(sa->tenant); 897 | } else if (g_regex_match_simple(TEAMS_GUID_REGEX_PATTERN, sa->tenant, 0, 0)) { 898 | tenant_host = g_strdup(sa->tenant); 899 | } else { 900 | tenant_host = g_strconcat(sa->tenant, ".onmicrosoft.com", NULL); 901 | } 902 | 903 | } else { 904 | tenant_host = g_strdup("Common"); 905 | } 906 | 907 | auth_url = g_strconcat("https://login.microsoftonline.com/", purple_url_encode(tenant_host), "/oauth2/token", NULL); 908 | 909 | request = purple_http_request_new(auth_url); 910 | purple_http_request_set_cookie_jar(request, sa->cookie_jar); 911 | purple_http_request_set_method(request, "POST"); 912 | purple_http_request_header_set(request, "Content-Type", "application/x-www-form-urlencoded"); 913 | purple_http_request_set_contents(request, postdata->str, postdata->len); 914 | 915 | purple_http_request(pc, request, teams_oauth_with_code_cb, sa); 916 | purple_http_request_unref(request); 917 | 918 | g_string_free(postdata, TRUE); 919 | g_free(auth_url); 920 | } 921 | 922 | static void 923 | teams_authcode_input_cb(gpointer user_data, const gchar *auth_code) 924 | { 925 | TeamsAccount *sa = user_data; 926 | PurpleConnection *pc = sa->pc; 927 | 928 | purple_connection_update_progress(pc, _("Authenticating"), 1, 3); 929 | teams_oauth_with_code(sa, auth_code); 930 | } 931 | 932 | static void 933 | teams_authcode_input_cancel_cb(gpointer user_data) 934 | { 935 | TeamsAccount *sa = user_data; 936 | purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE, _("User cancelled authorization")); 937 | } 938 | 939 | void 940 | teams_do_web_auth(TeamsAccount *sa) 941 | { 942 | PurpleConnection *pc = sa->pc; 943 | gchar *tenant_host; 944 | gchar *auth_url; 945 | 946 | //https://login.microsoftonline.com/Common/oauth2/authorize?resource=https%3A%2F%2Fapi.spaces.skype.com&client_id=1fec8e78-bce4-4aaf-ab1b-5451cc387264&response_type=code&redirect_uri=https%3A%2F%2Flogin.microsoftonline.com%2Fcommon%2Foauth2%2Fnativeclient&prompt=login&display=popup 947 | 948 | if (sa->tenant && *sa->tenant) { 949 | if (strchr(sa->tenant, '.')) { 950 | // Likely a FQDN 951 | tenant_host = g_strdup(sa->tenant); 952 | } else if (g_regex_match_simple(TEAMS_GUID_REGEX_PATTERN, sa->tenant, 0, 0)) { 953 | tenant_host = g_strdup(sa->tenant); 954 | } else { 955 | tenant_host = g_strconcat(sa->tenant, ".onmicrosoft.com", NULL); 956 | } 957 | 958 | } else { 959 | tenant_host = g_strdup("Common"); 960 | } 961 | 962 | auth_url = g_strconcat("https://login.microsoftonline.com/", purple_url_encode(tenant_host), "/oauth2/authorize?client_id=" TEAMS_OAUTH_CLIENT_ID "&response_type=code&display=popup&prompt=select_account&redirect_uri=https%3A%2F%2Flogin.microsoftonline.com%2Fcommon%2Foauth2%2Fnativeclient", NULL); 963 | 964 | purple_notify_uri(pc, auth_url); 965 | purple_request_input(pc, _("Authorization Code"), auth_url, 966 | _("Please login in your browser"), 967 | _("and then paste the URL of the blank page here (should contain 'nativeclient')"), FALSE, FALSE, NULL, 968 | _("OK"), G_CALLBACK(teams_authcode_input_cb), 969 | _("Cancel"), G_CALLBACK(teams_authcode_input_cancel_cb), 970 | purple_request_cpar_from_connection(pc), sa); 971 | 972 | g_free(tenant_host); 973 | g_free(auth_url); 974 | } 975 | 976 | //alternate flow: 977 | // POST to https://login.microsoftonline.com/{tenant}/oauth2/v2.0/devicecode 978 | // with client_id= TEAMS_OAUTH_CLIENT_ID &scope= {https://api.spaces.skype.com/.default offline_access profile openid} 979 | // comes back with device_code and tells user to go to https://microsoft.com/devicelogin with user_code ABCDEFGHI 980 | // { 981 | // "user_code": "ABCDEFGHI", 982 | // "device_code": "ABCDEFGHI--ABCDEFGHI-ABCDEFGHI", 983 | // "verification_uri": "https://microsoft.com/devicelogin", 984 | // "expires_in": 900, 985 | // "interval": 5, 986 | // "message": "To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code ABCDEFGHI to authenticate." 987 | // } 988 | // periodically (every interval seconds) poll with POST to https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token 989 | // with 990 | // client_id= TEAMS_OAUTH_CLIENT_ID &client_info=1&scope={scope}&grant_type=device_code&device_code={device_code} 991 | // and eventually it'll come back with client id's etc 992 | --------------------------------------------------------------------------------