author | Mikael Berthe <mikael@lilotux.net> |
Sat, 20 Mar 2010 16:47:12 +0100 | |
changeset 12 | 3b30c879c8cc |
parent 11 | 573a48de8ee1 |
child 13 | efe544108ca1 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
2 |
Copyright 2010 Mikael Berthe |
|
3 |
||
4 |
Module "lastmsg" -- adds a /lastmsg command |
|
5 |
Displays last personal messages |
|
6 |
||
7 |
This modules stores messages received in a MUC room while |
|
8 |
you are away (or not available) if they contain your nickname. |
|
9 |
When you're back, you can display them in the status window with |
|
10 |
the /lastmsg command. |
|
11 |
||
12 |
This module is free software: you can redistribute it and/or modify |
|
13 |
it under the terms of the GNU General Public License as published by |
|
14 |
the Free Software Foundation, either version 2 of the License, or |
|
15 |
(at your option) any later version. |
|
16 |
||
17 |
This program is distributed in the hope that it will be useful, |
|
18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 |
GNU General Public License for more details. |
|
21 |
||
22 |
You should have received a copy of the GNU General Public License |
|
23 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24 |
*/ |
|
25 |
||
26 |
#include <mcabber/modules.h> |
|
27 |
#include <mcabber/commands.h> |
|
28 |
#include <mcabber/hooks.h> |
|
29 |
#include <mcabber/logprint.h> |
|
30 |
||
31 |
static void lastmsg_init(void); |
|
32 |
static void lastmsg_uninit(void); |
|
33 |
||
34 |
/* Module description */ |
|
35 |
module_info_t info_lastmsg = { |
|
36 |
.branch = MCABBER_BRANCH, |
|
37 |
.api = MCABBER_API_VERSION, |
|
11
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
38 |
.version = "0.02", |
10 | 39 |
.description = "Add a command /lastmsg", |
40 |
.requires = NULL, |
|
41 |
.init = lastmsg_init, |
|
42 |
.uninit = lastmsg_uninit, |
|
43 |
.next = NULL, |
|
44 |
}; |
|
45 |
||
46 |
static GSList *lastmsg_list; |
|
47 |
||
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
48 |
static guint last_message_hid, last_status_hid; |
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
49 |
|
10 | 50 |
struct lastm_T { |
51 |
gchar *mucname; |
|
52 |
gchar *nickname; |
|
53 |
gchar *msg; |
|
54 |
}; |
|
55 |
||
56 |
static void do_lastmsg(char *args) |
|
57 |
{ |
|
58 |
if (!lastmsg_list) { |
|
59 |
scr_log_print(LPRINT_NORMAL, "You have no new message."); |
|
60 |
return; |
|
61 |
} |
|
62 |
||
63 |
for (GSList *li = lastmsg_list; li ; li = g_slist_next(li)) { |
|
64 |
struct lastm_T *lastm_item = li->data; |
|
65 |
scr_LogPrint(LPRINT_NORMAL, "In <#%s>, \"%s\" said:\n%s", |
|
66 |
lastm_item->mucname, lastm_item->nickname, |
|
67 |
lastm_item->msg); |
|
68 |
g_free(lastm_item->mucname); |
|
69 |
g_free(lastm_item->nickname); |
|
70 |
g_free(lastm_item->msg); |
|
71 |
} |
|
72 |
g_slist_free(lastmsg_list); |
|
73 |
lastmsg_list = NULL; |
|
74 |
} |
|
75 |
||
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
76 |
static guint last_message_hh(const gchar *hookname, hk_arg_t *args, |
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
77 |
gpointer userdata) |
10 | 78 |
{ |
79 |
enum imstatus status; |
|
80 |
const gchar *bjid, *res, *msg; |
|
11
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
81 |
gboolean muc = FALSE, urgent = FALSE; |
10 | 82 |
|
83 |
status = xmpp_getstatus(); |
|
84 |
||
85 |
if (status != notavail && status != away) |
|
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
86 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HOOKS; |
10 | 87 |
|
88 |
bjid = res = NULL; |
|
89 |
msg = NULL; |
|
90 |
||
91 |
for ( ; args->name; args++) { |
|
92 |
if (!g_strcmp0(args->name, "jid")) |
|
93 |
bjid = args->value; |
|
94 |
else if (!g_strcmp0(args->name, "resource")) |
|
95 |
res = args->value; |
|
96 |
else if (!g_strcmp0(args->name, "message")) |
|
97 |
msg = args->value; |
|
98 |
else if (!g_strcmp0(args->name, "groupchat")) { |
|
99 |
if (!g_strcmp0(args->value, "true")) |
|
100 |
muc = TRUE; |
|
11
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
101 |
} else if (!g_strcmp0(args->name, "urgent")) { |
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
102 |
if (!g_strcmp0(args->value, "true")) |
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
103 |
urgent = TRUE; |
10 | 104 |
} |
105 |
} |
|
106 |
||
11
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
107 |
if (muc && urgent && bjid && res && msg) { |
10 | 108 |
struct lastm_T *lastm_item; |
109 |
||
110 |
lastm_item = g_new(struct lastm_T, 1); |
|
111 |
lastm_item->mucname = g_strdup(bjid); |
|
112 |
lastm_item->nickname = g_strdup(res); |
|
113 |
lastm_item->msg = g_strdup(msg); |
|
114 |
lastmsg_list = g_slist_append(lastmsg_list, lastm_item); |
|
115 |
} |
|
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
116 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HOOKS; |
10 | 117 |
} |
118 |
||
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
119 |
static guint last_status_hh(const gchar *hookname, hk_arg_t *args, |
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
120 |
gpointer userdata) |
10 | 121 |
{ |
122 |
gboolean not_away = FALSE; |
|
123 |
||
124 |
for ( ; args->name; args++) { |
|
125 |
if (!g_strcmp0(args->name, "new_status")) { |
|
126 |
if (args->value && |
|
127 |
(args->value[0] != imstatus2char[away]) && |
|
128 |
(args->value[0] != imstatus2char[notavail])) { |
|
129 |
not_away = TRUE; |
|
130 |
break; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
134 |
if (!not_away || !lastmsg_list) |
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
135 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HOOKS; |
10 | 136 |
|
137 |
scr_log_print(LPRINT_NORMAL, "Looks like you're back..."); |
|
138 |
scr_log_print(LPRINT_NORMAL, "I've got news for you, use /lastmsg to " |
|
139 |
"read your messages!"); |
|
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
140 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HOOKS; |
10 | 141 |
} |
142 |
||
143 |
/* Initialization */ |
|
144 |
static void lastmsg_init(void) |
|
145 |
{ |
|
146 |
/* Add command */ |
|
147 |
cmd_add("lastmsg", "Display last missed messages", 0, 0, do_lastmsg, NULL); |
|
148 |
||
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
149 |
last_message_hid = hk_add_handler(last_message_hh, HOOK_POST_MESSAGE_IN, |
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
150 |
G_PRIORITY_DEFAULT_IDLE, NULL); |
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
151 |
last_status_hid = hk_add_handler(last_status_hh, HOOK_MY_STATUS_CHANGE, |
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
152 |
G_PRIORITY_DEFAULT_IDLE, NULL); |
10 | 153 |
} |
154 |
||
11
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
155 |
/* Uninitialization */ |
10 | 156 |
static void lastmsg_uninit(void) |
157 |
{ |
|
158 |
/* Unregister command */ |
|
159 |
cmd_del("lastmsg"); |
|
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
160 |
hk_del_handler(HOOK_POST_MESSAGE_IN, last_message_hid); |
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
161 |
hk_del_handler(HOOK_MY_STATUS_CHANGE, last_status_hid); |
10 | 162 |
|
163 |
for (GSList *li = lastmsg_list; li ; li = g_slist_next(li)) { |
|
164 |
struct lastm_T *lastm_item = li->data; |
|
165 |
g_free(lastm_item->mucname); |
|
166 |
g_free(lastm_item->nickname); |
|
167 |
g_free(lastm_item->msg); |
|
168 |
} |
|
169 |
g_slist_free(lastmsg_list); |
|
170 |
lastmsg_list = NULL; |
|
171 |
} |
|
172 |
||
173 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |