author | Mikael Berthe <mikael@lilotux.net> |
Sat, 13 Oct 2012 14:34:18 +0200 | |
changeset 44 | 3f94f3522960 |
parent 40 | 5cd4b42ada91 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
37 | 2 |
* Module "lastmsg" -- adds a /lastmsg command |
3 |
* Displays last personal messages |
|
4 |
* |
|
5 |
* This modules stores messages received in a MUC room while |
|
6 |
* you are away (or not available) if they contain your nickname. |
|
7 |
* When you're back, you can display them in the status window with |
|
8 |
* the /lastmsg command. |
|
9 |
* |
|
10 |
* Copyright (C) 2010 Mikael Berthe <mikael@lilotux.net> |
|
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 (at |
|
15 |
* your option) any later version. |
|
16 |
* |
|
17 |
* This program is distributed in the hope that it will be useful, but |
|
18 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
20 |
* 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 |
*/ |
|
10 | 25 |
|
26 |
#include <mcabber/modules.h> |
|
27 |
#include <mcabber/commands.h> |
|
28 |
#include <mcabber/hooks.h> |
|
26
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
29 |
#include <mcabber/screen.h> |
10 | 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 |
||
40
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
46 |
#ifdef MCABBER_API_HAVE_CMD_ID |
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
47 |
static gpointer lastmsg_cmdid; |
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
48 |
#endif |
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
49 |
|
10 | 50 |
static GSList *lastmsg_list; |
51 |
||
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
52 |
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
|
53 |
|
10 | 54 |
struct lastm_T { |
55 |
gchar *mucname; |
|
56 |
gchar *nickname; |
|
57 |
gchar *msg; |
|
58 |
}; |
|
59 |
||
60 |
static void do_lastmsg(char *args) |
|
61 |
{ |
|
15
d7cced6660a7
[lastmsg] Do not use ‘for’ loop initial declarations (C99)
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
62 |
GSList *li; |
26
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
63 |
guint count = 0; |
15
d7cced6660a7
[lastmsg] Do not use ‘for’ loop initial declarations (C99)
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
64 |
|
10 | 65 |
if (!lastmsg_list) { |
66 |
scr_log_print(LPRINT_NORMAL, "You have no new message."); |
|
67 |
return; |
|
68 |
} |
|
69 |
||
15
d7cced6660a7
[lastmsg] Do not use ‘for’ loop initial declarations (C99)
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
70 |
for (li = lastmsg_list; li ; li = g_slist_next(li)) { |
10 | 71 |
struct lastm_T *lastm_item = li->data; |
72 |
scr_LogPrint(LPRINT_NORMAL, "In <#%s>, \"%s\" said:\n%s", |
|
73 |
lastm_item->mucname, lastm_item->nickname, |
|
74 |
lastm_item->msg); |
|
75 |
g_free(lastm_item->mucname); |
|
76 |
g_free(lastm_item->nickname); |
|
77 |
g_free(lastm_item->msg); |
|
26
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
78 |
count++; |
10 | 79 |
} |
80 |
g_slist_free(lastmsg_list); |
|
81 |
lastmsg_list = NULL; |
|
26
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
82 |
if (count*2 > scr_getlogwinheight()) { |
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
83 |
scr_setmsgflag_if_needed(SPECIAL_BUFFER_STATUS_ID, TRUE); |
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
84 |
scr_setattentionflag_if_needed(SPECIAL_BUFFER_STATUS_ID, TRUE, |
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
85 |
ROSTER_UI_PRIO_STATUS_WIN_MESSAGE, prio_max); |
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
86 |
} |
10 | 87 |
} |
88 |
||
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
89 |
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
|
90 |
gpointer userdata) |
10 | 91 |
{ |
92 |
enum imstatus status; |
|
93 |
const gchar *bjid, *res, *msg; |
|
11
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
94 |
gboolean muc = FALSE, urgent = FALSE; |
10 | 95 |
|
96 |
status = xmpp_getstatus(); |
|
97 |
||
98 |
if (status != notavail && status != away) |
|
13
efe544108ca1
Update lastmsg module to API dev:11
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
99 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
10 | 100 |
|
101 |
bjid = res = NULL; |
|
102 |
msg = NULL; |
|
103 |
||
104 |
for ( ; args->name; args++) { |
|
105 |
if (!g_strcmp0(args->name, "jid")) |
|
106 |
bjid = args->value; |
|
107 |
else if (!g_strcmp0(args->name, "resource")) |
|
108 |
res = args->value; |
|
109 |
else if (!g_strcmp0(args->name, "message")) |
|
110 |
msg = args->value; |
|
111 |
else if (!g_strcmp0(args->name, "groupchat")) { |
|
112 |
if (!g_strcmp0(args->value, "true")) |
|
113 |
muc = TRUE; |
|
18
fb84350decc5
[lastmsg] Fix usage of hook-post-message-in parameters
Mikael Berthe <mikael@lilotux.net>
parents:
15
diff
changeset
|
114 |
} else if (!g_strcmp0(args->name, "attention")) { |
11
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
115 |
if (!g_strcmp0(args->value, "true")) |
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
116 |
urgent = TRUE; |
10 | 117 |
} |
118 |
} |
|
119 |
||
11
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
120 |
if (muc && urgent && bjid && res && msg) { |
10 | 121 |
struct lastm_T *lastm_item; |
122 |
||
123 |
lastm_item = g_new(struct lastm_T, 1); |
|
124 |
lastm_item->mucname = g_strdup(bjid); |
|
125 |
lastm_item->nickname = g_strdup(res); |
|
126 |
lastm_item->msg = g_strdup(msg); |
|
127 |
lastmsg_list = g_slist_append(lastmsg_list, lastm_item); |
|
128 |
} |
|
13
efe544108ca1
Update lastmsg module to API dev:11
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
129 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
10 | 130 |
} |
131 |
||
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
132 |
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
|
133 |
gpointer userdata) |
10 | 134 |
{ |
135 |
gboolean not_away = FALSE; |
|
136 |
||
137 |
for ( ; args->name; args++) { |
|
138 |
if (!g_strcmp0(args->name, "new_status")) { |
|
139 |
if (args->value && |
|
140 |
(args->value[0] != imstatus2char[away]) && |
|
141 |
(args->value[0] != imstatus2char[notavail])) { |
|
142 |
not_away = TRUE; |
|
143 |
break; |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
147 |
if (!not_away || !lastmsg_list) |
13
efe544108ca1
Update lastmsg module to API dev:11
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
148 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
10 | 149 |
|
150 |
scr_log_print(LPRINT_NORMAL, "Looks like you're back..."); |
|
151 |
scr_log_print(LPRINT_NORMAL, "I've got news for you, use /lastmsg to " |
|
152 |
"read your messages!"); |
|
13
efe544108ca1
Update lastmsg module to API dev:11
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
153 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
10 | 154 |
} |
155 |
||
156 |
/* Initialization */ |
|
157 |
static void lastmsg_init(void) |
|
158 |
{ |
|
159 |
/* Add command */ |
|
40
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
160 |
#ifdef MCABBER_API_HAVE_CMD_ID |
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
161 |
lastmsg_cmdid = cmd_add("lastmsg", "Display last missed messages", 0, 0, |
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
162 |
do_lastmsg, NULL); |
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
163 |
#else |
10 | 164 |
cmd_add("lastmsg", "Display last missed messages", 0, 0, do_lastmsg, NULL); |
40
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
165 |
#endif |
10 | 166 |
|
26
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
167 |
/* Add hook handlers */ |
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
168 |
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
|
169 |
G_PRIORITY_DEFAULT_IDLE, NULL); |
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
170 |
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
|
171 |
G_PRIORITY_DEFAULT_IDLE, NULL); |
10 | 172 |
} |
173 |
||
11
573a48de8ee1
Update lastmsg module to API dev:6
Mikael Berthe <mikael@lilotux.net>
parents:
10
diff
changeset
|
174 |
/* Uninitialization */ |
10 | 175 |
static void lastmsg_uninit(void) |
176 |
{ |
|
15
d7cced6660a7
[lastmsg] Do not use ‘for’ loop initial declarations (C99)
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
177 |
GSList *li; |
d7cced6660a7
[lastmsg] Do not use ‘for’ loop initial declarations (C99)
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
178 |
|
10 | 179 |
/* Unregister command */ |
40
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
180 |
#ifdef MCABBER_API_HAVE_CMD_ID |
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
181 |
cmd_del(lastmsg_cmdid); |
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
182 |
#else |
10 | 183 |
cmd_del("lastmsg"); |
40
5cd4b42ada91
Sync with current mcabber API
Mikael Berthe <mikael@lilotux.net>
parents:
37
diff
changeset
|
184 |
#endif |
26
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
185 |
/* Unregister handlers */ |
12
3b30c879c8cc
Update lastmsg module to API dev:8
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
186 |
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
|
187 |
hk_del_handler(HOOK_MY_STATUS_CHANGE, last_status_hid); |
10 | 188 |
|
26
a15aa7a218ec
[lastmsg] Add the status window message flag when /lastmsg output is long
Mikael Berthe <mikael@lilotux.net>
parents:
18
diff
changeset
|
189 |
/* Clean up data */ |
15
d7cced6660a7
[lastmsg] Do not use ‘for’ loop initial declarations (C99)
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
190 |
for (li = lastmsg_list; li ; li = g_slist_next(li)) { |
10 | 191 |
struct lastm_T *lastm_item = li->data; |
192 |
g_free(lastm_item->mucname); |
|
193 |
g_free(lastm_item->nickname); |
|
194 |
g_free(lastm_item->msg); |
|
195 |
} |
|
196 |
g_slist_free(lastmsg_list); |
|
197 |
lastmsg_list = NULL; |
|
198 |
} |
|
199 |
||
200 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |