Replace strcmp() with purple_strequal()
[pidgin-git.git] / pidgin / plugins / relnot.c
blobb4a90204fb9e896f4a4d5be927d2fc1c511cdaaf
1 /*
2 * Release Notification Plugin
4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02111-1301, USA.
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #ifndef PURPLE_PLUGINS
27 #define PURPLE_PLUGINS
28 #endif
30 #include "internal.h"
32 #include <string.h>
34 #include "connection.h"
35 #include "core.h"
36 #include "debug.h"
37 #include "gtkblist.h"
38 #include "gtkutils.h"
39 #include "notify.h"
40 #include "pidginstock.h"
41 #include "prefs.h"
42 #include "util.h"
43 #include "version.h"
45 #include "pidgin.h"
47 /* 1 day */
48 #define MIN_CHECK_INTERVAL 60 * 60 * 24
50 static void
51 release_hide()
53 /* No-op. We may use this method in the future to avoid showing
54 * the popup twice */
57 static void
58 release_show()
60 purple_notify_uri(NULL, PURPLE_WEBSITE);
63 static void
64 version_fetch_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
65 const gchar *response, size_t len, const gchar *error_message)
67 gchar *cur_ver;
68 const char *tmp, *changelog;
69 char response_code[4];
70 GtkWidget *release_dialog;
72 GString *message;
73 int i = 0;
75 if(error_message || !response || !len)
76 return;
78 memset(response_code, '\0', sizeof(response_code));
79 /* Parse the status code - the response should be in the form of "HTTP/?.? 200 ..." */
80 if ((tmp = strstr(response, " ")) != NULL) {
81 tmp++;
82 /* Read the 3 digit status code */
83 if (len - (tmp - response) > 3) {
84 memcpy(response_code, tmp, 3);
88 if (!purple_strequal(response_code, "200")) {
89 purple_debug_error("relnot", "Didn't recieve a HTTP status code of 200.\n");
90 return;
93 /* Go to the start of the data */
94 if((changelog = strstr(response, "\r\n\r\n")) == NULL) {
95 purple_debug_error("relnot", "Unable to find start of HTTP response data.\n");
96 return;
98 changelog += 4;
100 while(changelog[i] && changelog[i] != '\n') i++;
102 /* this basically means the version thing wasn't in the format we were
103 * looking for so sourceforge is probably having web server issues, and
104 * we should try again later */
105 if(i == 0)
106 return;
108 cur_ver = g_strndup(changelog, i);
110 message = g_string_new("");
111 g_string_append_printf(message, _("You can upgrade to %s %s today."),
112 PIDGIN_NAME, cur_ver);
114 release_dialog = pidgin_make_mini_dialog(
115 NULL, PIDGIN_STOCK_DIALOG_INFO,
116 _("New Version Available"),
117 message->str,
118 NULL,
119 _("Later"), PURPLE_CALLBACK(release_hide),
120 _("Download Now"), PURPLE_CALLBACK(release_show),
121 NULL);
123 pidgin_blist_add_alert(release_dialog);
125 g_string_free(message, TRUE);
126 g_free(cur_ver);
129 static void
130 do_check(void)
132 int last_check = purple_prefs_get_int("/plugins/gtk/relnot/last_check");
133 if(!last_check || time(NULL) - last_check > MIN_CHECK_INTERVAL) {
134 gchar *url, *request;
135 const char *host = "pidgin.im";
137 url = g_strdup_printf("https://%s/version.php?version=%s&build=%s",
138 host,
139 purple_core_get_version(),
140 #ifdef _WIN32
141 "purple-win32"
142 #else
143 "purple"
144 #endif
147 request = g_strdup_printf(
148 "GET %s HTTP/1.0\r\n"
149 "Connection: close\r\n"
150 "Accept: */*\r\n"
151 "Host: %s\r\n\r\n",
152 url,
153 host);
155 purple_util_fetch_url_request_len(url, TRUE, NULL, FALSE,
156 request, TRUE, -1, version_fetch_cb, NULL);
158 g_free(request);
159 g_free(url);
161 purple_prefs_set_int("/plugins/gtk/relnot/last_check", time(NULL));
166 static void
167 signed_on_cb(PurpleConnection *gc, void *data) {
168 do_check();
171 /**************************************************************************
172 * Plugin stuff
173 **************************************************************************/
174 static gboolean
175 plugin_load(PurplePlugin *plugin)
177 purple_signal_connect(purple_connections_get_handle(), "signed-on",
178 plugin, PURPLE_CALLBACK(signed_on_cb), NULL);
180 /* we don't check if we're offline */
181 if(purple_connections_get_all())
182 do_check();
184 return TRUE;
187 static PurplePluginInfo info =
189 PURPLE_PLUGIN_MAGIC,
190 PURPLE_MAJOR_VERSION,
191 PURPLE_MINOR_VERSION,
192 PURPLE_PLUGIN_STANDARD, /**< type */
193 NULL, /**< ui_requirement */
194 0, /**< flags */
195 NULL, /**< dependencies */
196 PURPLE_PRIORITY_DEFAULT, /**< priority */
198 "gtk-relnot", /**< id */
199 N_("Release Notification"), /**< name */
200 DISPLAY_VERSION, /**< version */
201 /** summary */
202 N_("Checks periodically for new releases."),
203 /** description */
204 N_("Checks periodically for new releases and notifies the user "
205 "with the ChangeLog."),
206 "Nathan Walp <faceprint@faceprint.com>", /**< author */
207 PURPLE_WEBSITE, /**< homepage */
209 plugin_load, /**< load */
210 NULL, /**< unload */
211 NULL, /**< destroy */
213 NULL, /**< ui_info */
214 NULL, /**< extra_info */
215 NULL,
216 NULL,
218 /* padding */
219 NULL,
220 NULL,
221 NULL,
222 NULL
225 static void
226 init_plugin(PurplePlugin *plugin)
228 purple_prefs_add_none("/plugins/gtk/relnot");
229 purple_prefs_add_int("/plugins/gtk/relnot/last_check", 0);
232 PURPLE_INIT_PLUGIN(relnot, init_plugin, info)