comparison mcabber/mcabber/xmpp_iqrequest.c @ 1706:a5263e93c5d7

Fix pinging oneself
author Mikael Berthe <mikael@lilotux.net>
date Mon, 08 Feb 2010 21:49:52 +0100
parents ac881b5f9248
children 184de76b5b18
comparison
equal deleted inserted replaced
1705:ac881b5f9248 1706:a5263e93c5d7
74 vcard_inet = 1<<6, 74 vcard_inet = 1<<6,
75 vcard_pref = 1<<7, 75 vcard_pref = 1<<7,
76 }; 76 };
77 77
78 static LmHandlerResult cb_ping(LmMessageHandler *h, LmConnection *c, 78 static LmHandlerResult cb_ping(LmMessageHandler *h, LmConnection *c,
79 LmMessage *message, gpointer udata) 79 LmMessage *m, gpointer user_data)
80 { 80 {
81 struct timeval *timestamp = (struct timeval *) udata; 81 struct timeval *timestamp = (struct timeval *)user_data;
82 struct timeval now; 82 struct timeval now;
83 time_t dsec; 83 time_t dsec;
84 suseconds_t dusec; 84 suseconds_t dusec;
85 const gchar *fjid;
86 gchar *bjid;
85 87
86 gettimeofday(&now, NULL); 88 gettimeofday(&now, NULL);
87 dsec = now.tv_sec - timestamp->tv_sec; 89 dsec = now.tv_sec - timestamp->tv_sec;
88 if (now.tv_usec < timestamp->tv_usec) { 90 if (now.tv_usec < timestamp->tv_usec) {
89 dusec = now.tv_usec + 1000000 - timestamp->tv_usec; 91 dusec = now.tv_usec + 1000000 - timestamp->tv_usec;
90 --dsec; 92 --dsec;
91 } else 93 } else
92 dusec = now.tv_usec - timestamp->tv_usec; 94 dusec = now.tv_usec - timestamp->tv_usec;
93 95
94 switch (lm_message_get_sub_type(message)) { 96 // Check IQ result sender
97 fjid = lm_message_get_from(m);
98 if (!fjid)
99 fjid = lm_connection_get_jid(lconnection); // No from means our JID...
100 if (!fjid) {
101 scr_LogPrint(LPRINT_LOGNORM, "Invalid IQ:version result (no sender name).");
102 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
103 }
104
105 bjid = jidtodisp(fjid);
106
107 switch (lm_message_get_sub_type(m)) {
95 case LM_MESSAGE_SUB_TYPE_RESULT: 108 case LM_MESSAGE_SUB_TYPE_RESULT:
96 { // print to buddy's buffer 109 { // print to buddy's buffer
97 LmMessageNode *node = lm_message_get_node(message);
98 gchar *jid = jidtodisp(lm_message_node_get_attribute(node, "from"));
99 gchar *mesg = g_strdup_printf("Pong: %d second%s %d ms.", 110 gchar *mesg = g_strdup_printf("Pong: %d second%s %d ms.",
100 (int)dsec, 111 (int)dsec,
101 dsec > 1 ? "s" : "", 112 dsec > 1 ? "s" : "",
102 (int)(dusec/1000L)); 113 (int)(dusec/1000L));
103 114
104 scr_WriteIncomingMessage(jid, mesg, 0, HBB_PREFIX_INFO, 0); 115 scr_WriteIncomingMessage(bjid, mesg, 0, HBB_PREFIX_INFO, 0);
105
106 g_free(mesg); 116 g_free(mesg);
107 g_free(jid);
108 } 117 }
109 break; 118 break;
110 119
111 case LM_MESSAGE_SUB_TYPE_ERROR: 120 case LM_MESSAGE_SUB_TYPE_ERROR:
112 { 121 {
113 LmMessageNode *node = lm_message_get_node(message); 122 LmMessageNode *node = lm_message_get_node(m);
114 const gchar *from = lm_message_node_get_attribute(node, "from");
115 const gchar *type; 123 const gchar *type;
116 const gchar *reason; 124 const gchar *reason;
117 125
118 node = lm_message_node_get_child(node, "error"); 126 node = lm_message_node_get_child(node, "error");
119 type = lm_message_node_get_attribute(node, "type"); 127 type = lm_message_node_get_attribute(node, "type");
121 reason = node->children->name; 129 reason = node->children->name;
122 else 130 else
123 reason = "undefined"; 131 reason = "undefined";
124 132
125 { // print to buddy's buffer 133 { // print to buddy's buffer
126 gchar *jid = jidtodisp(from);
127 gchar *mesg = g_strdup_printf("Ping to %s failed: %s - %s (response time %d second%s %d microseconds)", 134 gchar *mesg = g_strdup_printf("Ping to %s failed: %s - %s (response time %d second%s %d microseconds)",
128 from, type, reason, 135 fjid, type, reason,
129 (int)dsec, 136 (int)dsec,
130 dsec > 1 ? "s" : "", 137 dsec > 1 ? "s" : "",
131 (int)(dusec/1000L)); 138 (int)(dusec/1000L));
132 139
133 scr_WriteIncomingMessage(jid, mesg, 0, HBB_PREFIX_INFO, 0); 140 scr_WriteIncomingMessage(bjid, mesg, 0, HBB_PREFIX_INFO, 0);
134
135 g_free(mesg); 141 g_free(mesg);
136 g_free(jid);
137 } 142 }
138 } 143 }
139 break; 144 break;
140 145
141 default: 146 default:
147 g_free(bjid);
142 return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; 148 return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
143 break; 149 break;
144 } 150 }
145 151
152 g_free(bjid);
146 return LM_HANDLER_RESULT_REMOVE_MESSAGE; 153 return LM_HANDLER_RESULT_REMOVE_MESSAGE;
147 } 154 }
148 155
149 // Warning!! xmlns has to be a namespace from iq_request_handlers[].xmlns 156 // Warning!! xmlns has to be a namespace from iq_request_handlers[].xmlns
150 void xmpp_iq_request(const char *fulljid, const char *xmlns) 157 void xmpp_iq_request(const char *fulljid, const char *xmlns)