1 /*
2 * Purple Plugin Pack
3 * Copyright (C) 2003-2008
4 * See ../AUTHORS for a list of all authors
5 *
6 * bash: provides a /command to display the URL for a random or specified
7 * bash.org quote.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, USA.
22 */
23
24 /* If you can't figure out what this line is for, DON'T TOUCH IT. */
25 #include "../common/pp_internal.h"
26
27 /* libc */
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32
33 /* Purple */
34 #include <cmds.h>
35 #include <conversation.h>
36 #include <debug.h>
37 #include <plugin.h>
38
39 #define BASH_QUOTES 881844
40 #define QDB_QUOTES 294961
41
42 static PurpleCmdId bash, qdb;
43
44 static PurpleCmdRet
cmd_func(PurpleConversation * conv,const gchar * cmd,gchar ** args,gchar * error,void * data)45 cmd_func(PurpleConversation *conv, const gchar *cmd, gchar **args,
46 gchar *error, void *data)
47 {
48 GString *msgstr = NULL;
49 guint32 quotes = 0, quoteid = 0;
50
51 msgstr = g_string_new("");
52
53 srand(time(NULL));
54
55 if(!strcmp(cmd, "bash")) {
56 g_string_append(msgstr, "http://www.bash.org/?");
57 quotes = BASH_QUOTES;
58 } else {
59 g_string_append(msgstr, "http://qdb.us/");
60 quotes = QDB_QUOTES;
61 }
62
63 if(*args == NULL || args[0] == NULL)
64 quoteid = (rand() % quotes) + 1;
65 else
66 quoteid = atoi(args[0]);
67
68 if(quoteid > quotes)
69 quoteid %= quotes;
70
71 g_string_append_printf(msgstr, "%i", quoteid);
72
73 switch(purple_conversation_get_type(conv)) {
74 case PURPLE_CONV_TYPE_IM:
75 purple_conv_im_send(PURPLE_CONV_IM(conv), msgstr->str);
76 break;
77 case PURPLE_CONV_TYPE_CHAT:
78 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), msgstr->str);
79 break;
80 default:
81 g_string_free(msgstr, TRUE);
82 return PURPLE_CMD_RET_FAILED;
83 }
84
85 g_string_free(msgstr, TRUE);
86
87 return PURPLE_CMD_RET_OK;
88 }
89
90 static gboolean
plugin_load(PurplePlugin * plugin)91 plugin_load(PurplePlugin *plugin)
92 {
93 const gchar *bash_help, *qdb_help;
94 PurpleCmdFlag flags = PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_CHAT |
95 PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS;
96
97 bash_help = _("bash [n]: sends a link to a bash.org quote. Specify a"
98 " number for n and it will send a link to the quote with the"
99 " specified number.");
100
101 qdb_help = _("qdb [n]: sends a link to a qdb.us quote. Specify a number "
102 "for n and it will send a link to the quite with the specified "
103 "number.");
104
105 bash = purple_cmd_register("bash", "w", PURPLE_CMD_P_PLUGIN, flags, NULL,
106 PURPLE_CMD_FUNC(cmd_func), bash_help, NULL);
107
108 qdb = purple_cmd_register("qdb", "w", PURPLE_CMD_P_PLUGIN, flags, NULL,
109 PURPLE_CMD_FUNC(cmd_func), qdb_help, NULL);
110
111 return TRUE;
112 }
113
114 static gboolean
plugin_unload(PurplePlugin * plugin)115 plugin_unload(PurplePlugin *plugin)
116 {
117 purple_cmd_unregister(bash);
118 purple_cmd_unregister(qdb);
119
120 return TRUE;
121 }
122
123 static PurplePluginInfo bash_info =
124 {
125 PURPLE_PLUGIN_MAGIC, /* magic, my ass */
126 PURPLE_MAJOR_VERSION,
127 PURPLE_MINOR_VERSION,
128 PURPLE_PLUGIN_STANDARD,
129 NULL,
130 0,
131 NULL,
132 PURPLE_PRIORITY_DEFAULT,
133 "core-plugin_pack-bash",
134 NULL,
135 PP_VERSION,
136 NULL,
137 NULL,
138 "John Bailey <rekkanoryo@rekkanoryo.org>",
139 PP_WEBSITE,
140 plugin_load,
141 plugin_unload,
142 NULL,
143 NULL,
144 NULL,
145 NULL,
146 NULL,
147 NULL,
148 NULL,
149 NULL,
150 NULL
151 };
152
153 static void
init_plugin(PurplePlugin * plugin)154 init_plugin(PurplePlugin *plugin)
155 {
156 #ifdef ENABLE_NLS
157 bindtextdomain(GETTEXT_PACKAGE, PP_LOCALEDIR);
158 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
159 #endif /* ENABLE_NLS */
160
161 bash_info.name = _("bash.org");
162 bash_info.summary =
163 _("Generates links for quotes at bash.org");
164 bash_info.description =
165 _("Generates links for quotes at bash.org or allows the "
166 "user to specify a quote. Provides the /bash command.");
167
168 return;
169 }
170
171 PURPLE_INIT_PLUGIN(bash, init_plugin, bash_info)
172