]> git.babelmonkeys.de Git - mpdbot.git/blob - src/mpdbot.m
Auto-confirm subscription requests
[mpdbot.git] / src / mpdbot.m
1 /*
2  * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
3  * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
4  *
5  * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice is present in all copies.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
15  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  * POSSIBILITY OF SUCH DAMAGE.
22  */
23
24 #import <ObjFW/ObjFW.h>
25 #import <ObjXMPP/ObjXMPP.h>
26
27 #import "PEPThread.h"
28
29 #define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
30
31 @interface AppDelegate: OFObject
32 #ifdef OF_HAVE_OPTIONAL_PROTOCOLS
33     <OFApplicationDelegate, XMPPConnectionDelegate>
34 #endif
35
36 OFString *discoID;
37 PEPThread *pepper;
38 @end
39
40 OF_APPLICATION_DELEGATE(AppDelegate)
41
42 @implementation AppDelegate
43 - (void)applicationDidFinishLaunching
44 {
45         XMPPConnection *conn;
46         OFArray *arguments = [OFApplication arguments];
47
48         conn = [[XMPPConnection alloc] init];
49         [conn setDelegate: self];
50
51         if ([arguments count] != 3) {
52                 [of_stdout writeFormat: @"Usage: %@ <server> <user> <passwd>\n",
53                     [OFApplication programName]];
54                 [OFApplication terminateWithStatus: 1];
55         }
56
57         [conn setDomain: [arguments objectAtIndex: 0]];
58         [conn setUsername: [arguments objectAtIndex: 1]];
59         [conn setPassword: [arguments objectAtIndex: 2]];
60         [conn setResource: @"ObjXMPP"];
61
62         @try {
63                 [conn connect];
64                 [conn handleConnection];
65         } @catch (id e) {
66                 of_log(@"%@", e);
67         }
68 }
69
70 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
71 {
72         [of_stdout writeLine: @"Auth successful"];
73 }
74
75 - (void)connection: (XMPPConnection*)conn
76      wasBoundToJID: (XMPPJID*)jid
77 {
78         XMPPPresence *pres;
79         XMPPIQ *disco;
80
81         [of_stdout writeFormat: @"Bound to JID: %@\n", [jid fullJID]];
82
83         pres = [XMPPPresence presence];
84         [pres addPriority: 0];
85         [pres addStatus: @"Hello I'm MPDbot!"];
86         [conn sendStanza: pres];
87
88         discoID = [[conn generateStanzaID] retain];
89         disco = [XMPPIQ IQWithType: @"get"
90                                 ID: discoID];
91         disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
92         [disco addChild: [OFXMLElement
93         elementWithName: @"query"
94               namespace: NS_DISCO_INFO]];
95
96         [conn sendStanza: disco];
97 }
98
99 - (BOOL)connection: (XMPPConnection*)conn
100       didReceiveIQ: (XMPPIQ*)iq
101 {
102         OFXMLElement *query = [iq elementForName: @"query"
103                                        namespace: NS_DISCO_INFO];
104         if ([[iq ID] isEqual: discoID]) {
105                 for (OFXMLElement *identity
106                     in [query elementsForName: @"identity"
107                                     namespace: NS_DISCO_INFO]) {
108                         if ([[[identity attributeForName: @"category"]
109                             stringValue] isEqual: @"pubsub"] &&
110                             [[[identity attributeForName: @"type"] stringValue]
111                             isEqual: @"pep"]) {
112                                 pepper = [[PEPThread alloc]
113                                     initWithObject: conn];
114                                 [pepper start];
115                                 return YES;
116                         }
117                 }
118         }
119
120         return NO;
121 }
122
123 -  (void)connection: (XMPPConnection*)conn
124   didReceiveMessage: (XMPPMessage*)msg
125 {
126         of_log(@"Message: %@", msg);
127 }
128
129 -   (void)connection: (XMPPConnection*)conn
130   didReceivePresence: (XMPPPresence*)pres
131 {
132         if ([pres.type isEqual: @"subscribe"]) {
133                 XMPPPresence *answer;
134                 answer = [XMPPPresence presenceWithType: @"subscribed"
135                                                      ID: pres.ID];
136                 answer.to = [XMPPJID JIDWithString: [pres.from bareJID]];
137                 [conn sendStanza: answer];
138         }
139 }
140
141 - (void)connectionWasClosed: (XMPPConnection*)conn
142 {
143         [of_stdout writeLine: @"Connection was closed!"];
144 }
145 @end